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

feat: Add kernel root to blocks #496

Merged
merged 11 commits into from
Sep 13, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v0.6.0 (TBD)

- [BREAKING] Added `kernel_root` to block header's protobuf message definitions (#496).
- [BREAKING] Renamed `off-chain` and `on-chain` to `private` and `public` respectively for the account storage modes (#489).
- Optimized state synchronizations by removing unnecessary fetching and parsing of note details (#462).
- [BREAKING] Changed `GetAccountDetailsResponse` field to `details` (#481).
Expand Down
14 changes: 7 additions & 7 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 crates/block-producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tracing-forest = ["miden-node-utils/tracing-forest"]
async-trait = { version = "0.1" }
figment = { version = "0.10", features = ["toml", "env"] }
itertools = { version = "0.13" }
miden-lib = { workspace = true }
miden-node-proto = { workspace = true }
miden-node-utils = { workspace = true }
miden-objects = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion crates/block-producer/src/block_builder/prover/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::{SystemTime, UNIX_EPOCH};

use miden_lib::transaction::TransactionKernel;
use miden_objects::{assembly::Assembler, block::compute_tx_hash, BlockHeader, Digest};
use miden_processor::{execute, DefaultHost, ExecutionOptions, MemAdviceProvider, Program};
use miden_stdlib::StdLibrary;
Expand Down Expand Up @@ -61,7 +62,7 @@ impl BlockProver {
.expect("today is expected to be after 1970")
.as_secs()
.try_into()
.expect("timestamp must fit to `u32`");
.expect("timestamp must fit in a `u32`");

Ok(BlockHeader::new(
version,
Expand All @@ -72,6 +73,7 @@ impl BlockProver {
nullifier_root,
note_root,
tx_hash,
TransactionKernel::kernel_root(),
proof_hash,
timestamp,
))
Expand Down
2 changes: 2 additions & 0 deletions crates/block-producer/src/test_utils/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub async fn build_expected_block_header(
note_created_smt.root(),
Digest::default(),
Digest::default(),
Digest::default(),
1,
)
}
Expand Down Expand Up @@ -162,6 +163,7 @@ impl MockBlockBuilder {
note_created_smt_from_note_batches(created_notes.iter()).root(),
Digest::default(),
Digest::default(),
Digest::default(),
1,
);

Expand Down
1 change: 1 addition & 0 deletions crates/block-producer/src/test_utils/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl MockStoreSuccessBuilder {
note_root,
Digest::default(),
Digest::default(),
Digest::default(),
1,
);

Expand Down
43 changes: 24 additions & 19 deletions crates/proto/src/domain/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use miden_objects::{crypto::merkle::MerklePath, BlockHeader};

use crate::{
errors::{ConversionError, MissingFieldHelper},
generated::block,
generated::block as proto,
};

// BLOCK HEADER
// ================================================================================================

impl From<&BlockHeader> for block::BlockHeader {
impl From<&BlockHeader> for proto::BlockHeader {
fn from(header: &BlockHeader) -> Self {
Self {
version: header.version(),
Expand All @@ -19,60 +19,65 @@ impl From<&BlockHeader> for block::BlockHeader {
nullifier_root: Some(header.nullifier_root().into()),
note_root: Some(header.note_root().into()),
tx_hash: Some(header.tx_hash().into()),
kernel_root: Some(header.kernel_root().into()),
proof_hash: Some(header.proof_hash().into()),
timestamp: header.timestamp(),
}
}
}

impl From<BlockHeader> for block::BlockHeader {
impl From<BlockHeader> for proto::BlockHeader {
fn from(header: BlockHeader) -> Self {
(&header).into()
}
}

impl TryFrom<&block::BlockHeader> for BlockHeader {
impl TryFrom<&proto::BlockHeader> for BlockHeader {
type Error = ConversionError;

fn try_from(value: &block::BlockHeader) -> Result<Self, Self::Error> {
fn try_from(value: &proto::BlockHeader) -> Result<Self, Self::Error> {
value.try_into()
}
}

impl TryFrom<block::BlockHeader> for BlockHeader {
impl TryFrom<proto::BlockHeader> for BlockHeader {
type Error = ConversionError;

fn try_from(value: block::BlockHeader) -> Result<Self, Self::Error> {
fn try_from(value: proto::BlockHeader) -> Result<Self, Self::Error> {
Ok(BlockHeader::new(
value.version,
value
.prev_hash
.ok_or(block::BlockHeader::missing_field(stringify!(prev_hash)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(prev_hash)))?
.try_into()?,
value.block_num,
value
.chain_root
.ok_or(block::BlockHeader::missing_field(stringify!(chain_root)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(chain_root)))?
.try_into()?,
value
.account_root
.ok_or(block::BlockHeader::missing_field(stringify!(account_root)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(account_root)))?
.try_into()?,
value
.nullifier_root
.ok_or(block::BlockHeader::missing_field(stringify!(nullifier_root)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(nullifier_root)))?
.try_into()?,
value
.note_root
.ok_or(block::BlockHeader::missing_field(stringify!(note_root)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(note_root)))?
.try_into()?,
value
.tx_hash
.ok_or(block::BlockHeader::missing_field(stringify!(tx_hash)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(tx_hash)))?
.try_into()?,
value
.kernel_root
.ok_or(proto::BlockHeader::missing_field(stringify!(kernel_root)))?
.try_into()?,
value
.proof_hash
.ok_or(block::BlockHeader::missing_field(stringify!(proof_hash)))?
.ok_or(proto::BlockHeader::missing_field(stringify!(proof_hash)))?
.try_into()?,
value.timestamp,
))
Expand All @@ -87,7 +92,7 @@ pub struct BlockInclusionProof {
pub chain_length: u32,
}

impl From<BlockInclusionProof> for block::BlockInclusionProof {
impl From<BlockInclusionProof> for proto::BlockInclusionProof {
fn from(value: BlockInclusionProof) -> Self {
Self {
block_header: Some(value.block_header.into()),
Expand All @@ -97,18 +102,18 @@ impl From<BlockInclusionProof> for block::BlockInclusionProof {
}
}

impl TryFrom<block::BlockInclusionProof> for BlockInclusionProof {
impl TryFrom<proto::BlockInclusionProof> for BlockInclusionProof {
type Error = ConversionError;

fn try_from(value: block::BlockInclusionProof) -> Result<Self, ConversionError> {
fn try_from(value: proto::BlockInclusionProof) -> Result<Self, ConversionError> {
let result = Self {
block_header: value
.block_header
.ok_or(block::BlockInclusionProof::missing_field("block_header"))?
.ok_or(proto::BlockInclusionProof::missing_field("block_header"))?
.try_into()?,
mmr_path: (&value
.mmr_path
.ok_or(block::BlockInclusionProof::missing_field("mmr_path"))?)
.ok_or(proto::BlockInclusionProof::missing_field("mmr_path"))?)
.try_into()?,
chain_length: value.chain_length,
};
Expand Down
5 changes: 4 additions & 1 deletion crates/proto/src/generated/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ pub struct BlockHeader {
/// a hash of a STARK proof attesting to the correct state transition.
#[prost(message, optional, tag = "9")]
pub proof_hash: ::core::option::Option<super::digest::Digest>,
/// a commitment to all transaction kernels supported by this block.
#[prost(message, optional, tag = "10")]
pub kernel_root: ::core::option::Option<super::digest::Digest>,
/// the time when the block was created.
#[prost(fixed32, tag = "10")]
#[prost(fixed32, tag = "11")]
pub timestamp: u32,
}
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
4 changes: 3 additions & 1 deletion crates/rpc-proto/proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ message BlockHeader {
digest.Digest tx_hash = 8;
// a hash of a STARK proof attesting to the correct state transition.
digest.Digest proof_hash = 9;
// a commitment to all transaction kernels supported by this block.
digest.Digest kernel_root = 10;
// the time when the block was created.
fixed32 timestamp = 10;
fixed32 timestamp = 11;
}

message BlockInclusionProof {
Expand Down
10 changes: 6 additions & 4 deletions crates/store/src/db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ fn create_block(conn: &mut Connection, block_num: u32) {
num_to_rpo_digest(7),
num_to_rpo_digest(8),
num_to_rpo_digest(9),
10_u8.into(),
num_to_rpo_digest(10),
11_u8.into(),
);

let transaction = conn.transaction().unwrap();
Expand Down Expand Up @@ -735,9 +736,9 @@ fn test_db_block_header() {
num_to_rpo_digest(7),
num_to_rpo_digest(8),
num_to_rpo_digest(9),
10_u8.into(),
num_to_rpo_digest(10),
11_u8.into(),
);

// test insertion
let transaction = conn.transaction().unwrap();
sql::insert_block_header(&transaction, &block_header).unwrap();
Expand Down Expand Up @@ -767,7 +768,8 @@ fn test_db_block_header() {
num_to_rpo_digest(17),
num_to_rpo_digest(18),
num_to_rpo_digest(19),
20_u8.into(),
num_to_rpo_digest(20),
21_u8.into(),
);

let transaction = conn.transaction().unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/store/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl GenesisState {
*EmptySubtreeRoots::entry(NOTE_LEAF_DEPTH, 0),
Digest::default(),
Digest::default(),
Digest::default(),
self.timestamp,
);

Expand Down
4 changes: 3 additions & 1 deletion proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ message BlockHeader {
digest.Digest tx_hash = 8;
// a hash of a STARK proof attesting to the correct state transition.
digest.Digest proof_hash = 9;
// a commitment to all transaction kernels supported by this block.
digest.Digest kernel_root = 10;
// the time when the block was created.
fixed32 timestamp = 10;
fixed32 timestamp = 11;
}

message BlockInclusionProof {
Expand Down
Loading