Shell: implements Chain.live_blocks

This commit is contained in:
Grégoire Henry 2017-04-18 11:29:26 +02:00
parent 8d5155cf32
commit 329c8b185a
2 changed files with 24 additions and 0 deletions

View File

@ -132,3 +132,19 @@ let new_blocks ~from_block ~to_block =
path ancestor to_block >>= function path ancestor to_block >>= function
| None -> assert false | None -> assert false
| Some path -> Lwt.return (ancestor, path) | 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

View File

@ -46,3 +46,11 @@ val new_blocks:
(excluded) to [to_block] (included). The function raises an (excluded) to [to_block] (included). The function raises an
exception when the two provided blocks do not belong the the same exception when the two provided blocks do not belong the the same
[net]. *) [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.
*)