Skip to content

Commit 211cbf0

Browse files
committed
Add example/test
1 parent b8a9a18 commit 211cbf0

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# Alloy Reth State DB provider
2-
To test reth ExEx, it can be useful to not be dependent on a real database. This repository illustrates how the reth traits for the state can be implemented to forward the request to an RPC.
2+
This repository illustrates how the `StateProviderFactory` from [reth](https://github.com/paradigmxyz/reth) can be implemented with [alloy-rs](https://github.com/alloy-rs/alloy) to fetch all state using RPC. This can be useful to not be dependent on a real database when testing reth ExEx.
33

44
## Remarks
55
There is no `debug_codeByHash` implemented currently (see [feat(rpc): debug_codeByHash #14479](https://github.com/paradigmxyz/reth/issues/14479)). But since revm will first call `basic_account`, we can cache it and return it when `bytecode_by_hash` is called.
66

77
Currently, this repos purpose as copy/paste reference and not as library. Not all functions are implemented.
88

9+
## Example
10+
See tests in `src/alloy_reth_provider.rs` for an example.
11+
12+
```rust
13+
// Init the provider
14+
let provider = ProviderBuilder::new().on_http("https://eth.merkle.io".parse().unwrap());
15+
// Init the db provider
16+
let db_provider = AlloyRethProvider::new(provider.clone());
17+
// Use the StateProviderFactory
18+
let state = db_provider.state_by_block_id(BlockId::number(16148323)).unwrap();
19+
```
20+
921
## Acknowledgements
1022
Many, many thanks to the team of [reth](https://github.com/paradigmxyz/reth) and [alloy-rs](https://github.com/alloy-rs/alloy) for the awesome work they do. Some parts of the trait implementation are taken from reth. Also, many thanks to [revm](https://github.com/bluealloy/revm). The `alloy_db` part is a copy/paste from revm because this part is not included in the latest revm version, and it makes it easier to be in sync with the latest Alloy version.
1123

src/alloy_db/alloy_db_fork.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,18 @@ impl<N: Network, P: Provider<N>> DatabaseAsyncRef for AlloyDBFork<N, P> {
7676
mod tests {
7777
use super::*;
7878
use crate::alloy_db::async_db::WrapDatabaseAsync;
79+
use alloy_primitives::address;
7980
use alloy_primitives::ruint::__private::ruint_macro::uint;
8081
use alloy_provider::ProviderBuilder;
8182
use revm::DatabaseRef;
8283

8384
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
8485
async fn can_get_basic() {
8586
let client = ProviderBuilder::new().on_http("https://eth.merkle.io".parse().unwrap());
86-
let alloydb = AlloyDBFork::new(client, BlockId::from(16148323));
87+
let alloydb = AlloyDBFork::new(client, BlockId::number(16148323));
8788
let wrapped_alloydb = WrapDatabaseAsync::new(alloydb).unwrap();
8889

89-
let address: Address = "0x220866b1a2219f40e72f5c628b65d54268ca3a9d".parse().unwrap();
90-
91-
let acc_info = wrapped_alloydb.basic_ref(address).unwrap().unwrap();
90+
let acc_info = wrapped_alloydb.basic_ref(address!("220866b1a2219f40e72f5c628b65d54268ca3a9d")).unwrap().unwrap();
9291
assert!(acc_info.exists());
9392
assert_eq!(acc_info.nonce, 1);
9493
assert_eq!(acc_info.balance, uint!(250001010477701567100010_U256));

src/alloy_reth_provider.rs

+21
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,24 @@ where
363363
todo!()
364364
}
365365
}
366+
367+
#[cfg(test)]
368+
mod tests {
369+
use super::*;
370+
use alloy_eips::BlockId;
371+
use alloy_primitives::address;
372+
use alloy_provider::ProviderBuilder;
373+
use reth_provider::AccountReader;
374+
use ruint::__private::ruint_macro::uint;
375+
376+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
377+
async fn test_alloy_reth_state_provider_factory() {
378+
let provider = ProviderBuilder::new().on_http("https://eth.merkle.io".parse().unwrap());
379+
let db_provider = AlloyRethProvider::new(provider.clone());
380+
let state = db_provider.state_by_block_id(BlockId::number(16148323)).unwrap();
381+
let acc_info = state.basic_account(&address!("220866b1a2219f40e72f5c628b65d54268ca3a9d")).unwrap().unwrap();
382+
383+
assert_eq!(acc_info.nonce, 1);
384+
assert_eq!(acc_info.balance, uint!(250001010477701567100010_U256));
385+
}
386+
}

0 commit comments

Comments
 (0)