Skip to content

Commit fb96592

Browse files
committed
fix(node): update stale block time after resetting connections
If a block is considered stale, the `advance_state` function will reset connections with all peers. To reset the "staleness" of a block, the `LastBlockMonitor::update` function must be called. Currently that is done upon receiving new block headers, but no headers will ever be sent, as `advance_state` disconnects the peers before they can send them once a block is considered stale. Here we patch this to reset the "staleness" of a block after disconnecting from all peers.
1 parent 04a038b commit fb96592

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl LastBlockMonitor {
4848
Self { last_block: None }
4949
}
5050

51-
pub(crate) fn update(&mut self) {
51+
pub(crate) fn reset(&mut self) {
5252
self.last_block = Some(Instant::now())
5353
}
5454

src/core/node.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
188188
let mut client_recv = self.client_recv.lock().await;
189189
loop {
190190
// Try to advance the state of the node
191-
self.advance_state(&last_block).await;
191+
self.advance_state(&mut last_block).await;
192192
// Connect to more peers if we need them and remove old connections
193193
self.dispatch().await?;
194194
// If there are blocks we need in the queue, we should request them of a random peer
@@ -216,7 +216,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
216216
}
217217
PeerMessage::Addr(addresses) => self.handle_new_addrs(addresses).await,
218218
PeerMessage::Headers(headers) => {
219-
last_block.update();
219+
last_block.reset();
220220
self.dialog.send_dialog(format!("[Peer {}]: headers", peer_thread.nonce))
221221
.await;
222222
match self.handle_headers(peer_thread.nonce, headers).await {
@@ -424,7 +424,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
424424
}
425425

426426
// Try to continue with the syncing process
427-
async fn advance_state(&self, last_block: &LastBlockMonitor) {
427+
async fn advance_state(&self, last_block: &mut LastBlockMonitor) {
428428
let mut state = self.state.write().await;
429429
match *state {
430430
NodeState::Behind => {
@@ -476,6 +476,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
476476
.send_dialog("Disconnecting from remote nodes to find new connections")
477477
.await;
478478
self.broadcast(MainThreadMessage::Disconnect).await;
479+
last_block.reset();
479480
}
480481
}
481482
}

0 commit comments

Comments
 (0)