mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor: use async fns in minimal example (#7600)
This commit is contained in:
13
Cargo.toml
13
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" }
|
||||
|
||||
@ -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<Node: FullNodeTypes> {
|
||||
/// 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<Node: FullNodeTypes>(
|
||||
ctx: ExExContext<Node>,
|
||||
) -> eyre::Result<impl Future<Output = eyre::Result<()>>> {
|
||||
Ok(exex(ctx))
|
||||
}
|
||||
|
||||
impl<Node: FullNodeTypes> Future for MinimalExEx<Node> {
|
||||
type Output = eyre::Result<()>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
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<Node: FullNodeTypes>(mut ctx: ExExContext<Node>) -> 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?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user