1
- // FORK FROM: https://github.com/bluealloy/revm/blob/4e24be4 /crates/database/src/alloydb.rs
1
+ // FORK FROM: https://github.com/bluealloy/revm/blob/389be74 /crates/database/src/alloydb.rs
2
2
3
3
use crate :: alloy_db:: async_db:: DatabaseAsyncRef ;
4
4
pub use alloy_eips:: BlockId ;
5
- use alloy_network:: primitives:: HeaderResponse ;
6
5
use alloy_primitives:: { Address , B256 , U256 } ;
7
- use alloy_provider:: network:: primitives:: BlockTransactionsKind ;
8
- use alloy_provider:: network:: BlockResponse ;
9
- use alloy_provider:: { Network , Provider } ;
6
+ use alloy_provider:: {
7
+ network:: {
8
+ primitives:: { BlockTransactionsKind , HeaderResponse } ,
9
+ BlockResponse ,
10
+ } ,
11
+ Network , Provider ,
12
+ } ;
10
13
use alloy_transport:: TransportError ;
11
- use revm:: primitives:: { AccountInfo , Bytecode } ;
14
+ use core:: error:: Error ;
15
+ use revm_database:: DBErrorMarker ;
16
+ use revm_state:: { AccountInfo , Bytecode } ;
17
+ use std:: fmt:: Display ;
12
18
13
- /// An alloy-powered REVM [revm::Database].
19
+ #[ derive( Debug ) ]
20
+ pub struct DBTransportError ( pub TransportError ) ;
21
+
22
+ impl DBErrorMarker for DBTransportError { }
23
+
24
+ impl Display for DBTransportError {
25
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
26
+ write ! ( f, "Transport error: {}" , self . 0 )
27
+ }
28
+ }
29
+
30
+ impl Error for DBTransportError { }
31
+
32
+ impl From < TransportError > for DBTransportError {
33
+ fn from ( e : TransportError ) -> Self {
34
+ Self ( e)
35
+ }
36
+ }
37
+
38
+ /// An alloy-powered REVM [Database][database_interface::Database].
14
39
///
15
40
/// When accessing the database, it'll use the given provider to fetch the corresponding account's data.
16
41
#[ derive( Debug ) ]
@@ -19,23 +44,23 @@ pub struct AlloyDBFork<N: Network, P: Provider<N>> {
19
44
provider : P ,
20
45
/// The block number on which the queries will be based on.
21
46
block_number : BlockId ,
22
- _n : std :: marker:: PhantomData < N > ,
47
+ _marker : core :: marker:: PhantomData < fn ( ) -> N > ,
23
48
}
24
49
25
50
impl < N : Network , P : Provider < N > > AlloyDBFork < N , P > {
26
- /// Create a new AlloyDB instance, with a [Provider] and a block.
51
+ /// Creates a new AlloyDBFork instance, with a [Provider] and a block.
27
52
pub fn new ( provider : P , block_number : BlockId ) -> Self {
28
- Self { provider, block_number, _n : std :: marker:: PhantomData }
53
+ Self { provider, block_number, _marker : core :: marker:: PhantomData }
29
54
}
30
55
31
- /// Set the block number on which the queries will be based on.
56
+ /// Sets the block number on which the queries will be based on.
32
57
pub fn set_block_number ( & mut self , block_number : BlockId ) {
33
58
self . block_number = block_number;
34
59
}
35
60
}
36
61
37
62
impl < N : Network , P : Provider < N > > DatabaseAsyncRef for AlloyDBFork < N , P > {
38
- type Error = TransportError ;
63
+ type Error = DBTransportError ;
39
64
40
65
async fn basic_async_ref ( & self , address : Address ) -> Result < Option < AccountInfo > , Self :: Error > {
41
66
let nonce = self . provider . get_transaction_count ( address) . block_id ( self . block_number ) ;
@@ -56,7 +81,8 @@ impl<N: Network, P: Provider<N>> DatabaseAsyncRef for AlloyDBFork<N, P> {
56
81
let block = self
57
82
. provider
58
83
// SAFETY: We know number <= u64::MAX, so we can safely convert it to u64
59
- . get_block_by_number ( number. into ( ) , BlockTransactionsKind :: Hashes )
84
+ . get_block_by_number ( number. into ( ) )
85
+ . kind ( BlockTransactionsKind :: Hashes )
60
86
. await ?;
61
87
// SAFETY: If the number is given, the block is supposed to be finalized, so unwrapping is safe.
62
88
Ok ( B256 :: new ( * block. unwrap ( ) . header ( ) . hash ( ) ) )
@@ -68,28 +94,28 @@ impl<N: Network, P: Provider<N>> DatabaseAsyncRef for AlloyDBFork<N, P> {
68
94
}
69
95
70
96
async fn storage_async_ref ( & self , address : Address , index : U256 ) -> Result < U256 , Self :: Error > {
71
- self . provider . get_storage_at ( address, index) . block_id ( self . block_number ) . await
97
+ Ok ( self . provider . get_storage_at ( address, index) . block_id ( self . block_number ) . await ? )
72
98
}
73
99
}
74
100
75
101
#[ cfg( test) ]
76
102
mod tests {
77
103
use super :: * ;
78
- use crate :: alloy_db:: async_db:: WrapDatabaseAsync ;
79
- use alloy_primitives:: address;
80
- use alloy_primitives:: ruint:: __private:: ruint_macro:: uint;
104
+ use crate :: alloy_db:: WrapDatabaseAsync ;
81
105
use alloy_provider:: ProviderBuilder ;
82
- use revm :: DatabaseRef ;
106
+ use revm_database :: DatabaseRef ;
83
107
84
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
85
- async fn can_get_basic ( ) {
86
- let client = ProviderBuilder :: new ( ) . on_http ( "https://eth.merkle.io" . parse ( ) . unwrap ( ) ) ;
87
- let alloydb = AlloyDBFork :: new ( client, BlockId :: number ( 16148323 ) ) ;
108
+ #[ test]
109
+ #[ ignore = "flaky RPC" ]
110
+ fn can_get_basic ( ) {
111
+ let client = ProviderBuilder :: new ( ) . on_http ( "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27" . parse ( ) . unwrap ( ) ) ;
112
+ let alloydb = AlloyDBFork :: new ( client, BlockId :: from ( 16148323 ) ) ;
88
113
let wrapped_alloydb = WrapDatabaseAsync :: new ( alloydb) . unwrap ( ) ;
89
114
90
- let acc_info = wrapped_alloydb. basic_ref ( address ! ( "220866b1a2219f40e72f5c628b65d54268ca3a9d" ) ) . unwrap ( ) . unwrap ( ) ;
115
+ // ETH/USDT pair on Uniswap V2
116
+ let address: Address = "0x0d4a11d5EEaaC28EC3F61d100daF4d40471f1852" . parse ( ) . unwrap ( ) ;
117
+
118
+ let acc_info = wrapped_alloydb. basic_ref ( address) . unwrap ( ) . unwrap ( ) ;
91
119
assert ! ( acc_info. exists( ) ) ;
92
- assert_eq ! ( acc_info. nonce, 1 ) ;
93
- assert_eq ! ( acc_info. balance, uint!( 250001010477701567100010_ U256 ) ) ;
94
120
}
95
121
}
0 commit comments