2017-09-29 20:43:13 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-09-29 20:43:13 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-01-22 22:25:07 +04:00
|
|
|
open Lwt.Infix
|
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
type t = raw
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
(** Non private version of Block_store_locator.t for coercions *)
|
|
|
|
and raw = Block_header.t * Block_hash.t list
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
let raw x = x
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-02-19 21:25:57 +04:00
|
|
|
let pp ppf (hd, h_lst) =
|
|
|
|
let repeats = 10 in
|
|
|
|
let coef = 2 in
|
|
|
|
(* list of hashes *)
|
|
|
|
let rec pp_hash_list ppf (h_lst , acc , d , r) =
|
|
|
|
match h_lst with
|
|
|
|
| [] ->
|
|
|
|
Format.fprintf ppf ""
|
|
|
|
| hd :: tl ->
|
|
|
|
let new_d = if r > 1 then d else d * coef in
|
|
|
|
let new_r = if r > 1 then r - 1 else repeats in
|
|
|
|
Format.fprintf ppf "%a (%i)\n%a" Block_hash.pp hd acc pp_hash_list (tl , acc - d , new_d , new_r) in
|
|
|
|
Format.fprintf ppf "%a (head)\n%a"
|
|
|
|
Block_hash.pp (Block_header.hash hd)
|
|
|
|
pp_hash_list (h_lst , -1, 1, repeats - 1)
|
|
|
|
|
|
|
|
let pp_short ppf (hd, h_lst) =
|
|
|
|
Format.fprintf ppf "head: %a, %d predecessors"
|
|
|
|
Block_hash.pp (Block_header.hash hd)
|
|
|
|
(List.length h_lst)
|
2018-02-13 17:12:09 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
(* TODO add a [description] *)
|
2017-11-11 06:34:12 +04:00
|
|
|
(obj2
|
|
|
|
(req "current_head" (dynamic_size Block_header.encoding))
|
|
|
|
(req "history" (dynamic_size (list Block_hash.encoding))))
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-03-12 20:11:30 +04:00
|
|
|
(** Computes a locator for block [b] picking 10 times the immediate
|
|
|
|
predecessors of [b], then 10 times one predecessor every 2, then
|
|
|
|
10 times one predecessor every 4, ..., until genesis or it reaches
|
|
|
|
the desired size. *)
|
2018-01-25 15:00:42 +04:00
|
|
|
let compute ~predecessor ~genesis b header size =
|
2018-03-12 20:11:30 +04:00
|
|
|
if size < 0 then
|
|
|
|
invalid_arg "Block_locator.compute: negative size"
|
|
|
|
else
|
2018-01-25 15:00:42 +04:00
|
|
|
let repeats = 10 in (* number of repetitions for each power of 2 *)
|
|
|
|
let rec loop acc size step cnt b =
|
|
|
|
if size = 0 then
|
|
|
|
Lwt.return (List.rev acc)
|
|
|
|
else
|
|
|
|
predecessor b step >>= function
|
|
|
|
| None -> (* reached genesis before size *)
|
|
|
|
if Block_hash.equal b genesis then
|
|
|
|
Lwt.return (List.rev acc)
|
|
|
|
else
|
|
|
|
Lwt.return (List.rev (genesis :: acc))
|
|
|
|
| Some pred ->
|
|
|
|
if cnt = 1 then
|
2018-03-12 20:11:30 +04:00
|
|
|
loop (pred :: acc) (size - 1) (step * 2) repeats pred
|
2018-01-25 15:00:42 +04:00
|
|
|
else
|
2018-03-12 20:11:30 +04:00
|
|
|
loop (pred :: acc) (size - 1) step (cnt - 1) pred in
|
|
|
|
if size = 0 then
|
|
|
|
Lwt.return (header, [])
|
|
|
|
else
|
2018-01-25 15:00:42 +04:00
|
|
|
predecessor b 1 >>= function
|
|
|
|
| None -> Lwt.return (header, [])
|
|
|
|
| Some p ->
|
2018-03-12 20:11:30 +04:00
|
|
|
loop [p] (size-1) 1 (repeats-1) p >>= fun hist ->
|
2018-01-25 15:00:42 +04:00
|
|
|
Lwt.return (header, hist)
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
type validity =
|
|
|
|
| Unknown
|
|
|
|
| Known_valid
|
|
|
|
| Known_invalid
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
let unknown_prefix cond (head, hist) =
|
|
|
|
let rec loop hist acc =
|
|
|
|
match hist with
|
|
|
|
| [] -> Lwt.return_none
|
|
|
|
| h :: t ->
|
|
|
|
cond h >>= function
|
|
|
|
| Known_valid ->
|
|
|
|
Lwt.return_some (h, (List.rev (h :: acc)))
|
|
|
|
| Known_invalid ->
|
|
|
|
Lwt.return_none
|
|
|
|
| Unknown ->
|
|
|
|
loop t (h :: acc)
|
2017-09-29 20:43:13 +04:00
|
|
|
in
|
2017-12-17 22:51:06 +04:00
|
|
|
cond (Block_header.hash head) >>= function
|
|
|
|
| Known_valid ->
|
|
|
|
Lwt.return_some (Block_header.hash head, (head, []))
|
|
|
|
| Known_invalid ->
|
|
|
|
Lwt.return_none
|
|
|
|
| Unknown ->
|
|
|
|
loop hist [] >>= function
|
2017-09-29 20:43:13 +04:00
|
|
|
| None ->
|
2017-12-17 22:51:06 +04:00
|
|
|
Lwt.return_none
|
|
|
|
| Some (tail, hist) ->
|
|
|
|
Lwt.return_some (tail, (head, hist))
|