Skip to content

Commit

Permalink
feat: add p2p crates
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Sep 17, 2023
1 parent 1281b9c commit 053995a
Show file tree
Hide file tree
Showing 24 changed files with 4,531 additions and 479 deletions.
2,448 changes: 2,103 additions & 345 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/bundler",
"crates/contracts",
"crates/grpc",
"crates/p2p",
"crates/primitives",
"crates/rpc",
"crates/uopool",
Expand All @@ -24,8 +25,11 @@ anyhow = "1"
async-trait = "0.1"
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "fa3017715a298728d9fb341933818a5d0d84c2dc" }
expanded-pathbuf = "0.1"
lazy_static = "1.4.0"
parking_lot = "0.12"
serde_json = "1"
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs.git", rev = "8640128ec83071094d24fb4511147d6b9dd029bb" }
ssz_rs_derive = { git = "https://github.com/ralexstokes/ssz-rs.git", rev = "8640128ec83071094d24fb4511147d6b9dd029bb" }
tokio = { version = "1.18", features = ["full"] }
tracing = "0.1"

Expand Down
3 changes: 1 addition & 2 deletions crates/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ AA (ERC-4337) bundler smart contract interfaces
"""

[dependencies]

anyhow = { workspace = true }
ethers = { workspace = true }
lazy_static = "1.4.0"
lazy_static = { workspace = true }
serde = "1"
serde_json = "1"
silius-primitives = { path = "../primitives" }
Expand Down
39 changes: 39 additions & 0 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "aa-bundler-p2p"
version = "0.1.0"
authors = ["Vid Kersic <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/Vid201/aa-bundler"
readme = "README.md"
description = """
AA (ERC-4337) Bundler p2p components
"""
rust-version = "1.69.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
async-trait = "0.1"
discv5 = { version = "0.3.0", features = ["libp2p"] }
futures = "0.3.28"
lazy_static = { workspace = true }
libp2p-mplex = { version = "0.40.0" }
silius-primitives = { path = "../primitives" }
silius-uopool = { path = "../uopool" }
snap = "1"
ssz_rs = { workspace = true }
ssz_rs_derive = { workspace = true }
thiserror = "1"
tokio = { workspace = true }
tracing = { workspace = true }

[dependencies.libp2p]
version = "0.52.3"
features = ["identify", "yamux", "noise", "gossipsub", "dns", "tcp", "tokio", "secp256k1", "macros", "request-response"]

[dev-dependencies]
env_logger = "*"
ethers = { workspace = true }
test-log = "0.2.12"
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }
56 changes: 56 additions & 0 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::config::Config;
use crate::discovery::{self, Discovery};
use crate::enr::build_enr;
use crate::gossipsub::{create_gossisub, Gossipsub};
use crate::request_response;
use discv5::enr::CombinedKey;
use libp2p::gossipsub;
use libp2p::swarm::NetworkBehaviour;
use silius_uopool::MempoolId;

#[derive(NetworkBehaviour)]
#[behaviour(to_swarm = "Event", event_process = false)]
pub struct Behaviour {
pub gossipsub: Gossipsub,
pub reqrep: request_response::Behaviour,
pub discv5: Discovery,
}

impl Behaviour {
pub fn new(key: CombinedKey, config: Config, mempool_ids: MempoolId) -> anyhow::Result<Self> {
let enr = build_enr(&key, &config)?;
let gossipsub = create_gossisub(mempool_ids).map_err(|e| anyhow::anyhow!(e))?;
let reqrep = request_response::Behaviour::new(Default::default());
let discovery = Discovery::new(enr, key, config)?;
Ok(Self {
gossipsub,
reqrep,
discv5: discovery,
})
}
}

impl From<gossipsub::Event> for Event {
fn from(value: gossipsub::Event) -> Self {
Event::GossipSub(Box::new(value))
}
}

impl From<request_response::Event> for Event {
fn from(value: request_response::Event) -> Self {
Event::Reqrep(value)
}
}

impl From<discovery::DiscoverPeers> for Event {
fn from(value: discovery::DiscoverPeers) -> Self {
Event::Discovery(value)
}
}

#[derive(Debug)]
pub enum Event {
GossipSub(Box<gossipsub::Event>),
Reqrep(request_response::Event),
Discovery(discovery::DiscoverPeers),
}
105 changes: 105 additions & 0 deletions crates/p2p/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::net::{Ipv4Addr, Ipv6Addr};

use discv5::ListenConfig;
use libp2p::{multiaddr::Protocol, Multiaddr};

const DEFAULT_UDP_PORT: u16 = 9000;
const DEFAULT_TCP_PORT: u16 = 9000;
#[derive(Clone, Debug)]
pub struct ListenAddr<Ip> {
pub addr: Ip,
pub udp_port: u16,
pub tcp_port: u16,
}

pub enum ListenAddress {
Ipv4(ListenAddr<Ipv4Addr>),
Ipv6(ListenAddr<Ipv6Addr>),
Dual(ListenAddr<Ipv4Addr>, ListenAddr<Ipv6Addr>),
}

impl ListenAddress {
pub fn to_multi_addr(&self) -> Vec<Multiaddr> {
match self {
ListenAddress::Ipv4(v) => vec![Multiaddr::from(v.addr).with(Protocol::Tcp(v.tcp_port))],
ListenAddress::Ipv6(v) => vec![Multiaddr::from(v.addr).with(Protocol::Tcp(v.tcp_port))],
ListenAddress::Dual(ipv4, ipv6) => {
vec![
Multiaddr::from(ipv4.addr).with(Protocol::Tcp(ipv4.tcp_port)),
Multiaddr::from(ipv6.addr).with(Protocol::Tcp(ipv6.tcp_port)),
]
}
}
}
}

impl Default for ListenAddress {
fn default() -> Self {
Self::Ipv4(ListenAddr {
addr: Ipv4Addr::UNSPECIFIED,
udp_port: DEFAULT_UDP_PORT,
tcp_port: DEFAULT_TCP_PORT,
})
}
}

pub struct Config {
pub listen_addr: ListenAddress,

/// The ipv4 address to broadcast to peers about which address we are listening on.
pub ipv4_addr: Option<Ipv4Addr>,

/// The ipv6 address to broadcast to peers about which address we are listening on.
pub ipv6_addr: Option<Ipv6Addr>,

/// The udp4 port to broadcast to peers in order to reach back for discovery.
pub enr_udp4_port: Option<u16>,

/// The tcp4 port to broadcast to peers in order to reach back for libp2p services.
pub enr_tcp4_port: Option<u16>,

/// The udp6 port to broadcast to peers in order to reach back for discovery.
pub enr_udp6_port: Option<u16>,

/// The tcp6 port to broadcast to peers in order to reach back for libp2p services.
pub enr_tcp6_port: Option<u16>,
}

impl Default for Config {
fn default() -> Self {
Self {
listen_addr: ListenAddress::Ipv4(ListenAddr {
addr: Ipv4Addr::UNSPECIFIED,
udp_port: DEFAULT_UDP_PORT,
tcp_port: DEFAULT_TCP_PORT,
}),
ipv4_addr: Some(Ipv4Addr::UNSPECIFIED),
ipv6_addr: None,
enr_udp4_port: Some(DEFAULT_UDP_PORT),
enr_tcp4_port: None,
enr_udp6_port: None,
enr_tcp6_port: None,
}
}
}

impl Config {
pub fn to_listen_config(&self) -> ListenConfig {
match &self.listen_addr {
ListenAddress::Ipv4(v) => ListenConfig::Ipv4 {
ip: v.addr,
port: v.udp_port,
},
ListenAddress::Ipv6(v) => ListenConfig::Ipv6 {
ip: v.addr,
port: v.udp_port,
},
ListenAddress::Dual(ipv4, ipv6) => ListenConfig::DualStack {
ipv4: ipv4.addr,
ipv4_port: ipv4.udp_port,
ipv6: ipv6.addr,
ipv6_port: ipv6.udp_port,
},
}
}
}
Loading

0 comments on commit 053995a

Please sign in to comment.