Skip to content

Commit

Permalink
Improved doc comments + Changed panic to Error
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Oct 4, 2024
1 parent b1d38ad commit e42bd17
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
11 changes: 7 additions & 4 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export.apply_storage_offset
# => [offset_slot_index, storage_offset, storage_size]

# verify that slot_index is in bounds
movdn.2 add sub.1 dup.1 gte assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
movdn.2 add dup.1 gt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [offset_slot_index]
end

Expand Down Expand Up @@ -572,10 +572,11 @@ end
#! Returns the procedure information
#!
#! Stack: [index, ...]
#! Output: [PROC_ROOT, storage_offset, ...]
#! Output: [PROC_ROOT, storage_offset, storage_size]
#!
#! - PROC_ROOT is the hash of the procedure.
#! - storage_offset is the procedure storage offset.
#! - storage_size is the number of storage slots the procedure is allowed to access.
#!
#! Panics if
#! - index is out of bounds
Expand All @@ -600,9 +601,11 @@ end
#! Verifies that the procedure root is part of the account code
#!
#! Stack: [PROC_ROOT]
#! Output: [storage_offset]
#! Output: [storage_offset, storage_size]
#!
#! - PROC_ROOT is the hash of the procedure to authenticate.
#! - storage_offset is the procedure storage offset.
#! - storage_size is the number of storage slots the procedure is allowed to access.
#!
#! Panics if
#! - procedure root is not part of the account code.
Expand Down Expand Up @@ -744,7 +747,7 @@ end
#! Output: [storage_offset, storage_size]
#!
#! - storage_offset is the procedure storage offset.
#! - storage_size is the procedure storage size.
#! - storage_size is the number of storage slots the procedure is allowed to access.
proc.get_procedure_metadata
# get procedure storage metadata pointer
mul.2 exec.memory::get_acct_procedures_section_ptr add add.1
Expand Down
8 changes: 4 additions & 4 deletions miden-lib/src/transaction/procedures/kernel_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub const KERNEL0_PROCEDURES: [Digest; 32] = [
// get_account_id
digest!(0x386549d4435f79c1, 0x4a7add2e3b9f1b9e, 0x91c0af1138c14e77, 0xee8a5630e31bc74d),
// get_account_item
digest!(0x9afb3c813c39cdba, 0x9bc4f37538512aed, 0xcc592e7820104593, 0x59a7fedcd9c30dc9),
digest!(0x614250d8c36af706, 0x46d39fb65480d1f3, 0xe0ebb7d5f46a6f32, 0x2bc18e17712bbbc5),
// get_account_map_item
digest!(0x48cbbd91d5802b3d, 0x5a06de9f28e00bf3, 0xe4b05cb4d042df1d, 0x76a54f245408615d),
digest!(0xe055cca34d15fc7f, 0x815734bce550acd4, 0x50a827f81176640b, 0xb426738c7e29fb23),
// get_account_nonce
digest!(0x64d14d80f9eff37a, 0x7587e273b2d8a416, 0x3c041064332c03d3, 0xc327341072f4f1e8),
// get_account_vault_commitment
Expand All @@ -34,9 +34,9 @@ pub const KERNEL0_PROCEDURES: [Digest; 32] = [
// set_account_code
digest!(0x90bc1f541f7adc63, 0xffa3daf2197fe496, 0xc72c5cedeb3482b, 0x5d6eac8e22abda40),
// set_account_item
digest!(0xb60120026fe7a801, 0xe096b187ad23ebec, 0x86464c57975dd88, 0xe20e396dbc8a9649),
digest!(0xe77cd2a1c02ad66a, 0xa18d96ecd20c7ca8, 0x7114ec61e4db0bea, 0xe6b97475f1f4dcbc),
// set_account_map_item
digest!(0xe085d5a90510c979, 0x9bd95341f0f07fb0, 0x4caf05f9ba8a5662, 0xd04560419bee4c83),
digest!(0x49092f6ea0d561f, 0x11528bb53882af83, 0x228c1352560481a, 0x79667f86e9a32dd),
// burn_asset
digest!(0x58e53cf050c1218e, 0x498f9b3f9904c03f, 0xbc341b7737247115, 0x3ea366d3bc90fe32),
// get_fungible_faucet_total_issuance
Expand Down
8 changes: 4 additions & 4 deletions miden-tx/src/tests/kernel_tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ fn test_storage_offset() {
// TODO: We manually set the offsets here because we do not have the ability to set the
// offsets through MASM for now. Remove this code when we enable this functionality.
let procedures_with_offsets = vec![
AccountProcedureInfo::new(*code.procedures()[0].mast_root(), 2, 1),
AccountProcedureInfo::new(*code.procedures()[1].mast_root(), 2, 1),
AccountProcedureInfo::new(*code.procedures()[2].mast_root(), 1, 1),
AccountProcedureInfo::new(*code.procedures()[3].mast_root(), 1, 1),
AccountProcedureInfo::new(*code.procedures()[0].mast_root(), 2, 1).unwrap(),
AccountProcedureInfo::new(*code.procedures()[1].mast_root(), 2, 1).unwrap(),
AccountProcedureInfo::new(*code.procedures()[2].mast_root(), 1, 1).unwrap(),
AccountProcedureInfo::new(*code.procedures()[3].mast_root(), 1, 1).unwrap(),
];
let code = AccountCode::from_parts(code.mast().clone(), procedures_with_offsets.clone());

Expand Down
4 changes: 2 additions & 2 deletions miden-tx/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn executed_transaction_account_delta() {
let procedures = account_code
.procedures()
.iter()
.map(|proc| AccountProcedureInfo::new(*proc.mast_root(), proc.storage_offset(), 3))
.map(|proc| AccountProcedureInfo::new(*proc.mast_root(), proc.storage_offset(), 3).unwrap())
.collect();
let account_code = AccountCode::from_parts(account_code.mast(), procedures);
let (account, _) = AccountBuilder::new(ChaCha20Rng::from_entropy())
Expand Down Expand Up @@ -194,7 +194,7 @@ fn executed_transaction_account_delta() {
send_asset_script.push_str(&format!(
"
### note {i}
# prepare the stack for a new note creation
# prepare the stack for a new note creation
push.0.1.2.3 # recipient
push.{EXECUTION_HINT} # note_execution_hint
push.{NOTETYPE} # note_type
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/tests/integration/scripts/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn prove_faucet_contract_mint_fungible_asset_succeeds() {
.code()
.procedures()
.iter()
.map(|proc| AccountProcedureInfo::new(*proc.mast_root(), proc.storage_offset(), 2))
.map(|proc| AccountProcedureInfo::new(*proc.mast_root(), proc.storage_offset(), 2).unwrap())
.collect();
let account_code = AccountCode::from_parts(faucet_account.code().mast(), procedures);
let faucet_account = Account::from_parts(
Expand Down
2 changes: 1 addition & 1 deletion objects/src/accounts/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl AccountCode {
proc_mast_root,
storage_offset,
storage_size,
));
)?);
}
}

Expand Down
22 changes: 16 additions & 6 deletions objects/src/accounts/code/procedure.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::string::ToString;

use vm_core::{
utils::{ByteReader, ByteWriter, Deserializable, Serializable},
FieldElement,
};
use vm_processor::DeserializationError;

use super::{Digest, Felt};
use super::{AccountCode, Digest, Felt};
use crate::AccountError;

// ACCOUNT PROCEDURE INFO
Expand Down Expand Up @@ -43,12 +45,20 @@ impl AccountProcedureInfo {
///
/// # Panics
/// Panics if `storage_size` is 0 and `storage_offset` is not 0.
pub fn new(mast_root: Digest, storage_offset: u8, storage_size: u8) -> Self {
pub fn new(
mast_root: Digest,
storage_offset: u8,
storage_size: u8,
) -> Result<Self, AccountError> {
if storage_size == 0 && storage_offset != 0 {
panic!("storage_offset must be 0 when storage_size is 0");
return Err(AccountError::ProcedureNotAccessingStorageHasOffsets);
}

if (storage_offset + storage_size) as usize > AccountCode::MAX_NUM_PROCEDURES {
return Err(AccountError::StorageLimitOutOfBounds);
}

Self { mast_root, storage_offset, storage_size }
Ok(Self { mast_root, storage_offset, storage_size })
}

// PUBLIC ACCESSORS
Expand Down Expand Up @@ -132,8 +142,8 @@ impl Deserializable for AccountProcedureInfo {
let mast_root: Digest = source.read()?;
let storage_offset = source.read_u8()?;
let storage_size = source.read_u8()?;

Ok(Self::new(mast_root, storage_offset, storage_size))
Self::new(mast_root, storage_offset, storage_size)
.map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}

Expand Down
2 changes: 2 additions & 0 deletions objects/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum AccountError {
StorageSlotNotValue(u8),
StorageIndexOutOfBounds { max: u8, actual: u8 },
StorageTooManySlots(u64),
StorageLimitOutOfBounds,
ProcedureNotAccessingStorageHasOffsets,
}

impl fmt::Display for AccountError {
Expand Down

0 comments on commit e42bd17

Please sign in to comment.