Skip to content

Commit 9379782

Browse files
committed
Refactor send status function
1 parent 6e5b996 commit 9379782

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

sync/src/block/extension.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,40 @@ impl Extension {
173173
}
174174
}
175175

176+
fn send_status(&mut self, id: &NodeId) {
177+
let chain_info = self.client.chain_info();
178+
self.api.send(
179+
id,
180+
Arc::new(
181+
Message::Status {
182+
nonce: U256::from(self.nonce),
183+
best_hash: chain_info.best_proposal_block_hash,
184+
genesis_hash: chain_info.genesis_hash,
185+
}
186+
.rlp_bytes(),
187+
),
188+
);
189+
self.nonce += 1;
190+
}
191+
192+
fn send_status_broadcast(&mut self) {
193+
let chain_info = self.client.chain_info();
194+
for id in self.connected_nodes.iter() {
195+
self.api.send(
196+
id,
197+
Arc::new(
198+
Message::Status {
199+
nonce: U256::from(self.nonce),
200+
best_hash: chain_info.best_proposal_block_hash,
201+
genesis_hash: chain_info.genesis_hash,
202+
}
203+
.rlp_bytes(),
204+
),
205+
);
206+
self.nonce += 1;
207+
}
208+
}
209+
176210
fn send_header_request(&mut self, id: &NodeId, request: RequestMessage) {
177211
if let Some(requests) = self.requests.get_mut(id) {
178212
ctrace!(SYNC, "Send header request to {}", id);
@@ -325,19 +359,8 @@ impl NetworkExtension<Event> for Extension {
325359

326360
fn on_node_added(&mut self, id: &NodeId, _version: u64) {
327361
cinfo!(SYNC, "New peer detected #{}", id);
328-
let chain_info = self.client.chain_info();
329-
self.api.send(
330-
id,
331-
Arc::new(
332-
Message::Status {
333-
nonce: U256::from(self.nonce),
334-
best_hash: chain_info.best_proposal_block_hash,
335-
genesis_hash: chain_info.genesis_hash,
336-
}
337-
.rlp_bytes(),
338-
),
339-
);
340-
self.nonce += 1;
362+
self.send_status(id);
363+
341364
let t = self.connected_nodes.insert(*id);
342365
debug_assert!(t, "{} is already added to peer list", id);
343366

@@ -575,22 +598,7 @@ impl Extension {
575598
self.body_downloader.remove_target(&imported);
576599
self.body_downloader.remove_target(&invalid);
577600

578-
let chain_info = self.client.chain_info();
579-
580-
for id in &self.connected_nodes {
581-
self.api.send(
582-
id,
583-
Arc::new(
584-
Message::Status {
585-
nonce: U256::from(self.nonce),
586-
best_hash: chain_info.best_proposal_block_hash,
587-
genesis_hash: chain_info.genesis_hash,
588-
}
589-
.rlp_bytes(),
590-
),
591-
);
592-
self.nonce += 1;
593-
}
601+
self.send_status_broadcast();
594602
}
595603
}
596604

0 commit comments

Comments
 (0)