Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Aug 31, 2023
1 parent 4bf1345 commit 704fd22
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 141 deletions.
49 changes: 45 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "fa3017715a298728
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
11 changes: 9 additions & 2 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ lazy_static = { workspace = true }
snap = "1"
silius-primitives = { path = "../primitives" }
silius-uopool = {path = "../uopool"}
ssz_rs = "0.9.0"
ssz_rs_derive = "0.9.0"
ssz_rs = { workspace = true }
ssz_rs_derive = { workspace = true }
discv5 = {version = "0.3.0", features = ["libp2p"]}
tokio = { workspace = true }
tracing = { workspace = true }
Expand All @@ -33,3 +33,10 @@ libp2p-mplex = {path = "/chain/eth/rust-libp2p/muxers/mplex"}
path = "/chain/eth/rust-libp2p/libp2p"
# version = "0.52.3"
features = [ "identify", "yamux","noise", "gossipsub", "dns", "tcp", "tokio", "secp256k1", "macros", "request-response"]

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

#[derive(NetworkBehaviour)]
#[behaviour(to_swarm = "Event", event_process = false)]
Expand All @@ -18,14 +18,9 @@ pub struct Behaviour {
}

impl Behaviour {
pub fn new(key: CombinedKey, config: Config) -> anyhow::Result<Self> {
pub fn new(key: CombinedKey, config: Config, mempool_ids: MempoolId) -> anyhow::Result<Self> {
let enr = build_enr(&key, &config)?;
let gossipsub_config = ConfigBuilder::default()
.validation_mode(gossipsub::ValidationMode::Anonymous)
.build()
.map_err(|e| anyhow::anyhow!(e))?;
let gossipsub = Gossipsub::new(gossipsub::MessageAuthenticity::Anonymous, gossipsub_config)
.map_err(|e| anyhow::anyhow!(e))?;
let gossipsub = create_gossisub(mempool_ids).map_err(|e| anyhow::anyhow!(e))?;
let reqrep =
BundlerRequestResponse::new(BundlerCodec {}, request_response::Config::default());
let discovery = Discovery::new(enr, key, config)?;
Expand Down
32 changes: 21 additions & 11 deletions crates/p2p/src/gossipsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use libp2p::gossipsub::{
Behaviour, ConfigBuilder, DataTransform, IdentTopic, Message, MessageAuthenticity, RawMessage,
TopicHash, WhitelistSubscriptionFilter,
TopicHash, ValidationMode, WhitelistSubscriptionFilter,
};
use silius_uopool::MempoolId;
use snap::raw::{decompress_len, Decoder, Encoder};
Expand Down Expand Up @@ -82,24 +82,34 @@ impl DataTransform for SnappyTransform {
}
}

pub fn create_whitelist_filter(mempool_ids: &[MempoolId]) -> WhitelistSubscriptionFilter {
pub fn create_whitelist_filter(mempool_id: MempoolId) -> WhitelistSubscriptionFilter {
let mut possible_hashes: HashSet<TopicHash> = HashSet::new();
for mempool_id in mempool_ids.iter() {
let topic = IdentTopic::new(format!("/{TOPIC_PREFIX:}/{mempool_id:}/{USER_OPS_WITH_ENTRY_POINT_TOPIC:}/{SSZ_SNAPPY_ENCODING:}"));
possible_hashes.insert(topic.into());
}
let topic = topic(&mempool_id);
possible_hashes.insert(topic.into());
WhitelistSubscriptionFilter(possible_hashes)
}

pub fn create_gossisub(mempool_ids: &[MempoolId]) -> Result<Gossipsub, &'static str> {
let filter = create_whitelist_filter(mempool_ids);
let gs_config = ConfigBuilder::default().build()?;
pub fn topic(mempool_id: &MempoolId) -> IdentTopic {
IdentTopic::new(format!(
"/{TOPIC_PREFIX:}/{mempool_id:x}/{USER_OPS_WITH_ENTRY_POINT_TOPIC:}/{SSZ_SNAPPY_ENCODING:}"
))
}

pub fn create_gossisub(mempool_id: MempoolId) -> Result<Gossipsub, &'static str> {
let filter = create_whitelist_filter(mempool_id);
let gs_config = ConfigBuilder::default()
.validation_mode(ValidationMode::Anonymous)
.build()?;
let snappy_transform = SnappyTransform::new(MAX_GOSSIP_SNAP_SIZE);
Gossipsub::new_with_subscription_filter_and_transform(
let mut gossipsub = Gossipsub::new_with_subscription_filter_and_transform(
MessageAuthenticity::Anonymous,
gs_config,
None,
filter,
snappy_transform,
)
)?;
let _ = gossipsub
.subscribe(&topic(&mempool_id))
.map_err(|_| "subscribe error")?;
Ok(gossipsub)
}
Loading

0 comments on commit 704fd22

Please sign in to comment.