P2P: allow to limit the size of mempool

This commit is contained in:
Grégoire Henry 2018-06-02 18:30:40 +02:00 committed by Benjamin Canou
parent fed04d4b8f
commit 6f4a98f6fd
4 changed files with 22 additions and 2 deletions

View File

@ -19,9 +19,17 @@ let encoding =
(fun { known_valid ; pending } -> (known_valid, pending))
(fun (known_valid, pending) -> { known_valid ; pending })
(obj2
(req "known_valid" (dynamic_size (list Operation_hash.encoding)))
(req "known_valid" (list Operation_hash.encoding))
(req "pending" (dynamic_size Operation_hash.Set.encoding)))
let bounded_encoding ?max_operations () =
match max_operations with
| None -> encoding
| Some max_operations ->
Data_encoding.check_size
(8 + max_operations * Operation_hash.size)
encoding
let empty = {
known_valid = [] ;
pending = Operation_hash.Set.empty ;

View File

@ -19,6 +19,7 @@ type t = {
type mempool = t
val encoding: mempool Data_encoding.t
val bounded_encoding: ?max_operations:int -> unit -> mempool Data_encoding.t
val empty: mempool
(** Empty mempool. *)

View File

@ -93,6 +93,16 @@ module Bounded_encoding = struct
protocol_max_size := max
let protocol = delayed (fun () -> !protocol_cache)
let mempool_max_operations = ref None
let mempool_cache =
ref (Mempool.bounded_encoding ?max_operations:!mempool_max_operations ())
let update_mempool_encoding () =
mempool_cache :=
Mempool.bounded_encoding ?max_operations:!mempool_max_operations ()
let set_mempool_max_operations max =
mempool_max_operations := max
let mempool = delayed (fun () -> !mempool_cache)
end
type t =
@ -170,7 +180,7 @@ let encoding =
(obj3
(req "chain_id" Chain_id.encoding)
(req "current_block_header" (dynamic_size Bounded_encoding.block_header))
(req "current_mempool" Mempool.encoding))
(req "current_mempool" Bounded_encoding.mempool))
(function
| Current_head (chain_id, bh, mempool) -> Some (chain_id, bh, mempool)
| _ -> None)

View File

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