1
1
//! Helper types for working with [revm](foundry_evm::revm)
2
2
3
3
use crate :: { mem:: state:: trie_hash_db, revm:: primitives:: AccountInfo , U256 } ;
4
+ use alloy_primitives:: { Address as B160 , B256 , U256 as rU256} ;
4
5
use anvil_core:: eth:: trie:: KeccakHasher ;
5
6
use ethers:: {
6
7
prelude:: { Address , Bytes } ,
@@ -15,9 +16,10 @@ use foundry_evm::{
15
16
} ,
16
17
revm:: {
17
18
db:: { CacheDB , DbAccount } ,
18
- primitives:: { Bytecode , B160 , B256 , KECCAK_EMPTY , U256 as rU256 } ,
19
+ primitives:: { Bytecode , KECCAK_EMPTY } ,
19
20
Database , DatabaseCommit ,
20
21
} ,
22
+ utils:: { h160_to_b160, h256_to_b256, u256_to_ru256} ,
21
23
HashMap ,
22
24
} ;
23
25
use hash_db:: HashDB ;
@@ -84,30 +86,30 @@ pub trait Db:
84
86
85
87
/// Sets the nonce of the given address
86
88
fn set_nonce ( & mut self , address : Address , nonce : u64 ) -> DatabaseResult < ( ) > {
87
- let mut info = self . basic ( address . into ( ) ) ?. unwrap_or_default ( ) ;
89
+ let mut info = self . basic ( h160_to_b160 ( address ) ) ?. unwrap_or_default ( ) ;
88
90
info. nonce = nonce;
89
91
self . insert_account ( address, info) ;
90
92
Ok ( ( ) )
91
93
}
92
94
93
95
/// Sets the balance of the given address
94
96
fn set_balance ( & mut self , address : Address , balance : U256 ) -> DatabaseResult < ( ) > {
95
- let mut info = self . basic ( address . into ( ) ) ?. unwrap_or_default ( ) ;
96
- info. balance = balance . into ( ) ;
97
+ let mut info = self . basic ( h160_to_b160 ( address ) ) ?. unwrap_or_default ( ) ;
98
+ info. balance = u256_to_ru256 ( balance ) ;
97
99
self . insert_account ( address, info) ;
98
100
Ok ( ( ) )
99
101
}
100
102
101
103
/// Sets the balance of the given address
102
104
fn set_code ( & mut self , address : Address , code : Bytes ) -> DatabaseResult < ( ) > {
103
- let mut info = self . basic ( address . into ( ) ) ?. unwrap_or_default ( ) ;
105
+ let mut info = self . basic ( h160_to_b160 ( address ) ) ?. unwrap_or_default ( ) ;
104
106
let code_hash = if code. as_ref ( ) . is_empty ( ) {
105
107
KECCAK_EMPTY
106
108
} else {
107
109
B256 :: from_slice ( & keccak256 ( code. as_ref ( ) ) [ ..] )
108
110
} ;
109
111
info. code_hash = code_hash;
110
- info. code = Some ( Bytecode :: new_raw ( code. 0 ) . to_checked ( ) ) ;
112
+ info. code = Some ( Bytecode :: new_raw ( alloy_primitives :: Bytes ( code. 0 ) ) . to_checked ( ) ) ;
111
113
self . insert_account ( address, info) ;
112
114
Ok ( ( ) )
113
115
}
@@ -124,7 +126,7 @@ pub trait Db:
124
126
/// Deserialize and add all chain data to the backend storage
125
127
fn load_state ( & mut self , state : SerializableState ) -> DatabaseResult < bool > {
126
128
for ( addr, account) in state. accounts . into_iter ( ) {
127
- let old_account_nonce = DatabaseRef :: basic ( self , addr . into ( ) )
129
+ let old_account_nonce = DatabaseRef :: basic ( self , h160_to_b160 ( addr ) )
128
130
. ok ( )
129
131
. and_then ( |acc| acc. map ( |acc| acc. nonce ) )
130
132
. unwrap_or_default ( ) ;
@@ -135,12 +137,14 @@ pub trait Db:
135
137
self . insert_account (
136
138
addr,
137
139
AccountInfo {
138
- balance : account. balance . into ( ) ,
140
+ balance : u256_to_ru256 ( account. balance ) ,
139
141
code_hash : KECCAK_EMPTY , // will be set automatically
140
142
code : if account. code . 0 . is_empty ( ) {
141
143
None
142
144
} else {
143
- Some ( Bytecode :: new_raw ( account. code . 0 ) . to_checked ( ) )
145
+ Some (
146
+ Bytecode :: new_raw ( alloy_primitives:: Bytes ( account. code . 0 ) ) . to_checked ( ) ,
147
+ )
144
148
} ,
145
149
nonce,
146
150
} ,
@@ -176,15 +180,15 @@ pub trait Db:
176
180
/// [Backend::pending_block()](crate::eth::backend::mem::Backend::pending_block())
177
181
impl < T : DatabaseRef < Error = DatabaseError > + Send + Sync + Clone + fmt:: Debug > Db for CacheDB < T > {
178
182
fn insert_account ( & mut self , address : Address , account : AccountInfo ) {
179
- self . insert_account_info ( address . into ( ) , account)
183
+ self . insert_account_info ( h160_to_b160 ( address ) , account)
180
184
}
181
185
182
186
fn set_storage_at ( & mut self , address : Address , slot : U256 , val : U256 ) -> DatabaseResult < ( ) > {
183
- self . insert_account_storage ( address . into ( ) , slot . into ( ) , val . into ( ) )
187
+ self . insert_account_storage ( h160_to_b160 ( address ) , u256_to_ru256 ( slot ) , u256_to_ru256 ( val ) )
184
188
}
185
189
186
190
fn insert_block_hash ( & mut self , number : U256 , hash : H256 ) {
187
- self . block_hashes . insert ( number . into ( ) , hash . into ( ) ) ;
191
+ self . block_hashes . insert ( u256_to_ru256 ( number ) , h256_to_b256 ( hash ) ) ;
188
192
}
189
193
190
194
fn dump_state ( & self ) -> DatabaseResult < Option < SerializableState > > {
0 commit comments