Skip to content

Commit 66db2b6

Browse files
fix: improve FI insert logic (#599)
* fix: improve FI insert logic * bump version * fix: return Err not Ok
1 parent ccc06de commit 66db2b6

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ resolver = "2"
1313
default-members = ["whitelist"]
1414

1515
[workspace.package]
16-
version = "1.8.2"
16+
version = "1.9.0"
1717
edition = "2024"
1818
repository = "https://github.com/NethermindEth/Catalyst"
1919
license = "MIT"

whitelist/src/forced_inclusion/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ impl ForcedInclusion {
8282
Ok(fi)
8383
}
8484

85-
pub fn increment_index(&self) {
85+
fn increment_index(&self) {
8686
self.index.fetch_add(1, Ordering::SeqCst);
8787
}
88+
89+
pub async fn release_forced_inclusion(&self) {
90+
self.index.fetch_sub(1, Ordering::SeqCst);
91+
}
8892
}

whitelist/src/node/batch_manager/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use common::{
2222
};
2323
use config::BatchBuilderConfig;
2424
use std::sync::Arc;
25+
use tokio_util::sync::CancellationToken;
2526
use tracing::{debug, error, info, warn};
2627

2728
// Temporary struct while we don't have forced inclusion flag in extra data
@@ -40,6 +41,7 @@ pub struct BatchManager {
4041
forced_inclusion: Arc<ForcedInclusion>,
4142
cached_forced_inclusion_txs: CachedForcedInclusion,
4243
metrics: Arc<Metrics>,
44+
cancel_token: CancellationToken,
4345
}
4446

4547
impl BatchManager {
@@ -49,6 +51,7 @@ impl BatchManager {
4951
ethereum_l1: Arc<EthereumL1<ExecutionLayer>>,
5052
taiko: Arc<Taiko<ExecutionLayer>>,
5153
metrics: Arc<Metrics>,
54+
cancel_token: CancellationToken,
5255
) -> Self {
5356
info!(
5457
"Batch builder config:\n\
@@ -76,6 +79,7 @@ impl BatchManager {
7679
forced_inclusion,
7780
cached_forced_inclusion_txs: CachedForcedInclusion::Empty,
7881
metrics,
82+
cancel_token,
7983
}
8084
}
8185

@@ -570,6 +574,16 @@ impl BatchManager {
570574
operation_type: OperationType,
571575
anchor_block_id: u64,
572576
) -> Result<Option<BuildPreconfBlockResponse>, Error> {
577+
if self.batch_builder.has_current_forced_inclusion() {
578+
// we should not enter this function if we already have a forced inclusion
579+
// if we see this error in logs that means something is wrong in the code logic
580+
error!(
581+
"Bug in code: Try to add new l2 block with forced inclusion when we already have one"
582+
);
583+
return Err(anyhow::anyhow!(
584+
"Bug in code: Try to add new l2 block with forced inclusion when we already have one"
585+
));
586+
}
573587
// get current forced inclusion
574588
let start = std::time::Instant::now();
575589
let forced_inclusion = self.forced_inclusion.consume_forced_inclusion().await?;
@@ -622,18 +636,25 @@ impl BatchManager {
622636
"Failed to advance head to new forced inclusion L2 block: {}",
623637
err
624638
);
639+
self.forced_inclusion.release_forced_inclusion().await;
625640
self.batch_builder.remove_current_batch();
626-
return Ok(None); // TODO: why not return error here?
641+
return Err(anyhow::anyhow!(
642+
"Failed to advance head to new forced inclusion L2 block: {}",
643+
err
644+
));
627645
}
628646
};
629647
// set it to batch builder
630648
if !self
631649
.batch_builder
632650
.set_forced_inclusion(Some(forced_inclusion_batch))
633651
{
652+
// We should never enter here because it means we already have a forced inclusion
653+
// but we didn't set it yet. And at the beginning of the function we checked if
654+
// the forced inclusion is empty. This is a bug in the code logic
634655
error!("Failed to set forced inclusion to batch");
635-
self.batch_builder.remove_current_batch();
636-
return Ok(None); // TODO: why not return error here?
656+
self.cancel_token.cancel();
657+
return Err(anyhow::anyhow!("Failed to set forced inclusion to batch"));
637658
}
638659
return Ok(forced_inclusion_block_response);
639660
}
@@ -821,6 +842,7 @@ impl BatchManager {
821842
forced_inclusion: self.forced_inclusion.clone(),
822843
cached_forced_inclusion_txs: CachedForcedInclusion::Empty,
823844
metrics: self.metrics.clone(),
845+
cancel_token: self.cancel_token.clone(),
824846
}
825847
}
826848

whitelist/src/node/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl Node {
7171
ethereum_l1.clone(),
7272
taiko.clone(),
7373
metrics.clone(),
74+
cancel_token.clone(),
7475
);
7576
let head_verifier = L2HeadVerifier::new();
7677
let watchdog = common_utils::watchdog::Watchdog::new(

0 commit comments

Comments
 (0)