ligo/src/lib_base/block_locator.ml

74 lines
2.3 KiB
OCaml
Raw Normal View History

(**************************************************************************)
(* *)
2018-02-06 00:17:03 +04:00
(* Copyright (c) 2014 - 2018. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Lwt.Infix
2017-12-17 22:51:06 +04:00
type t = raw
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-12-17 22:51:06 +04:00
let raw x = x
let encoding =
let open Data_encoding in
(* TODO add a [description] *)
(obj2
(req "current_head" (dynamic_size Block_header.encoding))
(req "history" (dynamic_size (list Block_hash.encoding))))
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 =
if sz = 0 then
Lwt.return (List.rev acc)
else
pred b step >>= function
| None ->
2017-12-17 22:51:06 +04:00
Lwt.return (List.rev (b :: acc))
| Some predecessor ->
if cpt = 0 then
loop (b :: acc) ~sz:(sz - 1) (step * 2) 10 predecessor
else
loop (b :: acc) ~sz:(sz - 1) step (cpt - 1) predecessor in
pred h 1 >>= function
| None -> Lwt.return (bh, [])
| Some p ->
2017-12-17 22:51:06 +04:00
loop [] ~sz 1 9 p >>= fun hist ->
Lwt.return (bh, hist)
2017-12-17 22:51:06 +04:00
type validity =
| Unknown
| Known_valid
| Known_invalid
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)
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
| None ->
2017-12-17 22:51:06 +04:00
Lwt.return_none
| Some (tail, hist) ->
Lwt.return_some (tail, (head, hist))