Skip to content

Commit

Permalink
touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jun 20, 2024
1 parent 7f6edef commit d38e4d1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ to make a PR!
| [OP Bridge ExEx](./exex/op-bridge) | Illustrates an ExEx that decodes Optimism deposit and withdrawal receipts from L1 |
| [Rollup](./exex/rollup) | Illustrates a rollup ExEx that derives the state from L1 |
| [In Memory State](./exex/in-memory-state) | Illustrates an ExEx that tracks the plain state in memory |
| [Discv5 as ExEx](./exex/discv5) | Illustrates an ExEx that runs discv5 discovery stack |

## RPC

Expand Down
3 changes: 2 additions & 1 deletion examples/exex/discv5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "exex-discv5"
name = "example-exex-discv5"
version = "0.0.0"
publish = false
edition.workspace = true
Expand All @@ -14,6 +14,7 @@ reth.workspace = true
reth-exex.workspace = true
reth-node-api.workspace = true
reth-node-ethereum.workspace = true
reth-network-peers.workspace = true
reth-tracing.workspace = true

clap.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions examples/exex/discv5/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use network::{cli_ext::Discv5ArgsExt, DiscV5Node};
use network::{cli_ext::Discv5ArgsExt, DiscV5ExEx};
use reth_node_ethereum::EthereumNode;

mod network;
Expand All @@ -12,7 +12,7 @@ fn main() -> eyre::Result<()> {
let handle = builder
.node(EthereumNode::default())
.install_exex("exex-discv5", move |_ctx| async move {
DiscV5Node::new(tcp_port, udp_port).await
DiscV5ExEx::new(tcp_port, udp_port).await
})
.launch()
.await?;
Expand Down
26 changes: 12 additions & 14 deletions examples/exex/discv5/src/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#![allow(dead_code)]

use discv5::{enr::secp256k1::rand, Enr, Event, ListenConfig};
use reth::{network::config::SecretKey, primitives::NodeRecord};
use reth::network::config::SecretKey;
use reth_discv5::{enr::EnrCombinedKeyWrapper, Config, Discv5};

use reth_network_peers::NodeRecord;
use reth_tracing::tracing::info;
use std::{
future::Future,
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
task::{ready, Context, Poll},
};
use tokio::sync::mpsc;

pub(crate) mod cli_ext;

/// Helper struct to manage a discovery node using discv5.
pub(crate) struct DiscV5Node {
pub(crate) struct DiscV5ExEx {
/// The inner discv5 instance.
inner: Discv5,
/// The node record of the discv5 instance.
Expand All @@ -25,9 +25,9 @@ pub(crate) struct DiscV5Node {
events: mpsc::Receiver<discv5::Event>,
}

impl DiscV5Node {
impl DiscV5ExEx {
/// Starts a new discv5 node.
pub async fn new(udp_port: u16, tcp_port: u16) -> eyre::Result<DiscV5Node> {
pub async fn new(udp_port: u16, tcp_port: u16) -> eyre::Result<DiscV5ExEx> {
let secret_key = SecretKey::new(&mut rand::thread_rng());

let discv5_addr: SocketAddr = format!("127.0.0.1:{udp_port}").parse()?;
Expand Down Expand Up @@ -55,38 +55,36 @@ impl DiscV5Node {
}
}

impl Future for DiscV5Node {
impl Future for DiscV5ExEx {
type Output = eyre::Result<()>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut();
while let Poll::Ready(event) = this.events.poll_recv(cx) {
match event {
loop {
match ready!(this.events.poll_recv(cx)) {
Some(evt) => {
if let Event::SessionEstablished(enr, socket_addr) = evt {
info!(?enr, ?socket_addr, "Session established with a new peer.");
}
// more events here
}
None => return Poll::Ready(Ok(())),
}
}
Poll::Pending
}
}

#[cfg(test)]
mod tests {
use crate::network::DiscV5Node;
use crate::network::DiscV5ExEx;
use tracing::info;

#[tokio::test]
async fn can_establish_discv5_session_with_peer() {
reth_tracing::init_test_tracing();
let mut node_1 = DiscV5Node::new(30301, 30303).await.unwrap();
let mut node_1 = DiscV5ExEx::new(30301, 30303).await.unwrap();
let node_1_enr = node_1.local_enr();

let mut node_2 = DiscV5Node::new(30302, 30303).await.unwrap();
let mut node_2 = DiscV5ExEx::new(30302, 30303).await.unwrap();
let node_2_enr = node_2.local_enr();

info!(?node_1_enr, ?node_2_enr, "Started discovery nodes.");
Expand Down

0 comments on commit d38e4d1

Please sign in to comment.