-
Notifications
You must be signed in to change notification settings - Fork 6
Create a ChainSpec Wrapper to manage Anvil genesis config #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 36 commits
6bc93a2
38abb13
9b33fb1
30717d7
66ee492
85527e4
b4420d0
b592f9a
f807562
2ff0438
2302d04
56b192f
26e0fe7
7caa8a5
d513064
5e80c89
7bafd06
9d1a9d3
ce424ee
16d17dc
0897f31
43e35d4
329a139
ce9de23
6e67186
e2b2072
3ee3857
3bd0395
90da646
7221e82
867d680
327b973
7680f00
fcf200b
5e42322
c51999b
f8ba53b
560962e
e1abde2
c79e41b
c36ee17
10b6b83
c5c7f47
4730f8e
b3f7914
6a94b1a
8fa9e97
8a9689a
11493c5
5ae3b58
8915d00
e7cdd52
986fd16
a8ae12c
88009ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ use alloy_rpc_types::{TransactionRequest, anvil::MineOptions}; | |
| use alloy_serde::WithOtherFields; | ||
| use anvil_core::eth::{EthRequest, Params as MineParams}; | ||
| use anvil_rpc::response::ResponseResult; | ||
| use codec::Decode; | ||
| use futures::{StreamExt, channel::mpsc}; | ||
| use polkadot_sdk::{ | ||
| pallet_revive::evm::{Account, Block, Bytes, ReceiptInfo}, | ||
|
|
@@ -26,12 +27,19 @@ use polkadot_sdk::{ | |
| client::{Client as EthRpcClient, ClientError, SubscriptionType}, | ||
| subxt_client::{self, SrcChainConfig}, | ||
| }, | ||
| sp_core::{self, keccak_256}, | ||
| sc_client_api::StorageProvider, | ||
| sc_executor::WasmExecutor, | ||
| sc_runtime_utilities::fetch_latest_metadata_from_code_blob, | ||
| sp_core::{self, keccak_256, storage::StorageKey}, | ||
| sp_io::SubstrateHostFunctions, | ||
| sp_runtime::Cow, | ||
| sp_storage::well_known_keys::CODE as CODE_KEY, | ||
| }; | ||
| use sqlx::sqlite::SqlitePoolOptions; | ||
| use std::{sync::Arc, time::Duration}; | ||
| use subxt::{ | ||
| OnlineClient, backend::rpc::RpcClient, config::substrate::H256, | ||
| Metadata as SubxtMetadata, OnlineClient, backend::rpc::RpcClient, | ||
| client::RuntimeVersion as SubxtRuntimeVersion, config::substrate::H256, | ||
| ext::subxt_rpcs::LegacyRpcMethods, utils::H160, | ||
| }; | ||
|
|
||
|
|
@@ -411,7 +419,40 @@ impl ApiServer { | |
|
|
||
| async fn create_revive_rpc_client(substrate_service: &Service) -> Result<EthRpcClient> { | ||
| let rpc_client = RpcClient::new(InMemoryRpcClient(substrate_service.rpc_handlers.clone())); | ||
| let api = OnlineClient::<SrcChainConfig>::from_rpc_client(rpc_client.clone()).await?; | ||
|
|
||
| // Using best_hash because genesis hash is only set if genesis block number is equal to 0, but | ||
| // Anvil allows a custom genesis block number. | ||
| // https://github.com/paritytech/polkadot-sdk/blob/62c3a80f913a272c4f5dba2c91320056d39ec68e/substrate/client/api/src/in_mem.rs#L178 | ||
| let genesis_hash = substrate_service.client.chain_info().best_hash; | ||
|
|
||
| let runtime_version = substrate_service | ||
| .client | ||
| .runtime_version_at(genesis_hash) | ||
| .expect("Runtime version not found for given genesis hash"); | ||
|
||
| let subxt_runtime_version = SubxtRuntimeVersion { | ||
| spec_version: runtime_version.spec_version, | ||
| transaction_version: runtime_version.transaction_version, | ||
| }; | ||
|
|
||
| let code_bytes = substrate_service | ||
| .client | ||
| .storage(genesis_hash, &StorageKey(CODE_KEY.to_vec())) | ||
| .expect("Runtime code not found for given genesis hash") | ||
| .unwrap(); | ||
|
||
| let opaque_metadata = fetch_latest_metadata_from_code_blob( | ||
| &WasmExecutor::<SubstrateHostFunctions>::builder().build(), | ||
| Cow::Borrowed(code_bytes.0.as_slice()), | ||
| ) | ||
| .map_err(|_| Error::InvalidParams("Unable to fetch metadata".to_string()))?; | ||
| let subxt_metadata = SubxtMetadata::decode(&mut (*opaque_metadata).as_slice()) | ||
| .map_err(|_| Error::InvalidParams("Unable to decode metadata".to_string()))?; | ||
|
|
||
| let api = OnlineClient::<SrcChainConfig>::from_rpc_client_with( | ||
| genesis_hash, | ||
| subxt_runtime_version, | ||
| subxt_metadata, | ||
| rpc_client.clone(), | ||
| )?; | ||
alindima marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let rpc = LegacyRpcMethods::<SrcChainConfig>::new(rpc_client.clone()); | ||
|
|
||
| let block_provider = SubxtBlockInfoProvider::new(api.clone(), rpc.clone()).await?; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.