Skip to content

Commit 5363593

Browse files
committed
apollo_l1_provider: add verification that tx is pending when getting it from proposable index
1 parent 977f627 commit 5363593

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

crates/apollo_l1_provider/src/l1_provider_tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ fn add_tx_double_scraped_doesnt_update_scrape_timestamp() {
223223
assert_eq!(l1_provider, expected_l1_provider);
224224
}
225225

226+
#[test]
227+
#[should_panic(expected = "Only Pending transactions should be in the proposable index.")]
228+
fn get_txs_panics_if_transaction_on_proposable_index_is_not_pending() {
229+
// Setup.
230+
let mut l1_provider = L1ProviderContentBuilder::new()
231+
.with_txs([l1_handler(1)])
232+
.with_wrongly_committing_txs_in_proposable_index()
233+
.with_state(ProviderState::Pending)
234+
.build_into_l1_provider();
235+
236+
// Put the provider in the right mood for a proposal.
237+
l1_provider.start_block(BlockNumber(0), ProposeSession).unwrap();
238+
239+
// Test.
240+
l1_provider.get_txs(1, BlockNumber(0)).unwrap();
241+
}
242+
226243
#[test]
227244
fn pending_state_returns_error() {
228245
// Setup.

crates/apollo_l1_provider/src/test_utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ impl L1ProviderContentBuilder {
258258
self
259259
}
260260

261+
pub fn with_wrongly_committing_txs_in_proposable_index(mut self) -> Self {
262+
self.tx_manager_content_builder.use_wrongly_committing_txs_in_proposable_index = true;
263+
self
264+
}
265+
261266
pub fn build(mut self) -> L1ProviderContent {
262267
if let Some(config) = self.config {
263268
self.tx_manager_content_builder =
@@ -308,6 +313,7 @@ struct TransactionManagerContent {
308313
pub consumed: Option<Vec<ConsumedTransaction>>,
309314
pub cancel_requested: Option<Vec<CancellationRequest>>,
310315
pub config: Option<TransactionManagerConfig>,
316+
pub use_wrongly_committing_txs_in_proposable_index: bool,
311317
}
312318

313319
impl TransactionManagerContent {
@@ -426,6 +432,11 @@ impl From<TransactionManagerContent> for TransactionManager {
426432
}
427433

428434
let current_epoch = StagingEpoch::new();
435+
if content.use_wrongly_committing_txs_in_proposable_index {
436+
for (_tx_hash, record) in records.iter_mut() {
437+
record.mark_committed();
438+
}
439+
}
429440
TransactionManager::create_for_testing(
430441
records.into(),
431442
proposable_index,
@@ -444,6 +455,7 @@ struct TransactionManagerContentBuilder {
444455
consumed: Option<Vec<ConsumedTransaction>>,
445456
config: Option<TransactionManagerConfig>,
446457
cancel_requested: Option<Vec<CancellationRequest>>,
458+
use_wrongly_committing_txs_in_proposable_index: bool,
447459
}
448460

449461
impl TransactionManagerContentBuilder {
@@ -532,6 +544,8 @@ impl TransactionManagerContentBuilder {
532544
rejected: self.rejected,
533545
cancel_requested: self.cancel_requested,
534546
config: self.config,
547+
use_wrongly_committing_txs_in_proposable_index: self
548+
.use_wrongly_committing_txs_in_proposable_index,
535549
})
536550
}
537551

crates/apollo_l1_provider/src/transaction_manager.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ impl TransactionManager {
8484
.copied()
8585
.collect();
8686

87+
for &tx_hash in unstaged_tx_hashes.iter() {
88+
let record = self.records.get(&tx_hash).expect("transaction should exist");
89+
if record.state != TransactionState::Pending {
90+
panic!(
91+
"Transaction {tx_hash} has state {:?}. Only Pending transactions should be in \
92+
the proposable index.",
93+
record.state
94+
);
95+
}
96+
}
97+
8798
let mut txs = Vec::with_capacity(n_txs);
8899
let current_staging_epoch = self.current_staging_epoch; // borrow-checker constraint.
89100
for tx_hash in unstaged_tx_hashes {

0 commit comments

Comments
 (0)