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

chore: add tests for the state listener #143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 @@ rayon = { 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 @@ -89,7 +89,7 @@ pub(crate) mod test_utils {
use mocks::l1::TxStatus;
use ports::{
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 @@ -166,13 +166,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 @@ -186,10 +186,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 @@ -406,7 +403,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 @@ -437,8 +440,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 @@ -449,7 +458,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