Skip to content

Commit

Permalink
feat: add extra_fields to ChainConfig (alloy-rs#631)
Browse files Browse the repository at this point in the history
* feat: add extra_fields to ChainConfig

* add test

* String from alloc

* add serde_json to deps

* use serde_json::Value instead of reexporting from serde crate

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* remove extra line

* remove json_value import

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
2 people authored and ben186 committed Jul 27, 2024
1 parent 42ad666 commit 50329db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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
33 changes: 32 additions & 1 deletion crates/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

extern crate alloc;

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

use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_serde::{
Expand Down Expand Up @@ -422,6 +422,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 +1308,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"}));
}
}

0 comments on commit 50329db

Please sign in to comment.