Client/Revelation: reduce the size of RPC requests

This commit is contained in:
Grégoire Henry 2017-02-15 21:18:48 +01:00
parent 2f6863da89
commit 20057079c6
12 changed files with 45 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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) =

View File

@ -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 ->

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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