check v2 switch block is epoch number (#342)

* check v2 switch block is epoch number

* revert sync pr

* add test

* make default block number valid

* fix log

* fix test
This commit is contained in:
Liam 2023-10-30 19:03:34 +11:00 committed by GitHub
parent 95f19e9684
commit 24d02fe2b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 17 deletions

View file

@ -77,7 +77,7 @@ func (w *wizard) makeGenesis() {
genesis.Difficulty = big.NewInt(1) genesis.Difficulty = big.NewInt(1)
genesis.Config.Clique = &params.CliqueConfig{ genesis.Config.Clique = &params.CliqueConfig{
Period: 15, Period: 15,
Epoch: 30000, Epoch: 900,
} }
fmt.Println() fmt.Println()
fmt.Println("How many seconds should blocks take? (default = 15)") fmt.Println("How many seconds should blocks take? (default = 15)")
@ -114,7 +114,7 @@ func (w *wizard) makeGenesis() {
genesis.Difficulty = big.NewInt(1) genesis.Difficulty = big.NewInt(1)
genesis.Config.XDPoS = &params.XDPoSConfig{ genesis.Config.XDPoS = &params.XDPoSConfig{
Period: 15, Period: 15,
Epoch: 30000, Epoch: 900,
Reward: 0, Reward: 0,
V2: &params.V2{ V2: &params.V2{
SwitchBlock: big.NewInt(0), SwitchBlock: big.NewInt(0),

View file

@ -36,7 +36,7 @@ var TIP2019Block = big.NewInt(1)
var TIPSigning = big.NewInt(3000000) var TIPSigning = big.NewInt(3000000)
var TIPRandomize = big.NewInt(3464000) var TIPRandomize = big.NewInt(3464000)
var TIPV2SwitchBlock = big.NewInt(99999999999) var TIPV2SwitchBlock = big.NewInt(99999999900)
var TIPIncreaseMasternodes = big.NewInt(5000000) // Upgrade MN Count at Block. var TIPIncreaseMasternodes = big.NewInt(5000000) // Upgrade MN Count at Block.
var TIPNoHalvingMNReward = big.NewInt(38383838) // hardfork no halving masternodes reward var TIPNoHalvingMNReward = big.NewInt(38383838) // hardfork no halving masternodes reward

View file

@ -36,7 +36,7 @@ var TIP2019Block = big.NewInt(1)
var TIPSigning = big.NewInt(3000000) var TIPSigning = big.NewInt(3000000)
var TIPRandomize = big.NewInt(3464000) var TIPRandomize = big.NewInt(3464000)
var TIPV2SwitchBlock = big.NewInt(56000000) var TIPV2SwitchBlock = big.NewInt(56430000)
var TIPIncreaseMasternodes = big.NewInt(5000000) // Upgrade MN Count at Block. var TIPIncreaseMasternodes = big.NewInt(5000000) // Upgrade MN Count at Block.
var TIPNoHalvingMNReward = big.NewInt(23779191) // hardfork no halving masternodes reward var TIPNoHalvingMNReward = big.NewInt(23779191) // hardfork no halving masternodes reward

View file

@ -17,6 +17,7 @@
package XDPoS package XDPoS
import ( import (
"fmt"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -95,7 +96,11 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS {
} }
} }
log.Info("xdc config loading", "config", config) if config.V2.SwitchBlock.Uint64()%config.Epoch != 0 {
panic(fmt.Sprintf("v2 switch number is not epoch switch block %d, epoch %d", config.V2.SwitchBlock.Uint64(), config.Epoch))
}
log.Info("xdc config loading", "v2 config", config.V2)
minePeriodCh := make(chan int) minePeriodCh := make(chan int)

View file

@ -164,7 +164,7 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) er
} }
func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) error {
log.Info("[initial] initial v2 related parameters") log.Warn("[initial] initial v2 related parameters")
if x.highestQuorumCert.ProposedBlockInfo.Hash != (common.Hash{}) { // already initialized if x.highestQuorumCert.ProposedBlockInfo.Hash != (common.Hash{}) { // already initialized
log.Info("[initial] Already initialized", "x.highestQuorumCert.ProposedBlockInfo.Hash", x.highestQuorumCert.ProposedBlockInfo.Hash) log.Info("[initial] Already initialized", "x.highestQuorumCert.ProposedBlockInfo.Hash", x.highestQuorumCert.ProposedBlockInfo.Hash)
@ -219,6 +219,12 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er
log.Error("[initial] Error while get masternodes", "error", err) log.Error("[initial] Error while get masternodes", "error", err)
return err return err
} }
if len(masternodes) == 0 {
log.Error("[initial] masternodes are empty", "v2switch", x.config.V2.SwitchBlock.Uint64())
return fmt.Errorf("masternodes are empty v2 switch number: %d", x.config.V2.SwitchBlock.Uint64())
}
snap := newSnapshot(lastGapNum, lastGapHeader.Hash(), masternodes) snap := newSnapshot(lastGapNum, lastGapHeader.Hash(), masternodes)
x.snapshots.Add(snap.Hash, snap) x.snapshots.Add(snap.Hash, snap)
err = storeSnapshot(snap, x.db) err = storeSnapshot(snap, x.db)
@ -229,7 +235,7 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er
} }
// Initial timeout // Initial timeout
log.Info("[initial] miner wait period", "period", x.config.V2.CurrentConfig.MinePeriod) log.Warn("[initial] miner wait period", "period", x.config.V2.CurrentConfig.MinePeriod)
// avoid deadlock // avoid deadlock
go func() { go func() {
x.minePeriodCh <- x.config.V2.CurrentConfig.MinePeriod x.minePeriodCh <- x.config.V2.CurrentConfig.MinePeriod
@ -239,7 +245,7 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er
x.timeoutWorker.Reset(chain) x.timeoutWorker.Reset(chain)
x.isInitilised = true x.isInitilised = true
log.Info("[initial] finish initialisation") log.Warn("[initial] finish initialisation")
return nil return nil
} }

