diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml index f3a1f4d13c8bc6eaf561e18bf130dcdb1ba000c8..7f034590bb93615f21d8d9870315b569813f1177 100644 --- a/src/bin_node/node_config_file.ml +++ b/src/bin_node/node_config_file.ml @@ -76,6 +76,7 @@ let default_p2p_limits : P2p.limits = { connection_timeout = 10. ; authentication_timeout = 5. ; greylist_timeout = 86400 ; (* one day *) + maintenance_idle_time = 120. ; (* two minutes *) min_connections = 10 ; expected_connections = 50 ; max_connections = 100 ; @@ -133,6 +134,7 @@ let limit : P2p.limits Data_encoding.t = let open Data_encoding in conv (fun { P2p.connection_timeout ; authentication_timeout ; greylist_timeout ; + maintenance_idle_time ; min_connections ; expected_connections ; max_connections ; backlog ; max_incoming_connections ; max_download_speed ; max_upload_speed ; @@ -152,7 +154,7 @@ let limit : P2p.limits Data_encoding.t = incoming_message_queue_size, outgoing_message_queue_size, known_points_history_size, known_peer_ids_history_size, max_known_points)), - ( max_known_peer_ids, greylist_timeout)))) + ( max_known_peer_ids, greylist_timeout, maintenance_idle_time)))) (fun (((( connection_timeout, authentication_timeout, min_connections, expected_connections, max_connections, backlog, max_incoming_connections, @@ -162,8 +164,9 @@ let limit : P2p.limits Data_encoding.t = incoming_message_queue_size, outgoing_message_queue_size, known_points_history_size, known_peer_ids_history_size, max_known_points)), - ( max_known_peer_ids, greylist_timeout))) -> + ( max_known_peer_ids, greylist_timeout, maintenance_idle_time))) -> { connection_timeout ; authentication_timeout ; greylist_timeout ; + maintenance_idle_time ; min_connections ; expected_connections ; max_connections ; backlog ; max_incoming_connections ; max_download_speed ; max_upload_speed ; @@ -234,13 +237,17 @@ let limit : P2p.limits Data_encoding.t = default_p2p_limits.known_points_history_size) (opt "max_known_points" (tup2 uint16 uint16)) )) - (obj2 + (obj3 (opt "max_known_peer_ids" (tup2 uint16 uint16)) (dft "greylist-timeout" ~description: "GC delay for the greylists tables, in seconds." int31 default_p2p_limits.greylist_timeout) - - )) + (dft "maintenance-idle-time" + ~description: "How long to wait at most, in seconds, \ + before running a maintenance loop." + float default_p2p_limits.maintenance_idle_time) + ) + ) let p2p = let open Data_encoding in diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index 506bdaeb3bcd401e0d7bf5b13651832ba83d54c2..86bfd06c84d0d4a48bf6d56248200c830325b0d0 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -72,6 +72,7 @@ type limits = { connection_timeout : float ; authentication_timeout : float ; greylist_timeout : int ; + maintenance_idle_time : float ; min_connections : int ; expected_connections : int ; @@ -126,7 +127,6 @@ let create_connection_pool config limits meta_cfg conn_meta_cfg msg_cfg io_sched max_incoming_connections = limits.max_incoming_connections ; connection_timeout = limits.connection_timeout ; authentication_timeout = limits.authentication_timeout ; - greylist_timeout = limits.greylist_timeout ; incoming_app_message_queue_size = limits.incoming_app_message_queue_size ; incoming_message_queue_size = limits.incoming_message_queue_size ; outgoing_message_queue_size = limits.outgoing_message_queue_size ; @@ -171,11 +171,15 @@ let create_maintenance_worker limits pool config = bounds ~min:limits.min_connections ~expected:limits.expected_connections - ~max:limits.max_connections - in - let discovery = - may_create_discovery_worker limits config pool in - P2p_maintenance.create ?discovery bounds pool + ~max:limits.max_connections in + let maintenance_config = { + P2p_maintenance. + maintenance_idle_time = limits.maintenance_idle_time ; + greylist_timeout = limits.greylist_timeout ; + private_mode = config.private_mode ; + } in + let discovery = may_create_discovery_worker limits config pool in + P2p_maintenance.create ?discovery maintenance_config bounds pool let may_create_welcome_worker config limits pool = match config.listening_port with diff --git a/src/lib_p2p/p2p.mli b/src/lib_p2p/p2p.mli index c2cc68ca06995afef8dce596464889c8e2866a6f..b719a854e7391d2d32668ba40036ff8301781ab3 100644 --- a/src/lib_p2p/p2p.mli +++ b/src/lib_p2p/p2p.mli @@ -116,6 +116,9 @@ type limits = { greylist_timeout : int ; (** GC delay for the grelists tables, in seconds. *) + maintenance_idle_time: float ; + (** How long to wait at most, in seconds, before running a maintenance loop. *) + min_connections : int ; (** Strict minimum number of connections (triggers an urgent maintenance) *) diff --git a/src/lib_p2p/p2p_maintenance.ml b/src/lib_p2p/p2p_maintenance.ml index cd8f8df0930a07ed257777cd2da5ed4738431826..d6695e507bc673ec2a2b3f04ac2af6f8559129df 100644 --- a/src/lib_p2p/p2p_maintenance.ml +++ b/src/lib_p2p/p2p_maintenance.ml @@ -33,10 +33,17 @@ type bounds = { max_threshold: int ; } +type config = { + maintenance_idle_time: float ; + greylist_timeout: int ; + private_mode: bool ; +} + type 'meta pool = Pool : ('msg, 'meta, 'meta_conn) P2p_pool.t -> 'meta pool type 'meta t = { canceler: Lwt_canceler.t ; + config: config ; bounds: bounds ; pool: 'meta pool ; discovery: P2p_discovery.t option ; @@ -64,7 +71,6 @@ let connectable st start_time expected seen_points = | Some t1, Some t2 -> Time.compare t2 t1 end) in let acc = Bounded_point_info.create expected in - let private_mode = (P2p_pool.config pool).P2p_pool.private_mode in let seen_points = P2p_pool.Points.fold_known pool ~init:seen_points ~f:begin fun point pi seen_points -> @@ -75,7 +81,7 @@ let connectable st start_time expected seen_points = *) if P2p_point.Set.mem point seen_points || P2p_pool.Points.banned pool point || - (private_mode && not (P2p_point_state.Info.trusted pi)) + (st.config.private_mode && not (P2p_point_state.Info.trusted pi)) then seen_points else @@ -131,9 +137,8 @@ let rec try_to_contact let rec maintain st = let Pool pool = st.pool in let n_connected = P2p_pool.active_connections pool in - let pool_cfg = P2p_pool.config pool in let older_than = - Time.(add (now ()) (Int64.of_int (- pool_cfg.greylist_timeout))) + Time.(add (now ()) (Int64.of_int (- st.config.greylist_timeout))) in P2p_pool.gc_greylist pool ~older_than ; if n_connected < st.bounds.min_threshold then @@ -193,7 +198,7 @@ let rec worker_loop st = begin protect ~canceler:st.canceler begin fun () -> Lwt.pick [ - Lwt_unix.sleep 120. ; (* every two minutes *) + Lwt_unix.sleep st.config.maintenance_idle_time ; (* default: every two minutes *) Lwt_condition.wait st.please_maintain ; (* when asked *) P2p_pool.Pool_event.wait_too_few_connections pool ; (* limits *) P2p_pool.Pool_event.wait_too_many_connections pool ; @@ -213,8 +218,9 @@ let rec worker_loop st = | Error [ Canceled ] -> Lwt.return_unit | Error _ -> Lwt.return_unit -let create ?discovery bounds pool = { +let create ?discovery config bounds pool = { canceler = Lwt_canceler.create () ; + config ; bounds ; discovery ; pool = Pool pool ; diff --git a/src/lib_p2p/p2p_maintenance.mli b/src/lib_p2p/p2p_maintenance.mli index 0c93e15e5f1a879771c7db23a78afbb0a8ba87f4..675d1c7ff7a8a7bcbc3f93f5babe468cc4da9064 100644 --- a/src/lib_p2p/p2p_maintenance.mli +++ b/src/lib_p2p/p2p_maintenance.mli @@ -50,15 +50,32 @@ type bounds = { max_threshold: int ; } +type config = { + + maintenance_idle_time: float ; + (** How long to wait at most, in seconds, before running a maintenance loop. *) + + greylist_timeout: int ; + (** GC delay for the greylists tables, in seconds. *) + + private_mode: bool ; + (** If [true], only open outgoing/accept incoming connections + to/from peers whose addresses are in [trusted_peers], and inform + these peers that the identity of this node should be revealed to + the rest of the network. *) + +} + + type 'meta t (** Type of a maintenance worker. *) val create: ?discovery:P2p_discovery.t -> - bounds -> + config -> bounds -> ('msg, 'meta, 'meta_conn) P2p_pool.t -> 'meta t -(** [run ?discovery bounds pool] returns a maintenance worker, with +(** [run ?discovery config bounds pool] returns a maintenance worker, with the [discovery] worker if present, for [pool] with connection targets specified in [bounds]. *) diff --git a/src/lib_p2p/p2p_pool.ml b/src/lib_p2p/p2p_pool.ml index eb3587d8994884fed6e3549397a3f7cc84dec374..11a6341532c01aab0124f2dc8cdcaeb605c87a57 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -200,7 +200,6 @@ type config = { max_incoming_connections : int ; connection_timeout : float ; authentication_timeout : float ; - greylist_timeout : int ; incoming_app_message_queue_size : int option ; incoming_message_queue_size : int option ; diff --git a/src/lib_p2p/p2p_pool.mli b/src/lib_p2p/p2p_pool.mli index dddf716fd39f3a9967dc8fe832124f6916fcdb4c..7a14912e47031e3c3778327066483ea9b3494ee9 100644 --- a/src/lib_p2p/p2p_pool.mli +++ b/src/lib_p2p/p2p_pool.mli @@ -103,9 +103,6 @@ type config = { authentication_timeout : float ; (** Delay granted to a peer to perform authentication, in seconds. *) - greylist_timeout : int ; - (** GC delay for the grelists tables, in seconds. *) - incoming_app_message_queue_size : int option ; (** Size of the message queue for user messages (messages returned by this module's [read] function. *) diff --git a/src/lib_p2p/test/test_p2p_pool.ml b/src/lib_p2p/test/test_p2p_pool.ml index 8265221d34648c26432fe11cf633c1e33ae0c4d3..f4c26453d9469f01675f64cda4d70900d5d51e35 100644 --- a/src/lib_p2p/test/test_p2p_pool.ml +++ b/src/lib_p2p/test/test_p2p_pool.ml @@ -94,7 +94,6 @@ let detach_node f points n = max_incoming_connections = nb_points ; connection_timeout = 10. ; authentication_timeout = 2. ; - greylist_timeout = 2 ; incoming_app_message_queue_size = None ; incoming_message_queue_size = None ; outgoing_message_queue_size = None ;