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

Fix tests #101

Closed
wants to merge 6 commits into from
Closed
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
44 changes: 22 additions & 22 deletions block-producer/src/block_builder/prover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,12 @@ async fn test_compute_account_root_success() {
// Set up store's account SMT
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new()
.initial_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
)
.build();
let store = MockStoreSuccessBuilder::new().build_from_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
);

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -297,14 +295,12 @@ async fn test_compute_account_root_empty_batches() {
// Set up store's account SMT
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new()
.initial_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
)
.build();
let store = MockStoreSuccessBuilder::new().build_from_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
);

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -334,7 +330,7 @@ async fn test_compute_note_root_empty_batches_success() {
// Set up store
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::empty_store();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -363,7 +359,7 @@ async fn test_compute_note_root_empty_notes_success() {
// Set up store
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::empty_store();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -418,7 +414,7 @@ async fn test_compute_note_root_success() {
// Set up store
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::empty_store();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -487,7 +483,7 @@ async fn test_compute_note_root_success() {
/// Test that the chain mmr root is as expected if the batches are empty
#[tokio::test]
async fn test_compute_chain_mmr_root_empty_mmr() {
let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::empty_store();

let expected_block_header = build_expected_block_header(&store, &[]).await;
let actual_block_header = build_actual_block_header(&store, Vec::new()).await;
Expand All @@ -505,7 +501,9 @@ async fn test_compute_chain_mmr_root_mmr_1_peak() {
mmr
};

let store = MockStoreSuccessBuilder::new().initial_chain_mmr(initial_chain_mmr).build();
let store = MockStoreSuccessBuilder::new()
.initial_chain_mmr(initial_chain_mmr)
.build_from_accounts(std::iter::empty());

let expected_block_header = build_expected_block_header(&store, &[]).await;
let actual_block_header = build_actual_block_header(&store, Vec::new()).await;
Expand All @@ -527,7 +525,9 @@ async fn test_compute_chain_mmr_root_mmr_17_peaks() {
mmr
};

let store = MockStoreSuccessBuilder::new().initial_chain_mmr(initial_chain_mmr).build();
let store = MockStoreSuccessBuilder::new()
.initial_chain_mmr(initial_chain_mmr)
.build_from_accounts(std::iter::empty());

let expected_block_header = build_expected_block_header(&store, &[]).await;
let actual_block_header = build_actual_block_header(&store, Vec::new()).await;
Expand Down
6 changes: 2 additions & 4 deletions block-producer/src/block_builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ async fn test_apply_block_called_nonempty_batches() {
[Felt::from(1u64), Felt::from(1u64), Felt::from(1u64), Felt::from(1u64)].into();
let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(std::iter::once((account_id, account_initial_hash)))
.build(),
.build_from_accounts(std::iter::once((account_id, account_initial_hash))),
);

let block_builder = DefaultBlockBuilder::new(store.clone());
Expand Down Expand Up @@ -52,8 +51,7 @@ async fn test_apply_block_called_empty_batches() {
[Felt::from(1u64), Felt::from(1u64), Felt::from(1u64), Felt::from(1u64)].into();
let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(std::iter::once((account_id, account_hash)))
.build(),
.build_from_accounts(std::iter::once((account_id, account_hash))),
);

let block_builder = DefaultBlockBuilder::new(store.clone());
Expand Down
31 changes: 13 additions & 18 deletions block-producer/src/state_view/tests/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ async fn test_apply_block_ab1() {

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(iter::once((account.id, account.states[0])))
.build(),
.build_from_accounts(iter::once((account.id, account.states[0]))),
);

let tx = tx_gen.dummy_proven_tx_with_params(
Expand Down Expand Up @@ -58,14 +57,12 @@ async fn test_apply_block_ab2() {
let (txs, accounts): (Vec<_>, Vec<_>) = get_txs_and_accounts(&tx_gen, 3).unzip();

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
MockStoreSuccessBuilder::new().build_from_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
),
);

let state_view = DefaulStateView::new(store.clone());
Expand Down Expand Up @@ -107,14 +104,12 @@ async fn test_apply_block_ab3() {
let (txs, accounts): (Vec<_>, Vec<_>) = get_txs_and_accounts(&tx_gen, 3).unzip();

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
MockStoreSuccessBuilder::new().build_from_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
),
);

let state_view = DefaulStateView::new(store.clone());
Expand Down
64 changes: 23 additions & 41 deletions block-producer/src/state_view/tests/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::iter;
use tokio::task::JoinSet;

use super::*;
use crate::test_utils::MockStoreSuccessBuilder;
use crate::test_utils::{MockProvenTxBuilder, MockStoreSuccessBuilder};

/// Tests the happy path where 3 transactions who modify different accounts and consume different
/// notes all verify successfully
Expand All @@ -26,13 +26,11 @@ async fn test_verify_tx_happy_path() {
get_txs_and_accounts(&tx_gen, 3).unzip();

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
MockStoreSuccessBuilder::new().build_from_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
),
);

let state_view = DefaulStateView::new(store);
Expand All @@ -53,13 +51,11 @@ async fn test_verify_tx_happy_path_concurrent() {
get_txs_and_accounts(&tx_gen, 3).unzip();

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
MockStoreSuccessBuilder::new().build_from_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
),
);

let state_view = Arc::new(DefaulStateView::new(store));
Expand All @@ -79,24 +75,14 @@ async fn test_verify_tx_happy_path_concurrent() {
/// Verifies requirement VT1
#[tokio::test]
async fn test_verify_tx_vt1() {
let tx_gen = DummyProvenTxGenerator::new();

let account = MockPrivateAccount::<3>::from(0);

let tx =
MockProvenTxBuilder::from_account(account.id, account.states[1], account.states[2]).build();

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(iter::once((account.id, account.states[0])))
.build(),
);

// The transaction's initial account hash uses `account.states[1]`, where the store expects
// `account.states[0]`
let tx = tx_gen.dummy_proven_tx_with_params(
account.id,
account.states[1],
account.states[2],
vec![consumed_note_by_index(0)],
Vec::new(),
.build_from_accounts(iter::once((account.id, account.states[0]))),
);

let state_view = DefaulStateView::new(store);
Expand All @@ -120,7 +106,7 @@ async fn test_verify_tx_vt2() {
let account_not_in_store: MockPrivateAccount<3> = MockPrivateAccount::from(0);

// Notice: account is not added to the store
let store = Arc::new(MockStoreSuccessBuilder::new().build());
let store = Arc::new(MockStoreSuccessBuilder::empty_store());

let tx = tx_gen.dummy_proven_tx_with_params(
account_not_in_store.id,
Expand Down Expand Up @@ -155,9 +141,8 @@ async fn test_verify_tx_vt3() {
// Notice: `consumed_note_in_store` is added to the store
let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(iter::once((account.id, account.states[0])))
.initial_nullifiers(BTreeSet::from_iter(iter::once(consumed_note_in_store.nullifier())))
.build(),
.build_from_accounts(iter::once((account.id, account.states[0]))),
);

let tx = tx_gen.dummy_proven_tx_with_params(
Expand Down Expand Up @@ -189,8 +174,7 @@ async fn test_verify_tx_vt4() {

let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(iter::once((account.id, account.states[0])))
.build(),
.build_from_accounts(iter::once((account.id, account.states[0]))),
);

let tx1 = tx_gen.dummy_proven_tx_with_params(
Expand Down Expand Up @@ -234,13 +218,11 @@ async fn test_verify_tx_vt5() {

// Notice: `consumed_note_in_both_txs` is NOT in the store
let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(
vec![account_1, account_2]
.into_iter()
.map(|account| (account.id, account.states[0])),
)
.build(),
MockStoreSuccessBuilder::new().build_from_accounts(
vec![account_1, account_2]
.into_iter()
.map(|account| (account.id, account.states[0])),
),
);

let tx1 = tx_gen.dummy_proven_tx_with_params(
Expand Down
31 changes: 25 additions & 6 deletions block-producer/src/test_utils/proven_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ static NUM_ACCOUNTS_CREATED: Lazy<Arc<Mutex<u32>>> = Lazy::new(|| Arc::new(Mutex
static NUM_NOTES_CREATED: Lazy<Arc<Mutex<u64>>> = Lazy::new(|| Arc::new(Mutex::new(0)));

pub struct MockProvenTxBuilder {
mock_account: MockPrivateAccount,
account_id: AccountId,
initial_state: Digest,
final_state: Digest,
notes_created: Option<Vec<NoteEnvelope>>,
}

Expand All @@ -46,8 +48,25 @@ impl MockProvenTxBuilder {

account_index
};
let mock_account: MockPrivateAccount<2> = account_index.into();

Self {
mock_account: account_index.into(),
account_id: mock_account.id,
initial_state: mock_account.states[0],
final_state: mock_account.states[1],
notes_created: None,
}
}

pub fn from_account(
account_id: AccountId,
initial_state: Digest,
final_state: Digest,
) -> Self {
Self {
account_id,
initial_state,
final_state,
notes_created: None,
}
}
Expand All @@ -63,7 +82,7 @@ impl MockProvenTxBuilder {
.map(|note_index| {
let note_hash = Rpo256::hash(&note_index.to_be_bytes());

NoteEnvelope::new(note_hash, NoteMetadata::new(self.mock_account.id, ONE, ZERO))
NoteEnvelope::new(note_hash, NoteMetadata::new(self.account_id, ONE, ZERO))
})
.collect();

Expand All @@ -76,9 +95,9 @@ impl MockProvenTxBuilder {

pub fn build(self) -> ProvenTransaction {
ProvenTransaction::new(
self.mock_account.id,
self.mock_account.states[0],
self.mock_account.states[1],
self.account_id,
self.initial_state,
self.final_state,
Vec::new(),
self.notes_created.unwrap_or_default(),
None,
Expand Down
Loading
Loading