Limit each peer to one connection

Prevents dialing the same peer multiple times (successfully).
This commit is contained in:
Luke Parker
2025-01-09 00:06:51 -05:00
parent 75a00f2a1a
commit dda6e3e899
6 changed files with 19 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
use core::{pin::Pin, future::Future};
use std::{sync::Arc, io};
use std::io;
use zeroize::Zeroizing;
use rand_core::{RngCore, OsRng};
@@ -9,8 +9,6 @@ use schnorrkel::{Keypair, PublicKey, Signature};
use serai_client::primitives::PublicKey as Public;
use tokio::sync::RwLock;
use futures_util::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use libp2p::{
core::UpgradeInfo,

View File

@@ -5,8 +5,8 @@ use blake2::{Digest, Blake2s256};
use borsh::{BorshSerialize, BorshDeserialize};
use libp2p::gossipsub::{
TopicHash, IdentTopic, MessageId, MessageAuthenticity, ValidationMode, ConfigBuilder,
IdentityTransform, AllowAllSubscriptionFilter, Behaviour,
IdentTopic, MessageId, MessageAuthenticity, ValidationMode, ConfigBuilder, IdentityTransform,
AllowAllSubscriptionFilter, Behaviour,
};
pub use libp2p::gossipsub::Event;

View File

@@ -26,6 +26,7 @@ use libp2p::{
identity::{self, PeerId},
tcp::Config as TcpConfig,
yamux, allow_block_list,
connection_limits::{self, ConnectionLimits},
swarm::NetworkBehaviour,
SwarmBuilder,
};
@@ -40,10 +41,6 @@ use validators::UpdateValidatorsTask;
mod authenticate;
use authenticate::OnlyValidators;
/// The dial task, to find new peers to connect to
mod dial;
use dial::DialTask;
/// The ping behavior, used to ensure connection latency is below the limit
mod ping;
@@ -59,6 +56,10 @@ use gossip::Message;
mod swarm;
use swarm::SwarmTask;
/// The dial task, to find new peers to connect to
mod dial;
use dial::DialTask;
const PORT: u16 = 30563; // 5132 ^ (('c' << 8) | 'o')
// usize::max, manually implemented, as max isn't a const fn
@@ -113,6 +114,7 @@ struct Peers {
#[derive(NetworkBehaviour)]
struct Behavior {
allow_list: allow_block_list::Behaviour<allow_block_list::AllowedPeers>,
connection_limits: connection_limits::Behaviour,
ping: ping::Behavior,
reqres: reqres::Behavior,
gossip: gossip::Behavior,
@@ -169,6 +171,10 @@ impl Libp2p {
.unwrap()
.with_behaviour(|_| Behavior {
allow_list: allow_block_list::Behaviour::default(),
// Limit each per to a single connection
connection_limits: connection_limits::Behaviour::new(
ConnectionLimits::default().with_max_established_per_peer(Some(1)),
),
ping: ping::new_behavior(),
reqres: reqres::new_behavior(),
gossip: gossip::new_behavior(),

View File

@@ -225,8 +225,10 @@ impl SwarmTask {
}
}
SwarmEvent::Behaviour(BehaviorEvent::AllowList(event)) => {
// Ensure this is an unreachable case, not an actual event
SwarmEvent::Behaviour(
BehaviorEvent::AllowList(event) | BehaviorEvent::ConnectionLimits(event)
) => {
// Ensure these are unreachable cases, not actual events
let _: void::Void = event;
}
SwarmEvent::Behaviour(