Client: minor stylistic issue.

This commit is contained in:
Grégoire Henry 2017-02-15 17:20:10 +01:00
parent c66db98bfa
commit 285427bcba
3 changed files with 63 additions and 28 deletions

View File

@ -13,21 +13,25 @@ let run cctxt ?max_priority ~delay ?min_date delegates =
(* TODO really detach... *)
let endorsement =
if Client_proto_args.Daemon.(!all || !endorsement) then
Client_mining_blocks.monitor cctxt ?min_date ~min_heads:1 () >>= fun block_stream ->
Client_mining_blocks.monitor
cctxt ?min_date ~min_heads:1 () >>= fun block_stream ->
Client_mining_endorsement.create cctxt ~delay delegates block_stream
else
Lwt.return_unit
in
let denunciation =
if Client_proto_args.Daemon.(!all || !denunciation) then
Client_mining_operations.monitor_endorsement cctxt >>= fun endorsement_stream ->
Client_mining_operations.monitor_endorsement
cctxt >>= fun endorsement_stream ->
Client_mining_denunciation.create cctxt endorsement_stream
else
Lwt.return_unit
in
let forge =
Client_mining_blocks.monitor cctxt ?min_date ~min_heads:1 () >>= fun block_stream ->
Client_mining_operations.monitor_endorsement cctxt >>= fun endorsement_stream ->
Client_mining_blocks.monitor
cctxt ?min_date ~min_heads:1 () >>= fun block_stream ->
Client_mining_operations.monitor_endorsement
cctxt >>= fun endorsement_stream ->
if Client_proto_args.Daemon.(!all || !mining) then
Client_mining_forge.create cctxt
?max_priority delegates block_stream endorsement_stream

View File

