forked from foundry-rs/foundry
-
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
6bc93a2
draft chainspec wrapper
re-gius 38abb13
inject Anvil Node Config into Substrate CLI
re-gius 9b33fb1
Set chain_id from config.json
re-gius 30717d7
Add extra fields
re-gius 66ee492
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 85527e4
inject storage items into ChainSpec
re-gius b4420d0
implement ChainSpec trait for wrapper
re-gius b592f9a
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius f807562
fix clippy
re-gius 2ff0438
CR reorg
re-gius 2302d04
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 56b192f
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 26e0fe7
set System::Number as u32
re-gius 7caa8a5
nit
re-gius d513064
nit
re-gius 5e80c89
Add genesis timestamp to time manager of mining node
re-gius 7bafd06
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 9d1a9d3
add custom genesis block number support in RPC
re-gius ce424ee
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 16d17dc
fmt
re-gius 0897f31
fmt
re-gius 43e35d4
clippy + comments
re-gius 329a139
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius ce9de23
CR nits + reorg
re-gius 6e67186
Add genesis integration tests (chain ID missing)
re-gius e2b2072
comment nit
re-gius 3ee3857
add chain id test
re-gius 3bd0395
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 90da646
fix time manager creation
re-gius 7221e82
changed genesis milliseconds mismatch + clippy for unused import
re-gius 867d680
undo clippy change
re-gius 327b973
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 7680f00
merge fix - missing import
re-gius fcf200b
fix rpc client creation
re-gius 5e42322
clippy nit
re-gius c51999b
fix
re-gius f8ba53b
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 560962e
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius e1abde2
CR fixes
re-gius c79e41b
fix types
re-gius c36ee17
nit
re-gius 10b6b83
Use genesis block number to compute genesis hash
re-gius c5c7f47
improve error handling
re-gius 4730f8e
fix metadata retrieval
alindima b3f7914
use the impersonation executor
alindima 6a94b1a
use latest available metadata + error handling
re-gius 8fa9e97
add Chain Id RPC
re-gius 8a9689a
nit
re-gius 11493c5
Merge branch 'master' into re-gius/chainspec-wrapper
re-gius 5ae3b58
fmt + clippy
re-gius 8915d00
add `InternalError` variant
re-gius e7cdd52
make test less flaky
re-gius 986fd16
fix test with chain id RPC
re-gius a8ae12c
Use chain id RPC in genesis tests
re-gius 88009ff
fix deny license
re-gius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| //! Genesis settings | ||
|
|
||
| use crate::config::AnvilNodeConfig; | ||
| use alloy_genesis::GenesisAccount; | ||
| use alloy_primitives::Address; | ||
| use codec::Encode; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| // Hex-encode key: 0x9527366927478e710d3f7fb77c6d1f89 | ||
| pub const CHAIN_ID_KEY: [u8; 16] = [ | ||
| 149u8, 39u8, 54u8, 105u8, 39u8, 71u8, 142u8, 113u8, 13u8, 63u8, 127u8, 183u8, 124u8, 109u8, | ||
| 31u8, 137u8, | ||
| ]; | ||
|
|
||
| // Hex-encode key: 0xf0c365c3cf59d671eb72da0e7a4113c49f1f0515f462cdcf84e0f1d6045dfcbb | ||
| // twox_128(b"Timestamp") ++ twox_128(b"Now") | ||
| // corresponds to `Timestamp::Now` storage item in pallet-timestamp | ||
| pub const TIMESTAMP_KEY: [u8; 32] = [ | ||
| 240u8, 195u8, 101u8, 195u8, 207u8, 89u8, 214u8, 113u8, 235u8, 114u8, 218u8, 14u8, 122u8, 65u8, | ||
| 19u8, 196u8, 159u8, 31u8, 5u8, 21u8, 244u8, 98u8, 205u8, 207u8, 132u8, 224u8, 241u8, 214u8, | ||
| 4u8, 93u8, 252u8, 187u8, | ||
| ]; | ||
|
|
||
| // Hex-encode key: 0x26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac | ||
| // twox_128(b"System") ++ twox_128(b"Number") | ||
| // corresponds to `System::Number` storage item in pallet-system | ||
| pub const BLOCK_NUMBER_KEY: [u8; 32] = [ | ||
| 38u8, 170u8, 57u8, 78u8, 234u8, 86u8, 48u8, 224u8, 124u8, 72u8, 174u8, 12u8, 149u8, 88u8, | ||
| 206u8, 247u8, 2u8, 165u8, 193u8, 177u8, 154u8, 183u8, 160u8, 79u8, 83u8, 108u8, 81u8, 154u8, | ||
| 202u8, 73u8, 131u8, 172u8, | ||
| ]; | ||
|
|
||
| /// Genesis settings | ||
| #[derive(Clone, Debug, Default)] | ||
| pub struct GenesisConfig { | ||
| /// The chain id of the Substrate chain. | ||
| pub chain_id: u64, | ||
| /// The initial timestamp for the genesis block | ||
| pub timestamp: u64, | ||
| /// The genesis block author address. | ||
| pub coinbase: Option<Address>, | ||
alindima marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// All accounts that should be initialised at genesis with their info. | ||
| pub alloc: Option<BTreeMap<Address, GenesisAccount>>, | ||
| /// The initial number for the genesis block | ||
| pub number: u64, | ||
| /// The genesis header base fee | ||
| pub base_fee_per_gas: u64, | ||
| /// The genesis header gas limit. | ||
| pub gas_limit: Option<u128>, | ||
| } | ||
|
|
||
| impl From<AnvilNodeConfig> for GenesisConfig { | ||
alindima marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn from(anvil_config: AnvilNodeConfig) -> Self { | ||
| Self { | ||
| chain_id: anvil_config.get_chain_id(), | ||
| timestamp: anvil_config.get_genesis_timestamp(), | ||
| coinbase: anvil_config.genesis.as_ref().map(|g| g.coinbase), | ||
| alloc: anvil_config.genesis.as_ref().map(|g| g.alloc.clone()), | ||
| number: anvil_config.get_genesis_number(), | ||
| base_fee_per_gas: anvil_config.get_base_fee(), | ||
| gas_limit: anvil_config.gas_limit, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl GenesisConfig { | ||
| pub fn as_storage_key_value(&self) -> Vec<(Vec<u8>, Vec<u8>)> { | ||
| let storage = vec![ | ||
| (CHAIN_ID_KEY.to_vec(), self.chain_id.encode()), | ||
| (TIMESTAMP_KEY.to_vec(), self.timestamp.encode()), | ||
| (BLOCK_NUMBER_KEY.to_vec(), self.number.encode()), | ||
| ]; | ||
| // TODO: add other fields | ||
| storage | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use sp_io::hashing::twox_128; | ||
|
|
||
| #[test] | ||
| fn test_number_storage_key() { | ||
| let system_hash = twox_128(b"System"); | ||
| let number_hash = twox_128(b"Number"); | ||
| let mut concatenated_number_hash = [0u8; 32]; | ||
| concatenated_number_hash[..16].copy_from_slice(&system_hash); | ||
| concatenated_number_hash[16..].copy_from_slice(&number_hash); | ||
| assert_eq!(BLOCK_NUMBER_KEY, concatenated_number_hash); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_timestamp_storage_key() { | ||
| let timestamp_hash = twox_128(b"Timestamp"); | ||
| let now_hash = twox_128(b"Now"); | ||
| let mut concatenated_timestamp_hash = [0u8; 32]; | ||
| concatenated_timestamp_hash[..16].copy_from_slice(×tamp_hash); | ||
| concatenated_timestamp_hash[16..].copy_from_slice(&now_hash); | ||
| assert_eq!(TIMESTAMP_KEY, concatenated_timestamp_hash); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.