Skip to content

Commit

Permalink
dev: update NodeExitFuture (#9153)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
greged93 and mattsse authored Jun 28, 2024
1 parent ce8bcd8 commit c12ca99
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions crates/node-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ reth-net-nat.workspace = true
reth-network-peers.workspace = true
reth-tasks.workspace = true
reth-consensus-common.workspace = true
reth-beacon-consensus.workspace = true
reth-prune-types.workspace = true
reth-stages-types.workspace = true

Expand Down Expand Up @@ -102,7 +101,6 @@ optimism = [
"reth-primitives/optimism",
"reth-provider/optimism",
"reth-rpc-types-compat/optimism",
"reth-beacon-consensus/optimism",
"reth-rpc-eth-api/optimism",
"reth-rpc-eth-types/optimism"
]
Expand Down
52 changes: 27 additions & 25 deletions crates/node-core/src/exit.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
//! Helper types for waiting for the node to exit.
use futures::FutureExt;
use reth_beacon_consensus::BeaconConsensusEngineError;
use futures::{future::BoxFuture, FutureExt};
use std::{
fmt,
future::Future,
pin::Pin,
task::{ready, Context, Poll},
};
use tokio::sync::oneshot;

/// A Future which resolves when the node exits
#[derive(Debug)]
pub struct NodeExitFuture {
/// The receiver half of the channel for the consensus engine.
/// This can be used to wait for the consensus engine to exit.
consensus_engine_rx: Option<oneshot::Receiver<Result<(), BeaconConsensusEngineError>>>,
/// The consensus engine future.
/// This can be polled to wait for the consensus engine to exit.
consensus_engine_fut: Option<BoxFuture<'static, eyre::Result<()>>>,

/// Flag indicating whether the node should be terminated after the pipeline sync.
terminate: bool,
}

impl fmt::Debug for NodeExitFuture {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NodeExitFuture")
.field("consensus_engine_fut", &"...")
.field("terminate", &self.terminate)
.finish()
}
}

impl NodeExitFuture {
/// Create a new `NodeExitFuture`.
pub const fn new(
consensus_engine_rx: oneshot::Receiver<Result<(), BeaconConsensusEngineError>>,
terminate: bool,
) -> Self {
Self { consensus_engine_rx: Some(consensus_engine_rx), terminate }
pub fn new<F>(consensus_engine_fut: F, terminate: bool) -> Self
where
F: Future<Output = eyre::Result<()>> + 'static + Send,
{
Self { consensus_engine_fut: Some(Box::pin(consensus_engine_fut)), terminate }
}
}

Expand All @@ -35,18 +42,17 @@ impl Future for NodeExitFuture {

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Some(rx) = this.consensus_engine_rx.as_mut() {
if let Some(rx) = this.consensus_engine_fut.as_mut() {
match ready!(rx.poll_unpin(cx)) {
Ok(res) => {
this.consensus_engine_rx.take();
res?;
Ok(_) => {
this.consensus_engine_fut.take();
if this.terminate {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
Err(err) => Poll::Ready(Err(err.into())),
Err(err) => Poll::Ready(Err(err)),
}
} else {
Poll::Pending
Expand All @@ -61,11 +67,9 @@ mod tests {

#[tokio::test]
async fn test_node_exit_future_terminate_true() {
let (tx, rx) = oneshot::channel::<Result<(), BeaconConsensusEngineError>>();
let fut = async { Ok(()) };

let _ = tx.send(Ok(()));

let node_exit_future = NodeExitFuture::new(rx, true);
let node_exit_future = NodeExitFuture::new(fut, true);

let res = node_exit_future.await;

Expand All @@ -74,11 +78,9 @@ mod tests {

#[tokio::test]
async fn test_node_exit_future_terminate_false() {
let (tx, rx) = oneshot::channel::<Result<(), BeaconConsensusEngineError>>();

let _ = tx.send(Ok(()));
let fut = async { Ok(()) };

let mut node_exit_future = NodeExitFuture::new(rx, false);
let mut node_exit_future = NodeExitFuture::new(fut, false);
poll_fn(|cx| {
assert!(node_exit_future.poll_unpin(cx).is_pending());
Poll::Ready(())
Expand Down
5 changes: 4 additions & 1 deletion crates/node/builder/src/launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ where
on_node_started.on_event(full_node.clone())?;

let handle = NodeHandle {
node_exit_future: NodeExitFuture::new(rx, full_node.config.debug.terminate),
node_exit_future: NodeExitFuture::new(
async { Ok(rx.await??) },
full_node.config.debug.terminate,
),
node: full_node,
};

Expand Down

0 comments on commit c12ca99

Please sign in to comment.