From e5d0151b5e16cab16f335967e80fdd0a81290dee Mon Sep 17 00:00:00 2001 From: Philipp Gackstatter Date: Wed, 19 Feb 2025 12:43:14 +0100 Subject: [PATCH] chore: Update node to miden-base companion PR --- Cargo.lock | 140 +++++++++--------- Cargo.toml | 7 +- crates/block-producer/Cargo.toml | 2 +- .../block-producer/src/block_builder/mod.rs | 11 +- .../src/block_builder/prover/block_witness.rs | 10 +- .../src/block_builder/prover/mod.rs | 8 +- .../src/block_builder/prover/tests.rs | 18 +-- crates/block-producer/src/errors.rs | 11 +- crates/block-producer/src/store/mod.rs | 4 +- crates/block-producer/src/test_utils/batch.rs | 8 +- crates/block-producer/src/test_utils/block.rs | 28 ++-- crates/block-producer/src/test_utils/store.rs | 17 ++- crates/store/src/db/mod.rs | 6 +- crates/store/src/db/sql/mod.rs | 24 ++- crates/store/src/db/tests.rs | 10 +- crates/store/src/errors.rs | 8 +- crates/store/src/genesis.rs | 6 +- crates/store/src/server/api.rs | 8 +- crates/store/src/state.rs | 14 +- 19 files changed, 179 insertions(+), 161 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7eb2e6ce..ae688eaca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -385,9 +385,9 @@ checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" [[package]] name = "blake3" -version = "1.5.5" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ee0c1824c4dea5b5f81736aff91bae041d2c07ee1192bec91054e10e3e601e" +checksum = "1230237285e3e10cde447185e8975408ae24deaa67205ce684805c25bc0c7937" dependencies = [ "arrayref", "arrayvec", @@ -463,9 +463,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.11" +version = "1.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4730490333d58093109dc02c23174c3f4d490998c3fed3cc8e82d57afedb9cf" +checksum = "0c3d1b2e905a3a7b00a6141adb0e4c0bb941d11caf55349d863942a1cc44e3c9" dependencies = [ "jobserver", "libc", @@ -524,9 +524,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.28" +version = "4.5.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff" +checksum = "92b7b18d71fad5313a1e320fa9897994228ce274b60faa4d694fe0ea89cd9e6d" dependencies = [ "clap_builder", "clap_derive", @@ -534,9 +534,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.27" +version = "4.5.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7" +checksum = "a35db2071778a7344791a4fb4f95308b5673d219dee3ae348b86642574ecc90c" dependencies = [ "anstream", "anstyle", @@ -826,9 +826,9 @@ dependencies = [ [[package]] name = "equivalent" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "errno" @@ -880,6 +880,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + [[package]] name = "fnv" version = "1.0.7" @@ -1046,9 +1052,9 @@ checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" [[package]] name = "h2" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" +checksum = "5017294ff4bb30944501348f6f8e42e6ad28f42c8bbef7a74029aff064a4e3c2" dependencies = [ "atomic-waker", "bytes", @@ -1440,15 +1446,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.14.0" @@ -1503,7 +1500,7 @@ dependencies = [ "ena", "itertools 0.11.0", "lalrpop-util", - "petgraph", + "petgraph 0.6.5", "regex", "regex-syntax 0.8.5", "string_cache", @@ -1728,9 +1725,9 @@ dependencies = [ [[package]] name = "miden-crypto" -version = "0.13.2" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1945918276152bd9b8e8434643ad24d4968e075b68a5ed03927b53ac75490a79" +checksum = "1d8f76b64bfbb75705403ec3e2faad6a045544871d9c441758becc55415cfe64" dependencies = [ "blake3", "cc", @@ -1787,7 +1784,7 @@ dependencies = [ [[package]] name = "miden-lib" version = "0.8.0" -source = "git+https://github.com/0xPolygonMiden/miden-base?branch=next#66cf1bc8744cf739aa3ef726300c389796047394" +source = "git+https://github.com/0xPolygonMiden/miden-base?branch=pgackst-batch-expiration#fa7b5b1728f22d0c1b6264d62d96667002dbdb50" dependencies = [ "miden-assembly", "miden-objects", @@ -1983,7 +1980,7 @@ dependencies = [ [[package]] name = "miden-objects" version = "0.8.0" -source = "git+https://github.com/0xPolygonMiden/miden-base?branch=next#66cf1bc8744cf739aa3ef726300c389796047394" +source = "git+https://github.com/0xPolygonMiden/miden-base?branch=pgackst-batch-expiration#fa7b5b1728f22d0c1b6264d62d96667002dbdb50" dependencies = [ "getrandom 0.2.15", "miden-assembly", @@ -2042,7 +2039,7 @@ dependencies = [ [[package]] name = "miden-tx" version = "0.8.0" -source = "git+https://github.com/0xPolygonMiden/miden-base?branch=next#66cf1bc8744cf739aa3ef726300c389796047394" +source = "git+https://github.com/0xPolygonMiden/miden-base?branch=pgackst-batch-expiration#fa7b5b1728f22d0c1b6264d62d96667002dbdb50" dependencies = [ "async-trait", "miden-lib", @@ -2059,7 +2056,7 @@ dependencies = [ [[package]] name = "miden-tx-batch-prover" version = "0.8.0" -source = "git+https://github.com/0xPolygonMiden/miden-base?branch=next#66cf1bc8744cf739aa3ef726300c389796047394" +source = "git+https://github.com/0xPolygonMiden/miden-base?branch=pgackst-batch-expiration#fa7b5b1728f22d0c1b6264d62d96667002dbdb50" dependencies = [ "miden-core", "miden-crypto", @@ -2129,9 +2126,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924" +checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" dependencies = [ "adler2", ] @@ -2300,9 +2297,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.2" +version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "openssl-probe" @@ -2460,7 +2457,17 @@ version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ - "fixedbitset", + "fixedbitset 0.4.2", + "indexmap 2.7.1", +] + +[[package]] +name = "petgraph" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" +dependencies = [ + "fixedbitset 0.5.7", "indexmap 2.7.1", ] @@ -2596,9 +2603,9 @@ dependencies = [ [[package]] name = "prost" -version = "0.13.4" +version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c0fef6c4230e4ccf618a35c59d7ede15dea37de8427500f50aff708806e42ec" +checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5" dependencies = [ "bytes", "prost-derive", @@ -2606,16 +2613,16 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.13.4" +version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0f3e5beed80eb580c68e2c600937ac2c4eedabdfd5ef1e5b7ea4f3fba84497b" +checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf" dependencies = [ "heck", - "itertools 0.13.0", + "itertools 0.14.0", "log", "multimap", "once_cell", - "petgraph", + "petgraph 0.7.1", "prettyplease", "prost", "prost-types", @@ -2626,12 +2633,12 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.13.4" +version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3" +checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.13.0", + "itertools 0.14.0", "proc-macro2", "quote", "syn", @@ -2639,9 +2646,9 @@ dependencies = [ [[package]] name = "prost-reflect" -version = "0.14.5" +version = "0.14.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e92b959d24e05a3e2da1d0beb55b48bc8a97059b8336ea617780bd6addbbfb5a" +checksum = "a7b318f733603136dcc61aa9e77c928d67f87d2436c34ec052ba3f1b5ca219de" dependencies = [ "logos", "miette", @@ -2652,9 +2659,9 @@ dependencies = [ [[package]] name = "prost-types" -version = "0.13.4" +version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc2f1e56baa61e93533aebc21af4d2134b70f66275e0fcdf3cbe43d77ff7e8fc" +checksum = "52c2c1bf36ddb1a1c396b3601a3cec27c2462e45f07c386894ec3ccf5332bd16" dependencies = [ "prost", ] @@ -2835,15 +2842,14 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "ring" -version = "0.17.8" +version = "0.17.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +checksum = "e75ec5e92c4d8aede845126adc388046234541629e76029599ed35a003c7ed24" dependencies = [ "cc", "cfg-if", "getrandom 0.2.15", "libc", - "spin", "untrusted", "windows-sys 0.52.0", ] @@ -2917,9 +2923,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.22" +version = "0.23.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb9263ab4eb695e42321db096e3b8fbd715a59b154d5c88d82db2175b681ba7" +checksum = "47796c98c480fce5406ef69d1c76378375492c3b0a0de587be0c1d9feb12f395" dependencies = [ "log", "once_cell", @@ -3174,9 +3180,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.2" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" [[package]] name = "smawk" @@ -3307,9 +3313,9 @@ checksum = "42a4d50cdb458045afc8131fd91b64904da29548bcb63c7236e0844936c13078" [[package]] name = "tempfile" -version = "3.16.0" +version = "3.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" +checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" dependencies = [ "cfg-if", "fastrand", @@ -3525,9 +3531,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.19" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" dependencies = [ "serde", "serde_spanned", @@ -3546,9 +3552,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.23" +version = "0.22.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02a8b472d1a3d7c18e2d61a489aee3453fd9031c33e4f55bd533f4a7adca1bee" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" dependencies = [ "indexmap 2.7.1", "serde", @@ -3834,9 +3840,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.17.0" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" [[package]] name = "unarray" @@ -3861,9 +3867,9 @@ checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" [[package]] name = "unicode-linebreak" @@ -4357,18 +4363,18 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86e376c75f4f43f44db463cf729e0d3acbf954d13e22c51e26e4c264b4ab545f" +checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" dependencies = [ "memchr", ] [[package]] name = "winter-air" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a8fdb702503625f54dcaf9222aa2c7a0b2e868b3eb84b90d1837d68034bf999" +checksum = "827ef2aa5a5ab663936e0a6326286e0fc83321771df0d9ea20c46c72c8baa90d" dependencies = [ "libm", "winter-crypto", diff --git a/Cargo.toml b/Cargo.toml index 2c05af39a..33694e6ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,18 +28,17 @@ version = "0.8.0" assert_matches = { version = "1.5" } itertools = { version = "0.14" } miden-air = { version = "0.12" } -miden-lib = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "next" } +miden-lib = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "pgackst-batch-expiration" } miden-node-block-producer = { path = "crates/block-producer", version = "0.8" } miden-node-proto = { path = "crates/proto", version = "0.8" } miden-node-rpc = { path = "crates/rpc", version = "0.8" } miden-node-store = { path = "crates/store", version = "0.8" } miden-node-test-macro = { path = "crates/test-macro" } miden-node-utils = { path = "crates/utils", version = "0.8" } -miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "next" } +miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "pgackst-batch-expiration" } miden-processor = { version = "0.12" } miden-stdlib = { version = "0.12", default-features = false } -miden-tx = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "next" } -miden-tx-batch-prover = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" } +miden-tx = { git = "https://github.com/0xPolygonMiden/miden-base", branch = "pgackst-batch-expiration" } prost = { version = "0.13" } rand = { version = "0.8" } thiserror = { version = "2.0", default-features = false } diff --git a/crates/block-producer/Cargo.toml b/crates/block-producer/Cargo.toml index dabf8d7bd..89a3f0cbc 100644 --- a/crates/block-producer/Cargo.toml +++ b/crates/block-producer/Cargo.toml @@ -28,7 +28,7 @@ miden-objects = { workspace = true } miden-processor = { workspace = true } miden-stdlib = { workspace = true } miden-tx = { workspace = true } -miden-tx-batch-prover = { workspace = true } +miden-tx-batch-prover = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "pgackst-batch-expiration" } rand = { version = "0.8" } serde = { version = "1.0", features = ["derive"] } thiserror = { workspace = true } diff --git a/crates/block-producer/src/block_builder/mod.rs b/crates/block-producer/src/block_builder/mod.rs index 78dd05691..738260312 100644 --- a/crates/block-producer/src/block_builder/mod.rs +++ b/crates/block-producer/src/block_builder/mod.rs @@ -8,7 +8,7 @@ use miden_node_utils::tracing::OpenTelemetrySpanExt; use miden_objects::{ account::AccountId, batch::ProvenBatch, - block::{Block, BlockNumber}, + block::{BlockNumber, ProvenBlock}, note::{NoteHeader, NoteId, Nullifier}, transaction::{InputNoteCommitment, OutputNote}, }; @@ -160,12 +160,13 @@ impl BlockBuilder { let new_block_header = self.block_kernel.prove(block_header_witness)?; - let block = Block::new( + // TODO: Update. Temporarily left in an incorrect state. + let block = ProvenBlock::new_unchecked( new_block_header, updated_accounts, - summary.output_notes, + vec![], summary.nullifiers, - )?; + ); self.simulate_proving().await; @@ -278,7 +279,7 @@ struct BlockSummaryAndInputs { // TODO: Is this still needed? If so, what should be its name? struct ProvenBlockWrapper { - block: Block, + block: ProvenBlock, } impl SelectedBlock { diff --git a/crates/block-producer/src/block_builder/prover/block_witness.rs b/crates/block-producer/src/block_builder/prover/block_witness.rs index a0972e541..eac670ad4 100644 --- a/crates/block-producer/src/block_builder/prover/block_witness.rs +++ b/crates/block-producer/src/block_builder/prover/block_witness.rs @@ -2,7 +2,7 @@ use std::collections::{BTreeMap, BTreeSet}; use miden_objects::{ account::{delta::AccountUpdateDetails, AccountId}, - batch::{BatchAccountUpdate, ProvenBatch}, + batch::{BatchAccountUpdate, BatchNoteTree, ProvenBatch}, block::{BlockAccountUpdate, BlockHeader}, crypto::merkle::{EmptySubtreeRoots, MerklePath, MerkleStore, MmrPeaks, SmtProof}, note::Nullifier, @@ -44,7 +44,13 @@ impl BlockWitness { .iter() .enumerate() .filter(|(_, batch)| !batch.output_notes().is_empty()) - .map(|(batch_index, batch)| (batch_index, batch.output_notes_tree().root())) + .map(|(batch_index, batch)| { + let batch_note_tree = BatchNoteTree::with_contiguous_leaves( + batch.output_notes().iter().map(|note| (note.id(), note.metadata())), + ) + .expect("number of output notes in batch should be within the allowed range"); + (batch_index, batch_note_tree.root()) + }) .collect(); // Order account updates by account ID and each update's initial state hash. diff --git a/crates/block-producer/src/block_builder/prover/mod.rs b/crates/block-producer/src/block_builder/prover/mod.rs index ded93a7d4..2f141c352 100644 --- a/crates/block-producer/src/block_builder/prover/mod.rs +++ b/crates/block-producer/src/block_builder/prover/mod.rs @@ -1,11 +1,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use miden_lib::transaction::TransactionKernel; -use miden_objects::{ - assembly::Assembler, - block::{compute_tx_hash, BlockHeader}, - Digest, -}; +use miden_objects::{assembly::Assembler, block::BlockHeader, Digest}; use miden_processor::{execute, DefaultHost, ExecutionOptions, MemAdviceProvider, Program}; use miden_stdlib::StdLibrary; @@ -57,7 +53,7 @@ impl BlockProver { let block_num = witness.prev_header.block_num() + 1; let version = witness.prev_header.version(); - let tx_hash = compute_tx_hash(witness.transactions()); + let tx_hash = BlockHeader::compute_tx_commitment(witness.transactions()); let (account_root, note_root, nullifier_root, chain_root) = self.compute_roots(witness)?; let proof_hash = Digest::default(); diff --git a/crates/block-producer/src/block_builder/prover/tests.rs b/crates/block-producer/src/block_builder/prover/tests.rs index f020faf62..b26f3895d 100644 --- a/crates/block-producer/src/block_builder/prover/tests.rs +++ b/crates/block-producer/src/block_builder/prover/tests.rs @@ -644,21 +644,9 @@ async fn compute_note_root_success() { // The first 2 txs were put in the first batch; the 3rd was put in the second let note_tree = BlockNoteTree::with_entries([ - ( - BlockNoteIndex::new(0, 0).unwrap(), - notes_created[0].id(), - *notes_created[0].metadata(), - ), - ( - BlockNoteIndex::new(0, 1).unwrap(), - notes_created[1].id(), - *notes_created[1].metadata(), - ), - ( - BlockNoteIndex::new(1, 0).unwrap(), - notes_created[2].id(), - *notes_created[2].metadata(), - ), + (BlockNoteIndex::new(0, 0), notes_created[0].id(), *notes_created[0].metadata()), + (BlockNoteIndex::new(0, 1), notes_created[1].id(), *notes_created[1].metadata()), + (BlockNoteIndex::new(1, 0), notes_created[2].id(), *notes_created[2].metadata()), ]) .unwrap(); diff --git a/crates/block-producer/src/errors.rs b/crates/block-producer/src/errors.rs index 02bc01568..ec20a6b1d 100644 --- a/crates/block-producer/src/errors.rs +++ b/crates/block-producer/src/errors.rs @@ -6,10 +6,10 @@ use miden_objects::{ crypto::merkle::MerkleError, note::{NoteId, Nullifier}, transaction::TransactionId, - AccountDeltaError, BlockError, Digest, ProposedBatchError, + AccountDeltaError, Digest, ProposedBatchError, }; use miden_processor::ExecutionError; -use miden_tx_batch_prover::errors::BatchProveError; +use miden_tx_batch_prover::errors::ProvenBatchError; use thiserror::Error; use tokio::task::JoinError; @@ -143,7 +143,7 @@ pub enum BuildBatchError { ProposeBatchError(#[source] ProposedBatchError), #[error("failed to prove proposed transaction batch")] - ProveBatchError(#[source] BatchProveError), + ProveBatchError(#[source] ProvenBatchError), } // Block prover errors @@ -187,8 +187,9 @@ pub enum BuildBlockError { account_id: AccountId, source: AccountDeltaError, }, - #[error("block construction failed")] - BlockConstructionError(#[from] BlockError), + // TODO: Check if needed. + // #[error("block construction failed")] + // BlockConstructionError, /// We sometimes randomly inject errors into the batch building process to test our failure /// responses. #[error("nothing actually went wrong, failure was injected on purpose")] diff --git a/crates/block-producer/src/store/mod.rs b/crates/block-producer/src/store/mod.rs index 3d8009cda..d367a7a75 100644 --- a/crates/block-producer/src/store/mod.rs +++ b/crates/block-producer/src/store/mod.rs @@ -22,7 +22,7 @@ use miden_node_proto::{ use miden_node_utils::{formatting::format_opt, tracing::grpc::OtelInterceptor}; use miden_objects::{ account::AccountId, - block::{Block, BlockHeader, BlockNumber}, + block::{BlockHeader, BlockNumber, ProvenBlock}, note::{NoteId, Nullifier}, transaction::ProvenTransaction, utils::Serializable, @@ -228,7 +228,7 @@ impl StoreClient { } #[instrument(target = COMPONENT, name = "store.client.apply_block", skip_all, err)] - pub async fn apply_block(&self, block: &Block) -> Result<(), StoreError> { + pub async fn apply_block(&self, block: &ProvenBlock) -> Result<(), StoreError> { let request = tonic::Request::new(ApplyBlockRequest { block: block.to_bytes() }); self.inner.clone().apply_block(request).await.map(|_| ()).map_err(Into::into) diff --git a/crates/block-producer/src/test_utils/batch.rs b/crates/block-producer/src/test_utils/batch.rs index 37c2041fb..8e4371a3f 100644 --- a/crates/block-producer/src/test_utils/batch.rs +++ b/crates/block-producer/src/test_utils/batch.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use miden_objects::{ - batch::{BatchAccountUpdate, BatchId, BatchNoteTree, ProvenBatch}, + batch::{BatchAccountUpdate, BatchId, ProvenBatch}, block::BlockNumber, transaction::{InputNotes, ProvenTransaction}, Digest, @@ -56,16 +56,12 @@ impl TransactionBatchConstructor for ProvenBatch { output_notes.extend(tx.output_notes().iter().cloned()); } - ProvenBatch::new( + ProvenBatch::new_unchecked( BatchId::from_transactions(txs.into_iter()), Digest::default(), BlockNumber::GENESIS, account_updates, InputNotes::new_unchecked(input_notes), - BatchNoteTree::with_contiguous_leaves( - output_notes.iter().map(|x| (x.id(), x.metadata())), - ) - .unwrap(), output_notes, BlockNumber::from(u32::MAX), ) diff --git a/crates/block-producer/src/test_utils/block.rs b/crates/block-producer/src/test_utils/block.rs index 03ea004f3..eb7aef93f 100644 --- a/crates/block-producer/src/test_utils/block.rs +++ b/crates/block-producer/src/test_utils/block.rs @@ -2,7 +2,10 @@ use std::iter; use miden_objects::{ batch::ProvenBatch, - block::{Block, BlockAccountUpdate, BlockHeader, BlockNoteIndex, BlockNoteTree, NoteBatch}, + block::{ + BlockAccountUpdate, BlockHeader, BlockNoteIndex, BlockNoteTree, OutputNoteBatch, + ProvenBlock, + }, crypto::merkle::{Mmr, SimpleSmt}, note::Nullifier, transaction::OutputNote, @@ -103,7 +106,7 @@ pub struct MockBlockBuilder { last_block_header: BlockHeader, updated_accounts: Option>, - created_notes: Option>, + created_notes: Option>, produced_nullifiers: Option>, } @@ -140,7 +143,7 @@ impl MockBlockBuilder { } #[must_use] - pub fn created_notes(mut self, created_notes: Vec) -> Self { + pub fn created_notes(mut self, created_notes: Vec) -> Self { self.created_notes = Some(created_notes); self @@ -153,7 +156,7 @@ impl MockBlockBuilder { self } - pub fn build(self) -> Block { + pub fn build(self) -> ProvenBlock { let created_notes = self.created_notes.unwrap_or_default(); let header = BlockHeader::new( @@ -170,28 +173,27 @@ impl MockBlockBuilder { 1, ); - Block::new( + ProvenBlock::new_unchecked( header, self.updated_accounts.unwrap_or_default(), created_notes, self.produced_nullifiers.unwrap_or_default(), ) - .unwrap() } } pub(crate) fn flatten_output_notes<'a>( - batches: impl Iterator, + batches: impl Iterator, ) -> impl Iterator { batches.enumerate().flat_map(|(batch_idx, batch)| { - batch.iter().enumerate().map(move |(note_idx_in_batch, note)| { - (BlockNoteIndex::new(batch_idx, note_idx_in_batch).unwrap(), note) + batch.iter().map(move |(note_idx_in_batch, note)| { + (BlockNoteIndex::new(batch_idx, *note_idx_in_batch), note) }) }) } pub(crate) fn note_created_smt_from_note_batches<'a>( - batches: impl Iterator, + batches: impl Iterator, ) -> BlockNoteTree { let note_leaf_iterator = flatten_output_notes(batches).map(|(index, note)| (index, note.id(), *note.metadata())); @@ -201,6 +203,8 @@ pub(crate) fn note_created_smt_from_note_batches<'a>( pub(crate) fn block_output_notes<'a>( batches: impl Iterator + Clone, -) -> Vec> { - batches.map(|batch| batch.output_notes().to_vec()).collect() +) -> Vec { + batches + .map(|batch| batch.output_notes().iter().cloned().enumerate().collect()) + .collect() } diff --git a/crates/block-producer/src/test_utils/store.rs b/crates/block-producer/src/test_utils/store.rs index af404acd9..080570f64 100644 --- a/crates/block-producer/src/test_utils/store.rs +++ b/crates/block-producer/src/test_utils/store.rs @@ -6,7 +6,7 @@ use std::{ use miden_node_proto::domain::{block::BlockInclusionProof, note::NoteAuthenticationInfo}; use miden_objects::{ batch::ProvenBatch, - block::{Block, BlockHeader, BlockNumber, NoteBatch}, + block::{BlockHeader, BlockNumber, OutputNoteBatch, ProvenBlock}, crypto::merkle::{Mmr, SimpleSmt, Smt, ValuePath}, note::{NoteId, NoteInclusionProof, Nullifier}, transaction::ProvenTransaction, @@ -28,7 +28,7 @@ use crate::{ #[derive(Debug)] pub struct MockStoreSuccessBuilder { accounts: Option>, - notes: Option>, + notes: Option>, produced_nullifiers: Option>, chain_mmr: Option, block_num: Option, @@ -76,7 +76,10 @@ impl MockStoreSuccessBuilder { } #[must_use] - pub fn initial_notes<'a>(mut self, notes: impl Iterator + Clone) -> Self { + pub fn initial_notes<'a>( + mut self, + notes: impl Iterator + Clone, + ) -> Self { self.notes = Some(notes.cloned().collect()); self @@ -191,7 +194,7 @@ impl MockStoreSuccess { locked_accounts.root() } - pub async fn apply_block(&self, block: &Block) -> Result<(), StoreError> { + pub async fn apply_block(&self, block: &ProvenBlock) -> Result<(), StoreError> { // Intentionally, we take and hold both locks, to prevent calls to `get_tx_inputs()` from // going through while we're updating the store's data structure let mut locked_accounts = self.accounts.write().await; @@ -206,7 +209,7 @@ impl MockStoreSuccess { debug_assert_eq!(locked_accounts.root(), header.account_root()); // update nullifiers - for nullifier in block.nullifiers() { + for nullifier in block.created_nullifiers() { locked_produced_nullifiers .insert(nullifier.inner(), [header.block_num().into(), ZERO, ZERO, ZERO]); } @@ -219,11 +222,11 @@ impl MockStoreSuccess { } // build note tree - let note_tree = block.build_note_tree(); + let note_tree = block.build_output_note_tree(); // update notes let mut locked_notes = self.notes.write().await; - for (note_index, note) in block.notes() { + for (note_index, note) in block.output_notes() { locked_notes.insert( note.id(), NoteInclusionProof::new( diff --git a/crates/store/src/db/mod.rs b/crates/store/src/db/mod.rs index 51a678e22..4c9ba6912 100644 --- a/crates/store/src/db/mod.rs +++ b/crates/store/src/db/mod.rs @@ -11,7 +11,7 @@ use miden_node_proto::{ }; use miden_objects::{ account::{AccountDelta, AccountId}, - block::{Block, BlockHeader, BlockNoteIndex, BlockNumber}, + block::{BlockHeader, BlockNoteIndex, BlockNumber, ProvenBlock}, crypto::{hash::rpo::RpoDigest, merkle::MerklePath, utils::Deserializable}, note::{NoteId, NoteInclusionProof, NoteMetadata, Nullifier}, transaction::TransactionId, @@ -408,7 +408,7 @@ impl Db { &self, allow_acquire: oneshot::Sender<()>, acquire_done: oneshot::Receiver<()>, - block: Block, + block: ProvenBlock, notes: Vec, ) -> Result<()> { self.pool @@ -423,7 +423,7 @@ impl Db { &transaction, &block.header(), ¬es, - block.nullifiers(), + block.created_nullifiers(), block.updated_accounts(), )?; diff --git a/crates/store/src/db/sql/mod.rs b/crates/store/src/db/sql/mod.rs index 7ade42670..479638ea8 100644 --- a/crates/store/src/db/sql/mod.rs +++ b/crates/store/src/db/sql/mod.rs @@ -747,6 +747,11 @@ pub fn select_all_notes(conn: &mut Connection) -> Result> { let mut notes = vec![]; while let Some(row) = rows.next()? { + let batch_idx = row.get(1)?; + let note_idx_in_batch = row.get(2)?; + // SAFETY: We can assume the batch and note indices stored in the DB are valid so this + // should never panic. + let note_index = BlockNoteIndex::new(batch_idx, note_idx_in_batch); let note_id_data = row.get_ref(3)?.as_blob()?; let note_id = RpoDigest::read_from_bytes(note_id_data)?; @@ -768,7 +773,7 @@ pub fn select_all_notes(conn: &mut Connection) -> Result> { notes.push(NoteRecord { block_num: read_block_number(row, 0)?, - note_index: BlockNoteIndex::new(row.get(1)?, row.get(2)?)?, + note_index, note_id, metadata, details, @@ -856,7 +861,11 @@ pub fn select_notes_since_block_by_tag_and_sender( let mut res = Vec::new(); while let Some(row) = rows.next()? { let block_num = read_block_number(row, 0)?; - let note_index = BlockNoteIndex::new(row.get(1)?, row.get(2)?)?; + let batch_idx = row.get(1)?; + let note_idx_in_batch = row.get(2)?; + // SAFETY: We can assume the batch and note indices stored in the DB are valid so this + // should never panic. + let note_index = BlockNoteIndex::new(batch_idx, note_idx_in_batch); let note_id = read_from_blob_column(row, 3)?; let note_type = row.get::<_, u8>(4)?; let sender = read_from_blob_column(row, 5)?; @@ -919,6 +928,11 @@ pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result< let mut notes = Vec::new(); while let Some(row) = rows.next()? { + let batch_idx = row.get(1)?; + let note_idx_in_batch = row.get(2)?; + // SAFETY: We can assume the batch and note indices stored in the DB are valid so this + // should never panic. + let note_index = BlockNoteIndex::new(batch_idx, note_idx_in_batch); let note_id_data = row.get_ref(3)?.as_blob()?; let note_id = NoteId::read_from_bytes(note_id_data)?; @@ -939,7 +953,7 @@ pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result< notes.push(NoteRecord { block_num: read_block_number(row, 0)?, - note_index: BlockNoteIndex::new(row.get(1)?, row.get(2)?)?, + note_index, details, note_id: note_id.into(), metadata, @@ -989,7 +1003,9 @@ pub fn select_note_inclusion_proofs( let batch_index = row.get(2)?; let note_index = row.get(3)?; - let node_index_in_block = BlockNoteIndex::new(batch_index, note_index)?.leaf_index_value(); + // SAFETY: We can assume the batch and note indices stored in the DB are valid so this + // should never panic. + let node_index_in_block = BlockNoteIndex::new(batch_index, note_index).leaf_index_value(); let merkle_path_data = row.get_ref(4)?.as_blob()?; let merkle_path = MerklePath::read_from_bytes(merkle_path_data)?; diff --git a/crates/store/src/db/tests.rs b/crates/store/src/db/tests.rs index fe8d1684c..62919ea8e 100644 --- a/crates/store/src/db/tests.rs +++ b/crates/store/src/db/tests.rs @@ -175,7 +175,7 @@ fn sql_select_notes() { for i in 0..10 { let note = NoteRecord { block_num, - note_index: BlockNoteIndex::new(0, i as usize).unwrap(), + note_index: BlockNoteIndex::new(0, i as usize), note_id: num_to_rpo_digest(u64::from(i)), metadata: NoteMetadata::new( ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), @@ -215,7 +215,7 @@ fn sql_select_notes_different_execution_hints() { let note_none = NoteRecord { block_num, - note_index: BlockNoteIndex::new(0, 0).unwrap(), + note_index: BlockNoteIndex::new(0, 0), note_id: num_to_rpo_digest(0), metadata: NoteMetadata::new( ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), @@ -239,7 +239,7 @@ fn sql_select_notes_different_execution_hints() { let note_always = NoteRecord { block_num, - note_index: BlockNoteIndex::new(0, 1).unwrap(), + note_index: BlockNoteIndex::new(0, 1), note_id: num_to_rpo_digest(1), metadata: NoteMetadata::new( ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), @@ -263,7 +263,7 @@ fn sql_select_notes_different_execution_hints() { let note_after_block = NoteRecord { block_num, - note_index: BlockNoteIndex::new(0, 2).unwrap(), + note_index: BlockNoteIndex::new(0, 2), note_id: num_to_rpo_digest(2), metadata: NoteMetadata::new( ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), @@ -880,7 +880,7 @@ fn notes() { assert!(res.is_empty()); // test insertion - let note_index = BlockNoteIndex::new(0, 2).unwrap(); + let note_index = BlockNoteIndex::new(0, 2); let note_id = num_to_rpo_digest(3); let tag = 5u32; let sender = AccountId::try_from(ACCOUNT_ID_OFF_CHAIN_SENDER).unwrap(); diff --git a/crates/store/src/errors.rs b/crates/store/src/errors.rs index 2c6fa9e36..aecf56f33 100644 --- a/crates/store/src/errors.rs +++ b/crates/store/src/errors.rs @@ -11,7 +11,7 @@ use miden_objects::{ }, note::Nullifier, transaction::OutputNote, - AccountDeltaError, AccountError, BlockError, NoteError, + AccountDeltaError, AccountError, NoteError, }; use rusqlite::types::FromSqlError; use thiserror::Error; @@ -41,8 +41,9 @@ pub enum DatabaseError { AccountError(#[from] AccountError), #[error("account delta error")] AccountDeltaError(#[from] AccountDeltaError), + // TODO: Check if needed. #[error("block error")] - BlockError(#[from] BlockError), + BlockError, #[error("closed channel")] ClosedChannel(#[from] RecvError), #[error("deserialization failed")] @@ -136,8 +137,9 @@ pub enum GenesisError { // --------------------------------------------------------------------------------------------- #[error("database error")] DatabaseError(#[from] DatabaseError), + // TODO: Check if needed. #[error("block error")] - BlockError(#[from] BlockError), + BlockError, #[error("merkle error")] MerkleError(#[from] MerkleError), #[error("failed to deserialize genesis file")] diff --git a/crates/store/src/genesis.rs b/crates/store/src/genesis.rs index de128879c..90783f503 100644 --- a/crates/store/src/genesis.rs +++ b/crates/store/src/genesis.rs @@ -1,7 +1,7 @@ use miden_lib::transaction::TransactionKernel; use miden_objects::{ account::{delta::AccountUpdateDetails, Account}, - block::{Block, BlockAccountUpdate, BlockHeader, BlockNumber}, + block::{BlockAccountUpdate, BlockHeader, BlockNumber, ProvenBlock}, crypto::merkle::{EmptySubtreeRoots, MmrPeaks, SimpleSmt, Smt}, utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable}, Digest, ACCOUNT_TREE_DEPTH, BLOCK_NOTE_TREE_DEPTH, @@ -26,7 +26,7 @@ impl GenesisState { } /// Returns the block header and the account SMT - pub fn into_block(self) -> Result { + pub fn into_block(self) -> Result { let accounts: Vec = self .accounts .iter() @@ -65,7 +65,7 @@ impl GenesisState { self.timestamp, ); - Block::new(header, accounts, vec![], vec![]).map_err(Into::into) + Ok(ProvenBlock::new_unchecked(header, accounts, vec![], vec![])) } } diff --git a/crates/store/src/server/api.rs b/crates/store/src/server/api.rs index 24cde86a5..0064668e6 100644 --- a/crates/store/src/server/api.rs +++ b/crates/store/src/server/api.rs @@ -29,7 +29,7 @@ use miden_node_proto::{ }; use miden_objects::{ account::AccountId, - block::{Block, BlockNumber}, + block::{BlockNumber, ProvenBlock}, crypto::hash::rpo::RpoDigest, note::{NoteId, Nullifier}, utils::{Deserializable, Serializable}, @@ -315,7 +315,7 @@ impl api_server::Api for StoreApi { debug!(target: COMPONENT, ?request); - let block = Block::read_from_bytes(&request.block).map_err(|err| { + let block = ProvenBlock::read_from_bytes(&request.block).map_err(|err| { Status::invalid_argument(format!("Block deserialization error: {err}")) })?; @@ -326,8 +326,8 @@ impl api_server::Api for StoreApi { block_num, block_hash = %block.hash(), account_count = block.updated_accounts().len(), - note_count = block.notes().count(), - nullifier_count = block.nullifiers().len(), + note_count = block.output_notes().count(), + nullifier_count = block.created_nullifiers().len(), ); self.state.apply_block(block).await?; diff --git a/crates/store/src/state.rs b/crates/store/src/state.rs index b08348e3a..17ec2cf45 100644 --- a/crates/store/src/state.rs +++ b/crates/store/src/state.rs @@ -25,7 +25,7 @@ use miden_node_proto::{ use miden_node_utils::formatting::format_array; use miden_objects::{ account::{AccountDelta, AccountHeader, AccountId, StorageSlot}, - block::{Block, BlockHeader, BlockNumber}, + block::{BlockHeader, BlockNumber, ProvenBlock}, crypto::{ hash::rpo::RpoDigest, merkle::{ @@ -268,12 +268,12 @@ impl State { /// released. // TODO: This span is logged in a root span, we should connect it to the parent span. #[instrument(target = COMPONENT, skip_all, err)] - pub async fn apply_block(&self, block: Block) -> Result<(), ApplyBlockError> { + pub async fn apply_block(&self, block: ProvenBlock) -> Result<(), ApplyBlockError> { let _lock = self.writer.try_lock().map_err(|_| ApplyBlockError::ConcurrentWrite)?; let header = block.header(); - let tx_hash = block.compute_tx_hash(); + let tx_hash = BlockHeader::compute_tx_commitment(block.transactions()); if header.tx_hash() != tx_hash { return Err(InvalidBlockError::InvalidBlockTxHash { expected: tx_hash, @@ -324,7 +324,7 @@ impl State { // nullifiers can be produced only once let duplicate_nullifiers: Vec<_> = block - .nullifiers() + .created_nullifiers() .iter() .filter(|&n| inner.nullifier_tree.get_block_num(n).is_some()) .copied() @@ -343,7 +343,7 @@ impl State { // compute update for nullifier tree let nullifier_tree_update = inner.nullifier_tree.compute_mutations( - block.nullifiers().iter().map(|nullifier| (*nullifier, block_num)), + block.created_nullifiers().iter().map(|nullifier| (*nullifier, block_num)), ); if nullifier_tree_update.root() != header.nullifier_root() { @@ -373,13 +373,13 @@ impl State { }; // build note tree - let note_tree = block.build_note_tree(); + let note_tree = block.build_output_note_tree(); if note_tree.root() != header.note_root() { return Err(InvalidBlockError::NewBlockInvalidNoteRoot.into()); } let notes = block - .notes() + .output_notes() .map(|(note_index, note)| { let details = match note { OutputNote::Full(note) => Some(note.to_bytes()),