Skip to content

Commit b07a715

Browse files
committed
fix ChainConfig issue
1 parent c5f2b35 commit b07a715

File tree

7 files changed

+87
-10
lines changed

7 files changed

+87
-10
lines changed

consensus/congress/vmcaller/vmcaller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ethereum/go-ethereum/core/state"
77
"github.com/ethereum/go-ethereum/core/types"
88
"github.com/ethereum/go-ethereum/core/vm"
9+
"github.com/ethereum/go-ethereum/log"
910
"github.com/ethereum/go-ethereum/params"
1011
"math/big"
1112
)
@@ -19,7 +20,9 @@ func ExecuteMsg(msg core.Message, state *state.StateDB, header *types.Header, ch
1920
// Finalise the statedb so any changes can take effect,
2021
// and especially if the `from` account is empty, it can be finally deleted.
2122
state.Finalise(true)
22-
23+
if err != nil {
24+
log.Error("ExecuteMsg failed", "err", err, "ret", string(ret))
25+
}
2326
return ret, err
2427
}
2528

core/evm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/consensus"
2424
"github.com/ethereum/go-ethereum/core/types"
2525
"github.com/ethereum/go-ethereum/core/vm"
26+
"github.com/modern-go/reflect2"
2627
)
2728

2829
// ChainContext supports retrieving headers and consensus parameters from the
@@ -120,7 +121,7 @@ func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int)
120121
}
121122

