Skip to content

Commit 3c2ee24

Browse files
committed
fix(chain manager): unregister peers coincident with your beacon in case of different superblock consensus
1 parent c5aa51d commit 3c2ee24

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

data_structures/src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
proto::{schema::witnet, ProtobufConvert},
66
transaction::Transaction,
77
};
8+
use serde::{Deserialize, Serialize};
89

910
/// Witnet's protocol messages
1011
#[derive(Debug, Eq, PartialEq, Clone, ProtobufConvert)]
@@ -138,7 +139,7 @@ pub struct InventoryRequest {
138139
pub inventory: Vec<InventoryEntry>,
139140
}
140141

141-
#[derive(Debug, Eq, PartialEq, Clone, ProtobufConvert, Hash)]
142+
#[derive(Debug, Deserialize, Eq, PartialEq, Clone, ProtobufConvert, Serialize, Hash)]
142143
#[protobuf_convert(pb = "witnet::LastBeacon")]
143144
pub struct LastBeacon {
144145
pub highest_block_checkpoint: CheckpointBeacon,

node/.Cargo.toml.swp

-1 KB
Binary file not shown.

node/src/actors/chain_manager/handlers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ impl Handler<PeersBeacons> for ChainManager {
868868
let beacon_consensus = peers_beacons.superblock_consensus(consensus_threshold);
869869
let outbound_limit = peers_beacons.outbound_limit;
870870
let pb_len = peers_beacons.pb.len();
871+
self.last_received_beacons = peers_beacons.pb.clone();
871872
let peers_needed_for_consensus = outbound_limit
872873
.map(|x| {
873874
// ceil(x * consensus_threshold / 100)

node/src/actors/chain_manager/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::{
2929
cmp::{max, min, Ordering},
3030
collections::{HashMap, HashSet},
3131
convert::TryFrom,
32+
net::SocketAddr,
3233
time::Duration,
3334
};
3435

@@ -204,6 +205,8 @@ pub struct ChainManager {
204205
temp_superblock_votes: Vec<SuperBlockVote>,
205206
/// Commits and reveals to process later
206207
temp_commits_and_reveals: Vec<Transaction>,
208+
/// Last received Beacons
209+
last_received_beacons: Vec<(SocketAddr, Option<LastBeacon>)>,
207210
}
208211

209212
/// Wrapper around a block candidate that contains additional metadata regarding
@@ -1368,9 +1371,21 @@ impl ChainManager {
13681371
"Superblock consensus {} different from current superblock",
13691372
target_superblock_hash
13701373
);
1374+
1375+
// We are on a different chain than the one dictated by the network. Thus we need
1376+
// to throw away those outbound peers that were in the wrong consensus with us, and
1377+
// find new ones that can give us the blocks consolidated by the network
1378+
let mut peers_to_unregister: Vec<SocketAddr> = Vec::new();
1379+
for (addr, peer_beacon) in act.last_received_beacons.iter() {
1380+
if let Some(peer_beacon) = peer_beacon {
1381+
if peer_beacon.highest_block_checkpoint == act.get_chain_beacon() {
1382+
peers_to_unregister.push(*addr)
1383+
}
1384+
}
1385+
};
13711386
let sessions_manager_addr = SessionsManager::from_registry();
13721387

1373-
sessions_manager_addr.do_send(DropOutboundPeers {});
1388+
sessions_manager_addr.do_send(DropOutboundPeers {peers_to_drop: peers_to_unregister});
13741389
act.initialize_from_storage(ctx);
13751390
act.update_state_machine(StateMachine::WaitingConsensus);
13761391

node/src/actors/messages.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,10 @@ impl Message for LogMessage {
943943

944944
/// Drop all outbound peers
945945
#[derive(Clone, Debug)]
946-
pub struct DropOutboundPeers {}
946+
pub struct DropOutboundPeers {
947+
/// peers to be dropped
948+
pub peers_to_drop: Vec<SocketAddr>,
949+
}
947950
impl Message for DropOutboundPeers {
948951
type Result = ();
949952
}

node/src/actors/sessions_manager/handlers.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
fmt::{Debug, Display},
33
marker::Send,
4-
net::SocketAddr,
54
};
65

76
use actix::{
@@ -474,14 +473,7 @@ impl Handler<SetLastBeacon> for SessionsManager {
474473
impl Handler<DropOutboundPeers> for SessionsManager {
475474
type Result = <DropOutboundPeers as Message>::Result;
476475

477-
fn handle(&mut self, _msg: DropOutboundPeers, _ctx: &mut Context<Self>) -> Self::Result {
478-
let outbound_peers: Vec<SocketAddr> = self
479-
.sessions
480-
.outbound_consolidated
481-
.collection
482-
.keys()
483-
.cloned()
484-
.collect();
485-
self.drop_outbound_peers(outbound_peers.as_ref());
476+
fn handle(&mut self, msg: DropOutboundPeers, _ctx: &mut Context<Self>) -> Self::Result {
477+
self.drop_outbound_peers(msg.peers_to_drop.as_ref());
486478
}
487479
}

0 commit comments

Comments
 (0)