Shell/block_locator: count 'head' as a locator step

This commit is contained in:
Grégoire Henry 2018-03-12 17:11:30 +01:00
parent ee3f81b6e3
commit 487fce8be1

View File

@ -44,14 +44,14 @@ 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))))
(** (** Computes a locator for block [b] picking 10 times the immediate
Computes a locator for block [b] picking 10 times the immediate predecessors of [b], then 10 times one predecessor every 2, then
predecessors of [b], then 10 times one predecessor every 2, then 10 times one predecessor every 4, ..., until genesis or it reaches
10 times one predecessor every 4, ..., until genesis or it reaches the desired size. *)
the desired size.
*)
let compute ~predecessor ~genesis b header size = let compute ~predecessor ~genesis b header size =
if size < 0 then invalid_arg "compute: negative size" else if size < 0 then
invalid_arg "Block_locator.compute: negative size"
else
let repeats = 10 in (* number of repetitions for each power of 2 *) let repeats = 10 in (* number of repetitions for each power of 2 *)
let rec loop acc size step cnt b = let rec loop acc size step cnt b =
if size = 0 then if size = 0 then
@ -65,17 +65,16 @@ let compute ~predecessor ~genesis b header size =
Lwt.return (List.rev (genesis :: acc)) Lwt.return (List.rev (genesis :: acc))
| Some pred -> | Some pred ->
if cnt = 1 then if cnt = 1 then
loop (pred :: acc) (size - 1) loop (pred :: acc) (size - 1) (step * 2) repeats pred
(step * 2) repeats pred
else else
loop (pred :: acc) (size - 1) loop (pred :: acc) (size - 1) step (cnt - 1) pred in
step (cnt - 1) pred if size = 0 then
in Lwt.return (header, [])
if size = 0 then Lwt.return (header, []) else else
predecessor b 1 >>= function predecessor b 1 >>= function
| None -> Lwt.return (header, []) | None -> Lwt.return (header, [])
| Some p -> | Some p ->
loop [p] (size-1) 1 repeats p >>= fun hist -> loop [p] (size-1) 1 (repeats-1) p >>= fun hist ->
Lwt.return (header, hist) Lwt.return (header, hist)
type validity = type validity =