Skip to content

Add support for parsing Shelley genesis blocks - #331

Merged
rooooooooob merged 4 commits into
developfrom
shelley-genesis-parsing
Jul 17, 2024
Merged

Add support for parsing Shelley genesis blocks#331
rooooooooob merged 4 commits into
developfrom
shelley-genesis-parsing

Conversation

@SebastienGllmt

Copy link
Copy Markdown
Contributor

This is required as part of dcSpark/carp#187

Notably, the Shelley genesis block has an intialFunds field and a staking field which essentially allows having stake pools and shelley-era keys as part of the genesis block for newly spun up networks which is useful for testing

@SebastienGllmt SebastienGllmt self-assigned this May 20, 2024

@rooooooooob rooooooooob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few minor comments

pub decentralisation_param: Fraction,
pub e_max: u64,
pub extra_entropy: ShelleyGenesisExtraEntropy,
pub key_deposit: u64,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some of these could be Coin when relevant? It's still just an alias but it would be consistent with the rest of the protocol param structs.

}

pub fn parse_genesis_data<R: Read>(json: R) -> Result<config::ShelleyGenesisData, GenesisJSONError> {
let data_value: serde_json::Value = serde_json::from_reader(json)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the clone? I don't see it used anywhere else.

ipv6
));
},
_ => panic!("Only single host address relays are supported in cardano-node Relay JSON parsing")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to panic here it'd be good to say so in the function comment.

Edit: I read the comment on RelayTypeMap saying you couldn't even find JSON for other types so maybe this is okay.

Comment thread chain/rust/src/genesis/shelley/raw.rs Outdated
#[derive(Serialize, Deserialize, Debug)]
pub struct ShelleyGenesisCredential {
// for some reason there actually is a space in the JSON key emitted by the Haskell node
#[serde(rename = "key hash")]

@SebastienGllmt SebastienGllmt May 23, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like cardano-node changed it so that both key hash and keyHash are accepted, so we have to handle both those possibilities here as well. Not sure if Serde has an option to enable 2 different ways to represent a field in JSON

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SebastienGllmt You can use #[serde(alias = "keyHash")]

It can even be defined multiple times.

example:

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct Foo {
    #[serde(alias = "KeyHash")]
    #[serde(alias = "keyHash")]
    key_hash: String,
}

fn main() {
    let jsons = [
        "{ \"key_hash\": \"default\" }",
        "{ \"keyHash\": \"lowerCamelCase\" }",
        "{ \"KeyHash\": \"UpperCamelCase\" }",
        "{ \"keyhash\": \"should not parse\" }",
    ];
    for json in jsons {
        let foo: Result<Foo, _> = serde_json::from_str(json);
        println!("{json}      ->     {foo:?}");
    }
}

The first 3 are all accepted. First by it being the field name, and the next 2 for the alias = marker.

Running:

{ "key_hash": "default" }      ->     Ok(Foo { key_hash: "default" })
{ "keyHash": "lowerCamelCase" }      ->     Ok(Foo { key_hash: "lowerCamelCase" })
{ "KeyHash": "UpperCamelCase" }      ->     Ok(Foo { key_hash: "UpperCamelCase" })
{ "keyhash": "should not parse" }      ->     Err(Error("missing field `key_hash`", line: 1, column: 33))

rooooooooob and others added 3 commits July 16, 2024 16:38
* Add alias for `ShelleyGenesisCredential::keyHash` to account for
cardano-node json format

* Remove unnecessary clone()

* Change `u64` to `Coin` when appropriate in Genesis types

* ran cargo fmt
@rooooooooob
rooooooooob merged commit bf34fe4 into develop Jul 17, 2024
@SebastienGllmt
SebastienGllmt deleted the shelley-genesis-parsing branch July 17, 2024 13:34
rooooooooob added a commit that referenced this pull request Jul 23, 2024
* Add support for parsing Shelley genesis blocks

* Genesis keyHash alias + minor fixes

* Add alias for `ShelleyGenesisCredential::keyHash` to account for
cardano-node json format

* Remove unnecessary clone()

* Change `u64` to `Coin` when appropriate in Genesis types

* ran cargo fmt

* Update Genesis parsing for changes in #340

---------

Co-authored-by: rooooooooob <rooooooooob@users.noreply.github.com>
Co-authored-by: rooooooooob <raftias@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants