miden_objects/account/
partial.rs

1use vm_core::{
2    Felt,
3    utils::{Deserializable, Serializable},
4};
5
6use super::{Account, AccountCode, AccountId, PartialStorage};
7use crate::asset::PartialVault;
8
9/// A partial representation of an account.
10///
11/// A partial account is used as inputs to the transaction kernel and contains only the essential
12/// data needed for verification and transaction processing without requiring the full account
13/// state.
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct PartialAccount {
16    /// The ID for the partial account
17    id: AccountId,
18    /// The current transaction nonce of the account
19    nonce: Felt,
20    /// Account code
21    account_code: AccountCode,
22    /// Partial representation of the account's storage, containing the storage commitment and
23    /// proofs for specific storage slots that need to be accessed
24    partial_storage: PartialStorage,
25    /// Partial representation of the account's vault, containing the vault root and necessary
26    /// proof information for asset verification
27    partial_vault: PartialVault,
28}
29
30impl PartialAccount {
31    /// Creates a new instance of a partial account with the specified components.
32    pub fn new(
33        id: AccountId,
34        nonce: Felt,
35        account_code: AccountCode,
36        partial_storage: PartialStorage,
37        partial_vault: PartialVault,
38    ) -> Self {
39        Self {
40            id,
41            nonce,
42            account_code,
43            partial_storage,
44            partial_vault,
45        }
46    }
47
48    /// Returns the account's unique identifier.
49    pub fn id(&self) -> AccountId {
50        self.id
51    }
52
53    /// Returns the account's current nonce value.
54    pub fn nonce(&self) -> Felt {
55        self.nonce
56    }
57
58    /// Returns a reference to the account code.
59    pub fn code(&self) -> &AccountCode {
60        &self.account_code
61    }
62
63    /// Returns a reference to the partial storage representation of the account.
64    pub fn storage(&self) -> &PartialStorage {
65        &self.partial_storage
66    }
67
68    /// Returns a reference to the partial vault representation of the account.
69    pub fn vault(&self) -> &PartialVault {
70        &self.partial_vault
71    }
72}
73
74impl From<Account> for PartialAccount {
75    fn from(account: Account) -> Self {
76        PartialAccount::from(&account)
77    }
78}
79
80impl From<&Account> for PartialAccount {
81    fn from(account: &Account) -> Self {
82        PartialAccount::new(
83            account.id(),
84            account.nonce(),
85            account.code().clone(),
86            account.storage().into(),
87            account.vault().into(),
88        )
89    }
90}
91
92impl Serializable for PartialAccount {
93    fn write_into<W: vm_core::utils::ByteWriter>(&self, target: &mut W) {
94        target.write(self.id);
95        target.write(self.nonce);
96        target.write(&self.account_code);
97        target.write(&self.partial_storage);
98        target.write(&self.partial_vault);
99    }
100}
101
102impl Deserializable for PartialAccount {
103    fn read_from<R: vm_core::utils::ByteReader>(
104        source: &mut R,
105    ) -> Result<Self, vm_processor::DeserializationError> {
106        let account_id = source.read()?;
107        let nonce = source.read()?;
108        let account_code = source.read()?;
109        let partial_storage = source.read()?;
110        let partial_vault = source.read()?;
111
112        Ok(PartialAccount {
113            id: account_id,
114            nonce,
115            account_code,
116            partial_storage,
117            partial_vault,
118        })
119    }
120}