Skip to content

Commit

Permalink
add initial in verifyheader (ethereum#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
liam-lai authored Jul 11, 2022
1 parent e55fca6 commit 90395f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
28 changes: 17 additions & 11 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,25 @@ func (x *XDPoS_v2) SignHash(header *types.Header) (hash common.Hash) {
return sigHash(header)
}

// Initial V2 related parameters
func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) error {
log.Info("[Initial] initial v2 related parameters")
return x.initial(chain, header)
}

func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) error {
log.Info("[initial] initial v2 related parameters")

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)
x.isInitilised = true
return nil
}

var quorumCert *types.QuorumCert
var err error

if header.Number.Int64() == x.config.V2.SwitchBlock.Int64() {
log.Info("[Initial] highest QC for consensus v2 first block")
log.Info("[initial] highest QC for consensus v2 first block")
blockInfo := &types.BlockInfo{
Hash: header.Hash(),
Round: types.Round(0),
Expand All @@ -159,7 +165,7 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) er
x.highestQuorumCert = quorumCert

} else {
log.Info("[Initial] highest QC from current header")
log.Info("[initial] highest QC from current header")
quorumCert, _, _, err = x.getExtraFields(header)
if err != nil {
return err
Expand All @@ -179,32 +185,33 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) er
if snap == nil {
checkpointHeader := chain.GetHeaderByNumber(x.config.V2.SwitchBlock.Uint64())

log.Info("[Initial] init first snapshot")
log.Info("[initial] init first snapshot")
_, _, masternodes, err := x.getExtraFields(checkpointHeader)
if err != nil {
log.Error("[Initial] Error while get masternodes", "error", err)
log.Error("[initial] Error while get masternodes", "error", err)
return err
}
snap := newSnapshot(lastGapNum, lastGapHeader.Hash(), masternodes)
x.snapshots.Add(snap.Hash, snap)
err = storeSnapshot(snap, x.db)
if err != nil {
log.Error("[Initial] Error while store snapshot", "error", err)
log.Error("[initial] Error while store snapshot", "error", err)
return err
}
}

// Initial timeout
log.Info("[Initial] miner wait period", "period", x.config.V2.WaitPeriod)
log.Info("[initial] miner wait period", "period", x.config.V2.WaitPeriod)
// avoid deadlock
go func() {
x.waitPeriodCh <- x.config.V2.WaitPeriod
}()

// Kick-off the countdown timer
x.timeoutWorker.Reset(chain)
x.isInitilised = true

log.Info("[Initial] finish initialisation")
log.Info("[initial] finish initialisation")

return nil
}
Expand All @@ -215,12 +222,11 @@ func (x *XDPoS_v2) YourTurn(chain consensus.ChainReader, parent *types.Header, s
defer x.lock.RUnlock()

if !x.isInitilised {
err := x.Initial(chain, parent)
err := x.initial(chain, parent)
if err != nil {
log.Error("[YourTurn] Error while initialising last v2 variables", "ParentBlockHash", parent.Hash(), "Error", err)
return false, err
}
x.isInitilised = true
}

waitedTime := time.Now().Unix() - parent.Time.Int64()
Expand Down
7 changes: 7 additions & 0 deletions consensus/XDPoS/engines/engine_v2/verifyHeader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func (x *XDPoS_v2) verifyHeader(chain consensus.ChainReader, header *types.Heade
if x.config.V2.SkipV2Validation {
return nil
}

if !x.isInitilised {
if err := x.initial(chain, header); err != nil {
return err
}
}

_, check := x.verifiedHeaders.Get(header.Hash())
if check {
return nil
Expand Down

0 comments on commit 90395f5

Please sign in to comment.