Skip to content

Commit

Permalink
feat: add Block object and refactored AccountUpdate object (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
polydez authored Apr 25, 2024
1 parent 19f7d08 commit 579e655
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 228 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.3.0 (TBD)

* [BREAKING] Removed the transaction script root output from the transaction kernel (#608).
* [BREAKING] Refactored account update details, moved `Block` to `miden-objects` (#618, #621).

## 0.2.1 (2024-04-12)

Expand All @@ -19,7 +20,8 @@
* Improved `ProvenTransaction` serialization (#543).
* Implemented note tree wrapper structs (#560).
* [BREAKING] Migrated to v0.9 version of Miden VM (#567).
* [BREAKING] Added account storage type parameter to `create_basic_wallet` and `create_basic_fungible_faucet` (miden-lib crate only) (#587).
* [BREAKING] Added account storage type parameter to `create_basic_wallet` and `create_basic_fungible_faucet` (miden-lib
crate only) (#587).
* Removed serialization of source locations from account code (#590).

## 0.1.1 (2024-03-07) - `miden-objects` crate only
Expand Down
40 changes: 9 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions miden-tx/src/prover/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use miden_lib::transaction::{ToTransactionKernelInputs, TransactionKernel};
use miden_objects::{
accounts::delta::AccountUpdateDetails,
notes::Nullifier,
transaction::{
AccountDetails, InputNotes, ProvenTransaction, ProvenTransactionBuilder, TransactionWitness,
},
transaction::{InputNotes, ProvenTransaction, ProvenTransactionBuilder, TransactionWitness},
};
use miden_prover::prove;
pub use miden_prover::ProvingOptions;
Expand Down Expand Up @@ -75,18 +74,18 @@ impl TransactionProver {

let builder = match account_id.is_on_chain() {
true => {
let account_details = if tx_witness.account().is_new() {
let account_update_details = if tx_witness.account().is_new() {
let mut account = tx_witness.account().clone();
account
.apply_delta(&account_delta)
.map_err(TransactionProverError::InvalidAccountDelta)?;

AccountDetails::Full(account)
AccountUpdateDetails::New(account)
} else {
AccountDetails::Delta(account_delta)
AccountUpdateDetails::Delta(account_delta)
};

builder.account_details(account_details)
builder.account_update_details(account_update_details)
},
false => builder,
};
Expand Down
4 changes: 2 additions & 2 deletions miden-tx/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ impl TransactionVerifier {
// build stack inputs and outputs
let stack_inputs = TransactionKernel::build_input_stack(
transaction.account_id(),
transaction.initial_account_hash(),
transaction.account_update().init_state_hash(),
transaction.input_notes().commitment(),
transaction.block_ref(),
);
let stack_outputs = TransactionKernel::build_output_stack(
transaction.final_account_hash(),
transaction.account_update().final_state_hash(),
transaction.output_notes().commitment(),
);

Expand Down
56 changes: 55 additions & 1 deletion objects/src/accounts/delta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use alloc::string::ToString;

use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, Serializable, Word, ZERO,
Account, ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, Serializable,
Word, ZERO,
};
use crate::{assets::Asset, AccountDeltaError};

Expand Down Expand Up @@ -83,6 +84,28 @@ impl AccountDelta {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AccountUpdateDetails {
/// Account is private (no on-chain state change).
Private,

/// The whole state is needed for new accounts.
New(Account),

/// For existing accounts, only the delta is needed.
Delta(AccountDelta),
}

impl AccountUpdateDetails {
/// Returns `true` if the account update details are for private account.
pub fn is_private(&self) -> bool {
matches!(self, Self::Private)
}
}

// SERIALIZATION
// ================================================================================================

impl Serializable for AccountDelta {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.storage.write_into(target);
Expand All @@ -104,6 +127,37 @@ impl Deserializable for AccountDelta {
}
}

impl Serializable for AccountUpdateDetails {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
AccountUpdateDetails::Private => {
0_u8.write_into(target);
},
AccountUpdateDetails::New(account) => {
1_u8.write_into(target);
account.write_into(target);
},
AccountUpdateDetails::Delta(delta) => {
2_u8.write_into(target);
delta.write_into(target);
},
}
}
}

impl Deserializable for AccountUpdateDetails {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
match u8::read_from(source)? {
0 => Ok(Self::Private),
1 => Ok(Self::New(Account::read_from(source)?)),
2 => Ok(Self::Delta(AccountDelta::read_from(source)?)),
v => Err(DeserializationError::InvalidValue(format!(
"Unknown variant {v} for AccountDetails"
))),
}
}
}

// HELPER FUNCTIONS
// ================================================================================================

Expand Down
Loading

0 comments on commit 579e655

Please sign in to comment.