Skip to content

Commit

Permalink
fix(chain manager): drop beacon-coincident outbound peers if differen…
Browse files Browse the repository at this point in the history
…t superblock consensus
  • Loading branch information
girazoki committed Nov 23, 2020
1 parent 8605817 commit c5aa51d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
Binary file added node/.Cargo.toml.swp
Binary file not shown.
5 changes: 4 additions & 1 deletion node/src/actors/chain_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use crate::{
inventory_manager::InventoryManager,
json_rpc::JsonRpcServer,
messages::{
AddItem, AddItems, AddTransaction, Anycast, BlockNotify, Broadcast,
AddItem, AddItems, AddTransaction, Anycast, BlockNotify, Broadcast, DropOutboundPeers,
GetBlocksEpochRange, GetItemBlock, NodeStatusNotify, SendInventoryItem,
SendInventoryRequest, SendLastBeacon, SendSuperBlockVote, StoreInventoryItem,
SuperBlockNotify,
Expand Down Expand Up @@ -1368,6 +1368,9 @@ impl ChainManager {
"Superblock consensus {} different from current superblock",
target_superblock_hash
);
let sessions_manager_addr = SessionsManager::from_registry();

sessions_manager_addr.do_send(DropOutboundPeers {});
act.initialize_from_storage(ctx);
act.update_state_machine(StateMachine::WaitingConsensus);

Expand Down
7 changes: 7 additions & 0 deletions node/src/actors/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,13 @@ impl Message for LogMessage {
type Result = SessionsUnitResult;
}

/// Drop all outbound peers
#[derive(Clone, Debug)]
pub struct DropOutboundPeers {}
impl Message for DropOutboundPeers {
type Result = ();
}

/// Set the LastBeacon
#[derive(Clone, Debug)]
pub struct SetLastBeacon {
Expand Down
23 changes: 20 additions & 3 deletions node/src/actors/sessions_manager/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{Debug, Display},
marker::Send,
net::SocketAddr,
};

use actix::{
Expand All @@ -16,9 +17,10 @@ use crate::actors::{
chain_manager::ChainManager,
codec::P2PCodec,
messages::{
AddConsolidatedPeer, AddPeers, Anycast, Broadcast, Consolidate, Create, EpochNotification,
GetConsolidatedPeers, LogMessage, NumSessions, NumSessionsResult, PeerBeacon, Register,
RemoveAddressesFromTried, SessionsUnitResult, SetLastBeacon, TryMineBlock, Unregister,
AddConsolidatedPeer, AddPeers, Anycast, Broadcast, Consolidate, Create, DropOutboundPeers,
EpochNotification, GetConsolidatedPeers, LogMessage, NumSessions, NumSessionsResult,
PeerBeacon, Register, RemoveAddressesFromTried, SessionsUnitResult, SetLastBeacon,
TryMineBlock, Unregister,
},
peers_manager::PeersManager,
session::Session,
Expand Down Expand Up @@ -468,3 +470,18 @@ impl Handler<SetLastBeacon> for SessionsManager {
self.last_beacon = Some(msg.beacon);
}
}

impl Handler<DropOutboundPeers> for SessionsManager {
type Result = <DropOutboundPeers as Message>::Result;

fn handle(&mut self, _msg: DropOutboundPeers, _ctx: &mut Context<Self>) -> Self::Result {
let outbound_peers: Vec<SocketAddr> = self
.sessions
.outbound_consolidated
.collection
.keys()
.cloned()
.collect();
self.drop_outbound_peers(outbound_peers.as_ref());
}
}
16 changes: 11 additions & 5 deletions node/src/actors/sessions_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,8 @@ impl SessionsManager {
);
}
// Unregister peers out of consensus
act.drop_outbound_peers(peers_to_unregister.as_ref());
for peer in peers_to_unregister {
if let Some(a) =
act.sessions.outbound_consolidated.collection.get(&peer)
{
a.reference.do_send(CloseSession);
}
peers_to_keep.remove(&peer);
}
// Mark remaining peers as safu
Expand All @@ -291,6 +287,16 @@ impl SessionsManager {
.cloned(),
);
}

/// Drop outbound peers
fn drop_outbound_peers(&mut self, peers_to_unregister: &[SocketAddr]) {
// Unregister peers out of consensus
for peer in peers_to_unregister {
if let Some(a) = self.sessions.outbound_consolidated.collection.get(&peer) {
a.reference.do_send(CloseSession);
}
}
}
}

/// Required traits for being able to retrieve SessionsManager address from registry
Expand Down

0 comments on commit c5aa51d

Please sign in to comment.