Skip to content

Commit 1063c65

Browse files
committed
refactor(consensus): split ChainReader interface ethereum#20692
1 parent 398d9b6 commit 1063c65

20 files changed

Lines changed: 125 additions & 119 deletions

File tree

consensus/XDPoS/XDPoS.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (x *XDPoS) UpdateParams(header *types.Header) {
166166
}
167167
}
168168

169-
func (x *XDPoS) Initial(chain consensus.ChainReader, header *types.Header) error {
169+
func (x *XDPoS) Initial(chain consensus.ChainHeaderReader, header *types.Header) error {
170170
switch x.config.BlockConsensusVersion(header.Number) {
171171
case params.ConsensusEngineVersion2:
172172
return x.EngineV2.Initial(chain, header)
@@ -309,7 +309,7 @@ func (x *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent
309309
}
310310
}
311311

312-
func (x *XDPoS) HandleProposedBlock(chain consensus.ChainReader, header *types.Header) error {
312+
func (x *XDPoS) HandleProposedBlock(chain consensus.ChainHeaderReader, header *types.Header) error {
313313
switch x.config.BlockConsensusVersion(header.Number) {
314314
case params.ConsensusEngineVersion2:
315315
return x.EngineV2.ProposedBlockHandler(chain, header)
@@ -343,7 +343,7 @@ func (x *XDPoS) IsAuthorisedAddress(chain consensus.ChainReader, header *types.H
343343
}
344344
}
345345

346-
func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
346+
func (x *XDPoS) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address {
347347
switch x.config.BlockConsensusVersion(header.Number) {
348348
case params.ConsensusEngineVersion2:
349349
return x.EngineV2.GetMasternodes(chain, header)
@@ -352,7 +352,7 @@ func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header
352352
}
353353
}
354354

355-
func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainReader, blockNumber uint64) []common.Address {
355+
func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainHeaderReader, blockNumber uint64) []common.Address {
356356
blockHeader := chain.GetHeaderByNumber(blockNumber)
357357
if blockHeader == nil {
358358
log.Error("[GetMasternodesByNumber] Unable to find block", "Num", blockNumber)
@@ -375,7 +375,7 @@ func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, sign
375375
}
376376
}
377377

378-
func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) {
378+
func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error) {
379379
switch x.config.BlockConsensusVersion(header.Number) {
380380
default: // Default "v1", v2 does not need this function
381381
return x.EngineV1.GetValidator(creator, chain, header)
@@ -430,7 +430,7 @@ func (x *XDPoS) IsEpochSwitch(header *types.Header) (bool, uint64, error) {
430430
}
431431
}
432432

433-
func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNumber *big.Int) (uint64, uint64, error) {
433+
func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainHeaderReader, blockNumber *big.Int) (uint64, uint64, error) {
434434
switch x.config.BlockConsensusVersion(blockNumber) {
435435
case params.ConsensusEngineVersion2:
436436
return x.EngineV2.GetCurrentEpochSwitchBlock(chain, blockNumber)
@@ -439,7 +439,7 @@ func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNum
439439
}
440440
}
441441

442-
func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) {
442+
func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainHeaderReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) {
443443
switch x.config.BlockConsensusVersion(header.Number) {
444444
case params.ConsensusEngineVersion2:
445445
return x.EngineV2.CalculateMissingRounds(chain, header)
@@ -554,7 +554,7 @@ func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) ([]*types.Transaction, boo
554554
return x.signingTxsCache.Get(hash)
555555
}
556556

557-
func (x *XDPoS) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) {
557+
func (x *XDPoS) GetEpochSwitchInfoBetween(chain consensus.ChainHeaderReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) {
558558
beginBlockVersion := x.config.BlockConsensusVersion(begin.Number)
559559
endBlockVersion := x.config.BlockConsensusVersion(end.Number)
560560
if beginBlockVersion == params.ConsensusEngineVersion2 && endBlockVersion == params.ConsensusEngineVersion2 {

consensus/XDPoS/engines/engine_v1/engine.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func (x *XDPoS_v1) StoreSnapshot(snap *SnapshotV1) error {
363363
return snap.store(x.db)
364364
}
365365

366-
func (x *XDPoS_v1) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
366+
func (x *XDPoS_v1) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address {
367367
n := header.Number.Uint64()
368368
e := x.config.Epoch
369369
switch {
@@ -549,7 +549,7 @@ func (x *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
549549

550550
// VerifyUncles implements consensus.Engine, always returning an error for any
551551
// uncles as this consensus mechanism doesn't permit uncles.
552-
func (x *XDPoS_v1) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
552+
func (x *XDPoS_v1) VerifyUncles(chain consensus.ChainHeaderReader, block *types.Block) error {
553553
if len(block.Uncles()) > 0 {
554554
return errors.New("uncles not allowed")
555555
}
@@ -657,7 +657,7 @@ func (x *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
657657
return nil
658658
}
659659

660-
func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) {
660+
func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error) {
661661
epoch := x.config.Epoch
662662
no := header.Number.Uint64()
663663
cpNo := no
@@ -1019,7 +1019,7 @@ func removePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common.
10191019
return masternodes
10201020
}
10211021

1022-
func (x *XDPoS_v1) getSignersFromContract(chain consensus.ChainReader, checkpointHeader *types.Header) ([]common.Address, error) {
1022+
func (x *XDPoS_v1) getSignersFromContract(chain consensus.ChainHeaderReader, checkpointHeader *types.Header) ([]common.Address, error) {
10231023
startGapBlockHeader := checkpointHeader
10241024
number := checkpointHeader.Number.Uint64()
10251025
for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ {

consensus/XDPoS/engines/engine_v2/difficulty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import (
99
)
1010

1111
// TODO: what should be new difficulty
12-
func (x *XDPoS_v2) calcDifficulty(chain consensus.ChainReader, parent *types.Header, signer common.Address) *big.Int {
12+
func (x *XDPoS_v2) calcDifficulty(chain consensus.ChainHeaderReader, parent *types.Header, signer common.Address) *big.Int {
1313
return big.NewInt(1)
1414
}

consensus/XDPoS/engines/engine_v2/engine.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ func (x *XDPoS_v2) SignHash(header *types.Header) (hash common.Hash) {
182182
}
183183

184184
// Initial V2 related parameters
185-
func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) error {
185+
func (x *XDPoS_v2) Initial(chain consensus.ChainHeaderReader, header *types.Header) error {
186186
x.lock.Lock()
187187
defer x.lock.Unlock()
188188

189189
return x.initial(chain, header)
190190
}
191191

192-
func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) error {
192+
func (x *XDPoS_v2) initial(chain consensus.ChainHeaderReader, header *types.Header) error {
193193
log.Warn("[initial] initial v2 related parameters")
194194

195195
if x.highestQuorumCert.ProposedBlockInfo.Hash != (common.Hash{}) { // already initialized
@@ -555,11 +555,11 @@ func (x *XDPoS_v2) Seal(chain consensus.ChainReader, block *types.Block, stop <-
555555
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
556556
// that a new block should have based on the previous blocks in the chain and the
557557
// current signer.
558-
func (x *XDPoS_v2) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
558+
func (x *XDPoS_v2) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
559559
return x.calcDifficulty(chain, parent, x.signer)
560560
}
561561

562-
func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainReader, header *types.Header, address common.Address) bool {
562+
func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainHeaderReader, header *types.Header, address common.Address) bool {
563563
snap, err := x.GetSnapshot(chain, header)
564564
if err != nil {
565565
log.Error("[IsAuthorisedAddress] Can't get snapshot with at ", "number", header.Number, "hash", header.Hash().Hex(), "err", err)
@@ -573,7 +573,7 @@ func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainReader, header *type
573573
return false
574574
}
575575

576-
func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*SnapshotV2, error) {
576+
func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainHeaderReader, header *types.Header) (*SnapshotV2, error) {
577577
number := header.Number.Uint64()
578578
log.Trace("get snapshot", "number", number)
579579
snap, err := x.getSnapshot(chain, number, false)
@@ -583,7 +583,7 @@ func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainReader, header *types.Header
583583
return snap, nil
584584
}
585585

586-
func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error {
586+
func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainHeaderReader, header *types.Header, ms []utils.Masternode) error {
587587
number := header.Number.Uint64()
588588
log.Trace("[UpdateMasternodes]", "number", number, "hash", header.Hash())
589589
if number%x.config.Epoch != x.config.Epoch-x.config.Gap {
@@ -617,7 +617,7 @@ func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types.
617617

618618
// VerifyUncles implements consensus.Engine, always returning an error for any
619619
// uncles as this consensus mechanism doesn't permit uncles.
620-
func (x *XDPoS_v2) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
620+
func (x *XDPoS_v2) VerifyUncles(chain consensus.ChainHeaderReader, block *types.Block) error {
621621
if len(block.Uncles()) > 0 {
622622
return errors.New("uncles not allowed in XDPoS_v2")
623623
}
@@ -652,7 +652,7 @@ func (x *XDPoS_v2) VerifyHeaders(chain consensus.ChainReader, headers []*types.H
652652
/*
653653
Proposed Block workflow
654654
*/
655-
func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainReader, blockHeader *types.Header) error {
655+
func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainHeaderReader, blockHeader *types.Header) error {
656656
x.lock.Lock()
657657
defer x.lock.Unlock()
658658

@@ -695,7 +695,7 @@ func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainReader, blockHeader
695695
*/
696696

697697
// To be used by different message verification. Verify local DB block info against the received block information(i.e hash, blockNum, round)
698-
func (x *XDPoS_v2) VerifyBlockInfo(blockChainReader consensus.ChainReader, blockInfo *types.BlockInfo, blockHeader *types.Header) error {
698+
func (x *XDPoS_v2) VerifyBlockInfo(blockChainReader consensus.ChainHeaderReader, blockInfo *types.BlockInfo, blockHeader *types.Header) error {
699699
/*
700700
1. Check if is able to get header by hash from the chain
701701
2. Check the header from step 1 matches what's in the blockInfo. This includes the block number and the round
@@ -742,7 +742,7 @@ func (x *XDPoS_v2) VerifyBlockInfo(blockChainReader consensus.ChainReader, block
742742
return nil
743743
}
744744

745-
func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainReader, quorumCert *types.QuorumCert, parentHeader *types.Header) error {
745+
func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainHeaderReader, quorumCert *types.QuorumCert, parentHeader *types.Header) error {
746746
if quorumCert == nil {
747747
log.Warn("[verifyQC] QC is Nil")
748748
return utils.ErrInvalidQC
@@ -817,7 +817,7 @@ func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainReader, quorumCert *
817817
}
818818

819819
// Update local QC variables including highestQC & lockQuorumCert, as well as commit the blocks that satisfy the algorithm requirements
820-
func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainReader, incomingQuorumCert *types.QuorumCert) error {
820+
func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainHeaderReader, incomingQuorumCert *types.QuorumCert) error {
821821
log.Debug("[processQC][Before]", "HighQC", x.highestQuorumCert.ProposedBlockInfo.Round)
822822
// 1. Update HighestQC
823823
if incomingQuorumCert.ProposedBlockInfo.Round > x.highestQuorumCert.ProposedBlockInfo.Round {
@@ -862,7 +862,7 @@ func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainReader, incomingQuo
862862
3. Reset vote and timeout Pools
863863
4. Send signal to miner
864864
*/
865-
func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainReader, round types.Round) {
865+
func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainHeaderReader, round types.Round) {
866866
log.Info("[setNewRound] new round and reset pools and workers", "round", round)
867867
x.currentRound = round
868868
x.timeoutCount = 0
@@ -891,7 +891,7 @@ func (x *XDPoS_v2) getSyncInfo() *types.SyncInfo {
891891
}
892892

893893
// Find parent and grandparent, check round number, if so, commit grandparent(grandGrandParent of currentBlock)
894-
func (x *XDPoS_v2) commitBlocks(blockChainReader consensus.ChainReader, proposedBlockHeader *types.Header, proposedBlockRound *types.Round, incomingQc *types.QuorumCert) (bool, error) {
894+
func (x *XDPoS_v2) commitBlocks(blockChainReader consensus.ChainHeaderReader, proposedBlockHeader *types.Header, proposedBlockRound *types.Round, incomingQc *types.QuorumCert) (bool, error) {
895895
// XDPoS v1.0 switch to v2.0, skip commit
896896
if big.NewInt(0).Sub(proposedBlockHeader.Number, big.NewInt(2)).Cmp(x.config.V2.SwitchBlock) <= 0 {
897897
return false, nil
@@ -961,7 +961,7 @@ func (x *XDPoS_v2) GetMasternodesFromEpochSwitchHeader(epochSwitchHeader *types.
961961
}
962962

963963
// Given header, get master node from the epoch switch block of that epoch
964-
func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
964+
func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address {
965965
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
966966
if err != nil {
967967
log.Error("[GetMasternodes] Adaptor v2 getEpochSwitchInfo has error", "err", err)
@@ -971,7 +971,7 @@ func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Hea
971971
}
972972

973973
// Given header, get master node from the epoch switch block of that epoch
974-
func (x *XDPoS_v2) GetPenalties(chain consensus.ChainReader, header *types.Header) []common.Address {
974+
func (x *XDPoS_v2) GetPenalties(chain consensus.ChainHeaderReader, header *types.Header) []common.Address {
975975
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
976976
if err != nil {
977977
log.Error("[GetPenalties] Adaptor v2 getEpochSwitchInfo has error", "err", err)
@@ -980,7 +980,7 @@ func (x *XDPoS_v2) GetPenalties(chain consensus.ChainReader, header *types.Heade
980980
return epochSwitchInfo.Penalties
981981
}
982982

983-
func (x *XDPoS_v2) GetStandbynodes(chain consensus.ChainReader, header *types.Header) []common.Address {
983+
func (x *XDPoS_v2) GetStandbynodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address {
984984
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
985985
if err != nil {
986986
log.Error("[GetStandbynodes] Adaptor v2 getEpochSwitchInfo has error", "err", err)
@@ -1030,7 +1030,7 @@ func (x *XDPoS_v2) calcMasternodes(chain consensus.ChainReader, blockNum *big.In
10301030
}
10311031

10321032
// Given hash, get master node from the epoch switch block of the epoch
1033-
func (x *XDPoS_v2) GetMasternodesByHash(chain consensus.ChainReader, hash common.Hash) []common.Address {
1033+
func (x *XDPoS_v2) GetMasternodesByHash(chain consensus.ChainHeaderReader, hash common.Hash) []common.Address {
10341034
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, nil, hash)
10351035
if err != nil {
10361036
log.Error("[GetMasternodes] Adaptor v2 getEpochSwitchInfo has error, potentially bug", "err", err)
@@ -1040,7 +1040,7 @@ func (x *XDPoS_v2) GetMasternodesByHash(chain consensus.ChainReader, hash common
10401040
}
10411041

10421042
// Given hash, get master node from the epoch switch block of the previous `limit` epoch
1043-
func (x *XDPoS_v2) GetPreviousPenaltyByHash(chain consensus.ChainReader, hash common.Hash, limit int) []common.Address {
1043+
func (x *XDPoS_v2) GetPreviousPenaltyByHash(chain consensus.ChainHeaderReader, hash common.Hash, limit int) []common.Address {
10441044
currentEpochSwitchInfo, err := x.getEpochSwitchInfo(chain, nil, hash)
10451045
if err != nil {
10461046
log.Error("[GetPreviousPenaltyByHash] Adaptor v2 getPreviousEpochSwitchInfoByHash has error, potentially bug", "err", err)
@@ -1071,7 +1071,7 @@ func (x *XDPoS_v2) FindParentBlockToAssign(chain consensus.ChainReader) *types.B
10711071
return parent
10721072
}
10731073

1074-
func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types.Header, sendType string) bool {
1074+
func (x *XDPoS_v2) allowedToSend(chain consensus.ChainHeaderReader, blockHeader *types.Header, sendType string) bool {
10751075
// Don't hold the signFn for the whole signing operation
10761076
x.signLock.RLock()
10771077
signer := x.signer

consensus/XDPoS/engines/engine_v2/epochSwitch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// Given header and its hash, get epoch switch info from the epoch switch block of that epoch,
1414
// header is allow to be nil.
15-
func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) {
15+
func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainHeaderReader, header *types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) {
1616
epochSwitchInfo, ok := x.epochSwitches.Get(hash)
1717
if ok && epochSwitchInfo != nil {
1818
log.Debug("[getEpochSwitchInfo] cache hit", "number", epochSwitchInfo.EpochSwitchBlockInfo.Number, "hash", hash.Hex())
@@ -131,7 +131,7 @@ func (x *XDPoS_v2) isEpochSwitchAtRound(round types.Round, parentHeader *types.H
131131
return parentRound < epochStartRound, epochNum, nil
132132
}
133133

134-
func (x *XDPoS_v2) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNum *big.Int) (uint64, uint64, error) {
134+
func (x *XDPoS_v2) GetCurrentEpochSwitchBlock(chain consensus.ChainHeaderReader, blockNum *big.Int) (uint64, uint64, error) {
135135
header := chain.GetHeaderByNumber(blockNum.Uint64())
136136
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash())
137137
if err != nil {
@@ -178,7 +178,7 @@ func (x *XDPoS_v2) IsEpochSwitch(header *types.Header) (bool, uint64, error) {
178178

179179
// GetEpochSwitchInfoBetween get epoch switch between begin and end headers
180180
// Search backwardly from end number to begin number
181-
func (x *XDPoS_v2) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) {
181+
func (x *XDPoS_v2) GetEpochSwitchInfoBetween(chain consensus.ChainHeaderReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) {
182182
infos := make([]*types.EpochSwitchInfo, 0)
183183
// after the first iteration, it becomes nil since epoch switch info does not have header info
184184
iteratorHeader := end

0 commit comments

Comments
 (0)