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. + *)