@@ -4,6 +4,7 @@ use std::{
44 net:: IpAddr ,
55 path:: Path ,
66 sync:: Arc ,
7+ time:: Duration ,
78} ;
89
910use anyhow:: Context ;
@@ -34,13 +35,10 @@ pub struct NodeBuilder {
3435 rng_seed : [ u8 ; 32 ] ,
3536 custom_initial_time : Option < redux:: Timestamp > ,
3637 genesis_config : Arc < GenesisConfig > ,
38+ p2p : P2pConfig ,
3739 p2p_sec_key : Option < P2pSecretKey > ,
38- p2p_libp2p_port : Option < u16 > ,
3940 p2p_is_seed : bool ,
40- p2p_no_discovery : bool ,
4141 p2p_is_started : bool ,
42- initial_peers : Vec < P2pConnectionOutgoingInitOpts > ,
43- external_addrs : Vec < IpAddr > ,
4442 block_producer : Option < BlockProducerConfig > ,
4543 snarker : Option < SnarkerConfig > ,
4644 service : NodeServiceBuilder ,
@@ -68,13 +66,25 @@ impl NodeBuilder {
6866 rng_seed,
6967 custom_initial_time : None ,
7068 genesis_config,
69+ p2p : P2pConfig {
70+ libp2p_port : None ,
71+ listen_port : None ,
72+ // Must be replaced with builder api.
73+ identity_pub_key : P2pSecretKey :: deterministic ( 0 ) . public_key ( ) ,
74+ initial_peers : Vec :: new ( ) ,
75+ external_addrs : Vec :: new ( ) ,
76+ enabled_channels : ChannelId :: iter_all ( ) . collect ( ) ,
77+ peer_discovery : true ,
78+ meshsub : P2pMeshsubConfig {
79+ initial_time : Duration :: ZERO ,
80+ ..Default :: default ( )
81+ } ,
82+ timeouts : P2pTimeouts :: default ( ) ,
83+ limits : P2pLimits :: default ( ) . with_max_peers ( Some ( 100 ) ) ,
84+ } ,
7185 p2p_sec_key : None ,
72- p2p_libp2p_port : None ,
7386 p2p_is_seed : false ,
74- p2p_no_discovery : false ,
7587 p2p_is_started : false ,
76- initial_peers : Vec :: new ( ) ,
77- external_addrs : Vec :: new ( ) ,
7888 block_producer : None ,
7989 snarker : None ,
8090 service : NodeServiceBuilder :: new ( rng_seed) ,
@@ -94,12 +104,13 @@ impl NodeBuilder {
94104
95105 /// If not called, random one will be generated and used instead.
96106 pub fn p2p_sec_key ( & mut self , key : P2pSecretKey ) -> & mut Self {
107+ self . p2p . identity_pub_key = key. public_key ( ) ;
97108 self . p2p_sec_key = Some ( key) ;
98109 self
99110 }
100111
101112 pub fn p2p_libp2p_port ( & mut self , port : u16 ) -> & mut Self {
102- self . p2p_libp2p_port = Some ( port) ;
113+ self . p2p . libp2p_port = Some ( port) ;
103114 self
104115 }
105116
@@ -110,7 +121,7 @@ impl NodeBuilder {
110121 }
111122
112123 pub fn p2p_no_discovery ( & mut self ) -> & mut Self {
113- self . p2p_no_discovery = true ;
124+ self . p2p . peer_discovery = false ;
114125 self
115126 }
116127
@@ -119,19 +130,19 @@ impl NodeBuilder {
119130 & mut self ,
120131 peers : impl IntoIterator < Item = P2pConnectionOutgoingInitOpts > ,
121132 ) -> & mut Self {
122- self . initial_peers . extend ( peers) ;
133+ self . p2p . initial_peers . extend ( peers) ;
123134 self
124135 }
125136
126137 pub fn external_addrs ( & mut self , v : impl Iterator < Item = IpAddr > ) -> & mut Self {
127- self . external_addrs . extend ( v) ;
138+ self . p2p . external_addrs . extend ( v) ;
128139 self
129140 }
130141
131142 /// Extend p2p initial peers from file.
132143 pub fn initial_peers_from_file ( & mut self , path : impl AsRef < Path > ) -> anyhow:: Result < & mut Self > {
133144 peers_from_reader (
134- & mut self . initial_peers ,
145+ & mut self . p2p . initial_peers ,
135146 File :: open ( & path) . context ( anyhow:: anyhow!(
136147 "opening peer list file {:?}" ,
137148 path. as_ref( )
@@ -152,14 +163,19 @@ impl NodeBuilder {
152163 ) -> anyhow:: Result < & mut Self > {
153164 let url = url. into_url ( ) . context ( "failed to parse peers url" ) ?;
154165 peers_from_reader (
155- & mut self . initial_peers ,
166+ & mut self . p2p . initial_peers ,
156167 reqwest:: blocking:: get ( url. clone ( ) )
157168 . context ( anyhow:: anyhow!( "reading peer list url {url}" ) ) ?,
158169 )
159170 . context ( anyhow:: anyhow!( "reading peer list url {url}" ) ) ?;
160171 Ok ( self )
161172 }
162173
174+ pub fn p2p_max_peers ( & mut self , limit : usize ) -> & mut Self {
175+ self . p2p . limits = self . p2p . limits . with_max_peers ( Some ( limit) ) ;
176+ self
177+ }
178+
163179 /// Override default p2p task spawner.
164180 pub fn p2p_custom_task_spawner (
165181 & mut self ,
@@ -274,15 +290,15 @@ impl NodeBuilder {
274290 self
275291 }
276292
277- pub fn build ( self ) -> anyhow:: Result < Node > {
293+ pub fn build ( mut self ) -> anyhow:: Result < Node > {
278294 let p2p_sec_key = self . p2p_sec_key . unwrap_or_else ( P2pSecretKey :: rand) ;
279- let initial_peers = if self . initial_peers . is_empty ( ) && !self . p2p_is_seed {
280- default_peers ( )
281- } else {
282- self . initial_peers
283- } ;
295+ if self . p2p . initial_peers . is_empty ( ) && !self . p2p_is_seed {
296+ self . p2p . initial_peers = default_peers ( ) ;
297+ }
284298
285- let initial_peers = initial_peers
299+ self . p2p . initial_peers = self
300+ . p2p
301+ . initial_peers
286302 . into_iter ( )
287303 . filter_map ( |opts| match opts {
288304 P2pConnectionOutgoingInitOpts :: LibP2P ( mut opts) => {
@@ -293,8 +309,6 @@ impl NodeBuilder {
293309 } )
294310 . collect ( ) ;
295311
296- let external_addrs = self . external_addrs ;
297-
298312 let srs = self . verifier_srs . unwrap_or_else ( get_srs) ;
299313 let block_verifier_index = self
300314 . block_verifier_index
@@ -306,6 +320,9 @@ impl NodeBuilder {
306320 let initial_time = self
307321 . custom_initial_time
308322 . unwrap_or_else ( redux:: Timestamp :: global_now) ;
323+ self . p2p . meshsub . initial_time = initial_time
324+ . checked_sub ( redux:: Timestamp :: ZERO )
325+ . unwrap_or_default ( ) ;
309326
310327 let protocol_constants = self . genesis_config . protocol_constants ( ) ?;
311328 let consensus_consts =
@@ -319,23 +336,7 @@ impl NodeBuilder {
319336 consensus_constants : consensus_consts. clone ( ) ,
320337 testing_run : false ,
321338 } ,
322- p2p : P2pConfig {
323- libp2p_port : self . p2p_libp2p_port ,
324- listen_port : self . http_port ,
325- identity_pub_key : p2p_sec_key. public_key ( ) ,
326- initial_peers,
327- external_addrs,
328- enabled_channels : ChannelId :: iter_all ( ) . collect ( ) ,
329- peer_discovery : !self . p2p_no_discovery ,
330- meshsub : P2pMeshsubConfig {
331- initial_time : initial_time
332- . checked_sub ( redux:: Timestamp :: ZERO )
333- . unwrap_or_default ( ) ,
334- ..Default :: default ( )
335- } ,
336- timeouts : P2pTimeouts :: default ( ) ,
337- limits : P2pLimits :: default ( ) . with_max_peers ( Some ( 100 ) ) ,
338- } ,
339+ p2p : self . p2p ,
339340 ledger : LedgerConfig { } ,
340341 snark : SnarkConfig {
341342 block_verifier_index,
0 commit comments