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

1
Cargo.lock generated
View File

@@ -8339,6 +8339,7 @@ dependencies = [
"sp-runtime", "sp-runtime",
"tokio", "tokio",
"tributary-chain", "tributary-chain",
"void",
"zalloc", "zalloc",
"zeroize", "zeroize",
] ]

View File

@@ -55,6 +55,7 @@ env_logger = { version = "0.10", default-features = false, features = ["humantim
futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-util = { version = "0.3", default-features = false, features = ["std"] }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "sync", "time", "macros"] } tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "sync", "time", "macros"] }
void = { version = "1", default-features = false }
libp2p = { version = "0.52", default-features = false, features = ["tokio", "tcp", "noise", "yamux", "ping", "request-response", "gossipsub", "macros"] } libp2p = { version = "0.52", default-features = false, features = ["tokio", "tcp", "noise", "yamux", "ping", "request-response", "gossipsub", "macros"] }
serai-cosign = { path = "./cosign" } serai-cosign = { path = "./cosign" }

View File

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

View File

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

View File

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

View File

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