Skip to content

Commit 83b2312

Browse files
committed
Add DropReason, DropPeer, and DropNeighbor structs and do first pass with them
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent ab90a9d commit 83b2312

File tree

7 files changed

+270
-89
lines changed

7 files changed

+270
-89
lines changed

stackslib/src/net/download/epoch2x.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub struct BlockDownloader {
233233
/// statistics on peers' data-plane endpoints
234234
pub(crate) dead_peers: Vec<usize>,
235235
pub(crate) broken_peers: Vec<usize>,
236-
broken_neighbors: Vec<NeighborKey>, // disconnect peers who report invalid block inventories too
236+
broken_neighbors: Vec<DropNeighbor>, // disconnect peers who report invalid block inventories too
237237

238238
pub(crate) blocked_urls: HashMap<UrlString, u64>, // URLs that chronically don't work, and when we can try them again
239239

@@ -500,7 +500,12 @@ impl BlockDownloader {
500500
{
501501
info!("Invalid block from {:?} ({:?}): did not ask for block {}/{}", &block_key.neighbor, &block_key.data_url, block_key.consensus_hash, block.block_hash());
502502
self.broken_peers.push(event_id);
503-
self.broken_neighbors.push(block_key.neighbor.clone());
503+
self.broken_neighbors.push(DropNeighbor {
504+
key: block_key.neighbor.clone(),
505+
reason: DropReason::BrokenConnection(Some(
506+
"Remote neighbor sent an invalid block".into(),
507+
)),
508+
});
504509
} else {
505510
// got the block
506511
debug!(
@@ -519,14 +524,23 @@ impl BlockDownloader {
519524
// the fact that we asked this peer means that it's block inv indicated
520525
// it was present, so the absence is the mark of a broken peer
521526
self.broken_peers.push(event_id);
522-
self.broken_neighbors.push(block_key.neighbor.clone());
527+
self.broken_neighbors.push(DropNeighbor {
528+
key: block_key.neighbor.clone(),
529+
reason: DropReason::BrokenConnection(Some(
530+
"Remote neighbor was missing an expected block"
531+
.into(),
532+
)),
533+
});
523534
}
524535
Err(e) => {
525536
info!("Error decoding response from remote neighbor {:?} (at {}): {:?}", &block_key.neighbor, &block_key.data_url, &e;
526537
"consensus_hash" => %block_key.consensus_hash
527538
);
528539
self.broken_peers.push(event_id);
529-
self.broken_neighbors.push(block_key.neighbor.clone());
540+
self.broken_neighbors.push(DropNeighbor {
541+
key: block_key.neighbor.clone(),
542+
reason: DropReason::BrokenConnection(Some(format!("Error occurred decoding block response from neighbor: {e}")))
543+
});
530544
}
531545
}
532546
}
@@ -632,7 +646,10 @@ impl BlockDownloader {
632646
"consensus_hash" => %block_key.consensus_hash
633647
);
634648
self.broken_peers.push(event_id);
635-
self.broken_neighbors.push(block_key.neighbor.clone());
649+
self.broken_neighbors.push(DropNeighbor {
650+
key: block_key.neighbor.clone(),
651+
reason: DropReason::BrokenConnection(Some("Remote neighbor sent an unexpected zero-length microblock stream".into()))
652+
});
636653
} else {
637654
// have microblocks (but we don't know yet if they're well-formed)
638655
debug!(
@@ -664,7 +681,10 @@ impl BlockDownloader {
664681
"consensus_hash" => %block_key.consensus_hash
665682
);
666683
self.broken_peers.push(event_id);
667-
self.broken_neighbors.push(block_key.neighbor.clone());
684+
self.broken_neighbors.push(DropNeighbor {
685+
key: block_key.neighbor.clone(),
686+
reason: DropReason::BrokenConnection(Some(format!("Error occurred decoding microblock response from neighbor: {e}")))
687+
});
668688
}
669689
}
670690
}
@@ -890,7 +910,7 @@ impl BlockDownloader {
890910
}
891911

