diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index 25d338a06..86bfd06c8 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -72,7 +72,7 @@ type limits = { connection_timeout : float ; authentication_timeout : float ; greylist_timeout : int ; - maintenance_idle_time: float ; + maintenance_idle_time : float ; min_connections : int ; expected_connections : int ; @@ -127,8 +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 ; - maintenance_idle_time = limits.maintenance_idle_time ; 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 ; @@ -173,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_maintenance.ml b/src/lib_p2p/p2p_maintenance.ml index f6b8b95fa..d6695e507 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 @@ -190,11 +195,10 @@ and too_many_connections st n_connected = let rec worker_loop st = let Pool pool = st.pool in - let config = P2p_pool.config pool in begin protect ~canceler:st.canceler begin fun () -> Lwt.pick [ - Lwt_unix.sleep config.P2p_pool.maintenance_idle_time ; (* default: 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 ; @@ -214,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 0c93e15e5..675d1c7ff 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 87424efbc..11a634153 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -200,8 +200,6 @@ type config = { max_incoming_connections : int ; connection_timeout : float ; authentication_timeout : float ; - greylist_timeout : int ; - maintenance_idle_time: float ; 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 6ff77f449..7a14912e4 100644 --- a/src/lib_p2p/p2p_pool.mli +++ b/src/lib_p2p/p2p_pool.mli @@ -103,12 +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. *) - - maintenance_idle_time: float ; - (** How long to wait at most, in seconds, before running a maintenance loop. *) - 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 aa453e7f7..f4c26453d 100644 --- a/src/lib_p2p/test/test_p2p_pool.ml +++ b/src/lib_p2p/test/test_p2p_pool.ml @@ -94,8 +94,6 @@ let detach_node f points n = max_incoming_connections = nb_points ; connection_timeout = 10. ; authentication_timeout = 2. ; - greylist_timeout = 2 ; - maintenance_idle_time = 120. ; incoming_app_message_queue_size = None ; incoming_message_queue_size = None ; outgoing_message_queue_size = None ;