mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Limit each peer to one connection
Prevents dialing the same peer multiple times (successfully).
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -8339,6 +8339,7 @@ dependencies = [
|
||||
"sp-runtime",
|
||||
"tokio",
|
||||
"tributary-chain",
|
||||
"void",
|
||||
"zalloc",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
@@ -55,6 +55,7 @@ env_logger = { version = "0.10", default-features = false, features = ["humantim
|
||||
|
||||
futures-util = { version = "0.3", default-features = false, features = ["std"] }
|
||||
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"] }
|
||||
|
||||
serai-cosign = { path = "./cosign" }
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user