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

Add support for parsing Shelley genesis blocks #331

Merged
merged 4 commits into from
Jul 17, 2024

Conversation

SebastienGllmt
Copy link
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

Copy link
Contributor

@rooooooooob rooooooooob left a comment

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
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
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
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.

#[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")]
Copy link
Contributor Author

@SebastienGllmt SebastienGllmt May 23, 2024

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
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
1 check passed
@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 <[email protected]>
Co-authored-by: rooooooooob <[email protected]>
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