P2P: allow to limit the size of protocols

This commit is contained in:
Grégoire Henry 2018-06-02 18:23:08 +02:00 committed by Benjamin Canou
parent 99f6e40471
commit fed04d4b8f
4 changed files with 19 additions and 1 deletions

View File

@ -53,6 +53,11 @@ let encoding =
(req "expected_env_version" env_version_encoding) (req "expected_env_version" env_version_encoding)
(req "components" (list component_encoding))) (req "components" (list component_encoding)))
let bounded_encoding ?max_size () =
match max_size with
| None -> encoding
| Some max_size -> Data_encoding.check_size max_size encoding
let pp ppf op = let pp ppf op =
Data_encoding.Json.pp ppf Data_encoding.Json.pp ppf
(Data_encoding.Json.construct encoding op) (Data_encoding.Json.construct encoding op)

View File

@ -29,6 +29,8 @@ include S.HASHABLE with type t := t
and type hash := Protocol_hash.t and type hash := Protocol_hash.t
val of_bytes_exn: MBytes.t -> t val of_bytes_exn: MBytes.t -> t
val bounded_encoding: ?max_size:int -> unit -> t Data_encoding.t
module Meta: sig module Meta: sig
type t = { type t = {

View File

@ -83,6 +83,16 @@ module Bounded_encoding = struct
let operation_list = delayed (fun () -> !operation_list_cache) let operation_list = delayed (fun () -> !operation_list_cache)
let operation_hash_list = delayed (fun () -> !operation_hash_list_cache) let operation_hash_list = delayed (fun () -> !operation_hash_list_cache)
let protocol_max_size = ref None
let protocol_cache =
ref (Protocol.bounded_encoding ?max_size:!protocol_max_size ())
let update_protocol_encoding () =
protocol_cache :=
Protocol.bounded_encoding ?max_size:!protocol_max_size ()
let set_protocol_max_size max =
protocol_max_size := max
let protocol = delayed (fun () -> !protocol_cache)
end end
type t = type t =
@ -207,7 +217,7 @@ let encoding =
case ~tag:0x41 case ~tag:0x41
~title:"Protocol" ~title:"Protocol"
(obj1 (req "protocol" Protocol.encoding)) (obj1 (req "protocol" Bounded_encoding.protocol))
(function Protocol proto -> Some proto | _ -> None) (function Protocol proto -> Some proto | _ -> None)
(fun proto -> Protocol proto); (fun proto -> Protocol proto);

View File

@ -47,4 +47,5 @@ module Bounded_encoding : sig
val set_operation_list_max_size: int option -> unit val set_operation_list_max_size: int option -> unit
val set_operation_list_max_length: int option -> unit val set_operation_list_max_length: int option -> unit
val set_operation_max_pass: int option -> unit val set_operation_max_pass: int option -> unit
val set_protocol_max_size: int option -> unit
end end