Skip to content

Commit 0c14c32

Browse files
committed
feat: params.ChainConfig extra payload can use root JSON
1 parent b6f3eb9 commit 0c14c32

File tree

4 files changed

+233
-43
lines changed

4 files changed

+233
-43
lines changed

params/config.libevm.go

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package params
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"math/big"
76
"reflect"
@@ -14,6 +13,15 @@ import (
1413
// Extras are arbitrary payloads to be added as extra fields in [ChainConfig]
1514
// and [Rules] structs. See [RegisterExtras].
1615
type Extras[C ChainConfigHooks, R RulesHooks] struct {
16+
// ReuseJSONRoot, if true, signals that JSON unmarshalling of a
17+
// [ChainConfig] MUST reuse the root JSON input when unmarshalling the extra
18+
// payload. If false, it is assumed that the extra JSON payload is nested in
19+
// the "extra" key.
20+
//
21+
// *NOTE* this requires multiple passes for both marshalling and
22+
// unmarshalling of JSON so is inefficient and should be used as a last
23+
// resort.
24+
ReuseJSONRoot bool
1725
// NewRules, if non-nil is called at the end of [ChainConfig.Rules] with the
1826
// newly created [Rules] and other context from the method call. Its
1927
// returned value will be the extra payload of the [Rules]. If NewRules is
@@ -51,10 +59,11 @@ func RegisterExtras[C ChainConfigHooks, R RulesHooks](e Extras[C, R]) ExtraPaylo
5159

5260
getter := e.getter()
5361
registeredExtras = &extraConstructors{
54-
chainConfig: pseudo.NewConstructor[C](),
55-
rules: pseudo.NewConstructor[R](),
56-
newForRules: e.newForRules,
57-
getter: getter,
62+
chainConfig: pseudo.NewConstructor[C](),
63+
rules: pseudo.NewConstructor[R](),
64+
reuseJSONRoot: e.ReuseJSONRoot,
65+
newForRules: e.newForRules,
66+
getter: getter,
5867
}
5968
return getter
6069
}
@@ -87,6 +96,7 @@ var registeredExtras *extraConstructors
8796

8897
type extraConstructors struct {
8998
chainConfig, rules pseudo.Constructor
99+
reuseJSONRoot bool
90100
newForRules func(_ *ChainConfig, _ *Rules, blockNum *big.Int, isMerge bool, timestamp uint64) *pseudo.Type
91101
// use top-level hooksFrom<X>() functions instead of these as they handle
92102
// instances where no [Extras] were registered.
@@ -158,42 +168,6 @@ func (e ExtraPayloadGetter[C, R]) hooksFromRules(r *Rules) RulesHooks {
158168
return NOOPHooks{}
159169
}
160170

161-
// UnmarshalJSON implements the [json.Unmarshaler] interface.
162-
func (c *ChainConfig) UnmarshalJSON(data []byte) error {
163-
type raw ChainConfig // doesn't inherit methods so avoids recursing back here (infinitely)
164-
cc := &struct {
165-
*raw
166-
Extra *pseudo.Type `json:"extra"`
167-
}{
168-
raw: (*raw)(c), // embedded to achieve regular JSON unmarshalling
169-
}
170-
if e := registeredExtras; e != nil {
171-
cc.Extra = e.chainConfig.NilPointer() // `c.extra` is otherwise unexported
172-
}
173-
174-
if err := json.Unmarshal(data, cc); err != nil {
175-
return err
176-
}
177-
c.extra = cc.Extra
178-
return nil
179-
}
180-
181-
// MarshalJSON implements the [json.Marshaler] interface.
182-
func (c *ChainConfig) MarshalJSON() ([]byte, error) {
183-
// See UnmarshalJSON() for rationale.
184-
type raw ChainConfig
185-
cc := &struct {
186-
*raw
187-
Extra *pseudo.Type `json:"extra"`
188-
}{raw: (*raw)(c), Extra: c.extra}
189-
return json.Marshal(cc)
190-
}
191-
192-
var _ interface {
193-
json.Marshaler
194-
json.Unmarshaler
195-
} = (*ChainConfig)(nil)
196-
197171
// addRulesExtra is called at the end of [ChainConfig.Rules]; it exists to
198172
// abstract the libevm-specific behaviour outside of original geth code.
199173
func (c *ChainConfig) addRulesExtra(r *Rules, blockNum *big.Int, isMerge bool, timestamp uint64) {

params/config.libevm_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ func TestRegisterExtras(t *testing.T) {
104104
require.NoError(t, json.Unmarshal(buf, got))
105105
assert.Equal(t, tt.ccExtra.Interface(), got.extraPayload().Interface())
106106
assert.Equal(t, in, got)
107-
// TODO: do we need an explicit test of the JSON output, or is a
108-
// Marshal-Unmarshal round trip sufficient?
109107

110108
gotRules := got.Rules(nil, false, 0)
111109
assert.Equal(t, tt.wantRulesExtra, gotRules.extraPayload().Interface())

params/json.libevm.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package params
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/ethereum/go-ethereum/libevm/pseudo"
8+
)
9+
10+
var _ interface {
11+
json.Marshaler
12+
json.Unmarshaler
13+
} = (*ChainConfig)(nil)
14+
15+
// chainConfigWithoutMethods avoids infinite recurion into
16+
// [ChainConfig.UnmarshalJSON].
17+
type chainConfigWithoutMethods ChainConfig
18+
19+
// chainConfigWithExportedExtra supports JSON (un)marshalling of a [ChainConfig]
20+
// while exposing the `extra` field as the "extra" JSON key.
21+
type chainConfigWithExportedExtra struct {
22+
*chainConfigWithoutMethods // embedded to achieve regular JSON unmarshalling
23+
Extra *pseudo.Type `json:"extra"` // `c.extra` is otherwise unexported
24+
}
25+
26+
// UnmarshalJSON implements the [json.Unmarshaler] interface.
27+
func (c *ChainConfig) UnmarshalJSON(data []byte) error {
28+
extras := registeredExtras
29+
30+
if extras != nil && !extras.reuseJSONRoot {
31+
return c.unmarshalJSONWithExtra(data)
32+
}
33+
34+
if err := json.Unmarshal(data, (*chainConfigWithoutMethods)(c)); err != nil {
35+
return err
36+
}
37+
if extras == nil {
38+
return nil
39+
}
40+
41+
// Invariants if here:
42+
// - reg.reuseJSONRoot == true
43+
// - Non-extra ChainConfig fields already unmarshalled
44+
45+
c.extra = extras.chainConfig.NilPointer()
46+
if err := json.Unmarshal(data, c.extra); err != nil {
47+
c.extra = nil
48+
return err
49+
}
50+
return nil
51+
}
52+
53+
// unmarshalJSONWithExtra unmarshals JSON under the assumption that the
54+
// registered [Extras] payload is in the JSON "extra" key. All other
55+
// unmarshalling is performed as if no [Extras] were registered.
56+
func (c *ChainConfig) unmarshalJSONWithExtra(data []byte) error {
57+
cc := &chainConfigWithExportedExtra{
58+
chainConfigWithoutMethods: (*chainConfigWithoutMethods)(c),
59+
Extra: registeredExtras.chainConfig.NilPointer(),
60+
}
61+
if err := json.Unmarshal(data, cc); err != nil {
62+
return err
63+
}
64+
c.extra = cc.Extra
65+
return nil
66+
}
67+
68+
// MarshalJSON implements the [json.Marshaler] interface.
69+
func (c *ChainConfig) MarshalJSON() ([]byte, error) {
70+
switch extras := registeredExtras; {
71+
case extras == nil:
72+
return json.Marshal((*chainConfigWithoutMethods)(c))
73+
74+
case !extras.reuseJSONRoot:
75+
return c.marshalJSONWithExtra()
76+
77+
default:
78+
// The inverse of reusing the JSON root is merging two JSON buffers,
79+
// which isn't supported by the native package. So we use
80+
// map[string]json.RawMessage intermediates.
81+
geth, err := toJSONRawMessages((*chainConfigWithoutMethods)(c))
82+
if err != nil {
83+
return nil, err
84+
}
85+
extra, err := toJSONRawMessages(c.extra)
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
for k, v := range extra {
91+
if _, ok := geth[k]; ok {
92+
return nil, fmt.Errorf("duplicate JSON key %q in both %T and registered extra", k, c)
93+
}
94+
geth[k] = v
95+
}
96+
return json.Marshal(geth)
97+
}
98+
}
99+
100+
// marshalJSONWithExtra is the inverse of unmarshalJSONWithExtra().
101+
func (c *ChainConfig) marshalJSONWithExtra() ([]byte, error) {
102+
cc := &chainConfigWithExportedExtra{
103+
chainConfigWithoutMethods: (*chainConfigWithoutMethods)(c),
104+
Extra: c.extra,
105+
}
106+
return json.Marshal(cc)
107+
}
108+
109+
func toJSONRawMessages(v any) (map[string]json.RawMessage, error) {
110+
buf, err := json.Marshal(v)
111+
if err != nil {
112+
return nil, err
113+
}
114+
msgs := make(map[string]json.RawMessage)
115+
if err := json.Unmarshal(buf, &msgs); err != nil {
116+
return nil, err
117+
}
118+
return msgs, nil
119+
}

params/json.libevm_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package params
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"math/big"
7+
"testing"
8+
9+
"github.com/ethereum/go-ethereum/libevm/pseudo"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
type nestedChainConfigExtra struct {
15+
NestedFoo string `json:"foo"`
16+
17+
NOOPHooks
18+
}
19+
20+
type rootJSONChainConfigExtra struct {
21+
TopLevelFoo string `json:"foo"`
22+
23+
NOOPHooks
24+
}
25+
26+
func TestChainConfigJSONRoundTrip(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
register func()
30+
jsonInput string
31+
want *ChainConfig
32+
}{
33+
{
34+
name: "no registered extras",
35+
register: func() {},
36+
jsonInput: `{
37+
"chainId": 1234
38+
}`,
39+
want: &ChainConfig{
40+
ChainID: big.NewInt(1234),
41+
},
42+
},
43+
{
44+
name: "reuse top-level JSON",
45+
register: func() {
46+
RegisterExtras(Extras[rootJSONChainConfigExtra, NOOPHooks]{
47+
ReuseJSONRoot: true,
48+
})
49+
},
50+
jsonInput: `{
51+
"chainId": 5678,
52+
"foo": "hello"
53+
}`,
54+
want: &ChainConfig{
55+
ChainID: big.NewInt(5678),
56+
extra: pseudo.From(&rootJSONChainConfigExtra{TopLevelFoo: "hello"}).Type,
57+
},
58+
},
59+
{
60+
name: "nested JSON",
61+
register: func() {
62+
RegisterExtras(Extras[nestedChainConfigExtra, NOOPHooks]{
63+
ReuseJSONRoot: false, // explicit zero value only for tests
64+
})
65+
},
66+
jsonInput: `{
67+
"chainId": 42,
68+
"extra": {"foo": "world"}
69+
}`,
70+
want: &ChainConfig{
71+
ChainID: big.NewInt(42),
72+
extra: pseudo.From(&nestedChainConfigExtra{NestedFoo: "world"}).Type,
73+
},
74+
},
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
TestOnlyClearRegisteredExtras()
80+
t.Cleanup(TestOnlyClearRegisteredExtras)
81+
tt.register()
82+
83+
t.Run("json.Unmarshal()", func(t *testing.T) {
84+
got := new(ChainConfig)
85+
require.NoError(t, json.Unmarshal([]byte(tt.jsonInput), got))
86+
assert.Equal(t, tt.want, got)
87+
})
88+
89+
t.Run("json.Marshal()", func(t *testing.T) {
90+
var want bytes.Buffer
91+
require.NoError(t, json.Compact(&want, []byte(tt.jsonInput)), "json.Compact()")
92+
93+
got, err := json.Marshal(tt.want)
94+
require.NoError(t, err)
95+
assert.Equal(t, want.String(), string(got))
96+
})
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)