Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(katana): dont run sequencing task inside of pipeline #2740

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

15 changes: 5 additions & 10 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use katana_core::service::messaging::MessagingConfig;
use katana_db::mdbx::DbEnv;
use katana_executor::implementation::blockifier::BlockifierFactory;
use katana_executor::{ExecutionFlags, ExecutorFactory};
use katana_pipeline::{stage, Pipeline};
use katana_pipeline::stage::Sequencing;
use katana_pool::ordering::FiFo;
use katana_pool::validation::stateful::TxValidator;
use katana_pool::TxPool;
Expand Down Expand Up @@ -126,27 +126,22 @@ impl Node {
let block_producer = self.block_producer.clone();
let validator = self.block_producer.validator().clone();

// --- build sequencing stage
// --- build and run sequencing task

let sequencing = stage::Sequencing::new(
let sequencing = Sequencing::new(
pool.clone(),
backend.clone(),
self.task_manager.task_spawner(),
block_producer.clone(),
self.messaging_config.clone(),
);

// --- build and start the pipeline

let mut pipeline = Pipeline::new();
pipeline.add_stage(Box::new(sequencing));

self.task_manager
.task_spawner()
.build_task()
.critical()
.name("Pipeline")
.spawn(pipeline.into_future());
.name("Sequencing")
.spawn(sequencing.into_future());

let node_components = (pool, backend, block_producer, validator, self.forked_client.take());
let rpc = spawn(node_components, self.rpc_config.clone()).await?;
Expand Down
49 changes: 23 additions & 26 deletions crates/katana/pipeline/src/stage/sequencing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::future::IntoFuture;
use std::sync::Arc;

use anyhow::Result;
use futures::future;
use futures::future::{self, BoxFuture};
use katana_core::backend::Backend;
use katana_core::service::block_producer::{BlockProducer, BlockProductionError};
use katana_core::service::messaging::{MessagingConfig, MessagingService, MessagingTask};
Expand All @@ -11,8 +12,7 @@ use katana_pool::{TransactionPool, TxPool};
use katana_tasks::{TaskHandle, TaskSpawner};
use tracing::error;

use super::{StageId, StageResult};
use crate::Stage;
pub type SequencingFut = BoxFuture<'static, ()>;

/// The sequencing stage is responsible for advancing the chain state.
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -61,31 +61,28 @@ impl<EF: ExecutorFactory> Sequencing<EF> {
}
}

#[async_trait::async_trait]
impl<EF: ExecutorFactory> Stage for Sequencing<EF> {
fn id(&self) -> StageId {
StageId::Sequencing
}
impl<EF: ExecutorFactory> IntoFuture for Sequencing<EF> {
type Output = ();
type IntoFuture = SequencingFut;

#[tracing::instrument(skip(self), name = "Stage", fields(id = %self.id()))]
async fn execute(&mut self) -> StageResult {
// Build the messaging and block production tasks.
let messaging = self.run_messaging().await?;
let block_production = self.run_block_production();
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
// Build the messaging and block production tasks.
let messaging = self.run_messaging().await.unwrap();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ohayo, sensei! Avoid using unwrap() on run_messaging().await

Using unwrap() may cause a panic if run_messaging() returns an error. It's better to handle the error gracefully to prevent unexpected crashes.

Consider handling the error as follows:

-            let messaging = self.run_messaging().await.unwrap();
+            let messaging = match self.run_messaging().await {
+                Ok(handle) => handle,
+                Err(e) => {
+                    error!(target: "sequencing", reason = ?e, "Failed to start messaging task.");
+                    return;
+                }
+            };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let messaging = self.run_messaging().await.unwrap();
let messaging = match self.run_messaging().await {
Ok(handle) => handle,
Err(e) => {
error!(target: "sequencing", reason = ?e, "Failed to start messaging task.");
return;
}
};

let block_production = self.run_block_production();

// Neither of these tasks should complete as they are meant to be run forever,
// but if either of them do complete, the sequencing stage should return.
//
// Select on the tasks completion to prevent the task from failing silently (if any).
tokio::select! {
res = messaging => {
error!(target: "pipeline", reason = ?res, "Messaging task finished unexpectedly.");
},
res = block_production => {
error!(target: "pipeline", reason = ?res, "Block production task finished unexpectedly.");
// Neither of these tasks should complete as they are meant to be run forever,
// but if either of them do complete, the sequencing stage should return.
//
// Select on the tasks completion to prevent the task from failing silently (if any).
tokio::select! {
res = messaging => {
error!(target: "sequencing", reason = ?res, "Messaging task finished unexpectedly.");
},
res = block_production => {
error!(target: "sequencing", reason = ?res, "Block production task finished unexpectedly.");
}
}
}

Ok(())
})
}
}
Loading