ligo/src/proto_alpha/lib_baking/client_baking_blocks.ml

65 lines
2.8 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
2018-02-06 00:17:03 +04:00
(* Copyright (c) 2014 - 2018. *)
2016-09-08 21:13:10 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Proto_alpha
open Alpha_context
2016-09-08 21:13:10 +04:00
type block_info = {
hash: Block_hash.t ;
2018-02-16 04:26:24 +04:00
chain_id: Chain_id.t ;
2016-09-08 21:13:10 +04:00
predecessor: Block_hash.t ;
fitness: MBytes.t list ;
timestamp: Time.t ;
protocol: Protocol_hash.t ;
next_protocol: Protocol_hash.t ;
2016-09-08 21:13:10 +04:00
level: Level.t ;
}
let info cctxt ?(chain = `Main) block =
Chain_services.chain_id cctxt ~chain () >>=? fun chain_id ->
Block_services.hash cctxt ~chain ~block () >>=? fun hash ->
Block_services.Header.shell_header cctxt ~chain ~block () >>=? fun header ->
Block_services.Metadata.next_protocol_hash
cctxt ~chain ~block () >>=? fun next_protocol ->
Block_services.Metadata.protocol_hash
cctxt ~chain ~block () >>=? fun protocol ->
2018-04-19 20:10:28 +04:00
Block_services.Metadata.protocol_data cctxt ~chain ~block () >>=? fun { level } ->
let { Tezos_base.Block_header.predecessor ; fitness ; timestamp ; _ } = header in
return { hash ; chain_id ; predecessor ; fitness ;
timestamp ; protocol ; next_protocol ; level }
2016-09-08 21:13:10 +04:00
let monitor_valid_blocks cctxt ?chains ?protocols ?next_protocols () =
Monitor_services.valid_blocks cctxt
?chains ?protocols ?next_protocols () >>=? fun (block_stream, _stop) ->
return (Lwt_stream.map_s
(fun (chain, block) ->
info cctxt ~chain:(`Hash chain) (`Hash (block, 0))) block_stream)
2016-09-08 21:13:10 +04:00
let monitor_heads cctxt ?next_protocols chain =
Monitor_services.heads
cctxt ?next_protocols chain >>=? fun (block_stream, _stop) ->
return (Lwt_stream.map_s
(fun block -> info cctxt ~chain (`Hash (block, 0)))
block_stream)
2016-09-08 21:13:10 +04:00
let blocks_from_cycle cctxt ?(chain = `Main) block cycle =
Block_services.hash cctxt ~chain ~block () >>=? fun hash ->
2018-04-19 20:10:28 +04:00
Block_services.Metadata.protocol_data cctxt ~chain ~block () >>=? fun { level } ->
Alpha_services.Helpers.levels cctxt (chain, block) cycle >>=? fun (first, last) ->
let length = Int32.to_int (Raw_level.diff level.level first) in
Chain_services.Blocks.list cctxt ~heads:[hash] ~length () >>=? fun blocks ->
let blocks =
List.remove
(length - (Int32.to_int (Raw_level.diff last first)))
(List.hd blocks) in
if Raw_level.(level.level = last) then
return (hash :: blocks)
else
return blocks