892912
/// Clear out broken peers that told us they had blocks, but didn't serve them.
893-
fn clear_broken_peers(&mut self) -> (Vec<usize>, Vec<NeighborKey>) {
913+
fn clear_broken_peers(&mut self) -> (Vec<usize>, Vec<DropNeighbor>) {
894914
// remove dead/broken peers
895915
let mut disconnect = vec![];
896916
let mut disconnect_neighbors = vec![];
@@ -2348,7 +2368,7 @@ impl PeerNetwork {
23482368
Vec<(ConsensusHash, StacksBlock, u64)>,
23492369
Vec<(ConsensusHash, Vec<StacksMicroblock>, u64)>,
23502370
Vec<usize>,
2351-
Vec<NeighborKey>,
2371+
Vec<DropNeighbor>,
23522372
),
23532373
net_error,
23542374
> {

stackslib/src/net/download/nakamoto/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ use crate::net::inv::epoch2x::InvState;
152152
use crate::net::inv::nakamoto::{NakamotoInvStateMachine, NakamotoTenureInv};
153153
use crate::net::neighbors::rpc::NeighborRPC;
154154
use crate::net::neighbors::NeighborComms;
155-
use crate::net::p2p::PeerNetwork;
155+
use crate::net::p2p::{DropReason, PeerNetwork};
156156
use crate::net::server::HttpPeer;
157157
use crate::net::{Error as NetError, Neighbor, NeighborAddress, NeighborKey};
158158
use crate::util_lib::db::{DBConn, Error as DBError};
@@ -236,11 +236,11 @@ impl PeerNetwork {
236236
};
237237

238238
for broken in block_downloader.neighbor_rpc.take_broken() {
239-
self.deregister_and_ban_neighbor(&broken);
239+
self.deregister_and_ban_neighbor(&broken, DropReason::BrokenConnection(None));
240240
}
241241

242242
for dead in block_downloader.neighbor_rpc.take_dead() {
243-
self.deregister_neighbor(&dead);
243+
self.deregister_neighbor(&dead, DropReason::DeadConnection);
244244
}
245245

246246
self.block_downloader_nakamoto = Some(block_downloader);

stackslib/src/net/inv/epoch2x.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::net::codec::*;
3939
use crate::net::connection::{ConnectionOptions, ConnectionP2P, ReplyHandleP2P};
4040
use crate::net::db::{PeerDB, *};
4141
use crate::net::neighbors::MAX_NEIGHBOR_BLOCK_DELAY;
42-
use crate::net::p2p::{PeerNetwork, PeerNetworkWorkState};
42+
use crate::net::p2p::{DropReason, PeerNetwork, PeerNetworkWorkState};
4343
use crate::net::{
4444
Error as net_error, GetBlocksInv, Neighbor, NeighborKey, PeerAddress, StacksMessage, StacksP2P,
4545
*,
@@ -2678,12 +2678,12 @@ impl PeerNetwork {
26782678

26792679
// disconnect and ban broken peers
26802680
for broken in broken_neighbors.into_iter() {
2681-
self.deregister_and_ban_neighbor(&broken);
2681+
self.deregister_and_ban_neighbor(&broken, DropReason::BannedConnection);
26822682
}
26832683

26842684
// disconnect from dead connections
26852685
for dead in dead_neighbors.into_iter() {
2686-
self.deregister_neighbor(&dead);
2686+
self.deregister_neighbor(&dead, DropReason::DeadConnection);
26872687
}
26882688

26892689
(done, throttled)

stackslib/src/net/inv/nakamoto.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::net::db::PeerDB;
3030
use crate::net::neighbors::comms::PeerNetworkComms;
3131
use crate::net::p2p::PeerNetwork;
3232
use crate::net::{
33-
Error as NetError, GetNakamotoInvData, NackErrorCodes, NakamotoInvData, NeighborAddress,
34-
NeighborComms, NeighborKey, StacksMessage, StacksMessageType,
33+
DropReason, Error as NetError, GetNakamotoInvData, NackErrorCodes, NakamotoInvData,
34+
NeighborAddress, NeighborComms, NeighborKey, StacksMessage, StacksMessageType,
3535
};
3636
use crate::util_lib::db::Error as DBError;
3737

@@ -1128,12 +1128,12 @@ impl PeerNetwork {
11281128

11291129
// disconnect and ban broken peers
11301130
for broken in broken_neighbors.into_iter() {
1131-
self.deregister_and_ban_neighbor(&broken);
1131+
self.deregister_and_ban_neighbor(&broken, DropReason::BrokenConnection(None));
11321132
}
11331133

11341134
// disconnect from dead connections
11351135
for dead in dead_neighbors.into_iter() {
1136-
self.deregister_neighbor(&dead);
1136+
self.deregister_neighbor(&dead, DropReason::DeadConnection);
11371137
}
11381138

11391139
learned

stackslib/src/net/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use clarity::vm::{ClarityName, ContractName, Value};
3535
use libstackerdb::{
3636
Error as libstackerdb_error, SlotMetadata, StackerDBChunkAckData, StackerDBChunkData,
3737
};
38+
use p2p::{DropPeer, DropReason};
3839
use rand::{thread_rng, RngCore};
3940
use regex::Regex;
4041
use rusqlite::types::{ToSql, ToSqlOutput};
@@ -1310,6 +1311,15 @@ pub const BLOCKS_PUSHED_MAX: u32 = 32;
13101311
// message.
13111312
pub const NAKAMOTO_BLOCKS_PUSHED_MAX: u32 = 32;
13121313

1314+
/// Neighbor to drop
1315+
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
1316+
pub struct DropNeighbor {
1317+
/// the neighbor identifier
1318+
pub key: NeighborKey,
1319+
/// the reason for dropping the neighbor
1320+
pub reason: DropReason,
1321+
}
1322+
13131323
/// neighbor identifier
13141324
#[derive(Clone, Eq, PartialOrd, Ord)]
13151325
pub struct NeighborKey {

0 commit comments

Comments
 (0)