mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 13:21:37 +00:00
Add backward-compatible XDPoSConfig JSON decoding for the legacy key "foudationWalletAddr" introduced before PR #2063 renamed it to "foundationWalletAddr". Without this compatibility layer, old on-disk chain configs are decoded with a zero FoundationWalletAddr, causing XDPoSConfigEqual mismatch and startup rewind ("mismatching XDPoS not equal in database"). This patch: - Implements custom UnmarshalJSON for XDPoSConfig that reads both keys. - Prefers foundationWalletAddr when both keys are present. - Keeps existing behavior for all other fields. - Adds regression tests for legacy-key decoding and precedence. Validation: - go test ./params/...
172 lines
5.6 KiB
Go
172 lines
5.6 KiB
Go
// Copyright 2017 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package params
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/big"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCheckCompatible(t *testing.T) {
|
|
type test struct {
|
|
stored, new *ChainConfig
|
|
head uint64
|
|
wantErr *ConfigCompatError
|
|
}
|
|
tests := []test{
|
|
{stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 0, wantErr: nil},
|
|
{stored: AllEthashProtocolChanges, new: AllEthashProtocolChanges, head: 100, wantErr: nil},
|
|
{
|
|
stored: &ChainConfig{EIP150Block: big.NewInt(10)},
|
|
new: &ChainConfig{EIP150Block: big.NewInt(20)},
|
|
head: 9,
|
|
wantErr: nil,
|
|
},
|
|
{
|
|
stored: AllEthashProtocolChanges,
|
|
new: &ChainConfig{HomesteadBlock: nil},
|
|
head: 3,
|
|
wantErr: &ConfigCompatError{
|
|
What: "Homestead fork block",
|
|
StoredConfig: big.NewInt(0),
|
|
NewConfig: nil,
|
|
RewindTo: 0,
|
|
},
|
|
},
|
|
{
|
|
stored: AllEthashProtocolChanges,
|
|
new: &ChainConfig{HomesteadBlock: big.NewInt(1)},
|
|
head: 3,
|
|
wantErr: &ConfigCompatError{
|
|
What: "Homestead fork block",
|
|
StoredConfig: big.NewInt(0),
|
|
NewConfig: big.NewInt(1),
|
|
RewindTo: 0,
|
|
},
|
|
},
|
|
{
|
|
stored: &ChainConfig{HomesteadBlock: big.NewInt(30), EIP150Block: big.NewInt(10)},
|
|
new: &ChainConfig{HomesteadBlock: big.NewInt(25), EIP150Block: big.NewInt(20)},
|
|
head: 25,
|
|
wantErr: &ConfigCompatError{
|
|
What: "EIP150 fork block",
|
|
StoredConfig: big.NewInt(10),
|
|
NewConfig: big.NewInt(20),
|
|
RewindTo: 9,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
err := test.stored.CheckCompatible(test.new, test.head)
|
|
if !reflect.DeepEqual(err, test.wantErr) {
|
|
t.Errorf("error mismatch:\nstored: %v\nnew: %v\nhead: %v\nerr: %v\nwant: %v", test.stored, test.new, test.head, err, test.wantErr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUpdateV2Config(t *testing.T) {
|
|
TestXDPoSMockChainConfig.XDPoS.V2.BuildConfigIndex()
|
|
c := TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig
|
|
assert.Equal(t, 0.667, c.CertThreshold)
|
|
|
|
TestXDPoSMockChainConfig.XDPoS.V2.UpdateConfig(10)
|
|
c = TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig
|
|
assert.Equal(t, float64(0.667), c.CertThreshold)
|
|
|
|
TestXDPoSMockChainConfig.XDPoS.V2.UpdateConfig(900)
|
|
c = TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig
|
|
assert.Equal(t, 4, c.TimeoutSyncThreshold)
|
|
}
|
|
|
|
func TestV2Config(t *testing.T) {
|
|
TestXDPoSMockChainConfig.XDPoS.V2.BuildConfigIndex()
|
|
c := TestXDPoSMockChainConfig.XDPoS.V2.Config(1)
|
|
assert.Equal(t, 0.667, c.CertThreshold)
|
|
|
|
c = TestXDPoSMockChainConfig.XDPoS.V2.Config(5)
|
|
assert.Equal(t, 0.667, c.CertThreshold)
|
|
|
|
c = TestXDPoSMockChainConfig.XDPoS.V2.Config(10)
|
|
assert.Equal(t, 0.667, c.CertThreshold)
|
|
|
|
c = TestXDPoSMockChainConfig.XDPoS.V2.Config(11)
|
|
assert.Equal(t, float64(0.667), c.CertThreshold)
|
|
}
|
|
|
|
func TestBuildConfigIndex(t *testing.T) {
|
|
TestXDPoSMockChainConfig.XDPoS.V2.BuildConfigIndex()
|
|
index := TestXDPoSMockChainConfig.XDPoS.V2.ConfigIndex()
|
|
expected := []uint64{900, 10, 0}
|
|
assert.Equal(t, expected, index)
|
|
}
|
|
|
|
func TestBuildConfigIndexDescendingOrder(t *testing.T) {
|
|
v2 := &V2{
|
|
AllConfigs: map[uint64]*V2Config{
|
|
5: {SwitchRound: 5},
|
|
2: {SwitchRound: 2},
|
|
10: {SwitchRound: 10},
|
|
0: {SwitchRound: 0},
|
|
15: {SwitchRound: 15},
|
|
},
|
|
}
|
|
v2.BuildConfigIndex()
|
|
assert.Equal(t, []uint64{15, 10, 5, 2, 0}, v2.ConfigIndex())
|
|
}
|
|
|
|
// Test switch epoch is switchblock divide into epoch per block
|
|
func TestSwitchEpoch(t *testing.T) {
|
|
config := XDCMainnetChainConfig.XDPoS
|
|
epoch := config.Epoch
|
|
assert.Equal(t, config.V2.SwitchEpoch, config.V2.SwitchBlock.Uint64()/epoch)
|
|
|
|
config = TestnetChainConfig.XDPoS
|
|
epoch = config.Epoch
|
|
assert.Equal(t, config.V2.SwitchEpoch, config.V2.SwitchBlock.Uint64()/epoch)
|
|
|
|
config = DevnetChainConfig.XDPoS
|
|
epoch = config.Epoch
|
|
assert.Equal(t, config.V2.SwitchEpoch, config.V2.SwitchBlock.Uint64()/epoch)
|
|
|
|
config = TestXDPoSMockChainConfig.XDPoS
|
|
epoch = config.Epoch
|
|
assert.Equal(t, config.V2.SwitchEpoch, config.V2.SwitchBlock.Uint64()/epoch)
|
|
}
|
|
|
|
func TestXDPoSConfigUnmarshalLegacyFoundationWalletAddr(t *testing.T) {
|
|
const raw = `{"period":2,"epoch":900,"reward":5000,"rewardCheckpoint":900,"gap":450,"foudationWalletAddr":"xdc746249c61f5832c5eed53172776b460491bdcd5c"}`
|
|
|
|
var cfg XDPoSConfig
|
|
err := json.Unmarshal([]byte(raw), &cfg)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, common.HexToAddress("xdc746249c61f5832c5eed53172776b460491bdcd5c"), cfg.FoundationWalletAddr)
|
|
}
|
|
|
|
func TestXDPoSConfigUnmarshalFoundationWalletAddrPrecedence(t *testing.T) {
|
|
const raw = `{"period":2,"epoch":900,"reward":5000,"rewardCheckpoint":900,"gap":450,"foudationWalletAddr":"xdc746249c61f5832c5eed53172776b460491bdcd5c","foundationWalletAddr":"xdc92a289fe95a85c53b8d0d113cbaef0c1ec98ac65"}`
|
|
|
|
var cfg XDPoSConfig
|
|
err := json.Unmarshal([]byte(raw), &cfg)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, common.HexToAddress("xdc92a289fe95a85c53b8d0d113cbaef0c1ec98ac65"), cfg.FoundationWalletAddr)
|
|
}
|