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

Improvements in MockStoreSuccessBuilder #253

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions block-producer/src/batch_builder/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ impl TransactionBatch {
.map(|(account_id, account_states)| (*account_id, account_states.final_state))
}

/// Returns the nullifier of all consumed notes.
/// Returns an iterator over produced nullifiers for all consumed notes.
pub fn produced_nullifiers(&self) -> impl Iterator<Item = Nullifier> + '_ {
self.produced_nullifiers.iter().cloned()
}

/// Returns the hash of created notes.
/// Returns an iterator over created notes.
pub fn created_notes(&self) -> impl Iterator<Item = &NoteEnvelope> + '_ {
self.created_notes.iter()
}

/// Returns the root of the created notes SMT.
/// Returns the root hash of the created notes SMT.
pub fn created_notes_root(&self) -> Digest {
self.created_notes_smt.root()
}
Expand Down
3 changes: 1 addition & 2 deletions block-producer/src/block_builder/prover/block_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ impl BlockWitness {

// Notes stack inputs
{
let num_created_notes_roots = self.batch_created_notes_roots.len();
for (batch_index, batch_created_notes_root) in self.batch_created_notes_roots.iter() {
stack_inputs.extend(batch_created_notes_root.iter());

Expand All @@ -237,7 +236,7 @@ impl BlockWitness {
let empty_root = EmptySubtreeRoots::entry(BLOCK_OUTPUT_NOTES_TREE_DEPTH, 0);
stack_inputs.extend(*empty_root);
stack_inputs.push(
Felt::try_from(num_created_notes_roots as u64)
Felt::try_from(self.batch_created_notes_roots.len() as u64)
.expect("notes roots number is greater than or equal to the field modulus"),
);
}
Expand Down
2 changes: 1 addition & 1 deletion block-producer/src/block_builder/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl BlockProver {
let proof_hash = Digest::default();
let timestamp: Felt = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("today is expected to be before 1970")
.expect("today is expected to be after 1970")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

.as_millis()
.try_into()
.expect("timestamp is greater than or equal to the field modulus");
Expand Down
55 changes: 27 additions & 28 deletions block-producer/src/block_builder/prover/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, iter};

use miden_mock::mock::block::mock_block_header;
use miden_objects::{
Expand Down Expand Up @@ -188,14 +188,13 @@ 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::from_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
)
.build();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -272,14 +271,13 @@ 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::from_accounts(
account_ids
.iter()
.zip(account_initial_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into())),
)
.build();

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

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::from_batches(iter::empty()).build();

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

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::from_batches(iter::empty()).build();

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

let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::from_batches(iter::empty()).build();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -562,9 +560,7 @@ async fn test_compute_nullifier_root_empty_success() {
// Set up store
// ---------------------------------------------------------------------------------------------

let store = MockStoreSuccessBuilder::new()
.initial_accounts(batches.iter().flat_map(|batch| batch.account_initial_states()))
.build();
let store = MockStoreSuccessBuilder::from_batches(batches.iter()).build();

// Block prover
// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -621,8 +617,7 @@ async fn test_compute_nullifier_root_success() {
// ---------------------------------------------------------------------------------------------
let initial_block_num = 42;

let store = MockStoreSuccessBuilder::new()
.initial_accounts(batches.iter().flat_map(|batch| batch.account_initial_states()))
let store = MockStoreSuccessBuilder::from_batches(batches.iter())
.initial_block_num(initial_block_num)
.build();

Expand Down Expand Up @@ -660,7 +655,7 @@ async fn test_compute_nullifier_root_success() {
#[tokio::test]
#[miden_node_test_macro::enable_logging]
async fn test_compute_chain_mmr_root_empty_mmr() {
let store = MockStoreSuccessBuilder::new().build();
let store = MockStoreSuccessBuilder::from_batches(iter::empty()).build();

let expected_block_header = build_expected_block_header(&store, &[]).await;
let actual_block_header = build_actual_block_header(&store, Vec::new()).await;
Expand All @@ -679,7 +674,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::from_batches(iter::empty())
.initial_chain_mmr(initial_chain_mmr)
.build();

let expected_block_header = build_expected_block_header(&store, &[]).await;
let actual_block_header = build_actual_block_header(&store, Vec::new()).await;
Expand All @@ -702,7 +699,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::from_batches(iter::empty())
.initial_chain_mmr(initial_chain_mmr)
.build();

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

Expand Down Expand Up @@ -48,9 +47,7 @@ async fn test_apply_block_called_empty_batches() {
let account_hash: Digest =
[Felt::new(1u64), Felt::new(1u64), Felt::new(1u64), Felt::new(1u64)].into();
let store = Arc::new(
MockStoreSuccessBuilder::new()
.initial_accounts(std::iter::once((account_id, account_hash)))
.build(),
MockStoreSuccessBuilder::from_accounts(std::iter::once((account_id, account_hash))).build(),
);

let block_builder = DefaultBlockBuilder::new(store.clone(), store.clone());
Expand Down
2 changes: 2 additions & 0 deletions block-producer/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ pub enum BlockInputsError {

#[derive(Debug, PartialEq, Eq, Error)]
pub enum ApplyBlockError {
#[error("Merkle error: {0}")]
MerkleError(#[from] MerkleError),
#[error("gRPC client failed with error: {0}")]
GrpcClientError(String),
}
Expand Down
36 changes: 16 additions & 20 deletions block-producer/src/state_view/tests/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ async fn test_apply_block_ab1() {
let account: MockPrivateAccount<3> = MockPrivateAccount::from(0);

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

let tx =
Expand Down Expand Up @@ -52,14 +50,13 @@ async fn test_apply_block_ab2() {
let (txs, accounts): (Vec<_>, Vec<_>) = get_txs_and_accounts(0, 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::from_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
);

let state_view = DefaultStateView::new(store.clone(), false);
Expand Down Expand Up @@ -100,14 +97,13 @@ async fn test_apply_block_ab3() {
let (txs, accounts): (Vec<_>, Vec<_>) = get_txs_and_accounts(0, 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::from_accounts(
accounts
.clone()
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
);

let state_view = DefaultStateView::new(store.clone(), false);
Expand All @@ -132,7 +128,7 @@ async fn test_apply_block_ab3() {
let apply_block_res = state_view.apply_block(block).await;
assert!(apply_block_res.is_ok());

// Craft a new transaction which tries to consume the same note that was consumed in in the
// Craft a new transaction which tries to consume the same note that was consumed in the
// first tx
let tx_new = MockProvenTxBuilder::with_account(
accounts[0].id,
Expand Down
55 changes: 24 additions & 31 deletions block-producer/src/state_view/tests/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ async fn test_verify_tx_happy_path() {
get_txs_and_accounts(0, 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::from_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
);

let state_view = DefaultStateView::new(store, false);
Expand All @@ -53,13 +52,12 @@ async fn test_verify_tx_happy_path_concurrent() {
get_txs_and_accounts(0, 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::from_accounts(
accounts
.into_iter()
.map(|mock_account| (mock_account.id, mock_account.states[0])),
)
.build(),
);

let state_view = Arc::new(DefaultStateView::new(store, false));
Expand All @@ -83,9 +81,7 @@ async fn test_verify_tx_vt1() {
let account = MockPrivateAccount::<3>::from(1);

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

// The transaction's initial account hash uses `account.states[1]`, where the store expects
Expand Down Expand Up @@ -114,7 +110,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::from_batches(iter::empty()).build());

let tx = MockProvenTxBuilder::with_account(
account_not_in_store.id,
Expand All @@ -141,9 +137,9 @@ 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(nullifier_in_store.inner())), 1)
MockStoreSuccessBuilder::from_accounts(iter::once((account.id, account.states[0])))
.initial_nullifiers(BTreeSet::from_iter(iter::once(nullifier_in_store.inner())))
.initial_block_num(1)
.build(),
);

Expand All @@ -170,9 +166,7 @@ async fn test_verify_tx_vt4() {
let account: MockPrivateAccount<3> = MockPrivateAccount::from(1);

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

let tx1 =
Expand Down Expand Up @@ -205,13 +199,12 @@ 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::from_accounts(
vec![account_1, account_2]
.into_iter()
.map(|account| (account.id, account.states[0])),
)
.build(),
);

let tx1 =
Expand Down
Loading
Loading