2017-09-29 20:43:13 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
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
|
|
|
|
|
|
|
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-01-22 22:25:07 +04:00
|
|
|
let compute ~pred (h: Block_hash.t) (bh: Block_header.t) sz =
|
2017-12-17 22:51:06 +04:00
|
|
|
let rec loop acc ~sz step cpt b =
|
2017-09-29 20:43:13 +04:00
|
|
|
if sz = 0 then
|
|
|
|
Lwt.return (List.rev acc)
|
|
|
|
else
|
2018-01-22 22:25:07 +04:00
|
|
|
pred b step >>= function
|
2017-09-29 20:43:13 +04:00
|
|
|
| None ->
|
2017-12-17 22:51:06 +04:00
|
|
|
Lwt.return (List.rev (b :: acc))
|
2017-09-29 20:43:13 +04:00
|
|
|
| Some predecessor ->
|
|
|
|
if cpt = 0 then
|
2018-01-22 22:25:07 +04:00
|
|
|
loop (b :: acc) ~sz:(sz - 1) (step * 2) 10 predecessor
|
2017-09-29 20:43:13 +04:00
|
|
|
else
|
2018-01-22 22:25:07 +04:00
|
|
|
loop (b :: acc) ~sz:(sz - 1) step (cpt - 1) predecessor in
|
|
|
|
pred h 1 >>= function
|
|
|
|
| None -> Lwt.return (bh, [])
|
2017-11-11 06:34:12 +04:00
|
|
|
| Some p ->
|
2017-12-17 22:51:06 +04:00
|
|
|
loop [] ~sz 1 9 p >>= fun hist ->
|
2018-01-22 22:25:07 +04:00
|
|
|
Lwt.return (bh, 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))
|