Split initialise v2 into two scenarios

This commit is contained in:
Jianrong 2022-02-19 16:17:27 +11:00
parent 89acbdd742
commit 125f8a8957
2 changed files with 22 additions and 11 deletions

View file

@ -314,11 +314,15 @@ func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainReader, blockNumber
} }
func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (bool, error) { func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (bool, error) {
if x.config.V2.SwitchBlock != nil && parent.Number.Cmp(x.config.V2.SwitchBlock) == 0 { if x.config.V2.SwitchBlock != nil && parent.Number.Cmp(x.config.V2.SwitchBlock) != -1 {
err := x.initialV2(chain, parent) if parent.Number.Cmp(x.config.V2.SwitchBlock) == 0 {
if err != nil { err := x.initialV2FromLastV1(chain, parent)
log.Error("[YourTurn] Error when initialise v2", "Error", err, "ParentBlock", parent) if err != nil {
return false, err log.Error("[YourTurn] Error while initilising first v2 block from the last v1 block", "ParentBlockHash", parent.Hash(), "Error", err)
return false, err
}
} else if parent.Number.Cmp(x.config.V2.SwitchBlock) == 1 { // TODO: XIN-147
log.Info("[YourTurn] Initilising v2 after sync or restarted", "currentBlockNum", chain.CurrentHeader().Number, "currentBlockHash", chain.CurrentHeader().Hash())
} }
} }
switch x.config.BlockConsensusVersion(big.NewInt(parent.Number.Int64() + 1)) { switch x.config.BlockConsensusVersion(big.NewInt(parent.Number.Int64() + 1)) {
@ -481,12 +485,15 @@ func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) (interface{}, bool) {
return x.signingTxsCache.Get(hash) return x.signingTxsCache.Get(hash)
} }
//V2 // V2 specific helper function to initilise consensus engine variables
func (x *XDPoS) initialV2(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS) initialV2FromLastV1(chain consensus.ChainReader, header *types.Header) error {
checkpointBlockNumber := header.Number.Uint64() - header.Number.Uint64()%x.config.Epoch checkpointBlockNumber := header.Number.Uint64() - header.Number.Uint64()%x.config.Epoch
checkpointHeader := chain.GetHeaderByNumber(checkpointBlockNumber) checkpointHeader := chain.GetHeaderByNumber(checkpointBlockNumber)
masternodes := x.EngineV1.GetMasternodesFromCheckpointHeader(checkpointHeader) masternodes := x.EngineV1.GetMasternodesFromCheckpointHeader(checkpointHeader)
x.EngineV2.Initial(chain, header, masternodes) err := x.EngineV2.Initial(chain, header, masternodes)
if err != nil {
return err
}
return nil return nil
} }

View file

@ -119,7 +119,7 @@ func (x *XDPoS_v2) SignHash(header *types.Header) (hash common.Hash) {
func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, masternodes []common.Address) error { func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, masternodes []common.Address) error {
log.Info("[Initial] initial v2 related parameters") log.Info("[Initial] initial v2 related parameters")
if x.highestQuorumCert.ProposedBlockInfo.Round != 0 { //already initialized if x.highestQuorumCert.ProposedBlockInfo.Round != 0 { // already initialized
log.Warn("[Initial] Already initialized") log.Warn("[Initial] Already initialized")
return nil return nil
} }
@ -128,7 +128,7 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, ma
defer x.lock.Unlock() defer x.lock.Unlock()
// Check header if it is the first consensus v2 block, if so, assign initial values to current round and highestQC // Check header if it is the first consensus v2 block, if so, assign initial values to current round and highestQC
log.Info("[Initial] highest QC for consensus v2 first block", "Block Num", header.Number.String(), "BlockHash", header.Hash()) log.Info("[Initial] highest QC for consensus v2 first block", "BlockNum", header.Number.String(), "BlockHash", header.Hash())
// Generate new parent blockInfo and put it into QC // Generate new parent blockInfo and put it into QC
blockInfo := &utils.BlockInfo{ blockInfo := &utils.BlockInfo{
Hash: header.Hash(), Hash: header.Hash(),
@ -148,7 +148,11 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, ma
snap := newSnapshot(lastGapNum, lastGapHeader.Hash(), x.currentRound, x.highestQuorumCert, masternodes) snap := newSnapshot(lastGapNum, lastGapHeader.Hash(), x.currentRound, x.highestQuorumCert, masternodes)
x.snapshots.Add(snap.Hash, snap) x.snapshots.Add(snap.Hash, snap)
storeSnapshot(snap, x.db) err := storeSnapshot(snap, x.db)
if err != nil {
log.Error("[Initial] Error while storo snapshot", "error", err)
return err
}
// Initial timeout // Initial timeout
log.Info("[Initial] miner wait period", "period", x.config.WaitPeriod) log.Info("[Initial] miner wait period", "period", x.config.WaitPeriod)