Refactor: Move Mempool and Block_locator into lib_base

This commit is contained in:
Grégoire Henry 2018-01-22 19:25:07 +01:00
parent 4acdfc67fb
commit 552237673e
7 changed files with 31 additions and 24 deletions

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
open Lwt.Infix
type t = raw type t = raw
(** Non private version of Block_store_locator.t for coercions *) (** Non private version of Block_store_locator.t for coercions *)
@ -21,37 +23,24 @@ 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 predecessor (store : Store.Block.store) (b: Block_hash.t) = let compute ~pred (h: Block_hash.t) (bh: Block_header.t) sz =
Store.Block.Contents.read_exn (store, b) >>= fun contents ->
let predecessor = contents.header.shell.predecessor in
if Block_hash.equal b predecessor then
Lwt.return_none
else
Lwt.return_some predecessor
let compute (store : Store.Block.store) (b: Block_hash.t) sz =
let rec loop acc ~sz step cpt b = let rec loop acc ~sz step cpt b =
if sz = 0 then if sz = 0 then
Lwt.return (List.rev acc) Lwt.return (List.rev acc)
else else
predecessor store b >>= function pred b step >>= function
| None -> | None ->
Lwt.return (List.rev (b :: acc)) Lwt.return (List.rev (b :: acc))
| Some predecessor -> | Some predecessor ->
if cpt = 0 then if cpt = 0 then
loop (b :: acc) ~sz:(sz - 1) loop (b :: acc) ~sz:(sz - 1) (step * 2) 10 predecessor
(step * 2) (step * 20 - 1) predecessor
else if cpt mod step = 0 then
loop (b :: acc) ~sz:(sz - 1)
step (cpt - 1) predecessor
else else
loop acc ~sz step (cpt - 1) predecessor in loop (b :: acc) ~sz:(sz - 1) step (cpt - 1) predecessor in
Store.Block.Contents.read_exn (store, b) >>= fun { header } -> pred h 1 >>= function
predecessor store b >>= function | None -> Lwt.return (bh, [])
| None -> Lwt.return (header, [])
| Some p -> | Some p ->
loop [] ~sz 1 9 p >>= fun hist -> loop [] ~sz 1 9 p >>= fun hist ->
Lwt.return (header, hist) Lwt.return (bh, hist)
type validity = type validity =
| Unknown | Unknown

View File

@ -17,9 +17,10 @@ val raw: t -> raw
val encoding: t Data_encoding.t val encoding: t Data_encoding.t
val compute: Store.Block.store -> Block_hash.t -> int -> t Lwt.t val compute:
(** [compute block max_length] compute the sparse block locator for pred:(Block_hash.t -> int -> Block_hash.t option Lwt.t) ->
the [block]. The locator contains at most [max_length] elements. *) Block_hash.t -> Block_header.t -> int ->
t Lwt.t
type validity = type validity =
| Unknown | Unknown

View File

@ -33,5 +33,8 @@ module Protocol = Protocol
module Test_network_status = Test_network_status module Test_network_status = Test_network_status
module Preapply_result = Preapply_result module Preapply_result = Preapply_result
module Block_locator = Block_locator
module Mempool = Mempool
include Utils.Infix include Utils.Infix
include Error_monad include Error_monad

View File

@ -31,6 +31,8 @@ module Operation = Operation
module Protocol = Protocol module Protocol = Protocol
module Test_network_status = Test_network_status module Test_network_status = Test_network_status
module Preapply_result = Preapply_result module Preapply_result = Preapply_result
module Block_locator = Block_locator
module Mempool = Mempool
include (module type of (struct include Utils.Infix end)) include (module type of (struct include Utils.Infix end))
include (module type of (struct include Error_monad end)) include (module type of (struct include Error_monad end))

View File

@ -133,9 +133,21 @@ let update_chain_store { net_id ; context_index ; chain_state } f =
Lwt.return res Lwt.return res
end end
let rec predecessor (store : Store.Block.store) (b: Block_hash.t) n =
(* TODO optimize *)
if n = 0 then Lwt.return_some b else begin
Store.Block.Contents.read_exn (store, b) >>= fun contents ->
let pred = contents.header.shell.predecessor in
if Block_hash.equal b pred then
Lwt.return_none
else
predecessor store pred (n-1)
end
let compute_locator_from_hash (net : net_state) ?(size = 200) head = let compute_locator_from_hash (net : net_state) ?(size = 200) head =
Shared.use net.block_store begin fun block_store -> Shared.use net.block_store begin fun block_store ->
Block_locator.compute block_store head size Store.Block.Contents.read_exn (block_store, head) >>= fun { header } ->
Block_locator.compute ~pred:(predecessor block_store) head header size
end end
let compute_locator net ?size head = let compute_locator net ?size head =