Skip to content

Commit

Permalink
feat(hera): Run the Network Driver (#44)
Browse files Browse the repository at this point in the history
### Description

Runs the `NetworkDriver` in the `hera` binary.

Think the op stack enr data isn't being deserialized correctly. [Ether's
rlp
decoding](https://docs.rs/ethers/latest/ethers/core/utils/rlp/fn.encode.html)
uses an `RlpStream` which I'm not sure is the same way `alloy_rlp`
decodes.

---------

Co-authored-by: nicolas <[email protected]>
  • Loading branch information
refcell and merklefruit authored Aug 29, 2024
1 parent 3bf2d48 commit 28258e6
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 28 deletions.
10 changes: 3 additions & 7 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ incremental = false
[workspace.dependencies]
# Workspace
op-net = { path = "crates/net" }
kona-providers = { path = "crates/kona-providers" }
rollup = { path = "crates/rollup" }
ser = { path = "crates/ser" }

# Optimism
superchain-registry = { version = "0.2.6", default-features = false }
Expand Down Expand Up @@ -165,8 +168,9 @@ libp2p = { version = "0.54.0", features = ["macros", "tokio", "tcp", "noise", "g

# Misc
tracing = "0.1.0"
tracing-subscriber = "0.3.18"
eyre = "0.6.12"
clap = "4"
clap = { version = "4.5.4", features = ["derive", "env"] }
lazy_static = "1.5.0"
futures = "0.3.30"
async-trait = "0.1.81"
Expand Down
16 changes: 4 additions & 12 deletions bin/hera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ rollup = { path = "../../crates/rollup" }

# Workspace
eyre.workspace = true
alloy.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
url.workspace = true

# Reth Dependencies
reth.workspace = true
reth-exex.workspace = true
reth-node-api.workspace = true
reth-node-ethereum.workspace = true

# OP Stack Dependencies
superchain-registry = { workspace = true, default-features = false }
kona-derive = { workspace = true, features = ["online", "serde"] }

# Needed for compatibility with Kona's ChainProvider trait
anyhow = { version = "1.0.86", default-features = false }
# Workspace Crates
op-net.workspace = true
45 changes: 43 additions & 2 deletions bin/hera/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,53 @@
#![doc(issue_tracker_base_url = "https://github.com/paradigmxyz/op-rs/issues/")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy::primitives::address;
use clap::Parser;
use eyre::Result;
use op_net::driver::NetworkDriver;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

fn main() -> Result<()> {
/// The default L2 chain ID to use. This corresponds to OP Mainnet.
pub const DEFAULT_L2_CHAIN_ID: u64 = 10;

/// The Hera CLI Arguments.
#[derive(Debug, Clone, Parser)]
pub struct HeraArgs {
/// Chain ID of the L2 network
#[clap(long = "hera.l2-chain-id", default_value_t = DEFAULT_L2_CHAIN_ID)]
pub l2_chain_id: u64,
}

#[tokio::main]
async fn main() -> Result<()> {
let args = HeraArgs::parse();
rollup::init_telemetry_stack(8090)?;

tracing::info!("Hera OP Stack Rollup node");

Ok(())
let signer = address!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9099);
let mut driver = NetworkDriver::builder()
.with_chain_id(args.l2_chain_id)
.with_unsafe_block_signer(signer)
.with_gossip_addr(socket)
.build()
.expect("Failed to builder network driver");

// Call `.start()` on the driver.
let recv = driver.take_unsafe_block_recv().ok_or(eyre::eyre!("No unsafe block receiver"))?;
driver.start().expect("Failed to start network driver");

tracing::info!("NetworkDriver started successfully.");

loop {
match recv.recv() {
Ok(block) => {
tracing::info!("Received unsafe block: {:?}", block);
}
Err(e) => {
tracing::warn!("Failed to receive unsafe block: {:?}", e);
}
}
}
}
2 changes: 1 addition & 1 deletion bin/op-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ rollup = { path = "../../crates/rollup" }

# Workspace
eyre.workspace = true
tracing.workspace = true
clap.workspace = true
tracing.workspace = true

# Reth Dependencies
reth.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use op_net::driver::NetworkDriver;
// Build the network driver.
let signer = address!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9099);
let mut driver = NetworkDriver::builder()
let driver = NetworkDriver::builder()
.with_chain_id(10) // op mainnet chain id
.with_unsafe_block_signer(signer)
.with_gossip_addr(socket)
Expand Down
23 changes: 21 additions & 2 deletions crates/net/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use alloy::primitives::Address;
use discv5::ListenConfig;
use eyre::Result;
use std::net::{IpAddr, SocketAddr};
use std::{
net::{IpAddr, SocketAddr},
time::Duration,
};
use tokio::sync::watch::channel;

use libp2p::{
Expand Down Expand Up @@ -39,6 +42,8 @@ pub struct NetworkDriverBuilder {
pub noise_config: Option<NoiseConfig>,
/// The [YamuxConfig] for the swarm.
pub yamux_config: Option<YamuxConfig>,
/// The idle connection timeout.
pub timeout: Option<Duration>,
}

impl NetworkDriverBuilder {
Expand Down Expand Up @@ -95,6 +100,12 @@ impl NetworkDriverBuilder {
self
}

/// Set the swarm's idle connection timeout.
pub fn with_idle_connection_timeout(&mut self, timeout: Duration) -> &mut Self {
self.timeout = Some(timeout);
self
}

/// Specifies the [GossipConfig] for the `gossipsub` configuration.
///
/// If not set, the [NetworkDriverBuilder] will use the default gossipsub
Expand Down Expand Up @@ -192,6 +203,7 @@ impl NetworkDriverBuilder {
let behaviour = Behaviour::new(config, &[Box::new(handler.clone())])?;

// Build the swarm.
let timeout = self.timeout.take().unwrap_or(Duration::from_secs(60));
let noise_config = self.noise_config.take();
let keypair = self.keypair.take().unwrap_or(Keypair::generate_secp256k1());
let swarm = SwarmBuilder::with_existing_identity(keypair)
Expand All @@ -205,6 +217,7 @@ impl NetworkDriverBuilder {
|| self.yamux_config.take().unwrap_or_default(),
)?
.with_behaviour(|_| behaviour)?
.with_swarm_config(|c| c.with_idle_connection_timeout(timeout))
.build();

let gossip_addr =
Expand All @@ -227,7 +240,13 @@ impl NetworkDriverBuilder {
discovery_builder
}
.build()?;
Ok(NetworkDriver { unsafe_block_recv, unsafe_block_signer_sender, gossip, discovery })

Ok(NetworkDriver {
discovery,
gossip,
unsafe_block_recv: Some(unsafe_block_recv),
unsafe_block_signer_sender: Some(unsafe_block_signer_sender),
})
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/net/src/discovery/bootnodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@ lazy_static! {
Enr::from_str("enr:-J24QDXyyxvQYsd0yfsN0cRr1lZ1N11zGTplMNlW4xNEc7LkPXh0NAJ9iSOVdRO95GPYAIc6xmyoCCG6_0JxdL3a0zaGAYiOoAjFgmlkgnY0gmlwhAPckbGHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQJwoS7tzwxqXSyFL7g0JM-KWVbgvjfB8JA__T7yY_cYboN0Y3CCJAaDdWRwgiQG").unwrap(),
Enr::from_str("enr:-J24QHmGyBwUZXIcsGYMaUqGGSl4CFdx9Tozu-vQCn5bHIQbR7On7dZbU61vYvfrJr30t0iahSqhc64J46MnUO2JvQaGAYiOoCKKgmlkgnY0gmlwhAPnCzSHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQINc4fSijfbNIiGhcgvwjsjxVFJHUstK9L1T8OTKUjgloN0Y3CCJAaDdWRwgiQG").unwrap(),
Enr::from_str("enr:-J24QG3ypT4xSu0gjb5PABCmVxZqBjVw9ca7pvsI8jl4KATYAnxBmfkaIuEqy9sKvDHKuNCsy57WwK9wTt2aQgcaDDyGAYiOoGAXgmlkgnY0gmlwhDbGmZaHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQIeAK_--tcLEiu7HvoUlbV52MspE0uCocsx1f_rYvRenIN0Y3CCJAaDdWRwgiQG").unwrap(),

// Conduit Bootnode
// discv5::enr::NodeId::from("enode://9d7a3efefe442351217e73b3a593bcb8efffb55b4807699972145324eab5e6b382152f8d24f6301baebbfb5ecd4127bd3faab2842c04cd432bdf50ba092f6645@34.65.109.126:30305"),
].to_vec();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_bootnodes() {
assert_eq!(BOOTNODES.len(), 8);
}
}
14 changes: 12 additions & 2 deletions crates/net/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use tokio::{select, sync::watch};
/// - Peer discovery with `discv5`.
pub struct NetworkDriver {
/// Channel to receive unsafe blocks.
pub unsafe_block_recv: Receiver<ExecutionPayloadEnvelope>,
pub(crate) unsafe_block_recv: Option<Receiver<ExecutionPayloadEnvelope>>,
/// Channel to send unsafe signer updates.
pub unsafe_block_signer_sender: watch::Sender<Address>,
pub(crate) unsafe_block_signer_sender: Option<watch::Sender<Address>>,
/// The swarm instance.
pub gossip: GossipDriver,
/// The discovery service driver.
Expand All @@ -32,6 +32,16 @@ impl NetworkDriver {
NetworkDriverBuilder::new()
}

/// Take the unsafe block receiver.
pub fn take_unsafe_block_recv(&mut self) -> Option<Receiver<ExecutionPayloadEnvelope>> {
self.unsafe_block_recv.take()
}

/// Take the unsafe block signer sender.
pub fn take_unsafe_block_signer_sender(&mut self) -> Option<watch::Sender<Address>> {
self.unsafe_block_signer_sender.take()
}

/// Starts the Discv5 peer discovery & libp2p services
/// and continually listens for new peers and messages to handle
pub fn start(mut self) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/net/src/gossip/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ lazy_static! {
/// Notable defaults:
/// - flood_publish: false (call `.flood_publish(true)` on the [ConfigBuilder] to enable)
/// - backoff_slack: 1
/// - heart beat interval: 1 second
/// - peer exchange is disabled
/// - maximum byte size for gossip messages: 2048 bytes
///
Expand Down

0 comments on commit 28258e6

Please sign in to comment.