7
7
namespace eosio ::auto_bp_peering {
8
8
9
9
// /
10
- // / This file implements the functionality for block producers automatically establishing p2p connections to their
11
- // / neighbors on the producer schedule.
10
+ // / This file implements the functionality for block producers automatically establishing p2p connections to other bps.
12
11
// /
13
12
14
-
15
-
16
13
template <typename Derived, typename Connection>
17
14
class bp_connection_manager {
18
15
#ifdef BOOST_TEST
@@ -31,8 +28,8 @@ class bp_connection_manager {
31
28
} config; // thread safe only because modified at plugin startup currently
32
29
33
30
// the following member are only accessed from main thread
34
- flat_set<account_name> pending_neighbors ;
35
- flat_set<account_name> active_neighbors ;
31
+ flat_set<account_name> pending_configured_bps ;
32
+ flat_set<account_name> active_configured_bps ;
36
33
uint32_t pending_schedule_version = 0 ;
37
34
uint32_t active_schedule_version = 0 ;
38
35
@@ -44,56 +41,21 @@ class bp_connection_manager {
44
41
return boost::algorithm::join (peers | boost::adaptors::transformed ([](auto & p) { return p.to_string (); }), " ," );
45
42
}
46
43
47
- class neighbor_finder_type {
48
-
49
- const config_t & config;
50
- const std::vector<chain::producer_authority>& schedule;
51
- chain::flat_set<std::size_t > my_schedule_indices;
52
-
53
- public:
54
- neighbor_finder_type (const config_t & config,
55
- const std::vector<chain::producer_authority>& schedule)
56
- : config(config), schedule(schedule) {
57
- for (auto account : config.my_bp_accounts ) {
58
- auto itr = std::find_if (schedule.begin (), schedule.end (),
59
- [account](auto & e) { return e.producer_name == account; });
60
- if (itr != schedule.end ())
61
- my_schedule_indices.insert (itr - schedule.begin ());
62
- }
63
- }
64
-
65
- void add_neighbors_with_distance (chain::flat_set<account_name>& result, int distance) const {
66
- for (auto schedule_index : my_schedule_indices) {
67
- auto i = (schedule_index + distance) % schedule.size ();
68
- if (!my_schedule_indices.count (i)) {
69
- auto name = schedule[i].producer_name ;
70
- if (config.bp_peer_addresses .count (name))
71
- result.insert (name);
72
- }
44
+ // Only called from main thread
45
+ chain::flat_set<account_name> configured_bp_accounts (const config_t & config,
46
+ const std::vector<chain::producer_authority>& schedule) const
47
+ {
48
+ chain::flat_set<account_name> result;
49
+ for (const auto & auth : schedule) {
50
+ if (config.bp_peer_addresses .contains (auth.producer_name )) {
51
+ result.insert (auth.producer_name );
73
52
}
74
53
}
75
-
76
- flat_set<account_name> downstream_neighbors () const {
77
- chain::flat_set<account_name> result;
78
- for (std::size_t i = 0 ; i < proximity_count; ++i) { add_neighbors_with_distance (result, i + 1 ); }
79
- return result;
80
- }
81
-
82
- void add_upstream_neighbors (chain::flat_set<account_name>& result) const {
83
- for (std::size_t i = 0 ; i < proximity_count; ++i) { add_neighbors_with_distance (result, -1 - i); }
84
- }
85
-
86
- flat_set<account_name> neighbors () const {
87
- flat_set<account_name> result = downstream_neighbors ();
88
- add_upstream_neighbors (result);
89
- return result;
90
- }
91
- };
54
+ return result;
55
+ }
92
56
93
57
public:
94
- const static std::size_t proximity_count = 2 ;
95
-
96
- bool auto_bp_peering_enabled () const { return config.bp_peer_addresses .size () && config.my_bp_accounts .size (); }
58
+ bool auto_bp_peering_enabled () const { return !config.bp_peer_addresses .empty (); }
97
59
98
60
// Only called at plugin startup
99
61
void set_producer_accounts (const std::set<account_name>& accounts) {
@@ -117,6 +79,7 @@ class bp_connection_manager {
117
79
118
80
config.bp_peer_accounts [addr] = account;
119
81
config.bp_peer_addresses [account] = std::move (addr);
82
+ fc_dlog (self ()->get_logger (), " Setting auto-bp-peer ${a} -> ${d}" , (" a" , account)(" d" , config.bp_peer_addresses [account]));
120
83
}
121
84
} catch (eosio::chain::name_type_exception&) {
122
85
EOS_ASSERT (false , chain::plugin_config_exception, " the account supplied by --auto-bp-peer option is invalid" );
@@ -167,67 +130,60 @@ class bp_connection_manager {
167
130
established_client_connection (new_connection) && num_established_clients () > self ()->connections .get_max_client_count ();
168
131
}
169
132
170
- // Only called from main thread
171
- neighbor_finder_type neighbor_finder (const std::vector<chain::producer_authority>& schedule) const {
172
- return neighbor_finder_type (config, schedule);
173
- }
174
-
175
133
// Only called from main thread
176
134
void on_pending_schedule (const chain::producer_authority_schedule& schedule) {
177
- if (auto_bp_peering_enabled () && self ()->in_sync ()) {
135
+ if (auto_bp_peering_enabled () && ! self ()->is_lib_catchup ()) {
178
136
if (schedule.producers .size ()) {
179
137
if (pending_schedule_version != schedule.version ) {
180
- // / establish connection to the BPs within our pending scheduling proximity
138
+ // / establish connection to our configured BPs, resolve_and_connect ignored if already connected
181
139
182
140
fc_dlog (self ()->get_logger (), " pending producer schedule switches from version ${old} to ${new}" ,
183
141
(" old" , pending_schedule_version)(" new" , schedule.version ));
184
142
185
- auto finder = neighbor_finder (schedule.producers );
186
- auto pending_downstream_neighbors = finder.downstream_neighbors ();
143
+ auto pending_connections = configured_bp_accounts (config, schedule.producers );
187
144
188
- fc_dlog (self ()->get_logger (), " pending_downstream_neighbors: ${pending_downstream_neighbors}" ,
189
- (" pending_downstream_neighbors" , to_string (pending_downstream_neighbors)));
190
- for (auto neighbor : pending_downstream_neighbors) { self ()->connections .resolve_and_connect (config.bp_peer_addresses [neighbor], self ()->get_first_p2p_address () ); }
145
+ fc_dlog (self ()->get_logger (), " pending_connections: ${c}" , (" c" , to_string (pending_connections)));
146
+ for (const auto & i : pending_connections) {
147
+ self ()->connections .resolve_and_connect (config.bp_peer_addresses [i], self ()->get_first_p2p_address () );
148
+ }
191
149
192
- pending_neighbors = std::move (pending_downstream_neighbors);
193
- finder.add_upstream_neighbors (pending_neighbors);
150
+ pending_configured_bps = std::move (pending_connections);
194
151
195
152
pending_schedule_version = schedule.version ;
196
153
}
197
154
} else {
198
- fc_dlog (self ()->get_logger (), " pending producer schedule version ${v} has being cleared" ,
199
- (" v" , schedule.version ));
200
- pending_neighbors.clear ();
155
+ fc_dlog (self ()->get_logger (), " pending producer schedule version ${v} is being cleared" , (" v" , schedule.version ));
156
+ pending_configured_bps.clear ();
201
157
}
202
158
}
203
159
}
204
160
205
161
// Only called from main thread
206
162
void on_active_schedule (const chain::producer_authority_schedule& schedule) {
207
- if (auto_bp_peering_enabled () && active_schedule_version != schedule.version && self ()->in_sync ()) {
163
+ if (auto_bp_peering_enabled () && active_schedule_version != schedule.version && ! self ()->is_lib_catchup ()) {
208
164
// / drops any BP connection which is no longer within our scheduling proximity
209
-
210
165
fc_dlog (self ()->get_logger (), " active producer schedule switches from version ${old} to ${new}" ,
211
166
(" old" , active_schedule_version)(" new" , schedule.version ));
212
167
213
- auto old_neighbors = std::move (active_neighbors );
214
- active_neighbors = neighbor_finder ( schedule.producers ). neighbors ( );
168
+ auto old_bps = std::move (active_configured_bps );
169
+ active_configured_bps = configured_bp_accounts (config, schedule.producers );
215
170
216
- fc_dlog (self ()->get_logger (), " active_neighbors: ${active_neighbors}" ,
217
- (" active_neighbors" , to_string (active_neighbors)));
171
+ fc_dlog (self ()->get_logger (), " active_configured_bps: ${a}" , (" a" , to_string (active_configured_bps)));
218
172
219
173
flat_set<account_name> peers_to_stay;
220
- std::set_union (active_neighbors .begin (), active_neighbors .end (), pending_neighbors .begin (),
221
- pending_neighbors. end (), std::inserter (peers_to_stay, peers_to_stay.begin ()));
174
+ std::set_union (active_configured_bps .begin (), active_configured_bps .end (), pending_configured_bps .begin (), pending_configured_bps. end (),
175
+ std::inserter (peers_to_stay, peers_to_stay.begin ()));
222
176
223
- fc_dlog (self ()->get_logger (), " peers_to_stay: ${peers_to_stay }" , (" peers_to_stay " , to_string (peers_to_stay)));
177
+ fc_dlog (self ()->get_logger (), " peers_to_stay: ${p }" , (" p " , to_string (peers_to_stay)));
224
178
225
179
std::vector<account_name> peers_to_drop;
226
- std::set_difference (old_neighbors .begin (), old_neighbors .end (), peers_to_stay.begin (), peers_to_stay.end (),
180
+ std::set_difference (old_bps .begin (), old_bps .end (), peers_to_stay.begin (), peers_to_stay.end (),
227
181
std::back_inserter (peers_to_drop));
228
- fc_dlog (self ()->get_logger (), " peers to drop: ${peers_to_drop }" , (" peers_to_drop " , to_string (peers_to_drop)));
182
+ fc_dlog (self ()->get_logger (), " peers to drop: ${p }" , (" p " , to_string (peers_to_drop)));
229
183
230
- for (auto account : peers_to_drop) { self ()->connections .disconnect (config.bp_peer_addresses [account]); }
184
+ for (const auto & account : peers_to_drop) {
185
+ self ()->connections .disconnect (config.bp_peer_addresses [account]);
186
+ }
231
187
active_schedule_version = schedule.version ;
232
188
}
233
189
}
0 commit comments