-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): minimal viable ExEx (#7565)
Co-authored-by: Oliver Nordbjerg <hi@notbjerg.me> Co-authored-by: Oliver Nordbjerg <onbjerg@users.noreply.github.com>
- Loading branch information
1 parent
3ffc729
commit 39dea65
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "minimal" | ||
version = "0.0.0" | ||
publish = false | ||
edition.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
reth.workspace = true | ||
reth-exex.workspace = true | ||
reth-node-api.workspace = true | ||
reth-node-core.workspace = true | ||
reth-node-ethereum.workspace = true | ||
reth-primitives.workspace = true | ||
reth-provider.workspace = true | ||
|
||
eyre.workspace = true | ||
tokio.workspace = true | ||
futures.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::{ | ||
pin::Pin, | ||
task::{ready, Context, Poll}, | ||
}; | ||
|
||
use futures::Future; | ||
use reth::builder::FullNodeTypes; | ||
use reth_exex::{ExExContext, ExExEvent}; | ||
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> { | ||
ctx: ExExContext<Node>, | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
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 }) }) | ||
.launch() | ||
.await?; | ||
|
||
handle.wait_for_node_exit().await | ||
}) | ||
} |