From 125f8a8957e7e088f2169e756ea0024c7cf9ac18 Mon Sep 17 00:00:00 2001 From: Jianrong Date: Sat, 19 Feb 2022 16:17:27 +1100 Subject: [PATCH] Split initialise v2 into two scenarios --- consensus/XDPoS/XDPoS.go | 23 ++++++++++++++------- consensus/XDPoS/engines/engine_v2/engine.go | 10 ++++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index df2f283a9e..3dfb9efc27 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -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) { - if x.config.V2.SwitchBlock != nil && parent.Number.Cmp(x.config.V2.SwitchBlock) == 0 { - err := x.initialV2(chain, parent) - if err != nil { - log.Error("[YourTurn] Error when initialise v2", "Error", err, "ParentBlock", parent) - return false, err + if x.config.V2.SwitchBlock != nil && parent.Number.Cmp(x.config.V2.SwitchBlock) != -1 { + if parent.Number.Cmp(x.config.V2.SwitchBlock) == 0 { + err := x.initialV2FromLastV1(chain, parent) + if err != nil { + 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)) { @@ -481,12 +485,15 @@ func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) (interface{}, bool) { return x.signingTxsCache.Get(hash) } -//V2 -func (x *XDPoS) initialV2(chain consensus.ChainReader, header *types.Header) error { +// V2 specific helper function to initilise consensus engine variables +func (x *XDPoS) initialV2FromLastV1(chain consensus.ChainReader, header *types.Header) error { checkpointBlockNumber := header.Number.Uint64() - header.Number.Uint64()%x.config.Epoch checkpointHeader := chain.GetHeaderByNumber(checkpointBlockNumber) 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 } diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index edbbc1cfae..1ed03b3633 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -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 { 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") return nil } @@ -128,7 +128,7 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, ma defer x.lock.Unlock() // 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 blockInfo := &utils.BlockInfo{ 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) 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 log.Info("[Initial] miner wait period", "period", x.config.WaitPeriod)