View file

@ -1,6 +1,7 @@
package engine_v2_tests package engine_v2_tests
import ( import (
"encoding/json"
"math/big" "math/big"
"testing" "testing"
@ -124,3 +125,21 @@ func TestSnapshotShouldAlreadyCreatedByUpdateM1(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, uint64(1350), snap.Number) assert.Equal(t, uint64(1350), snap.Number)
} }
func TestInitialWithWrongSwitchNumber(t *testing.T) {
b, err := json.Marshal(params.TestXDPoSMockChainConfig)
assert.Nil(t, err)
configString := string(b)
var config params.ChainConfig
err = json.Unmarshal([]byte(configString), &config)
assert.Nil(t, err)
blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 800, &config, nil)
adaptor := blockchain.Engine().(*XDPoS.XDPoS)
header := currentBlock.Header()
config.XDPoS.V2.SwitchBlock = big.NewInt(800) // not epoch number
err = adaptor.EngineV2.Initial(blockchain, header)
assert.NotNil(t, err)
}

View file

@ -45,7 +45,7 @@ var (
CertThreshold: 73, // based on masternode is 108 CertThreshold: 73, // based on masternode is 108
TimeoutSyncThreshold: 3, TimeoutSyncThreshold: 3,
TimeoutPeriod: 60, TimeoutPeriod: 60,
MinePeriod: 10, MinePeriod: 2,
}, },
} }
@ -197,7 +197,7 @@ var (
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
XDPoS: &XDPoSConfig{ XDPoS: &XDPoSConfig{
Period: 15, Period: 15,
Epoch: 30000, Epoch: 900,
V2: &V2{ V2: &V2{
SwitchBlock: big.NewInt(9999999999), SwitchBlock: big.NewInt(9999999999),
CurrentConfig: MainnetV2Configs[0], CurrentConfig: MainnetV2Configs[0],
@ -218,8 +218,8 @@ var (
// //
// This configuration is intentionally not using keyed fields to force anyone // This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields. // adding flags to the config to also have to set these fields.
AllXDPoSProtocolChanges = &ChainConfig{big.NewInt(89), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &XDPoSConfig{Period: 0, Epoch: 30000}} AllXDPoSProtocolChanges = &ChainConfig{big.NewInt(89), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &XDPoSConfig{Period: 0, Epoch: 900}}
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil} AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 900}, nil}
// XDPoS config with v2 engine after block 901 // XDPoS config with v2 engine after block 901
TestXDPoSMockChainConfig = &ChainConfig{ TestXDPoSMockChainConfig = &ChainConfig{
@ -337,10 +337,6 @@ func (c *XDPoSConfig) String() string {
} }
func (c *XDPoSConfig) BlockConsensusVersion(num *big.Int, extraByte []byte, extraCheck bool) string { func (c *XDPoSConfig) BlockConsensusVersion(num *big.Int, extraByte []byte, extraCheck bool) string {
if extraCheck && (len(extraByte) == 0 || extraByte[0] != 2) {
return ConsensusEngineVersion1
}
if c.V2 != nil && c.V2.SwitchBlock != nil && num.Cmp(c.V2.SwitchBlock) > 0 { if c.V2 != nil && c.V2.SwitchBlock != nil && num.Cmp(c.V2.SwitchBlock) > 0 {
return ConsensusEngineVersion2 return ConsensusEngineVersion2
} }
@ -361,7 +357,7 @@ func (v *V2) UpdateConfig(round uint64) {
} }
} }
// update to current config // update to current config
log.Info("[updateV2Config] Update config", "index", index, "round", round, "SwitchRound", v.AllConfigs[index].SwitchRound) log.Warn("[updateV2Config] Update config", "index", index, "round", round, "SwitchRound", v.AllConfigs[index].SwitchRound)
v.CurrentConfig = v.AllConfigs[index] v.CurrentConfig = v.AllConfigs[index]
} }