-
Notifications
You must be signed in to change notification settings - Fork 5
tests: add test fixture for easier abstraction #407
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
Merged
+2,706
−1,737
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d80987f
tests: add test fixture for easier abstraction
greged93 0e79430
Merge branch 'main' into tests/improve-framework
greged93 395add0
fix: lints
greged93 3c6f416
fix: smol rework
greged93 e7da229
fix: fmt
greged93 81c5db8
tests: extend helpers and migrate
greged93 2d48822
tests: answer comments + finish migration
greged93 fe784c1
tests: migrate remaining tests
greged93 066609d
Merge branch 'main' into tests/improve-framework
greged93 8c2bfc7
tests: migration after merge
greged93 a64812f
tests: cleaning + fix
greged93 02bbc3e
tests: disable eth wire bridge
greged93 537b6a9
tests: use expect_tx
greged93 a94f09b
fix: clippy
greged93 1a95fd3
Merge branch 'main' into tests/improve-framework
greged93 7d8ab02
chore: answer comments
greged93 c3a51d6
Merge branch 'main' into tests/improve-framework
yiweichi 361a66e
merge main branch
yiweichi c3aaaee
Merge branch 'main' into tests/improve-framework
frisitano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
| //! Block building helpers for test fixtures. | ||
|
|
||
| use super::fixture::TestFixture; | ||
| use crate::test_utils::EventAssertions; | ||
|
|
||
| use alloy_primitives::B256; | ||
| use reth_primitives_traits::transaction::TxHashRef; | ||
| use reth_scroll_primitives::ScrollBlock; | ||
| use scroll_alloy_consensus::ScrollTransaction; | ||
|
|
||
| /// Builder for constructing and validating blocks in tests. | ||
| #[derive(Debug)] | ||
| pub struct BlockBuilder<'a> { | ||
| fixture: &'a mut TestFixture, | ||
| expected_tx_hashes: Vec<B256>, | ||
| expected_tx_count: Option<usize>, | ||
| expected_base_fee: Option<u64>, | ||
| expected_block_number: Option<u64>, | ||
| expect_l1_message: bool, | ||
| expected_l1_message_count: Option<usize>, | ||
frisitano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl<'a> BlockBuilder<'a> { | ||
| /// Create a new block builder. | ||
| pub(crate) fn new(fixture: &'a mut TestFixture) -> Self { | ||
| Self { | ||
| fixture, | ||
| expected_tx_hashes: Vec::new(), | ||
| expected_tx_count: None, | ||
| expected_block_number: None, | ||
| expected_base_fee: None, | ||
| expect_l1_message: false, | ||
| expected_l1_message_count: None, | ||
| } | ||
| } | ||
|
|
||
| /// Expect a specific transaction to be included in the block. | ||
| pub fn expect_tx(mut self, tx_hash: B256) -> Self { | ||
| self.expected_tx_hashes.push(tx_hash); | ||
| self | ||
| } | ||
|
|
||
| /// Expect a specific number of transactions in the block. | ||
| pub const fn expect_tx_count(mut self, count: usize) -> Self { | ||
| self.expected_tx_count = Some(count); | ||
| self | ||
| } | ||
|
|
||
| /// Expect a specific block number. | ||
| pub const fn expect_block_number(mut self, number: u64) -> Self { | ||
| self.expected_block_number = Some(number); | ||
| self | ||
| } | ||
|
|
||
| /// Expect at least one L1 message in the block. | ||
| pub const fn expect_l1_message(mut self) -> Self { | ||
| self.expect_l1_message = true; | ||
| self | ||
| } | ||
|
|
||
| /// Expect a specific number of L1 messages in the block. | ||
| pub const fn expect_l1_message_count(mut self, count: usize) -> Self { | ||
| self.expected_l1_message_count = Some(count); | ||
| self | ||
| } | ||
|
|
||
| /// Build the block and validate against expectations. | ||
| pub async fn await_block(self) -> eyre::Result<ScrollBlock> { | ||
frisitano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let sequencer_node = &self.fixture.nodes[0]; | ||
|
|
||
| // Get the sequencer from the rollup manager handle | ||
| let handle = &sequencer_node.rollup_manager_handle; | ||
|
|
||
| // Trigger block building | ||
| handle.build_block(); | ||
|
|
||
| // If extract the block number. | ||
| let expect = self.fixture.expect_event(); | ||
| let block = | ||
| if let Some(b) = self.expected_block_number { | ||
| expect.block_sequenced(b).await? | ||
| } else { | ||
| expect.extract(|e| { | ||
| if let rollup_node_chain_orchestrator::ChainOrchestratorEvent::BlockSequenced( | ||
| block, | ||
| ) = e | ||
| { | ||
| Some(block.clone()) | ||
| } else { | ||
| None | ||
| } | ||
| }).await? | ||
| }; | ||
|
|
||
| // Finally validate the block. | ||
| self.validate_block(&block) | ||
| } | ||
|
|
||
| /// Validate the block against expectations. | ||
| fn validate_block(self, block: &ScrollBlock) -> eyre::Result<ScrollBlock> { | ||
| // Check transaction count | ||
| if let Some(expected_count) = self.expected_tx_count { | ||
| if block.body.transactions.len() != expected_count { | ||
| return Err(eyre::eyre!( | ||
| "Expected {} transactions, but block has {}", | ||
| expected_count, | ||
| block.body.transactions.len() | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Check block number | ||
| if let Some(expected_number) = self.expected_block_number { | ||
| if block.header.number != expected_number { | ||
| return Err(eyre::eyre!( | ||
| "Expected {} number, but block has {}", | ||
| expected_number, | ||
| block.header.number | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Check specific transaction hashes | ||
| for expected_hash in &self.expected_tx_hashes { | ||
| if !block.body.transactions.iter().any(|tx| tx.tx_hash() == expected_hash) { | ||
| return Err(eyre::eyre!( | ||
| "Expected transaction {:?} not found in block", | ||
| expected_hash | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Check base fee | ||
| if let Some(expected_base_fee) = self.expected_base_fee { | ||
| let actual_base_fee = block | ||
| .header | ||
| .base_fee_per_gas | ||
| .ok_or_else(|| eyre::eyre!("Block has no base fee"))?; | ||
| if actual_base_fee != expected_base_fee { | ||
| return Err(eyre::eyre!( | ||
| "Expected base fee {}, but block has {}", | ||
| expected_base_fee, | ||
| actual_base_fee | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // Check L1 messages | ||
| if self.expect_l1_message { | ||
| let l1_message_count = | ||
| block.body.transactions.iter().filter(|tx| tx.queue_index().is_some()).count(); | ||
| if l1_message_count == 0 { | ||
| return Err(eyre::eyre!("Expected at least one L1 message, but block has none")); | ||
| } | ||
| } | ||
|
|
||
| if let Some(expected_count) = self.expected_l1_message_count { | ||
| let l1_message_count = | ||
| block.body.transactions.iter().filter(|tx| tx.queue_index().is_some()).count(); | ||
| if l1_message_count != expected_count { | ||
| return Err(eyre::eyre!( | ||
| "Expected {} L1 messages, but block has {}", | ||
| expected_count, | ||
| l1_message_count | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| Ok(block.clone()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.