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 *)
return_some best_slot
module Nonces_map = Map.Make(Block_hash)
(** [filter_outdated_nonces] removes nonces older than 5 cycles in the nonce file *)
let filter_outdated_nonces
(cctxt : #Proto_alpha.full)
?(chain = `Main)
head
nonces =
head =
Alpha_block_services.metadata
cctxt ~chain ~block:head () >>=? fun { protocol_data = { level = current_level } } ->
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
delta > 5l
in
filter_map_s (fun (hash, _) ->
Alpha_block_services.metadata cctxt ~chain ~block:(`Hash (hash, 0)) () >>=?
fun { protocol_data = { level = { Level.cycle } } } ->
let i = Cycle.to_int32 cycle in
if is_older_than_5_cycles i then
return_some hash
else
return_none
) nonces >>=? fun outdated_nonces ->
Client_baking_nonces.dels cctxt outdated_nonces
cctxt#with_lock begin fun () ->
Client_baking_nonces.load cctxt >>=? fun nonces ->
Block_hash.Map.fold
begin fun hash nonce acc ->
acc >>=? fun acc ->
Alpha_block_services.metadata cctxt ~chain ~block:(`Hash (hash, 0)) () >>=?
fun { protocol_data = { level = { Level.cycle } } } ->
let i = Cycle.to_int32 cycle in
if is_older_than_5_cycles i then
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 *)
let get_unrevealed_nonces
@ -967,13 +971,10 @@ let get_unrevealed_nonces
cctxt#with_lock begin fun () ->
Client_baking_nonces.load cctxt
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
cctxt head ~offset:(-1l) () >>=? fun blocks ->
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
| Some nonce ->
Alpha_block_services.metadata
@ -1007,7 +1008,7 @@ let get_unrevealed_nonces
- We entered a new cycle and we can clear old nonces ;
- A revelation was not included yet in the cycle beggining.
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
(** [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))
block_hashes >>= fun block_infos ->
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 ->
cctxt#warning "Cannot find nonces for block %a (ignoring)@."
Block_hash.pp_short bi.hash >>= fun () ->

View File

@ -27,11 +27,18 @@ open Proto_alpha
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 open Data_encoding in
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
(obj2
(req "block" Block_hash.encoding)
@ -40,39 +47,44 @@ let encoding : t Data_encoding.t =
let name = "nonce"
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 =
wallet#with_lock (fun () ->
wallet#write name list encoding)
wallet#with_lock begin fun () ->
wallet#write name list encoding
end
let mem (wallet : #Client_context.wallet) block_hash =
wallet#with_lock (fun () ->
load wallet >>|? fun data ->
List.mem_assoc block_hash data)
wallet#with_lock begin fun () ->
load wallet >>|? fun data ->
Block_hash.Map.mem block_hash data
end
let find (wallet : #Client_context.wallet) block_hash =
wallet#with_lock ( fun () ->
load wallet >>|? fun data ->
try Some (List.assoc block_hash data)
with Not_found -> None)
wallet#with_lock begin fun () ->
load wallet >>|? fun data ->
try Some (Block_hash.Map.find block_hash data)
with Not_found -> None
end
let add (wallet : #Client_context.wallet) block_hash nonce =
wallet#with_lock ( fun () ->
load wallet >>=? fun data ->
save wallet ((block_hash, nonce) ::
List.remove_assoc block_hash data))
wallet#with_lock begin fun () ->
load wallet >>=? fun data ->
save wallet (Block_hash.Map.add block_hash nonce data)
end
let del (wallet : #Client_context.wallet) block_hash =
wallet#with_lock ( fun () ->
load wallet >>=? fun data ->
save wallet (List.remove_assoc block_hash data))
wallet#with_lock begin fun () ->
load wallet >>=? fun data ->
save wallet (Block_hash.Map.remove block_hash data)
end
let dels (wallet : #Client_context.wallet) hashes =
wallet#with_lock ( fun () ->
load wallet >>=? fun data ->
save wallet @@
List.fold_left
(fun data hash -> List.remove_assoc hash data)
data hashes)
wallet#with_lock begin fun () ->
load wallet >>=? fun data ->
save wallet @@
List.fold_left
(fun data hash -> Block_hash.Map.remove hash data)
data hashes
end

View File

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