Skip to content
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 extra_fields to ChainConfig #631

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ alloy-serde.workspace = true

# serde
serde.workspace = true

[dev-dependencies]
serde_json.workspace = true

[features]
Expand Down
34 changes: 33 additions & 1 deletion crates/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::{collections::BTreeMap, string::String};

use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_serde::{
json_value,
num::{u128_opt_via_ruint, u128_via_ruint, u64_opt_via_ruint, u64_via_ruint},
storage::deserialize_storage_map,
ttd::deserialize_json_ttd_opt,
Expand Down Expand Up @@ -422,6 +423,10 @@ pub struct ChainConfig {
/// Clique parameters.
#[serde(skip_serializing_if = "Option::is_none")]
pub clique: Option<CliqueConfig>,

/// Additional fields specific to each chain.
#[serde(flatten, default)]
pub extra_fields: BTreeMap<String, serde_json::Value>,
}

impl ChainConfig {
Expand Down Expand Up @@ -1304,4 +1309,31 @@ mod tests {
let gen2 = serde_json::from_str::<Genesis>(&s).unwrap();
assert_eq!(gen, gen2);
}

#[test]
fn parse_extra_fields() {
let geth_genesis = r#"
{
"difficulty": "0x20000",
"gasLimit": "0x1",
"alloc": {},
"config": {
"ethash": {},
"chainId": 1,
"string_field": "string_value",
"numeric_field": 7,
"object_field": {
"sub_field": "sub_value"
}
}
}
"#;
let genesis: Genesis = serde_json::from_str(geth_genesis).unwrap();
let actual_string_value = genesis.config.extra_fields.get("string_field").unwrap();
assert_eq!(actual_string_value, "string_value");
let actual_numeric_value = genesis.config.extra_fields.get("numeric_field").unwrap();
assert_eq!(actual_numeric_value, 7);
let actual_object_value = genesis.config.extra_fields.get("object_field").unwrap();
assert_eq!(actual_object_value, &serde_json::json!({"sub_field": "sub_value"}));
}
}
Loading