Skip to content

Commit

Permalink
chore: add tests for the state listener (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet authored Nov 19, 2024
1 parent bcc9c2e commit 949ae96
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tracing = { workspace = true }
trait-variant = { workspace = true }

[dev-dependencies]
test-case = { workspace = true }
clock = { workspace = true, features = ["test-helpers"] }
delegate = { workspace = true }
eth = { workspace = true, features = ["test-helpers"] }
Expand Down
31 changes: 20 additions & 11 deletions packages/services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) mod test_utils {
use ports::{
l1::FragmentEncoder,
storage::Storage,
types::{CollectNonEmpty, CompressedFuelBlock, DateTime, Fragment, NonEmpty, Utc},
types::{CollectNonEmpty, CompressedFuelBlock, DateTime, Fragment, L1Tx, NonEmpty, Utc},
};
use rand::RngCore;
use storage::{DbWithProcess, PostgresProcess};
Expand Down Expand Up @@ -187,13 +187,13 @@ pub(crate) mod test_utils {
}

pub fn expects_state_submissions(
expectations: impl IntoIterator<Item = (Option<NonEmpty<Fragment>>, [u8; 32])>,
expectations: impl IntoIterator<Item = (Option<NonEmpty<Fragment>>, ports::types::L1Tx)>,
) -> ports::l1::MockApi {
let mut sequence = Sequence::new();

let mut l1_mock = ports::l1::MockApi::new();

for (fragment, tx_id) in expectations {
for (fragment, tx) in expectations {
l1_mock
.expect_submit_state_fragments()
.withf(move |data, _previous_tx| {
Expand All @@ -207,10 +207,7 @@ pub(crate) mod test_utils {
.return_once(move |fragments, _previous_tx| {
Box::pin(async move {
Ok((
ports::types::L1Tx {
hash: tx_id,
..Default::default()
},
tx,
FragmentsSubmitted {
num_fragments: min(fragments.len(), 6).try_into().unwrap(),
},
Expand Down Expand Up @@ -427,7 +424,13 @@ pub(crate) mod test_utils {
impl Setup {
pub async fn send_fragments(&self, eth_tx: [u8; 32]) {
StateCommitter::new(
mocks::l1::expects_state_submissions(vec![(None, eth_tx)]),
mocks::l1::expects_state_submissions(vec![(
None,
L1Tx {
hash: eth_tx,
..Default::default()
},
)]),
mocks::fuel::latest_height_is(0),
self.db(),
crate::StateCommitterConfig::default(),
Expand Down Expand Up @@ -458,8 +461,14 @@ pub(crate) mod test_utils {
let clock = TestClock::default();
clock.set_time(finalization_time);

let tx = [1; 32];
let l1_mock = mocks::l1::expects_state_submissions(vec![(None, tx)]);
let tx_hash = [1; 32];
let l1_mock = mocks::l1::expects_state_submissions(vec![(
None,
L1Tx {
hash: tx_hash,
..Default::default()
},
)]);
let fuel_mock = mocks::fuel::latest_height_is(0);
let mut committer = StateCommitter::new(
l1_mock,
Expand All @@ -470,7 +479,7 @@ pub(crate) mod test_utils {
);
committer.run().await.unwrap();

let l1_mock = mocks::l1::txs_finished(0, 0, [(tx, TxStatus::Success)]);
let l1_mock = mocks::l1::txs_finished(0, 0, [(tx_hash, TxStatus::Success)]);

StateListener::new(
l1_mock,
Expand Down
36 changes: 28 additions & 8 deletions packages/services/src/state_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,12 @@ mod tests {

let fragments = setup.insert_fragments(0, 4).await;

let tx_hash = [0; 32];
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([(
Some(NonEmpty::from_vec(fragments.clone()).unwrap()),
tx_hash,
L1Tx {
hash: [0; 32],
..Default::default()
},
)]);

let fuel_mock = test_utils::mocks::fuel::latest_height_is(0);
Expand Down Expand Up @@ -301,7 +303,10 @@ mod tests {
let tx_hash = [1; 32];
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([(
Some(NonEmpty::from_vec(fragments.clone()).unwrap()),
tx_hash,
L1Tx {
hash: tx_hash,
..Default::default()
},
)]);

let fuel_mock = test_utils::mocks::fuel::latest_height_is(0);
Expand Down Expand Up @@ -375,7 +380,10 @@ mod tests {
let tx_hash = [3; 32];
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([(
Some(NonEmpty::from_vec(fragments).unwrap()),
tx_hash,
L1Tx {
hash: tx_hash,
..Default::default()
},
)]);

let fuel_mock = test_utils::mocks::fuel::latest_height_is(0);
Expand Down Expand Up @@ -414,7 +422,10 @@ mod tests {
let tx_hash = [4; 32];
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([(
Some(NonEmpty::from_vec(fragments_to_submit).unwrap()),
tx_hash,
L1Tx {
hash: tx_hash,
..Default::default()
},
)]);

let fuel_mock = test_utils::mocks::fuel::latest_height_is(1);
Expand Down Expand Up @@ -453,7 +464,10 @@ mod tests {
let tx_hash = [5; 32];
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([(
Some(NonEmpty::from_vec(fragments.clone()).unwrap()),
tx_hash,
L1Tx {
hash: tx_hash,
..Default::default()
},
)]);

let fuel_mock = test_utils::mocks::fuel::latest_height_is(0);
Expand Down Expand Up @@ -495,11 +509,17 @@ mod tests {
let l1_mock_submit = test_utils::mocks::l1::expects_state_submissions([
(
Some(NonEmpty::from_vec(fragments.clone()).unwrap()),
tx_hash_1,
L1Tx {
hash: tx_hash_1,
..Default::default()
},
),
(
Some(NonEmpty::from_vec(fragments.clone()).unwrap()),
tx_hash_2,
L1Tx {
hash: tx_hash_2,
..Default::default()
},
),
]);

Expand Down
Loading

0 comments on commit 949ae96

Please sign in to comment.