revm_state/bal/
alloy.rs

1//! Alloy BAL types conversions.
2
3// Re-export Alloy BAL types.
4pub use alloy_eip7928::{
5    BalanceChange as AlloyBalanceChange, BlockAccessList as AlloyBal,
6    CodeChange as AlloyCodeChange, NonceChange as AlloyNonceChange,
7    StorageChange as AlloyStorageChange,
8};
9
10use crate::bal::{AccountBal, Bal, BalWrites};
11use bytecode::{Bytecode, BytecodeDecodeError};
12use primitives::{IndexMap, B256, U256};
13use std::vec::Vec;
14
15impl TryFrom<AlloyBal> for Bal {
16    type Error = BytecodeDecodeError;
17
18    fn try_from(alloy_bal: AlloyBal) -> Result<Self, Self::Error> {
19        let accounts = IndexMap::from_iter(
20            alloy_bal
21                .into_iter()
22                .map(AccountBal::try_from_alloy)
23                .collect::<Result<Vec<_>, _>>()?,
24        );
25
26        Ok(Self { accounts })
27    }
28}
29
30impl From<Vec<AlloyBalanceChange>> for BalWrites<U256> {
31    fn from(value: Vec<AlloyBalanceChange>) -> Self {
32        Self {
33            writes: value
34                .into_iter()
35                .map(|change| (change.block_access_index, change.post_balance))
36                .collect(),
37        }
38    }
39}
40
41impl From<Vec<AlloyNonceChange>> for BalWrites<u64> {
42    fn from(value: Vec<AlloyNonceChange>) -> Self {
43        Self {
44            writes: value
45                .into_iter()
46                .map(|change| (change.block_access_index, change.new_nonce))
47                .collect(),
48        }
49    }
50}
51
52impl From<Vec<AlloyStorageChange>> for BalWrites<U256> {
53    fn from(value: Vec<AlloyStorageChange>) -> Self {
54        Self {
55            writes: value
56                .into_iter()
57                .map(|change| (change.block_access_index, change.new_value))
58                .collect(),
59        }
60    }
61}
62
63impl TryFrom<Vec<AlloyCodeChange>> for BalWrites<(B256, Bytecode)> {
64    type Error = BytecodeDecodeError;
65
66    fn try_from(value: Vec<AlloyCodeChange>) -> Result<Self, Self::Error> {
67        Ok(Self {
68            writes: value
69                .into_iter()
70                .map(|change| {
71                    // convert bytes to bytecode.
72                    Bytecode::new_raw_checked(change.new_code).map(|bytecode| {
73                        let hash = bytecode.hash_slow();
74                        (change.block_access_index, (hash, bytecode))
75                    })
76                })
77                .collect::<Result<Vec<_>, Self::Error>>()?,
78        })
79    }
80}