122123
func GetCanCreateFn(chain ChainContext) vm.CanCreateFunc {
123-
if chain == nil || chain.Engine() == nil {
124+
if reflect2.IsNil(chain) || chain.Engine() == nil {
124125
return func(db vm.StateDB, address common.Address, height *big.Int) bool {
125126
return true
126127
}

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
245245
case ghash == params.TestnetGenesisHash:
246246
return params.TestnetChainConfig
247247
default:
248-
return params.AllEthashProtocolChanges
248+
return params.AllCongressProtocolChanges
249249
}
250250
}
251251

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/kylelemons/godebug v1.1.0 // indirect
4949
github.com/mattn/go-colorable v0.1.8
5050
github.com/mattn/go-isatty v0.0.12
51+
github.com/modern-go/reflect2 v1.0.1
5152
github.com/naoina/go-stringutil v0.1.0 // indirect
5253
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
5354
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxd
318318
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
319319
github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A=
320320
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
321+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
321322
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
323+
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
322324
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
323325
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
324326
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

params/config.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ var (
125125
// adding flags to the config to also have to set these fields.
126126
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
127127

128+
AllCongressProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, big.NewInt(2), big.NewInt(3), nil, nil, &CongressConfig{Period: 0, Epoch: 30000}}
129+
128130
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, nil}
129131
TestRules = TestChainConfig.Rules(new(big.Int))
130132
)
@@ -210,8 +212,8 @@ type ChainConfig struct {
210212
// the network that triggers the consensus upgrade.
211213
TerminalTotalDifficulty *big.Int `json:"terminalTotalDifficulty,omitempty"`
212214

213-
RedCoastBlock *big.Int `json:"redCoastBlock,omitempty"` // RedCoast switch block (nil = no fork, 0 = already activated)
214-
SophonBlock *big.Int `json:"sophonBlock,omitempty"`
215+
RedCoastBlock *big.Int `json:"redCoastBlock,omitempty"` // RedCoast switch block (nil = no fork, set value ≥ 2 to activate it)
216+
SophonBlock *big.Int `json:"sophonBlock,omitempty"` // Sophon switch block (nil = no fork, set > RedCoastBlock to activate it)
215217

216218
// Various consensus engines
217219
Ethash *EthashConfig `json:"ethash,omitempty"`
@@ -264,7 +266,7 @@ func (c *ChainConfig) String() string {
264266
default:
265267
engine = "unknown"
266268
}
267-
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, RedCoastBlock: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Engine: %v}",
269+
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, RedCoastBlock: %v, Berlin: %v, London: %v, Sophon: %v, Engine: %v}",
268270
c.ChainID,
269271
c.HomesteadBlock,
270272
c.DAOForkBlock,
@@ -280,7 +282,7 @@ func (c *ChainConfig) String() string {
280282
c.RedCoastBlock,
281283
c.BerlinBlock,
282284
c.LondonBlock,
283-
c.ArrowGlacierBlock,
285+
c.SophonBlock,
284286
engine,
285287
)
286288
}
@@ -395,6 +397,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
395397
name string
396398
block *big.Int
397399
optional bool // if true, the fork may be nil and next fork is still allowed
400+
minValue *big.Int
398401
}
399402
var lastFork fork
400403
for _, cur := range []fork{
@@ -408,20 +411,47 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
408411
{name: "petersburgBlock", block: c.PetersburgBlock},
409412
{name: "istanbulBlock", block: c.IstanbulBlock},
410413
{name: "muirGlacierBlock", block: c.MuirGlacierBlock, optional: true},
411-
{name: "redCoastBlock", block: c.RedCoastBlock, optional: true},
412414
{name: "berlinBlock", block: c.BerlinBlock},
413415
{name: "londonBlock", block: c.LondonBlock},
416+
} {
417+
if lastFork.name != "" {
418+
// Next one must be higher or the same number
419+
if lastFork.block == nil && cur.block != nil {
420+
return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
421+
lastFork.name, cur.name, cur.block)
422+
}
423+
if lastFork.block != nil && cur.block != nil {
424+
if lastFork.block.Cmp(cur.block) > 0 {
425+
return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
426+
lastFork.name, lastFork.block, cur.name, cur.block)
427+
}
428+
}
429+
}
430+
// If it was optional and not set, then ignore it
431+
if !cur.optional || cur.block != nil {
432+
lastFork = cur
433+
}
434+
}
435+
// congress fork
436+
lastFork = fork{}
437+
for _, cur := range []fork{
438+
{name: "redCoastBlock", block: c.RedCoastBlock, minValue: big.NewInt(2)},
414439
{name: "sophonBlock", block: c.SophonBlock},
415-
{name: "arrowGlacierBlock", block: c.ArrowGlacierBlock, optional: true},
416440
} {
441+
// check minimal fork block
442+
if cur.block != nil && cur.minValue != nil {
443+
if cur.block.Cmp(cur.minValue) < 0 {
444+
return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but it must greater than %v ", cur.name, cur.block, cur.minValue)
445+
}
446+
}
417447
if lastFork.name != "" {
418448
// Next one must be higher number
419449
if lastFork.block == nil && cur.block != nil {
420450
return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
421451
lastFork.name, cur.name, cur.block)
422452
}
423453
if lastFork.block != nil && cur.block != nil {
424-
if lastFork.block.Cmp(cur.block) > 0 {
454+
if lastFork.block.Cmp(cur.block) >= 0 {
425455
return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
426456
lastFork.name, lastFork.block, cur.name, cur.block)
427457
}

params/config_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ func TestCheckCompatible(t *testing.T) {
8787
RewindTo: 30,
8888
},
8989
},
90+
{
91+
stored: AllCongressProtocolChanges,
92+
new: AllCongressProtocolChanges,
93+
head: uint64(0),
94+
wantErr: nil,
95+
},
96+
{
97+
stored: AllCongressProtocolChanges,
98+
new: AllCongressProtocolChanges,
99+
head: uint64(100),
100+
wantErr: nil,
101+
},
90102
}
91103

92104
for _, test := range tests {
@@ -96,3 +108,31 @@ func TestCheckCompatible(t *testing.T) {
96108
}
97109
}
98110
}
111+
112+
func TestCheckConfigForkOrder(t *testing.T) {
113+
type test struct {
114+
new *ChainConfig
115+
isErr bool
116+
}
117+
tests := []test{
118+
{new: MainnetChainConfig},
119+
{new: TestnetChainConfig},
120+
{new: AllEthashProtocolChanges},
121+
{new: AllCongressProtocolChanges},
122+
{new: AllCliqueProtocolChanges},
123+
{new: TestChainConfig},
124+
{new: &ChainConfig{RedCoastBlock: big.NewInt(0)}, isErr: true},
125+
{new: &ChainConfig{RedCoastBlock: big.NewInt(1)}, isErr: true},
126+
{new: &ChainConfig{SophonBlock: big.NewInt(3)}, isErr: true},
127+
{new: &ChainConfig{RedCoastBlock: big.NewInt(2), SophonBlock: big.NewInt(2)}, isErr: true},
128+
}
129+
for _, tc := range tests {
130+
err := tc.new.CheckConfigForkOrder()
131+
if !tc.isErr && err != nil {
132+
t.Error(err)
133+
}
134+
if tc.isErr && err == nil {
135+
t.Errorf("can't checkout wrong config: %v\n", tc.new)
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)