From 9f45ae9126250797d33509c18cb6cdf6e50fe1a3 Mon Sep 17 00:00:00 2001 From: Milo Davis Date: Sun, 26 Nov 2017 22:21:56 +0100 Subject: [PATCH] Node: listing of invalid blocks --- src/node/shell/node.ml | 3 +++ src/node/shell/node.mli | 3 +++ src/node/shell/node_rpc.ml | 6 ++++++ src/node/shell/node_rpc_services.ml | 13 +++++++++++++ src/node/shell/node_rpc_services.mli | 3 +++ src/node/shell/state.ml | 6 ++++++ src/node/shell/state.mli | 1 + 7 files changed, 35 insertions(+) diff --git a/src/node/shell/node.ml b/src/node/shell/node.ml index 7daa2d220..4be8e7f67 100644 --- a/src/node/shell/node.ml +++ b/src/node/shell/node.ml @@ -589,6 +589,9 @@ module RPC = struct heads >>= fun (_, blocks) -> Lwt.return (List.rev blocks) + let list_invalid node = + State.Block.list_invalid (Net_validator.net_state node.mainnet_validator) + let block_header_watcher node = Distributed_db.watch_block_header node.distributed_db diff --git a/src/node/shell/node.mli b/src/node/shell/node.mli index 0c058a850..9fa4a90e0 100644 --- a/src/node/shell/node.mli +++ b/src/node/shell/node.mli @@ -64,6 +64,9 @@ module RPC : sig val list: t -> int -> Block_hash.t list -> block_info list list Lwt.t + val list_invalid: + t -> (Block_hash.t * int32 * error list) list Lwt.t + val block_info: t -> block -> block_info Lwt.t diff --git a/src/node/shell/node_rpc.ml b/src/node/shell/node_rpc.ml index 91c4281ce..858a6ef12 100644 --- a/src/node/shell/node_rpc.ml +++ b/src/node/shell/node_rpc.ml @@ -329,6 +329,10 @@ let list_blocks RPC.Answer.return_stream { next ; shutdown } end +let list_invalid node () = + Node.RPC.list_invalid node >>= fun l -> + RPC.Answer.return l + let list_protocols node {Services.Protocols.monitor; contents} = let monitor = match monitor with None -> false | Some x -> x in let include_contents = match contents with None -> false | Some x -> x in @@ -369,6 +373,8 @@ let build_rpc_directory node = let dir = RPC.empty in let dir = RPC.register0 dir Services.Blocks.list (list_blocks node) in + let dir = + RPC.register0 dir Services.Blocks.list_invalid (list_invalid node) in let dir = register_bi_dir node dir in let dir = let implementation block = diff --git a/src/node/shell/node_rpc_services.ml b/src/node/shell/node_rpc_services.ml index 1fdb833ac..527affc08 100644 --- a/src/node/shell/node_rpc_services.ml +++ b/src/node/shell/node_rpc_services.ml @@ -406,6 +406,19 @@ module Blocks = struct ~output: (obj1 (req "blocks" (list (list block_info_encoding)))) RPC.Path.(root / "blocks") + let list_invalid = + RPC.service + ~description: + "Lists blocks that have been declared invalid along with the errors\ + that led to them being declared invalid" + ~input:empty + ~output:(Data_encoding.list + (obj3 + (req "block" Block_hash.encoding) + (req "level" int32) + (req "errors" (Data_encoding.list error_encoding)))) + RPC.Path.(root / "invalid_blocks") + end module Protocols = struct diff --git a/src/node/shell/node_rpc_services.mli b/src/node/shell/node_rpc_services.mli index 6977efb10..0e3bf6f5e 100644 --- a/src/node/shell/node_rpc_services.mli +++ b/src/node/shell/node_rpc_services.mli @@ -87,6 +87,9 @@ module Blocks : sig val list: (unit, unit, list_param, block_info list list) RPC.service + val list_invalid: + (unit, unit, unit, (Block_hash.t * int32 * error list) list) RPC.service + type preapply_param = { timestamp: Time.t ; proto_header: MBytes.t ; diff --git a/src/node/shell/state.ml b/src/node/shell/state.ml index 808a73d8f..8e1fce062 100644 --- a/src/node/shell/state.ml +++ b/src/node/shell/state.ml @@ -380,6 +380,12 @@ module Block = struct Shared.use net_state.block_store begin fun store -> Store.Block.Invalid_block.read_opt store hash end + let list_invalid net_state = + Shared.use net_state.block_store begin fun store -> + Store.Block.Invalid_block.fold store ~init:[] + ~f:(fun hash { level ; errors } acc -> + Lwt.return ((hash, level, errors) :: acc)) + end let known net_state hash = Shared.use net_state.block_store begin fun store -> diff --git a/src/node/shell/state.mli b/src/node/shell/state.mli index e593d96cd..73b6ee9ec 100644 --- a/src/node/shell/state.mli +++ b/src/node/shell/state.mli @@ -98,6 +98,7 @@ module Block : sig val known_valid: Net.t -> Block_hash.t -> bool Lwt.t val known_invalid: Net.t -> Block_hash.t -> bool Lwt.t val read_invalid: Net.t -> Block_hash.t -> Store.Block.invalid_block option Lwt.t + val list_invalid: Net.t -> (Block_hash.t * int32 * error list) list Lwt.t val read: Net.t -> Block_hash.t -> block tzresult Lwt.t val read_opt: Net.t -> Block_hash.t -> block option Lwt.t