Locator: rewrote compute to use efficient predecessor from state
This commit is contained in:
parent
95a4ede273
commit
f62ce16e5c
@ -23,24 +23,39 @@ let encoding =
|
|||||||
(req "current_head" (dynamic_size Block_header.encoding))
|
(req "current_head" (dynamic_size Block_header.encoding))
|
||||||
(req "history" (dynamic_size (list Block_hash.encoding))))
|
(req "history" (dynamic_size (list Block_hash.encoding))))
|
||||||
|
|
||||||
let compute ~pred (h: Block_hash.t) (bh: Block_header.t) sz =
|
(**
|
||||||
let rec loop acc ~sz step cpt b =
|
Computes a locator for block [b] picking 10 times the immediate
|
||||||
if sz = 0 then
|
predecessors of [b], then 10 times one predecessor every 2, then
|
||||||
Lwt.return (List.rev acc)
|
10 times one predecessor every 4, ..., until genesis or it reaches
|
||||||
else
|
the desired size.
|
||||||
pred b step >>= function
|
*)
|
||||||
| None ->
|
let compute ~predecessor ~genesis b header size =
|
||||||
Lwt.return (List.rev (b :: acc))
|
if size < 0 then invalid_arg "compute: negative size" else
|
||||||
| Some predecessor ->
|
let repeats = 10 in (* number of repetitions for each power of 2 *)
|
||||||
if cpt = 0 then
|
let rec loop acc size step cnt b =
|
||||||
loop (b :: acc) ~sz:(sz - 1) (step * 2) 10 predecessor
|
if size = 0 then
|
||||||
else
|
Lwt.return (List.rev acc)
|
||||||
loop (b :: acc) ~sz:(sz - 1) step (cpt - 1) predecessor in
|
else
|
||||||
pred h 1 >>= function
|
predecessor b step >>= function
|
||||||
| None -> Lwt.return (bh, [])
|
| None -> (* reached genesis before size *)
|
||||||
| Some p ->
|
if Block_hash.equal b genesis then
|
||||||
loop [] ~sz 1 9 p >>= fun hist ->
|
Lwt.return (List.rev acc)
|
||||||
Lwt.return (bh, hist)
|
else
|
||||||
|
Lwt.return (List.rev (genesis :: acc))
|
||||||
|
| Some pred ->
|
||||||
|
if cnt = 1 then
|
||||||
|
loop (pred :: acc) (size - 1)
|
||||||
|
(step * 2) repeats pred
|
||||||
|
else
|
||||||
|
loop (pred :: acc) (size - 1)
|
||||||
|
step (cnt - 1) pred
|
||||||
|
in
|
||||||
|
if size = 0 then Lwt.return (header, []) else
|
||||||
|
predecessor b 1 >>= function
|
||||||
|
| None -> Lwt.return (header, [])
|
||||||
|
| Some p ->
|
||||||
|
loop [p] (size-1) 1 repeats p >>= fun hist ->
|
||||||
|
Lwt.return (header, hist)
|
||||||
|
|
||||||
type validity =
|
type validity =
|
||||||
| Unknown
|
| Unknown
|
||||||
|
@ -17,10 +17,10 @@ val raw: t -> raw
|
|||||||
|
|
||||||
val encoding: t Data_encoding.t
|
val encoding: t Data_encoding.t
|
||||||
|
|
||||||
val compute:
|
val compute: predecessor: (Block_hash.t -> int -> Block_hash.t option Lwt.t) ->
|
||||||
pred:(Block_hash.t -> int -> Block_hash.t option Lwt.t) ->
|
genesis:Block_hash.t -> Block_hash.t -> Block_header.t -> int -> t Lwt.t
|
||||||
Block_hash.t -> Block_header.t -> int ->
|
(** [compute block max_length] compute the sparse block locator for
|
||||||
t Lwt.t
|
the [block]. The locator contains at most [max_length] elements. *)
|
||||||
|
|
||||||
type validity =
|
type validity =
|
||||||
| Unknown
|
| Unknown
|
||||||
|
@ -243,10 +243,11 @@ let predecessor_n (store: Store.Block.store) (b: Block_hash.t) (distance: int)
|
|||||||
in
|
in
|
||||||
loop b distance
|
loop b distance
|
||||||
|
|
||||||
let compute_locator_from_hash (net : net_state) ?(size = 200) head =
|
let compute_locator_from_hash (net : net_state) ?(size = 200) head_hash =
|
||||||
Shared.use net.block_store begin fun block_store ->
|
Shared.use net.block_store begin fun block_store ->
|
||||||
Store.Block.Contents.read_exn (block_store, head) >>= fun { header } ->
|
Store.Block.Contents.read_exn (block_store, head_hash) >>= fun { header } ->
|
||||||
Block_locator.compute ~pred:(predecessor block_store) head header size
|
Block_locator.compute ~predecessor:(predecessor_n block_store)
|
||||||
|
~genesis:net.genesis.block head_hash header size
|
||||||
end
|
end
|
||||||
|
|
||||||
let compute_locator net ?size head =
|
let compute_locator net ?size head =
|
||||||
|
Loading…
Reference in New Issue
Block a user