Alpha/Baker: keeping future slot for each delegate
This commit is contained in:
parent
470a1e91f1
commit
f221e21444
@ -344,16 +344,23 @@ let get_baking_slot cctxt
|
|||||||
| Error errs ->
|
| Error errs ->
|
||||||
log_error "Error while fetching baking possibilities:\n%a"
|
log_error "Error while fetching baking possibilities:\n%a"
|
||||||
pp_print_error errs ;
|
pp_print_error errs ;
|
||||||
Lwt.return_none
|
Lwt.return []
|
||||||
| Ok [] ->
|
| Ok [] ->
|
||||||
Lwt.return_none
|
Lwt.return []
|
||||||
| Ok ((slot : Alpha_services.Delegate.Baking_rights.t) :: _) ->
|
| Ok slots ->
|
||||||
match slot.timestamp with
|
let slots =
|
||||||
| None -> Lwt.return_none
|
List.filter_map
|
||||||
| Some timestamp ->
|
(function
|
||||||
Lwt.return_some (timestamp, (bi, slot.priority, slot.delegate))
|
| { Alpha_services.Delegate.Baking_rights.timestamp = None } -> None
|
||||||
|
| { timestamp = Some timestamp ; priority ; delegate } ->
|
||||||
|
Some (timestamp, (bi, priority, delegate))
|
||||||
|
)
|
||||||
|
slots
|
||||||
|
in
|
||||||
|
Lwt.return slots
|
||||||
|
|
||||||
let rec insert_baking_slot slot = function
|
let rec insert_baking_slot slot = function
|
||||||
|
(* This is just a sorted-insert *)
|
||||||
| [] -> [slot]
|
| [] -> [slot]
|
||||||
| ((timestamp,_) :: _) as slots when Time.(fst slot < timestamp) ->
|
| ((timestamp,_) :: _) as slots when Time.(fst slot < timestamp) ->
|
||||||
slot :: slots
|
slot :: slots
|
||||||
@ -380,20 +387,22 @@ let drop_old_slots ~before state =
|
|||||||
(fun (t, _slot) -> Time.compare before t <= 0)
|
(fun (t, _slot) -> Time.compare before t <= 0)
|
||||||
state.future_slots
|
state.future_slots
|
||||||
|
|
||||||
|
let compute_timeout time =
|
||||||
|
let delay = Time.diff time (Time.now ()) in
|
||||||
|
if delay < 0L then
|
||||||
|
None
|
||||||
|
else
|
||||||
|
Some (Lwt_unix.sleep (Int64.to_float delay))
|
||||||
|
|
||||||
let compute_timeout { future_slots } =
|
let compute_timeout { future_slots } =
|
||||||
match future_slots with
|
match future_slots with
|
||||||
| [] ->
|
| [] ->
|
||||||
|
(* No slots, just wait for new blocks which will give more info *)
|
||||||
Lwt_utils.never_ending
|
Lwt_utils.never_ending
|
||||||
| (timestamp, _) :: _ ->
|
| (timestamp, _) :: _ ->
|
||||||
let now = Time.now () in
|
match compute_timeout timestamp with
|
||||||
let delay = Time.diff timestamp now in
|
| None -> Lwt_utils.never_ending
|
||||||
if delay <= 0L then
|
| Some timeout -> timeout
|
||||||
if delay <= -1800L then
|
|
||||||
Lwt_unix.sleep 10.
|
|
||||||
else
|
|
||||||
Lwt.return_unit
|
|
||||||
else
|
|
||||||
Lwt_unix.sleep (Int64.to_float delay)
|
|
||||||
|
|
||||||
let get_unrevealed_nonces
|
let get_unrevealed_nonces
|
||||||
(cctxt : #Proto_alpha.full) ?(force = false) ?(chain = `Main) block =
|
(cctxt : #Proto_alpha.full) ?(force = false) ?(chain = `Main) block =
|
||||||
@ -459,18 +468,26 @@ let insert_block
|
|||||||
end ;
|
end ;
|
||||||
get_delegates cctxt state >>=? fun delegates ->
|
get_delegates cctxt state >>=? fun delegates ->
|
||||||
get_baking_slot cctxt ?max_priority bi delegates >>= function
|
get_baking_slot cctxt ?max_priority bi delegates >>= function
|
||||||
| None ->
|
| [] ->
|
||||||
lwt_debug
|
lwt_debug
|
||||||
"Can't compute slot for %a" Block_hash.pp_short bi.hash >>= fun () ->
|
"Can't compute slots for %a" Block_hash.pp_short bi.hash >>= fun () ->
|
||||||
return ()
|
|
||||||
| Some ((timestamp, (_,_,delegate)) as slot) ->
|
|
||||||
Client_keys.Public_key_hash.name cctxt delegate >>=? fun name ->
|
|
||||||
lwt_log_info "New baking slot at %a for %s after %a"
|
|
||||||
Time.pp_hum timestamp
|
|
||||||
name
|
|
||||||
Block_hash.pp_short bi.hash >>= fun () ->
|
|
||||||
state.future_slots <- insert_baking_slot slot state.future_slots ;
|
|
||||||
return ()
|
return ()
|
||||||
|
| (_ :: _) as slots ->
|
||||||
|
iter_p
|
||||||
|
(fun ((timestamp, (_, _, delegate)) as slot) ->
|
||||||
|
Client_keys.Public_key_hash.name cctxt delegate >>=? fun name ->
|
||||||
|
lwt_log_info "New baking slot at %a for %s after %a"
|
||||||
|
Time.pp_hum timestamp
|
||||||
|
name
|
||||||
|
Block_hash.pp_short bi.hash >>= fun () ->
|
||||||
|
(* FIXME: the timestamp returned by [get_baking_slot] is always now.
|
||||||
|
This needs a proper fix, but in the meantime, we artifically
|
||||||
|
increase this time to be able to work on the rest of the code. *)
|
||||||
|
let slot = (Time.(max (add (now ()) 60L) (fst slot)), snd slot) in
|
||||||
|
state.future_slots <- insert_baking_slot slot state.future_slots ;
|
||||||
|
return ()
|
||||||
|
)
|
||||||
|
slots
|
||||||
|
|
||||||
let pop_baking_slots state =
|
let pop_baking_slots state =
|
||||||
let now = Time.now () in
|
let now = Time.now () in
|
||||||
@ -568,12 +585,16 @@ let pp_operation_list_list =
|
|||||||
information (e.g., slot) is available in the [state]. *)
|
information (e.g., slot) is available in the [state]. *)
|
||||||
let bake (cctxt : #Proto_alpha.full) state =
|
let bake (cctxt : #Proto_alpha.full) state =
|
||||||
let slots = pop_baking_slots state in
|
let slots = pop_baking_slots state in
|
||||||
|
lwt_log_info "Found %d current slots and %d future slots."
|
||||||
|
(List.length slots)
|
||||||
|
(List.length state.future_slots) >>= fun () ->
|
||||||
let seed_nonce = generate_seed_nonce () in
|
let seed_nonce = generate_seed_nonce () in
|
||||||
let seed_nonce_hash = Nonce.hash seed_nonce in
|
let seed_nonce_hash = Nonce.hash seed_nonce in
|
||||||
|
|
||||||
(* baking for each slot *)
|
(* baking for each slot *)
|
||||||
filter_map_s (bake_slot cctxt state seed_nonce_hash) slots >>=? fun candidates ->
|
filter_map_s (bake_slot cctxt state seed_nonce_hash) slots >>=? fun candidates ->
|
||||||
|
|
||||||
|
(* FIXME: pick one block per-delegate *)
|
||||||
(* selecting the candidate baked block *)
|
(* selecting the candidate baked block *)
|
||||||
let candidates = List.sort fittest candidates in
|
let candidates = List.sort fittest candidates in
|
||||||
match candidates with
|
match candidates with
|
||||||
@ -698,9 +719,6 @@ let create
|
|||||||
(* NOTE: this is not a tight loop because of Lwt_stream.get *)
|
(* NOTE: this is not a tight loop because of Lwt_stream.get *)
|
||||||
wait_for_first_block ()
|
wait_for_first_block ()
|
||||||
| Some (Ok bi) ->
|
| Some (Ok bi) ->
|
||||||
create
|
create cctxt ?max_priority delegates block_stream bi
|
||||||
cctxt ?max_priority delegates
|
|
||||||
block_stream bi
|
|
||||||
in
|
in
|
||||||
wait_for_first_block ()
|
wait_for_first_block ()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user