forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 2
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.ChainConfig
extra payload can use root JSON
#8
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package params | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/libevm/pseudo" | ||
) | ||
|
||
var _ interface { | ||
json.Marshaler | ||
json.Unmarshaler | ||
} = (*ChainConfig)(nil) | ||
|
||
// chainConfigWithoutMethods avoids infinite recurion into | ||
// [ChainConfig.UnmarshalJSON]. | ||
type chainConfigWithoutMethods ChainConfig | ||
|
||
// chainConfigWithExportedExtra supports JSON (un)marshalling of a [ChainConfig] | ||
// while exposing the `extra` field as the "extra" JSON key. | ||
type chainConfigWithExportedExtra struct { | ||
*chainConfigWithoutMethods // embedded to achieve regular JSON unmarshalling | ||
Extra *pseudo.Type `json:"extra"` // `c.extra` is otherwise unexported | ||
} | ||
|
||
// UnmarshalJSON implements the [json.Unmarshaler] interface. | ||
func (c *ChainConfig) UnmarshalJSON(data []byte) error { | ||
extras := registeredExtras | ||
|
||
if extras != nil && !extras.reuseJSONRoot { | ||
return c.unmarshalJSONWithExtra(data) | ||
} | ||
|
||
if err := json.Unmarshal(data, (*chainConfigWithoutMethods)(c)); err != nil { | ||
return err | ||
} | ||
if extras == nil { | ||
return nil | ||
} | ||
|
||
// Invariants if here: | ||
// - reg.reuseJSONRoot == true | ||
// - Non-extra ChainConfig fields already unmarshalled | ||
|
||
c.extra = extras.chainConfig.NilPointer() | ||
if err := json.Unmarshal(data, c.extra); err != nil { | ||
c.extra = nil | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// unmarshalJSONWithExtra unmarshals JSON under the assumption that the | ||
// registered [Extras] payload is in the JSON "extra" key. All other | ||
// unmarshalling is performed as if no [Extras] were registered. | ||
func (c *ChainConfig) unmarshalJSONWithExtra(data []byte) error { | ||
cc := &chainConfigWithExportedExtra{ | ||
chainConfigWithoutMethods: (*chainConfigWithoutMethods)(c), | ||
Extra: registeredExtras.chainConfig.NilPointer(), | ||
} | ||
if err := json.Unmarshal(data, cc); err != nil { | ||
return err | ||
} | ||
c.extra = cc.Extra | ||
return nil | ||
} | ||
|
||
// MarshalJSON implements the [json.Marshaler] interface. | ||
func (c *ChainConfig) MarshalJSON() ([]byte, error) { | ||
switch extras := registeredExtras; { | ||
case extras == nil: | ||
return json.Marshal((*chainConfigWithoutMethods)(c)) | ||
|
||
case !extras.reuseJSONRoot: | ||
return c.marshalJSONWithExtra() | ||
|
||
default: | ||
// The inverse of reusing the JSON root is merging two JSON buffers, | ||
// which isn't supported by the native package. So we use | ||
// map[string]json.RawMessage intermediates. | ||
geth, err := toJSONRawMessages((*chainConfigWithoutMethods)(c)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
extra, err := toJSONRawMessages(c.extra) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for k, v := range extra { | ||
if _, ok := geth[k]; ok { | ||
return nil, fmt.Errorf("duplicate JSON key %q in both %T and registered extra", k, c) | ||
} | ||
geth[k] = v | ||
} | ||
return json.Marshal(geth) | ||
} | ||
} | ||
|
||
// marshalJSONWithExtra is the inverse of unmarshalJSONWithExtra(). | ||
func (c *ChainConfig) marshalJSONWithExtra() ([]byte, error) { | ||
cc := &chainConfigWithExportedExtra{ | ||
chainConfigWithoutMethods: (*chainConfigWithoutMethods)(c), | ||
Extra: c.extra, | ||
} | ||
return json.Marshal(cc) | ||
} | ||
|
||
func toJSONRawMessages(v any) (map[string]json.RawMessage, error) { | ||
buf, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msgs := make(map[string]json.RawMessage) | ||
if err := json.Unmarshal(buf, &msgs); err != nil { | ||
return nil, err | ||
} | ||
return msgs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package params | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/libevm/pseudo" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type nestedChainConfigExtra struct { | ||
NestedFoo string `json:"foo"` | ||
|
||
NOOPHooks | ||
} | ||
|
||
type rootJSONChainConfigExtra struct { | ||
TopLevelFoo string `json:"foo"` | ||
|
||
NOOPHooks | ||
} | ||
|
||
func TestChainConfigJSONRoundTrip(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
register func() | ||
jsonInput string | ||
want *ChainConfig | ||
}{ | ||
{ | ||
name: "no registered extras", | ||
register: func() {}, | ||
jsonInput: `{ | ||
"chainId": 1234 | ||
}`, | ||
want: &ChainConfig{ | ||
ChainID: big.NewInt(1234), | ||
}, | ||
}, | ||
{ | ||
name: "reuse top-level JSON", | ||
register: func() { | ||
RegisterExtras(Extras[rootJSONChainConfigExtra, NOOPHooks]{ | ||
ReuseJSONRoot: true, | ||
}) | ||
}, | ||
jsonInput: `{ | ||
"chainId": 5678, | ||
"foo": "hello" | ||
}`, | ||
want: &ChainConfig{ | ||
ChainID: big.NewInt(5678), | ||
extra: pseudo.From(&rootJSONChainConfigExtra{TopLevelFoo: "hello"}).Type, | ||
}, | ||
}, | ||
{ | ||
name: "nested JSON", | ||
register: func() { | ||
RegisterExtras(Extras[nestedChainConfigExtra, NOOPHooks]{ | ||
ReuseJSONRoot: false, // explicit zero value only for tests | ||
}) | ||
}, | ||
jsonInput: `{ | ||
"chainId": 42, | ||
"extra": {"foo": "world"} | ||
}`, | ||
want: &ChainConfig{ | ||
ChainID: big.NewInt(42), | ||
extra: pseudo.From(&nestedChainConfigExtra{NestedFoo: "world"}).Type, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
TestOnlyClearRegisteredExtras() | ||
t.Cleanup(TestOnlyClearRegisteredExtras) | ||
tt.register() | ||
|
||
t.Run("json.Unmarshal()", func(t *testing.T) { | ||
got := new(ChainConfig) | ||
require.NoError(t, json.Unmarshal([]byte(tt.jsonInput), got)) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
|
||
t.Run("json.Marshal()", func(t *testing.T) { | ||
var want bytes.Buffer | ||
require.NoError(t, json.Compact(&want, []byte(tt.jsonInput)), "json.Compact()") | ||
|
||
got, err := json.Marshal(tt.want) | ||
require.NoError(t, err) | ||
assert.Equal(t, want.String(), string(got)) | ||
ARR4N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed in a couple cases that
MarshalJSON()
being defined on the value vs. pointer makes a difference.Since it is only a couple instances so far I switched the uses to
&
to move on, but perhaps this should be defined on the value to avoid invoking the default MarshalJSON when the receiver is non-pointer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! Do you remember where this was, so I can take a look before I merge this? I defined it on the pointer because that's what tends to be carried around, e.g. in
vm.EVM
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to leave it as defined on the pointer for now. In the geth code base there are only two instances where a non-pointer
ChainConfig
is used (note that most of the results are eithernew(ChainConfig)
or immediately address the value with&
).This is in contrast to the 65 files with a
*ChainConfig
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The *ChainConfig instances will still work if the receiver is value. We can leave it as is for now.