miden-protocol 0.17.0-rc.6

Core components of the Miden protocol
Documentation
use miden_crypto::merkle::smt::Smt;
#[cfg(not(target_family = "wasm"))]
use miden_crypto::rand::test_utils::rand_value;

use crate::Word;
use crate::account::Account;
use crate::block::account_tree::{AccountIdKey, AccountTree};
use crate::block::{BlockHeader, BlockNumber, FeeParameters, ValidatorConfig};
use crate::protocol_config::ProtocolConfig;
use crate::testing::random_secret_key::random_secret_key;

impl BlockHeader {
    /// Creates a mock block. The account tree is formed from the provided `accounts`,
    /// and the chain commitment and note root are set to the provided `chain_commitment` and
    /// `note_root` values respectively.
    ///
    /// For non-WASM targets, the remaining header values are initialized randomly. For WASM
    /// targets, values are initialized to [Default::default()]
    pub fn mock(
        block_num: impl Into<BlockNumber>,
        chain_commitment: Option<Word>,
        note_root: Option<Word>,
        accounts: &[Account],
    ) -> Self {
        let smt = Smt::with_entries(
            accounts
                .iter()
                .map(|acct| (AccountIdKey::from(acct.id()).as_word(), acct.to_commitment())),
        )
        .expect("failed to create account db");
        let acct_db = AccountTree::new(smt).expect("failed to create account tree");
        let account_root = acct_db.root();
        let fee_parameters = FeeParameters::new(500);
        let validator_config = ValidatorConfig::new(vec![random_secret_key().public_key()], 1)
            .expect("randomly generated validator keys should be distinct");

        #[cfg(not(target_family = "wasm"))]
        let (
            prev_block_commitment,
            chain_commitment,
            nullifier_root,
            note_root,
            tx_commitment,
            timestamp,
        ) = {
            let prev_block_commitment = rand_value::<Word>();
            let chain_commitment = chain_commitment.unwrap_or(rand_value::<Word>());
            let nullifier_root = rand_value::<Word>();
            let note_root = note_root.unwrap_or(rand_value::<Word>());
            let tx_commitment = rand_value::<Word>();
            let timestamp = rand_value::<u32>();

            (
                prev_block_commitment,
                chain_commitment,
                nullifier_root,
                note_root,
                tx_commitment,
                timestamp,
            )
        };

        #[cfg(target_family = "wasm")]
        let (
            prev_block_commitment,
            chain_commitment,
            nullifier_root,
            note_root,
            tx_commitment,
            timestamp,
        ) = {
            (
                Default::default(),
                chain_commitment.unwrap_or_default(),
                Default::default(),
                note_root.unwrap_or_default(),
                Default::default(),
                Default::default(),
            )
        };

        BlockHeader::new(
            prev_block_commitment,
            block_num.into(),
            chain_commitment,
            account_root,
            nullifier_root,
            note_root,
            tx_commitment,
            validator_config,
            fee_parameters,
            ProtocolConfig::mock().to_commitment(),
            None,
            timestamp,
        )
    }
}