-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
4,531 additions
and
479 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} | ||
} |
Oops, something went wrong.