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: AccountHeader serialization #996

Merged
merged 2 commits into from
Nov 29, 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 @@ -4,6 +4,7 @@

### Changes

- Implemented serialization for `AccountHeader` (#996).
- Refactored `miden-tx-prover` proxy load balancing strategy (#976).
- [BREAKING] Better error display when queues are full in the prover service (#967).
- [BREAKING] Remove `AccountBuilder::build_testing` and make `Account::initialize_from_components` private (#969).
Expand Down
64 changes: 64 additions & 0 deletions objects/src/accounts/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use vm_core::utils::{Deserializable, Serializable};

use super::{hash_account, Account, AccountId, Digest, Felt};

// ACCOUNT HEADER
Expand Down Expand Up @@ -101,3 +103,65 @@ impl From<&Account> for AccountHeader {
}
}
}

impl Serializable for AccountHeader {
fn write_into<W: vm_core::utils::ByteWriter>(&self, target: &mut W) {
self.id.write_into(target);
self.nonce.write_into(target);
self.vault_root.write_into(target);
self.storage_commitment.write_into(target);
self.code_commitment.write_into(target);
}
}

impl Deserializable for AccountHeader {
fn read_from<R: vm_core::utils::ByteReader>(
source: &mut R,
) -> Result<Self, vm_processor::DeserializationError> {
let id = AccountId::read_from(source)?;
let nonce = Felt::read_from(source)?;
let vault_root = Digest::read_from(source)?;
let storage_commitment = Digest::read_from(source)?;
let code_commitment = Digest::read_from(source)?;

Ok(AccountHeader {
id,
nonce,
vault_root,
storage_commitment,
code_commitment,
})
}
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use vm_core::{
utils::{Deserializable, Serializable},
Felt,
};

use super::AccountHeader;
use crate::{
accounts::{tests::build_account, StorageSlot},
assets::FungibleAsset,
};

#[test]
fn test_serde_account_storage() {
let init_nonce = Felt::new(1);
let asset_0 = FungibleAsset::mock(99);
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let storage_slot = StorageSlot::Value(word);
let account = build_account(vec![asset_0], init_nonce, vec![storage_slot]);

let account_header: AccountHeader = account.into();

let header_bytes = account_header.to_bytes();
let deserialized_header = AccountHeader::read_from_bytes(&header_bytes).unwrap();
assert_eq!(deserialized_header, account_header);
}
}
Loading