From 329c8b185ad84972ad2533fa322cf8a15dd428cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 18 Apr 2017 11:29:26 +0200 Subject: [PATCH] Shell: implements `Chain.live_blocks` --- src/node/shell/chain_traversal.ml | 16 ++++++++++++++++ src/node/shell/chain_traversal.mli | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/src/node/shell/chain_traversal.ml b/src/node/shell/chain_traversal.ml index 02f88ab17..6294aec07 100644 --- a/src/node/shell/chain_traversal.ml +++ b/src/node/shell/chain_traversal.ml @@ -132,3 +132,19 @@ let new_blocks ~from_block ~to_block = path ancestor to_block >>= function | None -> assert false | Some path -> Lwt.return (ancestor, path) + +let live_blocks block n = + let rec loop bacc oacc block n = + Block.all_operation_hashes block >>= fun hashes -> + let oacc = + List.fold_left + (List.fold_left + (fun oacc op -> Operation_hash.Set.add op oacc)) + oacc hashes in + let bacc = Block_hash.Set.add (Block.hash block) bacc in + if n = 0 then Lwt.return (bacc, oacc) + else + Block.predecessor block >>= function + | None -> Lwt.return (bacc, oacc) + | Some predecessor -> loop bacc oacc predecessor (pred n) in + loop Block_hash.Set.empty Operation_hash.Set.empty block n diff --git a/src/node/shell/chain_traversal.mli b/src/node/shell/chain_traversal.mli index 67680038f..2afafcd83 100644 --- a/src/node/shell/chain_traversal.mli +++ b/src/node/shell/chain_traversal.mli @@ -46,3 +46,11 @@ val new_blocks: (excluded) to [to_block] (included). The function raises an exception when the two provided blocks do not belong the the same [net]. *) + +val live_blocks: + Block.t -> int -> (Block_hash.Set.t * Operation_hash.Set.t) Lwt.t +(** [live_blocks b n] return a pair [(blocks,operations)] where + [blocks] is the set of arity [n], that contains [b] and its [n-1] + predecessors. And where [operations] is the set of operations + included in those blocks. + *)