From d950bce3f5a5ec3e619bcf4115cddebc892a5126 Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Fri, 12 Apr 2024 17:11:14 +0200 Subject: [PATCH] refactor: use async fns in minimal example (#7600) --- Cargo.toml | 13 +++++-- examples/exex/minimal/src/main.rs | 65 +++++++++++++------------------ 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98b18dd6d..df9416813 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -260,8 +260,13 @@ reth-trie = { path = "crates/trie" } reth-trie-parallel = { path = "crates/trie-parallel" } # revm -revm = { version = "8.0.0", features = ["std", "secp256k1"], default-features = false } -revm-primitives = { version = "3.1.0", features = ["std"], default-features = false } +revm = { version = "8.0.0", features = [ + "std", + "secp256k1", +], default-features = false } +revm-primitives = { version = "3.1.0", features = [ + "std", +], default-features = false } revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors", rev = "21f8f3d" } # eth @@ -276,7 +281,9 @@ alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "987b alloy-rpc-types-engine = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } alloy-genesis = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } alloy-node-bindings = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } -alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "987b393", default-features = false, features = ["reqwest"] } +alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "987b393", default-features = false, features = [ + "reqwest", +] } alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } alloy-signer = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } alloy-signer-wallet = { git = "https://github.com/alloy-rs/alloy", rev = "987b393" } diff --git a/examples/exex/minimal/src/main.rs b/examples/exex/minimal/src/main.rs index 3cc8c6b06..410a4a4ea 100644 --- a/examples/exex/minimal/src/main.rs +++ b/examples/exex/minimal/src/main.rs @@ -1,55 +1,46 @@ -use std::{ - pin::Pin, - task::{ready, Context, Poll}, -}; - use futures::Future; use reth::builder::FullNodeTypes; -use reth_exex::{ExExContext, ExExEvent}; +use reth_exex::ExExContext; use reth_node_ethereum::EthereumNode; use reth_provider::CanonStateNotification; -/// A minimal example of an ExEx that simply prints out commit and reorg notifications. -struct MinimalExEx { +/// The initialization logic of the ExEx is just an async function. +/// +/// During initialization you can wait for resources you need to be up for the ExEx to function, +/// like a database connection. +async fn exex_init( ctx: ExExContext, +) -> eyre::Result>> { + Ok(exex(ctx)) } -impl Future for MinimalExEx { - type Output = eyre::Result<()>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.get_mut(); - - // Process all new chain state notifications until there are no more - while let Some(notification) = ready!(this.ctx.notifications.poll_recv(cx)) { - // Process one notification - match ¬ification { - CanonStateNotification::Commit { new } => { - println!("Received commit: {:?}", new.first().number..=new.tip().number); - } - CanonStateNotification::Reorg { old, new } => { - println!( - "Received reorg: {:?} -> {:?}", - old.first().number..=old.tip().number, - new.first().number..=new.tip().number - ); - } - }; - - // Send a finished height event, signaling the node that we don't need any blocks below - // this height anymore - this.ctx.events.send(ExExEvent::FinishedHeight(notification.tip().number))?; - } - - Poll::Pending +/// An ExEx is just a future, which means you can implement all of it in an async function! +/// +/// This ExEx just prints out whenever a state transition happens, either a new chain segment being +/// added, or a chain segment being re-orged. +async fn exex(mut ctx: ExExContext) -> eyre::Result<()> { + while let Some(notification) = ctx.notifications.recv().await { + match ¬ification { + CanonStateNotification::Commit { new } => { + println!("Received commit: {:?}", new.first().number..=new.tip().number); + } + CanonStateNotification::Reorg { old, new } => { + println!( + "Received reorg: {:?} -> {:?}", + old.first().number..=old.tip().number, + new.first().number..=new.tip().number + ); + } + }; } + Ok(()) } fn main() -> eyre::Result<()> { reth::cli::Cli::parse_args().run(|builder, _| async move { let handle = builder .node(EthereumNode::default()) - .install_exex("Minimal", move |ctx| async { Ok(MinimalExEx { ctx }) }) + .install_exex("Minimal", exex_init) .launch() .await?;