-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: add support for Ethereum genesis.json initialization #2034
base: main
Are you sure you want to change the base?
Conversation
This PR adds support for initializing Stratus from a standard Ethereum genesis.json file. ## Features - Parse and load accounts from genesis.json - Support for EOA accounts with balance and nonce - Basic structure for contract accounts with code and storage - Automatic detection of genesis.json in the execution directory ## Implementation Details - Added new module `src/eth/genesis.rs` with structures to parse genesis.json - Integrated with existing reset_to_genesis functionality in stratus_storage.rs - Added support for hex and decimal balance parsing - Maintained camelCase field names for compatibility with standard Ethereum genesis format ## Limitations and Future Work - Contract code loading is implemented but not fully working yet - Contract storage is processed but not applied to the state - These limitations will be addressed in future PRs ## Usage Place a genesis.json file in the Stratus execution directory. When Stratus starts with the `dev` feature enabled, it will automatically detect and use this file to initialize the blockchain state. ## Testing An example genesis.json file is included in the PR. To test: 1. Place the genesis.json in the execution directory 2. Run `just stratus` to start Stratus with dev features 3. Verify account creation with: curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "latest"],"id":1}' http://localhost:3000
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
- Enhance genesis file loading with environment-specific and local config support - Improve error handling and hex string parsing for addresses and balances - Simplify account conversion logic with more robust parsing - Add test case for minimal genesis file loading - Remove unused imports and clean up code structure - Update storage reset to use improved genesis loading mechanism
src/eth/storage/stratus_storage.rs
Outdated
/// If a genesis.json file is available, it will be used. | ||
/// Otherwise, it will use the hardcoded genesis block and test accounts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove the hardcoded stuff and leave all this for the genesis.json. This would help validate the PR against all E2E tests as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine, although with the introduction of genesis.json, this stuff is not really "hardcoded" as much as it is a default value. What I do think you should do is implement Default
for GenesisConfig
and use that (the implementations should be different depending on weather the dev
feature flag is set).
src/eth/genesis.rs
Outdated
pub fn find_genesis_file(custom_path: Option<&str>) -> Option<PathBuf> { | ||
if let Some(path) = custom_path { | ||
let path_buf = PathBuf::from(path); | ||
if path_buf.exists() { | ||
return Some(path_buf); | ||
} | ||
} | ||
|
||
// Check for environment-specific genesis file | ||
if let Ok(env) = std::env::var("ENV") { | ||
let env_path = format!("config/genesis.{}.json", env.to_lowercase()); | ||
let env_path_buf = PathBuf::from(&env_path); | ||
if env_path_buf.exists() { | ||
return Some(env_path_buf); | ||
} | ||
} | ||
|
||
// Check for default local genesis file | ||
let local_path = PathBuf::from("config/genesis.local.json"); | ||
if local_path.exists() { | ||
return Some(local_path); | ||
} | ||
|
||
// Fallback to current directory | ||
let default_path = PathBuf::from("genesis.json"); | ||
if default_path.exists() { | ||
return Some(default_path); | ||
} | ||
|
||
None | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should accept an ENV VAR
for this genesis.json path. Something like GENESIS_JSON_PATH
. And not use ENV
related names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define this type of configuration in src/config.rs
(using clap)
src/eth/genesis.rs
Outdated
fn from_str_hex(hex: &str) -> Result<Self>; | ||
} | ||
|
||
impl FromStrHex for Address { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implement traits for primitive types in its declaration file (stratus/src/eth/primitives/address.rs
). Also, this conversion already exists, (although I guess we should have a docstring explaining that the FromStr
implementation for Address
converts from a hex string).
… default setup - Add GenesisFileConfig to support genesis.json path configuration via CLI and environment variable - Improve genesis file discovery in GenesisConfig with environment variable support - Implement default genesis configuration for dev and non-dev environments - Update Address parsing to use more robust hex decoding - Refactor genesis loading in StratusStorage to use default genesis config when no file is found - Add comprehensive tests for genesis configuration and path resolution
User description
This PR adds support for initializing Stratus from a standard Ethereum genesis.json file.
Features
Implementation Details
src/eth/genesis.rs
with structures to parse genesis.jsonLimitations and Future Work
Usage
Place a genesis.json file in the Stratus execution directory. When Stratus starts with the
dev
feature enabled, it will automatically detect and use this file to initialize the blockchain state.Testing
An example genesis.json file is included in the PR. To test:
just stratus
to start Stratus with dev featuresPR Type
Enhancement
Description
Add support for Ethereum genesis.json initialization
Parse and load accounts from genesis.json
Support EOA and contract accounts
Integrate with existing reset_to_genesis functionality
Changes walkthrough 📝
genesis.rs
Implement genesis.json parsing and account conversion
src/eth/genesis.rs
mod.rs
Add genesis module to eth namespace
src/eth/mod.rs
stratus_storage.rs
Integrate genesis.json with reset_to_genesis function
src/eth/storage/stratus_storage.rs
genesis.json
Add example genesis.json file
genesis.json