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

enhancement(lib/genesis): Create struct of Runtime field of the Genesis struct. #2808

Merged
merged 35 commits into from
Jul 11, 2023

Conversation

axaysagathiya
Copy link
Contributor

@axaysagathiya axaysagathiya commented Sep 7, 2022

Changes

  • Created struct for Runtime field of Genesis struct.

  • Created structs for all the fields of Runtime struct.

  • Wrote Marshal and Unmarshal methods for

    • some fields of Runtime struct
    • AuthorityAsAddress struct in lib/crypto/keypair.go
  • Wrote Marshal method for Uint128.

  • added empty string check for function argument of PublicAddressToByteArray function in lib/crypto/keypair.go

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

fixes #2766

Primary Reviewer

@timwu20

@codecov
Copy link

codecov bot commented Sep 7, 2022

Codecov Report

Merging #2808 (81ce2b8) into development (1b9043b) will increase coverage by 0.15%.
The diff coverage is 42.60%.

❗ Current head 81ce2b8 differs from pull request most recent head 25c54f6. Consider uploading reports for the commit 25c54f6 to get more accurate results

Additional details and impacted files
@@               Coverage Diff               @@
##           development    #2808      +/-   ##
===============================================
+ Coverage        51.30%   51.45%   +0.15%     
===============================================
  Files              226      227       +1     
  Lines            28228    28370     +142     
===============================================
+ Hits             14481    14599     +118     
- Misses           12351    12372      +21     
- Partials          1396     1399       +3     

@axaysagathiya axaysagathiya changed the title Add MarshalJSON/UnmarshalJSON methods for Runtime field of the Genesis struct. enhancement(lib/genesis): Add MarshalJSON/UnmarshalJSON methods for Runtime field of the Genesis struct. Sep 7, 2022
Copy link
Contributor

@qdm12 qdm12 left a comment

Choose a reason for hiding this comment

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

#2766 is not totally explicit/clear on what's expected to be done, sorry about that, but let me clarify! 😉

We need a Runtime struct

type Runtime struct {
    System System `json:"system"`
    Babe     Babe    `json:"babe"`
    // ... other fields ...
}

You can check the runtime child JSON fields in chain/**/*-spec.json files. For example:

You will need to define other child structs such as for "babe" with the right json struct field tags.

Start with implementing these structs;

We don't need a custom MarshalJSON (json.Marshal will take care of it correctly using the json:"fieldname" struct field tag), but we will need a custom UnmarshalJSON only on Runtime since some genesis-spec.json files have the system json key as "system" and some older ones as "System". Note however you can and should unmarshal within this custom UnmarshalJSON with json.Unmarshal(data, r) (where r is the *Runtime), and if the System struct field is still empty try to extract the "System" key as a fall back.

You might also want to add a unit test (I can help just tag me @qdm12) for the custom UnmarshalJSON since it's tricky to get right without testing it.

@axaysagathiya
Copy link
Contributor Author

axaysagathiya commented Sep 20, 2022

@qdm12
Why these files have different keys ?

@qdm12
Copy link
Contributor

qdm12 commented Sep 20, 2022

@danforbes any idea? By the way where is the 'latest' JSON structure documented? We could use the latest official doc at least to encode from our Go struct to JSON (and thus set the json struct field tags).

At least, the 'structure' didn't change it seems, it's just the field names that changed over time. As a consequence, we can still use the same Go struct for all JSON decoding, but we will need to support decoding using different JSON field keys (trying the latest naming format first, then the older ones) using custom UnmarshalJSON methods.

Until this is resolved, you can still add Go struct for each JSON object, even if the key differ.
You can set Go struct json field tag to the ones precised in chain/dev-v3substrate/genesis-spec.json and chain/dev-v3substrate/genesis-spec.json

@qdm12
Copy link
Contributor

qdm12 commented Sep 20, 2022

You can define the Go structs for the genesis (even if the json keys differ), and we will just focus on the latest format, so that means no custom MarshalJSON or UnmarshalJSON, but only the struct field json tags (using the latest json key).

The latest format can be found by digging into https://paritytech.github.io/substrate/master/src/sc_chain_spec/chain_spec.rs.html#205 (parity uses serde for their JSON serialization). On the other hand, if digging out json keys turns out to be a nightmare, @timwu20 should create soon a separate issue in order to upgrade our existing *-spec.json files to the latest format (so we'll have only one json format with the same json keys).

@axaysagathiya axaysagathiya changed the title enhancement(lib/genesis): Add MarshalJSON/UnmarshalJSON methods for Runtime field of the Genesis struct. enhancement(lib/genesis): Create struct of Runtime field of the Genesis struct. Dec 5, 2022
axaysagathiya and others added 3 commits May 20, 2023 18:45
modified Runtime struct fields to use the fields given in local/westend-local file.
removed unnecessary code, fixed tests, modified structs
@axaysagathiya axaysagathiya marked this pull request as draft May 23, 2023 19:09
@axaysagathiya axaysagathiya marked this pull request as ready for review May 24, 2023 19:53
dot/types/authority_test.go Outdated Show resolved Hide resolved
dot/types/authority_test.go Outdated Show resolved Hide resolved
dot/types/authority.go Outdated Show resolved Hide resolved
lib/genesis/genesis.go Outdated Show resolved Hide resolved
dot/types/authority_test.go Outdated Show resolved Hide resolved
lib/genesis/genesis_test.go Outdated Show resolved Hide resolved
lib/genesis/genesis_test.go Outdated Show resolved Hide resolved
lib/genesis/pallet_test.go Outdated Show resolved Hide resolved
lib/genesis/pallet_test.go Outdated Show resolved Hide resolved
lib/genesis/pallet_test.go Outdated Show resolved Hide resolved
lib/genesis/helpers.go Outdated Show resolved Hide resolved
lib/genesis/helpers.go Outdated Show resolved Hide resolved
lib/genesis/helpers.go Outdated Show resolved Hide resolved
lib/genesis/helpers.go Show resolved Hide resolved
lib/genesis/helpers.go Outdated Show resolved Hide resolved
lib/genesis/helpers.go Outdated Show resolved Hide resolved
lib/genesis/pallet.go Outdated Show resolved Hide resolved
Copy link
Member

@EclesioMeloJunior EclesioMeloJunior 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 tiny comment over the scale.Uint128 MarshalJSON method

pkg/scale/uint128.go Show resolved Hide resolved
Co-authored-by: Eclésio Junior <eclesiomelo.1@gmail.com>
@kishansagathiya kishansagathiya merged commit 25a2b79 into ChainSafe:development Jul 11, 2023
21 checks passed
Copy link

🎉 This PR is included in version 0.8.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

Unmarshal Method for Genesis Runtime Field
7 participants