1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use super::{hash_account, Account, AccountId, Digest, Felt};

// ACCOUNT STUB
// ================================================================================================

/// A stub of an account which contains information that succinctly describes the state of the
/// components of the account.
///
/// The [AccountStub] is composed of:
/// - id: the account id ([AccountId]) of the account.
/// - nonce: the nonce of the account.
/// - vault_root: a commitment to the account's vault ([AccountVault]).
/// - storage_root: accounts storage root ([AccountStorage]).
/// - code_root: a commitment to the account's code ([AccountCode]).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct AccountStub {
    id: AccountId,
    nonce: Felt,
    vault_root: Digest,
    storage_root: Digest,
    code_root: Digest,
}

impl AccountStub {
    // CONSTRUCTORS
    // --------------------------------------------------------------------------------------------
    /// Creates a new [AccountStub].
    pub fn new(
        id: AccountId,
        nonce: Felt,
        vault_root: Digest,
        storage_root: Digest,
        code_root: Digest,
    ) -> Self {
        Self {
            id,
            nonce,
            vault_root,
            storage_root,
            code_root,
        }
    }

    // PUBLIC ACCESSORS
    // --------------------------------------------------------------------------------------------
    /// Returns hash of this account.
    ///
    /// Hash of an account is computed as hash(id, nonce, vault_root, storage_root, code_root).
    /// Computing the account hash requires 2 permutations of the hash function.
    pub fn hash(&self) -> Digest {
        hash_account(self.id, self.nonce, self.vault_root, self.storage_root, self.code_root)
    }

    /// Returns the id of this account.
    pub fn id(&self) -> AccountId {
        self.id
    }

    /// Returns the nonce of this account.
    pub fn nonce(&self) -> Felt {
        self.nonce
    }

    /// Returns the vault root of this account.
    pub fn vault_root(&self) -> Digest {
        self.vault_root
    }

    /// Returns the storage root of this account.
    pub fn storage_root(&self) -> Digest {
        self.storage_root
    }

    /// Returns the code root of this account.
    pub fn code_root(&self) -> Digest {
        self.code_root
    }
}

impl From<Account> for AccountStub {
    fn from(account: Account) -> Self {
        (&account).into()
    }
}

impl From<&Account> for AccountStub {
    fn from(account: &Account) -> Self {
        Self {
            id: account.id(),
            nonce: account.nonce(),
            vault_root: account.vault().commitment(),
            storage_root: account.storage().root(),
            code_root: account.code().root(),
        }
    }
}