Skip to content

Commit 9c885a6

Browse files
ARR4NDarioush Jalali
authored and
Darioush Jalali
committed
refactor: simplify ChainConfig.UnmarshalJSON() branches
1 parent 0c14c32 commit 9c885a6

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

params/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func (c *ChainConfig) Description() string {
478478
if c.VerkleTime != nil {
479479
banner += fmt.Sprintf(" - Verkle: @%-10v\n", *c.VerkleTime)
480480
}
481-
return banner
481+
return banner + c.extraDescription()
482482
}
483483

484484
// IsHomestead returns whether num is either equal to the homestead block or greater.
@@ -671,7 +671,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
671671
lastFork = cur
672672
}
673673
}
674-
return nil
674+
return c.extraCheckConfigForkOrder()
675675
}
676676

677677
func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError {
@@ -742,7 +742,7 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, headNumber *big.Int,
742742
if isForkTimestampIncompatible(c.VerkleTime, newcfg.VerkleTime, headTimestamp) {
743743
return newTimestampCompatError("Verkle fork timestamp", c.VerkleTime, newcfg.VerkleTime)
744744
}
745-
return nil
745+
return c.extraCheckCompatible(newcfg, headNumber, headTimestamp)
746746
}
747747

748748
// BaseFeeChangeDenominator bounds the amount the base fee can change between blocks.

params/config.libevm.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,48 @@ func (r *Rules) extraPayload() *pseudo.Type {
205205
}
206206
return r.extra
207207
}
208+
209+
func (c *ChainConfig) extraDescription() string {
210+
if registeredExtras == nil || c.extra == nil {
211+
return ""
212+
}
213+
214+
type Descriptioner interface {
215+
Description() string
216+
}
217+
d, ok := c.extra.Interface().(Descriptioner)
218+
if !ok {
219+
return ""
220+
}
221+
return d.Description()
222+
}
223+
224+
func (c *ChainConfig) extraCheckCompatible(newcfg *ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError {
225+
if registeredExtras == nil || c.extra == nil {
226+
return nil
227+
}
228+
229+
type CompatibleChecker interface {
230+
CheckCompatible(*ChainConfig, *big.Int, uint64) *ConfigCompatError
231+
}
232+
cc, ok := c.extra.Interface().(CompatibleChecker)
233+
if !ok {
234+
return nil
235+
}
236+
return cc.CheckCompatible(newcfg, headNumber, headTimestamp)
237+
}
238+
239+
func (c *ChainConfig) extraCheckConfigForkOrder() error {
240+
if registeredExtras == nil || c.extra == nil {
241+
return nil
242+
}
243+
244+
type ForkOrderChecker interface {
245+
CheckConfigForkOrder() error
246+
}
247+
cc, ok := c.extra.Interface().(ForkOrderChecker)
248+
if !ok {
249+
return nil
250+
}
251+
return cc.CheckConfigForkOrder()
252+
}

params/json.libevm.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,20 @@ type chainConfigWithExportedExtra struct {
2525

2626
// UnmarshalJSON implements the [json.Unmarshaler] interface.
2727
func (c *ChainConfig) UnmarshalJSON(data []byte) error {
28-
extras := registeredExtras
29-
30-
if extras != nil && !extras.reuseJSONRoot {
28+
switch reg := registeredExtras; {
29+
case reg != nil && !reg.reuseJSONRoot:
3130
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
4431

45-
c.extra = extras.chainConfig.NilPointer()
46-
if err := json.Unmarshal(data, c.extra); err != nil {
47-
c.extra = nil
48-
return err
32+
case reg != nil && reg.reuseJSONRoot: // although the latter is redundant, it's clearer
33+
c.extra = reg.chainConfig.NilPointer()
34+
if err := json.Unmarshal(data, c.extra); err != nil {
35+
c.extra = nil
36+
return err
37+
}
38+
fallthrough // Important! We've only unmarshalled the extra field.
39+
default: // reg == nil
40+
return json.Unmarshal(data, (*chainConfigWithoutMethods)(c))
4941
}
50-
return nil
5142
}
5243

5344
// unmarshalJSONWithExtra unmarshals JSON under the assumption that the
@@ -67,14 +58,14 @@ func (c *ChainConfig) unmarshalJSONWithExtra(data []byte) error {
6758

6859
// MarshalJSON implements the [json.Marshaler] interface.
6960
func (c *ChainConfig) MarshalJSON() ([]byte, error) {
70-
switch extras := registeredExtras; {
71-
case extras == nil:
61+
switch reg := registeredExtras; {
62+
case reg == nil:
7263
return json.Marshal((*chainConfigWithoutMethods)(c))
7364

74-
case !extras.reuseJSONRoot:
65+
case !reg.reuseJSONRoot:
7566
return c.marshalJSONWithExtra()
7667

77-
default:
68+
default: // reg.reuseJSONRoot == true
7869
// The inverse of reusing the JSON root is merging two JSON buffers,
7970
// which isn't supported by the native package. So we use
8071
// map[string]json.RawMessage intermediates.

0 commit comments

Comments
 (0)