Baker/Nonces: use Block_hash.Map.t instead of association list

This commit is contained in:
Grégoire Henry 2018-11-07 16:08:01 +01:00
parent 2e2a40b331
commit e966d2784e
No known key found for this signature in database
GPG Key ID: 50D984F20BD445D2
4 changed files with 61 additions and 45 deletions

View File

@ -935,14 +935,11 @@ let compute_best_slot_on_current_level
(* Found at least a slot *) (* Found at least a slot *)
return_some best_slot return_some best_slot
module Nonces_map = Map.Make(Block_hash)
(** [filter_outdated_nonces] removes nonces older than 5 cycles in the nonce file *) (** [filter_outdated_nonces] removes nonces older than 5 cycles in the nonce file *)
let filter_outdated_nonces let filter_outdated_nonces
(cctxt : #Proto_alpha.full) (cctxt : #Proto_alpha.full)
?(chain = `Main) ?(chain = `Main)
head head =
nonces =
Alpha_block_services.metadata Alpha_block_services.metadata
cctxt ~chain ~block:head () >>=? fun { protocol_data = { level = current_level } } -> cctxt ~chain ~block:head () >>=? fun { protocol_data = { level = current_level } } ->
let current_cycle = Cycle.to_int32 current_level.Level.cycle in let current_cycle = Cycle.to_int32 current_level.Level.cycle in
@ -950,16 +947,23 @@ let filter_outdated_nonces
let delta = Int32.sub current_cycle block_cycle in let delta = Int32.sub current_cycle block_cycle in
delta > 5l delta > 5l
in in
filter_map_s (fun (hash, _) -> cctxt#with_lock begin fun () ->
Alpha_block_services.metadata cctxt ~chain ~block:(`Hash (hash, 0)) () >>=? Client_baking_nonces.load cctxt >>=? fun nonces ->
fun { protocol_data = { level = { Level.cycle } } } -> Block_hash.Map.fold
let i = Cycle.to_int32 cycle in begin fun hash nonce acc ->
if is_older_than_5_cycles i then acc >>=? fun acc ->
return_some hash Alpha_block_services.metadata cctxt ~chain ~block:(`Hash (hash, 0)) () >>=?
else fun { protocol_data = { level = { Level.cycle } } } ->
return_none let i = Cycle.to_int32 cycle in
) nonces >>=? fun outdated_nonces -> if is_older_than_5_cycles i then
Client_baking_nonces.dels cctxt outdated_nonces return acc
else
return (Block_hash.Map.add hash nonce acc)
end
nonces
(return Block_hash.Map.empty) >>=? fun new_nonces ->
Client_baking_nonces.save cctxt new_nonces
end
(** [get_unrevealed_nonces] retrieve registered nonces *) (** [get_unrevealed_nonces] retrieve registered nonces *)
let get_unrevealed_nonces let get_unrevealed_nonces
@ -967,13 +971,10 @@ let get_unrevealed_nonces
cctxt#with_lock begin fun () -> cctxt#with_lock begin fun () ->
Client_baking_nonces.load cctxt Client_baking_nonces.load cctxt
end >>=? fun nonces -> end >>=? fun nonces ->
let nonces_map = List.fold_left
(fun map (hash, nonce) -> Nonces_map.add hash nonce map)
Nonces_map.empty nonces in
Client_baking_blocks.blocks_from_current_cycle Client_baking_blocks.blocks_from_current_cycle
cctxt head ~offset:(-1l) () >>=? fun blocks -> cctxt head ~offset:(-1l) () >>=? fun blocks ->
filter_map_s (fun hash -> filter_map_s (fun hash ->
match Nonces_map.find_opt hash nonces_map with match Block_hash.Map.find_opt hash nonces with
| None -> return_none | None -> return_none
| Some nonce -> | Some nonce ->
Alpha_block_services.metadata Alpha_block_services.metadata
@ -1007,7 +1008,7 @@ let get_unrevealed_nonces
- We entered a new cycle and we can clear old nonces ; - We entered a new cycle and we can clear old nonces ;
- A revelation was not included yet in the cycle beggining. - A revelation was not included yet in the cycle beggining.
So, it is safe to only filter outdated_nonces there *) So, it is safe to only filter outdated_nonces there *)
filter_outdated_nonces cctxt ~chain head nonces >>=? fun () -> filter_outdated_nonces cctxt ~chain head >>=? fun () ->
return x return x
(** [reveal_potential_nonces] reveal registered nonces *) (** [reveal_potential_nonces] reveal registered nonces *)

View File

@ -128,7 +128,7 @@ let reveal_block_nonces (cctxt : #Proto_alpha.full) block_hashes =
Lwt.return_none)) Lwt.return_none))
block_hashes >>= fun block_infos -> block_hashes >>= fun block_infos ->
filter_map_s (fun (bi : Client_baking_blocks.block_info) -> filter_map_s (fun (bi : Client_baking_blocks.block_info) ->
match List.assoc_opt bi.hash nonces with match Block_hash.Map.find_opt bi.hash nonces with
| None -> | None ->
cctxt#warning "Cannot find nonces for block %a (ignoring)@." cctxt#warning "Cannot find nonces for block %a (ignoring)@."
Block_hash.pp_short bi.hash >>= fun () -> Block_hash.pp_short bi.hash >>= fun () ->

View File

@ -27,11 +27,18 @@ open Proto_alpha
open Alpha_context open Alpha_context
type t = (Block_hash.t * Nonce.t) list type t = Nonce.t Block_hash.Map.t
let encoding : t Data_encoding.t = let encoding : t Data_encoding.t =
let open Data_encoding in let open Data_encoding in
def "seed_nonce" @@ def "seed_nonce" @@
conv
(fun m ->
Block_hash.Map.fold (fun hash nonce acc -> (hash, nonce) :: acc) m [])
(fun l ->
List.fold_left
(fun map (hash, nonce) -> Block_hash.Map.add hash nonce map)
Block_hash.Map.empty l) @@
list list
(obj2 (obj2
(req "block" Block_hash.encoding) (req "block" Block_hash.encoding)
@ -40,39 +47,44 @@ let encoding : t Data_encoding.t =
let name = "nonce" let name = "nonce"
let load (wallet : #Client_context.wallet) = let load (wallet : #Client_context.wallet) =
wallet#load ~default:[] name encoding wallet#load ~default:Block_hash.Map.empty name encoding
let save (wallet : #Client_context.wallet) list = let save (wallet : #Client_context.wallet) list =
wallet#with_lock (fun () -> wallet#with_lock begin fun () ->
wallet#write name list encoding) wallet#write name list encoding
end
let mem (wallet : #Client_context.wallet) block_hash = let mem (wallet : #Client_context.wallet) block_hash =
wallet#with_lock (fun () -> wallet#with_lock begin fun () ->
load wallet >>|? fun data -> load wallet >>|? fun data ->
List.mem_assoc block_hash data) Block_hash.Map.mem block_hash data
end
let find (wallet : #Client_context.wallet) block_hash = let find (wallet : #Client_context.wallet) block_hash =
wallet#with_lock ( fun () -> wallet#with_lock begin fun () ->
load wallet >>|? fun data -> load wallet >>|? fun data ->
try Some (List.assoc block_hash data) try Some (Block_hash.Map.find block_hash data)
with Not_found -> None) with Not_found -> None
end
let add (wallet : #Client_context.wallet) block_hash nonce = let add (wallet : #Client_context.wallet) block_hash nonce =
wallet#with_lock ( fun () -> wallet#with_lock begin fun () ->
load wallet >>=? fun data -> load wallet >>=? fun data ->
save wallet ((block_hash, nonce) :: save wallet (Block_hash.Map.add block_hash nonce data)
List.remove_assoc block_hash data)) end
let del (wallet : #Client_context.wallet) block_hash = let del (wallet : #Client_context.wallet) block_hash =
wallet#with_lock ( fun () -> wallet#with_lock begin fun () ->
load wallet >>=? fun data -> load wallet >>=? fun data ->
save wallet (List.remove_assoc block_hash data)) save wallet (Block_hash.Map.remove block_hash data)
end
let dels (wallet : #Client_context.wallet) hashes = let dels (wallet : #Client_context.wallet) hashes =
wallet#with_lock ( fun () -> wallet#with_lock begin fun () ->
load wallet >>=? fun data -> load wallet >>=? fun data ->
save wallet @@ save wallet @@
List.fold_left List.fold_left
(fun data hash -> List.remove_assoc hash data) (fun data hash -> Block_hash.Map.remove hash data)
data hashes) data hashes
end

View File

@ -26,11 +26,14 @@
open Proto_alpha open Proto_alpha
open Alpha_context open Alpha_context
type t = (Block_hash.t * Nonce.t) list type t = Nonce.t Block_hash.Map.t
val load: val load:
#Client_context.wallet -> #Client_context.wallet ->
t tzresult Lwt.t t tzresult Lwt.t
val save:
#Client_context.wallet ->
t -> unit tzresult Lwt.t
val mem: val mem:
#Client_context.wallet -> #Client_context.wallet ->
Block_hash.t -> bool tzresult Lwt.t Block_hash.t -> bool tzresult Lwt.t