kona_executor/db/
account.rs

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
//! This module contains the [TrieAccount] struct.

use alloy_primitives::{B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use revm::primitives::{Account, AccountInfo};

/// An Ethereum account as represented in the trie.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
pub struct TrieAccount {
    /// Account nonce.
    pub nonce: u64,
    /// Account balance.
    pub balance: U256,
    /// Account's storage root.
    pub storage_root: B256,
    /// Hash of the account's bytecode.
    pub code_hash: B256,
}

impl From<(Account, B256)> for TrieAccount {
    fn from((account, storage_root): (Account, B256)) -> Self {
        Self {
            nonce: account.info.nonce,
            balance: account.info.balance,
            storage_root,
            code_hash: account.info.code_hash,
        }
    }
}

impl From<(AccountInfo, B256)> for TrieAccount {
    fn from((account, storage_root): (AccountInfo, B256)) -> Self {
        Self {
            nonce: account.nonce,
            balance: account.balance,
            storage_root,
            code_hash: account.code_hash,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::uint;

    #[test]
    fn test_trie_account_from_account() {
        let account = Account {
            info: AccountInfo {
                nonce: 1,
                balance: uint!(2_U256),
                code_hash: B256::default(),
                code: Default::default(),
            },
            status: Default::default(),
            storage: Default::default(),
        };
        let storage_root = B256::default();
        let trie_account = TrieAccount::from((account, storage_root));
        assert_eq!(trie_account.nonce, 1);
        assert_eq!(trie_account.balance, uint!(2_U256));
        assert_eq!(trie_account.storage_root, B256::default());
        assert_eq!(trie_account.code_hash, B256::default());
    }

    #[test]
    fn test_trie_account_from_account_info() {
        let account_info = AccountInfo {
            nonce: 1,
            balance: uint!(2_U256),
            code_hash: B256::default(),
            code: Default::default(),
        };
        let storage_root = B256::default();
        let trie_account = TrieAccount::from((account_info, storage_root));
        assert_eq!(trie_account.nonce, 1);
        assert_eq!(trie_account.balance, uint!(2_U256));
        assert_eq!(trie_account.storage_root, B256::default());
        assert_eq!(trie_account.code_hash, B256::default());
    }
}