@ -35,7 +35,10 @@ end = struct
let open Data_encoding in
conv
(fun x -> LevelMap.bindings x)
(fun l -> List.fold_left (fun x (y, z) -> LevelMap.add y z x) LevelMap.empty l)
(fun l ->
List.fold_left
(fun x (y, z) -> LevelMap.add y z x)
LevelMap.empty l)
(list (obj2
(req "level" Raw_level.encoding)
(req "endorsement"
@ -52,11 +55,13 @@ end = struct
if not (Sys.file_exists filename) then return LevelMap.empty else
Data_encoding_ezjsonm.read_file filename >>= function
| Error _ ->
cctxt.Client_commands.error "couldn't to read the endorsement file"
cctxt.Client_commands.error
"couldn't to read the endorsement file"
| Ok json ->
match Data_encoding.Json.destruct encoding json with
| exception _ -> (* TODO print_error *)
cctxt.Client_commands.error "didn't understand the endorsement file"
cctxt.Client_commands.error
"didn't understand the endorsement file"
| map ->
return map
@ -72,7 +77,8 @@ end = struct
| Error _ -> failwith "Json.write_file"
| Ok () -> return ())
(fun exn ->
cctxt.Client_commands.error "could not write the endorsement file: %s."
cctxt.Client_commands.error
"could not write the endorsement file: %s."
(Printexc.to_string exn))
let lock = Lwt_mutex.create ()
@ -122,7 +128,8 @@ let inject_endorsement cctxt
~slot:slot
() >>=? fun bytes ->
let signed_bytes = Ed25519.append_signature src_sk bytes in
Client_node_rpcs.inject_operation cctxt ?force ?wait signed_bytes >>=? fun oph ->
Client_node_rpcs.inject_operation
cctxt ?force ?wait signed_bytes >>=? fun oph ->
State.record_endorsement cctxt level block_hash slot oph >>=? fun () ->
return oph
@ -151,11 +158,15 @@ let forge_endorsement cctxt
match slot with
| Some slot -> return slot
| None ->
get_signing_slots cctxt ?max_priority block src_pkh level >>=? function
get_signing_slots
cctxt ?max_priority block src_pkh level >>=? function
| slot::_ -> return slot
| [] -> cctxt.error "No slot found at level %a" Raw_level.pp level
end >>=? fun slot ->
(if force then return () else check_endorsement cctxt level slot) >>=? fun () ->
begin
if force then return ()
else check_endorsement cctxt level slot
end >>=? fun () ->
inject_endorsement cctxt
block level ~wait:true ~force
src_sk src_pk slot
@ -210,7 +221,8 @@ let schedule_endorsements cctxt state bis =
let same_slot e =
e.block.level = block.level && e.slot = slot in
let old = List.find same_slot state.to_endorse in
if Fitness.compare old.block.fitness block.fitness < 0 then begin
if Fitness.compare old.block.fitness block.fitness < 0
then begin
lwt_log_info
"Schedule endorsement for block %a \
\ (level %a, slot %d, time %a) (replace block %a)"
@ -223,10 +235,14 @@ let schedule_endorsements cctxt state bis =
state.to_endorse <-
insert
{ time ; delegate ; block ; slot }
(List.filter (fun e -> not (same_slot e)) state.to_endorse) ;
(List.filter
(fun e -> not (same_slot e))
state.to_endorse) ;
return ()
end else begin
lwt_debug "slot %d: better pending endorsement" slot >>= fun () ->
lwt_debug
"slot %d: better pending endorsement"
slot >>= fun () ->
return ()
end
with Not_found ->

View File

@ -19,8 +19,10 @@ let generate_seed_nonce () =
| Error _ -> assert false
| Ok nonce -> nonce
let rec compute_stamp cctxt block delegate_sk shell mining_slot seed_nonce_hash =
Client_proto_rpcs.Constants.stamp_threshold cctxt block >>=? fun stamp_threshold ->
let rec compute_stamp
cctxt block delegate_sk shell mining_slot seed_nonce_hash =
Client_proto_rpcs.Constants.stamp_threshold
cctxt block >>=? fun stamp_threshold ->
let rec loop () =
let proof_of_work_nonce = generate_proof_of_work_nonce () in
let unsigned_header =
@ -81,7 +83,8 @@ let forge_block cctxt block
begin
match operations with
| None ->
Client_node_rpcs.Blocks.pending_operations cctxt block >|= fun (ops, pendings) ->
Client_node_rpcs.Blocks.pending_operations
cctxt block >|= fun (ops, pendings) ->
Operation_hash.Set.elements @@
Operation_hash.Set.union (Updater.operations ops) pendings
| Some operations -> Lwt.return operations
@ -89,7 +92,8 @@ let forge_block cctxt block
begin
match priority with
| Some prio -> begin
Client_proto_rpcs.Helpers.minimal_time cctxt block ~prio () >>=? fun time ->
Client_proto_rpcs.Helpers.minimal_time
cctxt block ~prio () >>=? fun time ->
return (prio, Some time)
end
| None ->
@ -121,7 +125,8 @@ let forge_block cctxt block
return (Some timestamp)
end >>=? fun timestamp ->
let request = List.length operations in
Client_node_rpcs.Blocks.preapply cctxt block ?timestamp ~sort operations >>=?
Client_node_rpcs.Blocks.preapply
cctxt block ?timestamp ~sort operations >>=?
fun { operations ; fitness ; timestamp } ->
let valid = List.length operations.applied in
lwt_log_info "Found %d valid operations (%d refused) for timestamp %a"
@ -159,7 +164,10 @@ end = struct
let open Data_encoding in
conv
(fun x -> LevelMap.bindings x)
(fun l -> List.fold_left (fun x (y, z) -> LevelMap.add y z x) LevelMap.empty l)
(fun l ->
List.fold_left
(fun x (y, z) -> LevelMap.add y z x)
LevelMap.empty l)
(list (obj2
(req "level" Raw_level.encoding)
(req "blocks" (list Block_hash.encoding))))
@ -243,14 +251,17 @@ let get_mining_slot cctxt
Lwt.return (Some (Utils.filter_map convert slots)))
delegates >>= fun slots ->
let sorted_slots =
List.sort (fun (t1,_) (t2,_) -> Time.compare t1 t2) (List.flatten slots) in
List.sort
(fun (t1,_) (t2,_) -> Time.compare t1 t2)
(List.flatten slots) in
match sorted_slots with
| [] -> Lwt.return None
| slot :: _ -> Lwt.return (Some slot)
let rec insert_mining_slot slot = function
| [] -> [slot]
| ((timestamp,_) :: _) as slots when Time.(fst slot < timestamp) -> slot :: slots
| ((timestamp,_) :: _) as slots when Time.(fst slot < timestamp) ->
slot :: slots
| slot' :: slots -> slot' :: insert_mining_slot slot slots
type state = {
@ -271,7 +282,7 @@ let create_state genesis delegates best =
let drop_old_slots ~before state =
state.future_slots <-
List.filter
(fun (t, slot) -> Time.compare t before < 0)
(fun (t, _slot) -> Time.compare t before < 0)
state.future_slots
let compute_timeout { future_slots } =
@ -341,7 +352,8 @@ let insert_block
name
Block_hash.pp_short bi.hash >>= fun () ->
if Time.compare bi.timestamp state.best.timestamp = 0 then
drop_old_slots ~before: (Time.add state.best.timestamp (-1800L)) state ;
drop_old_slots
~before:(Time.add state.best.timestamp (-1800L)) state ;
state.future_slots <- insert_mining_slot slot state.future_slots ;
Lwt.return_unit
@ -431,13 +443,16 @@ let mine cctxt state =
lwt_debug "No valid candidates." >>= fun () ->
return ()
let create cctxt ?max_priority delegates
(block_stream: Client_mining_blocks.block_info list Lwt_stream.t)
(endorsement_stream: Client_mining_operations.valid_endorsement Lwt_stream.t) =
let create
cctxt ?max_priority delegates
(block_stream:
Client_mining_blocks.block_info list Lwt_stream.t)
(endorsement_stream:
Client_mining_operations.valid_endorsement Lwt_stream.t) =
Lwt_stream.get block_stream >>= function
| None | Some [] ->
cctxt.Client_commands.error "Can't fetch the current block head."
| Some ({ Client_mining_blocks.fitness } as bi :: _ as initial_heads) ->
| Some (bi :: _ as initial_heads) ->
Client_node_rpcs.Blocks.hash cctxt `Genesis >>= fun genesis_hash ->
let last_get_block = ref None in
let get_block () =