2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
type block_info = {
|
|
|
|
hash: Block_hash.t ;
|
|
|
|
predecessor: Block_hash.t ;
|
|
|
|
fitness: MBytes.t list ;
|
|
|
|
timestamp: Time.t ;
|
2017-04-10 19:06:11 +04:00
|
|
|
protocol: Protocol_hash.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
level: Level.t ;
|
|
|
|
}
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let convert_block_info cctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
( { hash ; predecessor ; fitness ; timestamp ; protocol }
|
|
|
|
: Client_node_rpcs.Blocks.block_info ) =
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.level cctxt (`Hash hash) >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Ok level ->
|
2017-04-10 19:06:11 +04:00
|
|
|
Lwt.return
|
|
|
|
(Some { hash ; predecessor ; fitness ; timestamp ; protocol ; level })
|
2016-09-08 21:13:10 +04:00
|
|
|
| Error _ ->
|
|
|
|
(* TODO log error *)
|
|
|
|
Lwt.return_none
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let convert_block_info_err cctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
( { hash ; predecessor ; fitness ; timestamp ; protocol }
|
|
|
|
: Client_node_rpcs.Blocks.block_info ) =
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.level cctxt (`Hash hash) >>=? fun level ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return { hash ; predecessor ; fitness ; timestamp ; protocol ; level }
|
|
|
|
|
2017-04-10 19:06:11 +04:00
|
|
|
let info cctxt ?include_ops block =
|
|
|
|
Client_node_rpcs.Blocks.info cctxt ?include_ops block >>=? fun block ->
|
2016-12-03 16:05:02 +04:00
|
|
|
convert_block_info_err cctxt block
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let compare (bi1 : block_info) (bi2 : block_info) =
|
|
|
|
match Fitness.compare bi1.fitness bi2.fitness with
|
|
|
|
| 0 -> begin
|
|
|
|
match compare bi1.level bi2.level with
|
|
|
|
| 0 -> begin
|
|
|
|
match Time.compare bi1.timestamp bi2.timestamp with
|
|
|
|
| 0 -> Block_hash.compare bi1.predecessor bi2.predecessor
|
|
|
|
| x -> - x
|
|
|
|
end
|
|
|
|
| x -> - x
|
|
|
|
end
|
|
|
|
| x -> x
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let sort_blocks cctxt ?(compare = compare) blocks =
|
2017-02-28 11:18:06 +04:00
|
|
|
Lwt_list.filter_map_p (convert_block_info cctxt) blocks >|= fun blocks ->
|
2016-09-08 21:13:10 +04:00
|
|
|
List.sort compare blocks
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let monitor cctxt
|
2017-04-10 19:06:11 +04:00
|
|
|
?include_ops ?length ?heads ?delay
|
2016-09-21 18:22:43 +04:00
|
|
|
?min_date ?min_heads ?compare () =
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.Blocks.monitor cctxt
|
2017-04-10 19:06:11 +04:00
|
|
|
?include_ops ?length ?heads ?delay ?min_date ?min_heads
|
2017-04-05 01:35:41 +04:00
|
|
|
() >>=? fun block_stream ->
|
|
|
|
let convert blocks =
|
|
|
|
Lwt.return blocks >>=? fun blocks ->
|
|
|
|
sort_blocks cctxt ?compare (List.flatten blocks) >>= return in
|
|
|
|
return (Lwt_stream.map_s convert block_stream)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let blocks_from_cycle cctxt block cycle =
|
2017-04-20 10:26:43 +04:00
|
|
|
let block = Client_rpcs.last_mined_block block in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.level cctxt block >>=? fun level ->
|
2017-02-16 00:18:48 +04:00
|
|
|
Client_proto_rpcs.Helpers.levels cctxt block cycle >>=? fun (first, last) ->
|
|
|
|
let length = Int32.to_int (Raw_level.diff level.level first) in
|
2017-04-05 01:35:41 +04:00
|
|
|
Client_node_rpcs.Blocks.predecessors cctxt block length >>=? fun blocks ->
|
2017-02-16 00:18:48 +04:00
|
|
|
let blocks =
|
|
|
|
Utils.remove_elem_from_list
|
|
|
|
(length - (1 + Int32.to_int (Raw_level.diff last first))) blocks in
|
|
|
|
return blocks
|