From 20057079c61cdd862d610bcd676e2cc3eb590e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Wed, 15 Feb 2017 21:18:48 +0100 Subject: [PATCH] Client/Revelation: reduce the size of RPC requests --- .../alpha/baker/client_mining_blocks.ml | 24 ++++++------------- .../alpha/baker/client_mining_blocks.mli | 2 +- .../alpha/baker/client_mining_forge.ml | 22 +++++++++-------- .../alpha/baker/client_mining_operations.ml | 2 +- .../embedded/alpha/client_proto_rpcs.mli | 2 +- src/proto/alpha/level_repr.ml | 7 ++++++ src/proto/alpha/level_repr.mli | 1 + src/proto/alpha/raw_level_repr.ml | 2 ++ src/proto/alpha/raw_level_repr.mli | 2 ++ src/proto/alpha/services.ml | 5 +++- src/proto/alpha/services_registration.ml | 5 +++- src/proto/alpha/tezos_context.mli | 3 +++ 12 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/client/embedded/alpha/baker/client_mining_blocks.ml b/src/client/embedded/alpha/baker/client_mining_blocks.ml index 9375f6d03..0d25c8bba 100644 --- a/src/client/embedded/alpha/baker/client_mining_blocks.ml +++ b/src/client/embedded/alpha/baker/client_mining_blocks.ml @@ -69,21 +69,11 @@ let blocks_from_cycle cctxt block cycle = | `Prevalidation -> `Head 0 | `Test_prevalidation -> `Test_head 0 | _ -> block in - Client_node_rpcs.Blocks.hash cctxt block >>= fun block_hash -> Client_proto_rpcs.Context.level cctxt block >>=? fun level -> - Client_proto_rpcs.Helpers.levels cctxt block cycle >>=? fun block_levels -> - begin - match List.sort Level.compare block_levels with - | [] -> failwith "Internal error" - | hd :: _ -> return hd - end >>=? fun min_level -> - let length = 1 + Int32.to_int (Level.diff level min_level) in - begin - Client_node_rpcs.Blocks.list cctxt ~length ~heads:[block_hash] () >>= function - | [] | _::_::_ -> failwith "Unexpected RPC result" - | [blocks] -> return blocks - end >>=? fun block_infos -> - let block_infos = - Utils.remove_elem_from_list (length - List.length block_levels) block_infos in - map_s (convert_block_info_err cctxt) block_infos >>=? fun block_res -> - return block_res + Client_proto_rpcs.Helpers.levels cctxt block cycle >>=? fun (first, last) -> + let length = Int32.to_int (Raw_level.diff level.level first) in + Client_node_rpcs.Blocks.predecessors cctxt block length >>= fun blocks -> + let blocks = + Utils.remove_elem_from_list + (length - (1 + Int32.to_int (Raw_level.diff last first))) blocks in + return blocks diff --git a/src/client/embedded/alpha/baker/client_mining_blocks.mli b/src/client/embedded/alpha/baker/client_mining_blocks.mli index f6ff288ce..654fda8c0 100644 --- a/src/client/embedded/alpha/baker/client_mining_blocks.mli +++ b/src/client/embedded/alpha/baker/client_mining_blocks.mli @@ -34,4 +34,4 @@ val blocks_from_cycle: Client_commands.context -> Client_node_rpcs.Blocks.block -> Cycle.t -> - block_info list tzresult Lwt.t + Block_hash.t list tzresult Lwt.t diff --git a/src/client/embedded/alpha/baker/client_mining_forge.ml b/src/client/embedded/alpha/baker/client_mining_forge.ml index 43a33fd6c..549c498cb 100644 --- a/src/client/embedded/alpha/baker/client_mining_forge.ml +++ b/src/client/embedded/alpha/baker/client_mining_forge.ml @@ -307,29 +307,31 @@ let get_unrevealed_nonces cctxt ?(force = false) block = | None -> return [] | Some cycle -> Client_mining_blocks.blocks_from_cycle - cctxt block cycle >>=? fun block_infos -> - map_filter_s (fun (bi : Client_mining_blocks.block_info) -> - Client_proto_nonces.find cctxt bi.hash >>= function + cctxt block cycle >>=? fun blocks -> + map_filter_s (fun hash -> + Client_proto_nonces.find cctxt hash >>= function | None -> return None | Some nonce -> + Client_proto_rpcs.Context.level + cctxt (`Hash hash) >>=? fun level -> if force then - return (Some (bi.hash, (bi.level.level, nonce))) + return (Some (hash, (level.level, nonce))) else Client_proto_rpcs.Context.Nonce.get - cctxt block bi.level.level >>=? function + cctxt block level.level >>=? function | Missing nonce_hash when Nonce.check_hash nonce nonce_hash -> cctxt.warning "Found nonce for %a (level: %a)@." - Block_hash.pp_short bi.hash - Level.pp bi.level >>= fun () -> - return (Some (bi.hash, (bi.level.level, nonce))) + Block_hash.pp_short hash + Level.pp level >>= fun () -> + return (Some (hash, (level.level, nonce))) | Missing _nonce_hash -> cctxt.error "Incoherent nonce for level %a" - Raw_level.pp bi.level.level >>= fun () -> + Raw_level.pp level.level >>= fun () -> return None | Forgotten -> return None | Revealed _ -> return None) - block_infos + blocks let insert_block cctxt ?max_priority state (bi: Client_mining_blocks.block_info) = diff --git a/src/client/embedded/alpha/baker/client_mining_operations.ml b/src/client/embedded/alpha/baker/client_mining_operations.ml index ab14ef488..604bd7ed2 100644 --- a/src/client/embedded/alpha/baker/client_mining_operations.ml +++ b/src/client/embedded/alpha/baker/client_mining_operations.ml @@ -95,7 +95,7 @@ let monitor_endorsement cctxt = let endorsement_stream, push = Lwt_stream.create () in Lwt.async begin fun () -> Lwt_stream.closed ops_stream >|= fun () -> push None - end; + end ; Lwt.async begin fun () -> Lwt_stream.iter_p (Lwt_list.iter_p (fun e -> diff --git a/src/client/embedded/alpha/client_proto_rpcs.mli b/src/client/embedded/alpha/client_proto_rpcs.mli index d4be335af..ea74062af 100644 --- a/src/client/embedded/alpha/client_proto_rpcs.mli +++ b/src/client/embedded/alpha/client_proto_rpcs.mli @@ -162,7 +162,7 @@ module Helpers : sig block -> ?offset:int32 -> Raw_level.t -> Level.t tzresult Lwt.t val levels: Client_commands.context -> - block -> Cycle.t -> Level.t list tzresult Lwt.t + block -> Cycle.t -> (Raw_level.t * Raw_level.t) tzresult Lwt.t module Rights : sig type slot = Raw_level.t * int * Time.t option diff --git a/src/proto/alpha/level_repr.ml b/src/proto/alpha/level_repr.ml index b39f700ed..7972b3fcd 100644 --- a/src/proto/alpha/level_repr.ml +++ b/src/proto/alpha/level_repr.ml @@ -20,6 +20,13 @@ type level = t let pp ppf { level } = Raw_level_repr.pp ppf level +let pp_full ppf l = + Format.fprintf ppf + "%a (cycle %a.%ld) (vote %a.%ld)" + Raw_level_repr.pp l.level + Cycle_repr.pp l.cycle l.cycle_position + Voting_period_repr.pp l.voting_period l.voting_period_position + let encoding = let open Data_encoding in conv diff --git a/src/proto/alpha/level_repr.mli b/src/proto/alpha/level_repr.mli index 842f60373..4358be1e5 100644 --- a/src/proto/alpha/level_repr.mli +++ b/src/proto/alpha/level_repr.mli @@ -18,6 +18,7 @@ type t = private { type level = t val encoding: level Data_encoding.t val pp: Format.formatter -> level -> unit +val pp_full: Format.formatter -> level -> unit include Compare.S with type t := level val root: level diff --git a/src/proto/alpha/raw_level_repr.ml b/src/proto/alpha/raw_level_repr.ml index ab1d73b81..3047588c6 100644 --- a/src/proto/alpha/raw_level_repr.ml +++ b/src/proto/alpha/raw_level_repr.ml @@ -32,6 +32,8 @@ let pred l = then None else Some (Int32.pred l) +let diff = Int32.sub + let to_int32 l = l let of_int32_exn l = if Compare.Int32.(l >= 0l) diff --git a/src/proto/alpha/raw_level_repr.mli b/src/proto/alpha/raw_level_repr.mli index 3ac9868e1..fac16df55 100644 --- a/src/proto/alpha/raw_level_repr.mli +++ b/src/proto/alpha/raw_level_repr.mli @@ -17,6 +17,8 @@ include Compare.S with type t := raw_level val to_int32: raw_level -> int32 val of_int32_exn: int32 -> raw_level +val diff: raw_level -> raw_level -> int32 + val root: raw_level val succ: raw_level -> raw_level diff --git a/src/proto/alpha/services.ml b/src/proto/alpha/services.ml index 2e7267364..7fec376fd 100644 --- a/src/proto/alpha/services.ml +++ b/src/proto/alpha/services.ml @@ -415,7 +415,10 @@ module Helpers = struct ~description: "Levels of a cycle" ~input: empty ~output: (wrap_tzerror @@ - describe ~title: "levels of a cycle" (list Level.encoding)) + describe ~title: "levels of a cycle" + (obj2 + (req "first" Raw_level.encoding) + (req "last" Raw_level.encoding))) RPC.Path.(custom_root / "helpers" / "levels" /: Cycle.arg) module Rights = struct diff --git a/src/proto/alpha/services_registration.ml b/src/proto/alpha/services_registration.ml index 2231435db..06593e495 100644 --- a/src/proto/alpha/services_registration.ml +++ b/src/proto/alpha/services_registration.ml @@ -263,7 +263,10 @@ let compute_level ctxt raw offset = let () = register2 Services.Helpers.level compute_level let levels ctxt cycle () = - return (Level.levels_in_cycle ctxt cycle) + let levels = Level.levels_in_cycle ctxt cycle in + let first = List.hd (List.rev levels) in + let last = List.hd levels in + return (first.level, last.level) let () = register2 Services.Helpers.levels levels diff --git a/src/proto/alpha/tezos_context.mli b/src/proto/alpha/tezos_context.mli index e89cf4a40..95116d2d2 100644 --- a/src/proto/alpha/tezos_context.mli +++ b/src/proto/alpha/tezos_context.mli @@ -79,6 +79,8 @@ module Raw_level : sig type raw_level = t val arg: raw_level RPC.Arg.arg + val diff: raw_level -> raw_level -> int32 + val root: raw_level val succ: raw_level -> raw_level val pred: raw_level -> raw_level option @@ -229,6 +231,7 @@ module Level : sig voting_period_position: int32 ; } include BASIC_DATA with type t := t + val pp_full: Format.formatter -> t -> unit type level = t val root: level