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(params): UnmarshalChainJSONConfig and MarshalChainConfigJSON #92

Merged
merged 48 commits into from
Jan 12, 2025

Conversation

qdm12
Copy link
Collaborator

@qdm12 qdm12 commented Dec 17, 2024

Why this should be merged

  • Allow wallets and other clients to parse blocks from both coreth and subnet-evm without registering extra types
  • Allow wallets and other clients to make their genesis/chain configurations without registering extra types

How this works

  • UnmarshalChainConfigJSON is a standalone deserialization function taking a data byte slice, the destination pointers *ChainConfig and a generic extra *C, as well as a reuseJSONRoot boolean.
  • MarshalChainConfigJSON is a standalone serialization function taking a ChainConfig, an extra of generic type C and reuseJSONRoot boolean.

How this was tested

New unit tests TestUnmarshalChainConfigJSON_Errors and TestMarshalChainConfigJSON_Errors for unhappy code paths, existing test TestChainConfigJSONRoundTrip for happy code paths.

@qdm12 qdm12 force-pushed the qdm12/params/unmarshal-config branch from 32afb0b to a41a78d Compare December 17, 2024 18:20
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
@qdm12 qdm12 requested a review from ARR4N December 19, 2024 12:55
@qdm12 qdm12 marked this pull request as ready for review December 19, 2024 13:50
params/json.libevm.go Outdated Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/params/unmarshal-config branch 3 times, most recently from a6ebb69 to a46252d Compare December 20, 2024 14:29
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@darioush darioush left a comment

Choose a reason for hiding this comment

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

nice, let's add MarshalJSON, we can do it in this or a separate PR

params/json.libevm_test.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
return json.Unmarshal(data, (*chainConfigWithoutMethods)(c))
}
extraConstructors := registeredExtras.Get()
Copy link
Collaborator

Choose a reason for hiding this comment

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

(style) Please don't use such verbose variable names when the scope is only 3 lines; they distract from reading of the code. I'd personally be happy with just r, but understand that others are not—as long as it's not 5 syllables.

See style guide

Copy link
Collaborator Author

@qdm12 qdm12 Dec 23, 2024

Choose a reason for hiding this comment

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

I've read a few style guides including that one, my view on it, unless it's a really obvious use (i.e. i for a range index), it's nice to have a meaningful name almost always. For example c instead of extraConstructors made me wonder what was c and had to lookup where it was created and what .Get() was returning, and, because it's a generic function, had to dig into extraConstructors struct. Not a big deal obviously, but it does make readability worst in my opinion.

Obviously that's my opinion on this, so I'll conform to the existing code and use shorter variable names 😉

EDIT: also one more reason is I do like to use a single letter for the receiving struct for methods, so using single letter variables in there makes things confusing imo

EDIT2: my brain is kind of very formed (malformed or ok-formed, debatable 😄) to use explicit non-single-letter variable names, so feel free to hammer with style comments in case you feel like it should be a smaller variable name. I got the extraConstructors -> ec but after going through the rest of the code I wrote, I'm feeling like:

params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
params/json.libevm_test.go Outdated Show resolved Hide resolved
jsonData string // string for convenience
extra *testExtra
reuseJSONRoot bool
expectedConfig ChainConfig
Copy link
Collaborator

Choose a reason for hiding this comment

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

The rest of libevm uses "want" instead of "expected" and "got" instead of "actual".

Copy link
Collaborator Author

@qdm12 qdm12 Dec 23, 2024

Choose a reason for hiding this comment

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

Changed expected prefixes to want prefixes.
However - ha I want shorter variable names perhaps - regarding actual (and got), I think i.e. data, ok := MarshalXYZ in a test is better than actualData, actualOk := MarshalXYZ and there is no need for an actual prefix really? And if it's only one variable being checked, why not name it to what it actually is i.e. data instead of actual

@qdm12 qdm12 force-pushed the qdm12/params/unmarshal-config branch from da45de2 to 2172aa7 Compare December 23, 2024 12:21
@qdm12 qdm12 changed the title feat(params): UnmarshalChainConfig function feat(params): UnmarshalChainJSONConfig and MarshalChainJSONConfig Dec 23, 2024
@qdm12 qdm12 requested a review from ARR4N December 26, 2024 17:07
Copy link
Collaborator

@ARR4N ARR4N left a comment

Choose a reason for hiding this comment

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

Approving with changes requested because I don't need to re-review them. Most are suggestions (since we're new to working together and sharing my preferred style for libevm) and I'm happy for you to use your judgement in deciding whether or not to implement them, but the following I'd like to discuss if you disagree with them:

  1. The 4 external comments on ChainConfig.{Un}Marshal() and on {Un}MarshalChainConfigJSON().
  2. All 3 comments on the tests.

params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
params/json.libevm.go Outdated Show resolved Hide resolved
// type generic nature which cannot be embedded in such a combined struct.
configJSONRaw, err := toJSONRawMessages((chainConfigWithoutMethods)(config))
if err != nil {
return nil, fmt.Errorf("converting config to JSON raw messages: %s", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think for this and the extras too we should just return a raw error and decorate it in toJSONRawMessages() instead, for consistency. I'd add a suggestion but GitHub review UI sucks and I can't comment on unmodified code in the same file.

fmt.Errorf("encoding %T into %T: %s", v, msgs, err) on line 154.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think fmt.Errorf("encoding %T into %T: %s", v, msgs, err) is a bit confusing, since technically we are encoding then decoding into a different Go element (map). I still think converting config to JSON raw messages explains it alright. I've added error wrappings in 359448d but it might be a bit too-muchey, feel free to let me know and i'll drop that commit.

params/json.libevm_test.go Outdated Show resolved Hide resolved
"invalid_json": {
extra: &testExtra{},
wantExtra: &testExtra{},
wantErrMessage: "decoding config to combined of chain config and *params.testExtra: " +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using a full error message like this is a change-detector test that results in false positives (e.g. they will all break if you accept my suggestions). Alternative options:

  1. Limit to a pertinent and specific sub-string for each error.
  2. Create (unexported) var errX global variables and wrap them on return, using errors.Is in the test.
  3. Create a custom error type with an internal identifier that you can assert here (e.g. although not appropriate, something like gRPC status codes, but unique).

This applies to the marshalling tests too.

See https://google.github.io/styleguide/go/decisions#test-error-semantics

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, although I would like to add checking error messages does help build decent error messages through error wrappings and avoid repetitions (and also make sure it logs out well formed things, not pointer addresses for example). But the cost of this outweights the benefits, so changing this to an error message regex checking the 'important part' and not the whole error message.

params/json.libevm_test.go Outdated Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/params/unmarshal-config branch 3 times, most recently from c656984 to 0d06fe7 Compare January 8, 2025 16:32
qdm12 and others added 25 commits January 9, 2025 14:56
…igJSON`

Co-authored-by: Arran Schlosberg <[email protected]>
Signed-off-by: Quentin McGaw <[email protected]>
@qdm12 qdm12 force-pushed the qdm12/params/unmarshal-config branch from 0d06fe7 to b48fb8a Compare January 9, 2025 13:57
@qdm12 qdm12 changed the title feat(params): UnmarshalChainJSONConfig and MarshalChainJSONConfig feat(params): UnmarshalChainJSONConfig and MarshalChainConfigJSON Jan 12, 2025
@qdm12 qdm12 merged commit d08d0f0 into main Jan 12, 2025
4 checks passed
@qdm12 qdm12 deleted the qdm12/params/unmarshal-config branch January 12, 2025 17:35
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.

3 participants