@@ -10,13 +10,14 @@ pub struct NodeInner {
10
10
local_id : u64 ,
11
11
peers : BTreeMap < u64 , Vec < SocketAddr > > ,
12
12
responded_ids : HashSet < u64 > ,
13
- pending_actions_buffer : VecDeque < NodeAction > ,
13
+ bootstrap_addrs : Vec < Vec < SocketAddr > > ,
14
14
}
15
15
16
16
#[ derive( Debug , Copy , Clone ) ]
17
17
enum State {
18
- Cold ,
19
- Warm ,
18
+ Init ,
19
+ ColdBootstrap ,
20
+ WarmBootstrap ,
20
21
Offline ,
21
22
Done ,
22
23
}
@@ -46,51 +47,59 @@ impl NodeInner {
46
47
let mut peers = BTreeMap :: new ( ) ;
47
48
peers. insert ( local_id, local_addrs) ;
48
49
49
- let mut node_inner = NodeInner {
50
- state : State :: Cold ,
50
+ NodeInner {
51
+ state : State :: Init ,
51
52
local_id,
52
53
peers,
53
54
responded_ids : Default :: default ( ) ,
54
- pending_actions_buffer : Default :: default ( ) ,
55
- } ;
56
- node_inner. poll_seeds ( bootstrap_addrs. into_iter ( ) ) ;
57
- node_inner
55
+ bootstrap_addrs,
56
+ }
58
57
}
59
58
60
59
pub fn update ( & mut self , events : & mut VecDeque < NodeEvent > , actions : & mut VecDeque < NodeAction > ) {
60
+ if let State :: Init = self . state {
61
+ self . init ( actions) ;
62
+ }
63
+
61
64
while let Some ( event) = events. pop_front ( ) {
62
- self . handle_event ( event) ;
65
+ self . handle_event ( event, actions ) ;
63
66
}
67
+ }
64
68
65
- for action in self . pending_actions_buffer . drain ( ..) {
66
- actions. push_back ( action) ;
69
+ fn init ( & mut self , actions_buf : & mut VecDeque < NodeAction > ) {
70
+ for ( id, seed_addrs) in self . bootstrap_addrs . clone ( ) . into_iter ( ) . enumerate ( ) {
71
+ let id = ConnectionId :: Seed ( id) ;
72
+ actions_buf. push_back ( NodeAction :: Connect ( id. clone ( ) , seed_addrs) ) ;
73
+ self . send_bootstrap_request ( id, actions_buf) ;
67
74
}
75
+
76
+ self . state = State :: ColdBootstrap ;
68
77
}
69
78
70
- fn handle_event ( & mut self , event : NodeEvent ) {
79
+ fn handle_event ( & mut self , event : NodeEvent , actions_buf : & mut VecDeque < NodeAction > ) {
71
80
use NodeEvent as E ;
72
81
use State as S ;
73
82
74
83
let new_state = match ( self . state , event) {
75
- ( S :: Cold , E :: Request ( req) )
76
- | ( S :: Cold , E :: Response ( req) )
84
+ ( S :: ColdBootstrap , E :: Request ( req) )
85
+ | ( S :: ColdBootstrap , E :: Response ( req) )
77
86
| ( S :: Offline , E :: Request ( req) ) => {
78
- self . handle_msg ( req) ;
79
- Some ( S :: Warm )
87
+ self . handle_msg ( req, actions_buf ) ;
88
+ Some ( S :: WarmBootstrap )
80
89
}
81
- ( S :: Warm , E :: Request ( req) ) | ( S :: Warm , E :: Response ( req) ) => {
82
- self . handle_msg ( req) ;
90
+ ( S :: WarmBootstrap , E :: Request ( req) ) | ( S :: WarmBootstrap , E :: Response ( req) ) => {
91
+ self . handle_msg ( req, actions_buf ) ;
83
92
84
93
let num_peers = self . peers . len ( ) ;
85
94
let num_responded = self . responded_ids . len ( ) ;
86
95
if num_peers == ( num_responded + 1 ) {
87
- self . send ( NodeAction :: Completed ) ;
96
+ actions_buf . push_back ( NodeAction :: Completed ) ;
88
97
Some ( S :: Done )
89
98
} else {
90
99
None
91
100
}
92
101
}
93
- ( S :: Cold , E :: Timeout ) => Some ( S :: Offline ) ,
102
+ ( S :: ColdBootstrap , E :: Timeout ) => Some ( S :: Offline ) ,
94
103
( S :: Offline , E :: Timeout ) => None ,
95
104
_ => panic ! ( "invalid state" ) ,
96
105
} ;
@@ -100,7 +109,7 @@ impl NodeInner {
100
109
}
101
110
}
102
111
103
- fn handle_msg ( & mut self , req : rpc:: BootstrapMsg ) {
112
+ fn handle_msg ( & mut self , req : rpc:: BootstrapMsg , actions_buf : & mut VecDeque < NodeAction > ) {
104
113
if req. from_id == self . local_id {
105
114
return ;
106
115
}
@@ -109,31 +118,22 @@ impl NodeInner {
109
118
let new_nodes = self . merge_nodes_list ( & req. nodes ) ;
110
119
for ( id, addrs) in new_nodes {
111
120
let id = ConnectionId :: Peer ( id) ;
112
- self . send ( NodeAction :: Connect ( id. clone ( ) , addrs) ) ;
113
- self . send_bootstrap_request ( id) ;
121
+ actions_buf . push_back ( NodeAction :: Connect ( id. clone ( ) , addrs) ) ;
122
+ self . send_bootstrap_request ( id, actions_buf ) ;
114
123
}
115
124
self . responded_ids . insert ( req. from_id ) ;
116
125
}
117
126
}
118
127
119
128
#[ inline]
120
- fn poll_seeds ( & mut self , addrs : impl Iterator < Item = Vec < SocketAddr > > ) {
121
- for ( id, seed_addrs) in addrs. enumerate ( ) {
122
- let id = ConnectionId :: Seed ( id) ;
123
- self . send ( NodeAction :: Connect ( id. clone ( ) , seed_addrs) ) ;
124
- self . send_bootstrap_request ( id) ;
125
- }
126
- }
127
-
128
- #[ inline]
129
- fn send_bootstrap_request ( & mut self , to : ConnectionId ) {
129
+ fn send_bootstrap_request ( & mut self , to : ConnectionId , actions_buf : & mut VecDeque < NodeAction > ) {
130
130
let nodes = self
131
131
. peers
132
132
. iter ( )
133
133
. map ( |( id, addrs) | ( * id, addrs. clone ( ) ) )
134
134
. collect ( ) ;
135
135
136
- self . send ( NodeAction :: Request (
136
+ actions_buf . push_back ( NodeAction :: Request (
137
137
to,
138
138
rpc:: BootstrapMsg {
139
139
from_id : self . local_id ,
@@ -142,11 +142,6 @@ impl NodeInner {
142
142
) ) ;
143
143
}
144
144
145
- #[ inline]
146
- fn send ( & mut self , action : NodeAction ) {
147
- self . pending_actions_buffer . push_back ( action)
148
- }
149
-
150
145
/// Merges `other` nodes list to already known. Returns new nodes count
151
146
fn merge_nodes_list (
152
147
& mut self ,
0 commit comments