diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index 0edf37312005..1e596fe7883f 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -166,7 +166,7 @@ func (x *XDPoS) UpdateParams(header *types.Header) { } } -func (x *XDPoS) Initial(chain consensus.ChainReader, header *types.Header) error { +func (x *XDPoS) Initial(chain consensus.ChainHeaderReader, header *types.Header) error { switch x.config.BlockConsensusVersion(header.Number) { case params.ConsensusEngineVersion2: return x.EngineV2.Initial(chain, header) @@ -309,7 +309,7 @@ func (x *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent } } -func (x *XDPoS) HandleProposedBlock(chain consensus.ChainReader, header *types.Header) error { +func (x *XDPoS) HandleProposedBlock(chain consensus.ChainHeaderReader, header *types.Header) error { switch x.config.BlockConsensusVersion(header.Number) { case params.ConsensusEngineVersion2: return x.EngineV2.ProposedBlockHandler(chain, header) @@ -343,7 +343,7 @@ func (x *XDPoS) IsAuthorisedAddress(chain consensus.ChainReader, header *types.H } } -func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address { +func (x *XDPoS) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address { switch x.config.BlockConsensusVersion(header.Number) { case params.ConsensusEngineVersion2: return x.EngineV2.GetMasternodes(chain, header) @@ -352,7 +352,7 @@ func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header } } -func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainReader, blockNumber uint64) []common.Address { +func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainHeaderReader, blockNumber uint64) []common.Address { blockHeader := chain.GetHeaderByNumber(blockNumber) if blockHeader == nil { log.Error("[GetMasternodesByNumber] Unable to find block", "Num", blockNumber) @@ -375,7 +375,7 @@ func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, sign } } -func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) { +func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error) { switch x.config.BlockConsensusVersion(header.Number) { default: // Default "v1", v2 does not need this function return x.EngineV1.GetValidator(creator, chain, header) @@ -430,7 +430,7 @@ func (x *XDPoS) IsEpochSwitch(header *types.Header) (bool, uint64, error) { } } -func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNumber *big.Int) (uint64, uint64, error) { +func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainHeaderReader, blockNumber *big.Int) (uint64, uint64, error) { switch x.config.BlockConsensusVersion(blockNumber) { case params.ConsensusEngineVersion2: return x.EngineV2.GetCurrentEpochSwitchBlock(chain, blockNumber) @@ -439,7 +439,7 @@ func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNum } } -func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) { +func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainHeaderReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) { switch x.config.BlockConsensusVersion(header.Number) { case params.ConsensusEngineVersion2: return x.EngineV2.CalculateMissingRounds(chain, header) @@ -554,7 +554,7 @@ func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) ([]*types.Transaction, boo return x.signingTxsCache.Get(hash) } -func (x *XDPoS) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) { +func (x *XDPoS) GetEpochSwitchInfoBetween(chain consensus.ChainHeaderReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) { beginBlockVersion := x.config.BlockConsensusVersion(begin.Number) endBlockVersion := x.config.BlockConsensusVersion(end.Number) if beginBlockVersion == params.ConsensusEngineVersion2 && endBlockVersion == params.ConsensusEngineVersion2 { diff --git a/consensus/XDPoS/engines/engine_v1/engine.go b/consensus/XDPoS/engines/engine_v1/engine.go index c83b4d08d007..cb4b992a01f5 100644 --- a/consensus/XDPoS/engines/engine_v1/engine.go +++ b/consensus/XDPoS/engines/engine_v1/engine.go @@ -363,7 +363,7 @@ func (x *XDPoS_v1) StoreSnapshot(snap *SnapshotV1) error { return snap.store(x.db) } -func (x *XDPoS_v1) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address { +func (x *XDPoS_v1) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address { n := header.Number.Uint64() e := x.config.Epoch switch { @@ -549,7 +549,7 @@ func (x *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com // VerifyUncles implements consensus.Engine, always returning an error for any // uncles as this consensus mechanism doesn't permit uncles. -func (x *XDPoS_v1) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { +func (x *XDPoS_v1) VerifyUncles(chain consensus.ChainHeaderReader, block *types.Block) error { if len(block.Uncles()) > 0 { return errors.New("uncles not allowed") } @@ -657,7 +657,7 @@ func (x *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header, return nil } -func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) { +func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error) { epoch := x.config.Epoch no := header.Number.Uint64() cpNo := no @@ -1019,7 +1019,7 @@ func removePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common. return masternodes } -func (x *XDPoS_v1) getSignersFromContract(chain consensus.ChainReader, checkpointHeader *types.Header) ([]common.Address, error) { +func (x *XDPoS_v1) getSignersFromContract(chain consensus.ChainHeaderReader, checkpointHeader *types.Header) ([]common.Address, error) { startGapBlockHeader := checkpointHeader number := checkpointHeader.Number.Uint64() for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ { diff --git a/consensus/XDPoS/engines/engine_v2/difficulty.go b/consensus/XDPoS/engines/engine_v2/difficulty.go index 342afa7219a3..e5263df806fe 100644 --- a/consensus/XDPoS/engines/engine_v2/difficulty.go +++ b/consensus/XDPoS/engines/engine_v2/difficulty.go @@ -9,6 +9,6 @@ import ( ) // TODO: what should be new difficulty -func (x *XDPoS_v2) calcDifficulty(chain consensus.ChainReader, parent *types.Header, signer common.Address) *big.Int { +func (x *XDPoS_v2) calcDifficulty(chain consensus.ChainHeaderReader, parent *types.Header, signer common.Address) *big.Int { return big.NewInt(1) } diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 90c8096d19f2..ef6a96eb45d5 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -182,14 +182,14 @@ func (x *XDPoS_v2) SignHash(header *types.Header) (hash common.Hash) { } // Initial V2 related parameters -func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header) error { +func (x *XDPoS_v2) Initial(chain consensus.ChainHeaderReader, header *types.Header) error { x.lock.Lock() defer x.lock.Unlock() return x.initial(chain, header) } -func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) error { +func (x *XDPoS_v2) initial(chain consensus.ChainHeaderReader, header *types.Header) error { log.Warn("[initial] initial v2 related parameters") 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 <- // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // that a new block should have based on the previous blocks in the chain and the // current signer. -func (x *XDPoS_v2) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { +func (x *XDPoS_v2) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { return x.calcDifficulty(chain, parent, x.signer) } -func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainReader, header *types.Header, address common.Address) bool { +func (x *XDPoS_v2) IsAuthorisedAddress(chain consensus.ChainHeaderReader, header *types.Header, address common.Address) bool { snap, err := x.GetSnapshot(chain, header) if err != nil { 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 return false } -func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*SnapshotV2, error) { +func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainHeaderReader, header *types.Header) (*SnapshotV2, error) { number := header.Number.Uint64() log.Trace("get snapshot", "number", number) snap, err := x.getSnapshot(chain, number, false) @@ -583,7 +583,7 @@ func (x *XDPoS_v2) GetSnapshot(chain consensus.ChainReader, header *types.Header return snap, nil } -func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error { +func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainHeaderReader, header *types.Header, ms []utils.Masternode) error { number := header.Number.Uint64() log.Trace("[UpdateMasternodes]", "number", number, "hash", header.Hash()) 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. // VerifyUncles implements consensus.Engine, always returning an error for any // uncles as this consensus mechanism doesn't permit uncles. -func (x *XDPoS_v2) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { +func (x *XDPoS_v2) VerifyUncles(chain consensus.ChainHeaderReader, block *types.Block) error { if len(block.Uncles()) > 0 { return errors.New("uncles not allowed in XDPoS_v2") } @@ -652,7 +652,7 @@ func (x *XDPoS_v2) VerifyHeaders(chain consensus.ChainReader, headers []*types.H /* Proposed Block workflow */ -func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainReader, blockHeader *types.Header) error { +func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainHeaderReader, blockHeader *types.Header) error { x.lock.Lock() defer x.lock.Unlock() @@ -695,7 +695,7 @@ func (x *XDPoS_v2) ProposedBlockHandler(chain consensus.ChainReader, blockHeader */ // To be used by different message verification. Verify local DB block info against the received block information(i.e hash, blockNum, round) -func (x *XDPoS_v2) VerifyBlockInfo(blockChainReader consensus.ChainReader, blockInfo *types.BlockInfo, blockHeader *types.Header) error { +func (x *XDPoS_v2) VerifyBlockInfo(blockChainReader consensus.ChainHeaderReader, blockInfo *types.BlockInfo, blockHeader *types.Header) error { /* 1. Check if is able to get header by hash from the chain 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 return nil } -func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainReader, quorumCert *types.QuorumCert, parentHeader *types.Header) error { +func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainHeaderReader, quorumCert *types.QuorumCert, parentHeader *types.Header) error { if quorumCert == nil { log.Warn("[verifyQC] QC is Nil") return utils.ErrInvalidQC @@ -817,7 +817,7 @@ func (x *XDPoS_v2) verifyQC(blockChainReader consensus.ChainReader, quorumCert * } // Update local QC variables including highestQC & lockQuorumCert, as well as commit the blocks that satisfy the algorithm requirements -func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainReader, incomingQuorumCert *types.QuorumCert) error { +func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainHeaderReader, incomingQuorumCert *types.QuorumCert) error { log.Debug("[processQC][Before]", "HighQC", x.highestQuorumCert.ProposedBlockInfo.Round) // 1. Update HighestQC if incomingQuorumCert.ProposedBlockInfo.Round > x.highestQuorumCert.ProposedBlockInfo.Round { @@ -862,7 +862,7 @@ func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainReader, incomingQuo 3. Reset vote and timeout Pools 4. Send signal to miner */ -func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainReader, round types.Round) { +func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainHeaderReader, round types.Round) { log.Info("[setNewRound] new round and reset pools and workers", "round", round) x.currentRound = round x.timeoutCount = 0 @@ -891,7 +891,7 @@ func (x *XDPoS_v2) getSyncInfo() *types.SyncInfo { } // Find parent and grandparent, check round number, if so, commit grandparent(grandGrandParent of currentBlock) -func (x *XDPoS_v2) commitBlocks(blockChainReader consensus.ChainReader, proposedBlockHeader *types.Header, proposedBlockRound *types.Round, incomingQc *types.QuorumCert) (bool, error) { +func (x *XDPoS_v2) commitBlocks(blockChainReader consensus.ChainHeaderReader, proposedBlockHeader *types.Header, proposedBlockRound *types.Round, incomingQc *types.QuorumCert) (bool, error) { // XDPoS v1.0 switch to v2.0, skip commit if big.NewInt(0).Sub(proposedBlockHeader.Number, big.NewInt(2)).Cmp(x.config.V2.SwitchBlock) <= 0 { return false, nil @@ -961,7 +961,7 @@ func (x *XDPoS_v2) GetMasternodesFromEpochSwitchHeader(epochSwitchHeader *types. } // Given header, get master node from the epoch switch block of that epoch -func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address { +func (x *XDPoS_v2) GetMasternodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash()) if err != nil { 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 } // Given header, get master node from the epoch switch block of that epoch -func (x *XDPoS_v2) GetPenalties(chain consensus.ChainReader, header *types.Header) []common.Address { +func (x *XDPoS_v2) GetPenalties(chain consensus.ChainHeaderReader, header *types.Header) []common.Address { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash()) if err != nil { 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 return epochSwitchInfo.Penalties } -func (x *XDPoS_v2) GetStandbynodes(chain consensus.ChainReader, header *types.Header) []common.Address { +func (x *XDPoS_v2) GetStandbynodes(chain consensus.ChainHeaderReader, header *types.Header) []common.Address { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash()) if err != nil { 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 } // Given hash, get master node from the epoch switch block of the epoch -func (x *XDPoS_v2) GetMasternodesByHash(chain consensus.ChainReader, hash common.Hash) []common.Address { +func (x *XDPoS_v2) GetMasternodesByHash(chain consensus.ChainHeaderReader, hash common.Hash) []common.Address { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, nil, hash) if err != nil { 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 } // Given hash, get master node from the epoch switch block of the previous `limit` epoch -func (x *XDPoS_v2) GetPreviousPenaltyByHash(chain consensus.ChainReader, hash common.Hash, limit int) []common.Address { +func (x *XDPoS_v2) GetPreviousPenaltyByHash(chain consensus.ChainHeaderReader, hash common.Hash, limit int) []common.Address { currentEpochSwitchInfo, err := x.getEpochSwitchInfo(chain, nil, hash) if err != nil { 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 return parent } -func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types.Header, sendType string) bool { +func (x *XDPoS_v2) allowedToSend(chain consensus.ChainHeaderReader, blockHeader *types.Header, sendType string) bool { // Don't hold the signFn for the whole signing operation x.signLock.RLock() signer := x.signer diff --git a/consensus/XDPoS/engines/engine_v2/epochSwitch.go b/consensus/XDPoS/engines/engine_v2/epochSwitch.go index 9bcc06198625..ae3b6be549d9 100644 --- a/consensus/XDPoS/engines/engine_v2/epochSwitch.go +++ b/consensus/XDPoS/engines/engine_v2/epochSwitch.go @@ -12,7 +12,7 @@ import ( // Given header and its hash, get epoch switch info from the epoch switch block of that epoch, // header is allow to be nil. -func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) { +func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainHeaderReader, header *types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) { epochSwitchInfo, ok := x.epochSwitches.Get(hash) if ok && epochSwitchInfo != nil { 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 return parentRound < epochStartRound, epochNum, nil } -func (x *XDPoS_v2) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNum *big.Int) (uint64, uint64, error) { +func (x *XDPoS_v2) GetCurrentEpochSwitchBlock(chain consensus.ChainHeaderReader, blockNum *big.Int) (uint64, uint64, error) { header := chain.GetHeaderByNumber(blockNum.Uint64()) epochSwitchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash()) if err != nil { @@ -178,7 +178,7 @@ func (x *XDPoS_v2) IsEpochSwitch(header *types.Header) (bool, uint64, error) { // GetEpochSwitchInfoBetween get epoch switch between begin and end headers // Search backwardly from end number to begin number -func (x *XDPoS_v2) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) { +func (x *XDPoS_v2) GetEpochSwitchInfoBetween(chain consensus.ChainHeaderReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) { infos := make([]*types.EpochSwitchInfo, 0) // after the first iteration, it becomes nil since epoch switch info does not have header info iteratorHeader := end diff --git a/consensus/XDPoS/engines/engine_v2/forensics.go b/consensus/XDPoS/engines/engine_v2/forensics.go index 3a483e481dd6..f67763c53923 100644 --- a/consensus/XDPoS/engines/engine_v2/forensics.go +++ b/consensus/XDPoS/engines/engine_v2/forensics.go @@ -38,7 +38,7 @@ func (f *Forensics) SubscribeForensicsEvent(ch chan<- types.ForensicsEvent) even return f.scope.Track(f.forensicsFeed.Subscribe(ch)) } -func (f *Forensics) ForensicsMonitoring(chain consensus.ChainReader, engine *XDPoS_v2, headerQcToBeCommitted []types.Header, incomingQC types.QuorumCert) error { +func (f *Forensics) ForensicsMonitoring(chain consensus.ChainHeaderReader, engine *XDPoS_v2, headerQcToBeCommitted []types.Header, incomingQC types.QuorumCert) error { f.ProcessForensics(chain, engine, incomingQC) return f.SetCommittedQCs(headerQcToBeCommitted, incomingQC) } @@ -78,7 +78,7 @@ func (f *Forensics) SetCommittedQCs(headers []types.Header, incomingQC types.Quo return nil } -func (f *Forensics) ProcessForensics(chain consensus.ChainReader, engine *XDPoS_v2, incomingQC types.QuorumCert) error { +func (f *Forensics) ProcessForensics(chain consensus.ChainHeaderReader, engine *XDPoS_v2, incomingQC types.QuorumCert) error { return nil } @@ -129,7 +129,7 @@ func (f *Forensics) ProcessForensics(chain consensus.ChainReader, engine *XDPoS_ */ // Last step of forensics which sends out detailed proof to report service. -func (f *Forensics) SendForensicProof(chain consensus.ChainReader, engine *XDPoS_v2, firstQc types.QuorumCert, secondQc types.QuorumCert) error { +func (f *Forensics) SendForensicProof(chain consensus.ChainHeaderReader, engine *XDPoS_v2, firstQc types.QuorumCert, secondQc types.QuorumCert) error { // Re-order the QC by its round number to make the function cleaner. lowerRoundQC := firstQc higherRoundQC := secondQc @@ -234,7 +234,7 @@ func (f *Forensics) getQcSignerAddresses(quorumCert types.QuorumCert) []string { return signerList } -func (f *Forensics) FindAncestorBlockHash(chain consensus.ChainReader, firstBlockInfo *types.BlockInfo, secondBlockInfo *types.BlockInfo) (common.Hash, []string, []string, error) { +func (f *Forensics) FindAncestorBlockHash(chain consensus.ChainHeaderReader, firstBlockInfo *types.BlockInfo, secondBlockInfo *types.BlockInfo) (common.Hash, []string, []string, error) { // Re-arrange by block number lowerBlockNumHash := firstBlockInfo.Hash higherBlockNumberHash := secondBlockInfo.Hash @@ -299,7 +299,7 @@ func generateVoteEquivocationId(signer common.Address, round1, round2 types.Roun return fmt.Sprintf("%x:%d:%d", signer, round1, round2) } -func (f *Forensics) ProcessVoteEquivocation(chain consensus.ChainReader, engine *XDPoS_v2, incomingVote *types.Vote) error { +func (f *Forensics) ProcessVoteEquivocation(chain consensus.ChainHeaderReader, engine *XDPoS_v2, incomingVote *types.Vote) error { return nil } diff --git a/consensus/XDPoS/engines/engine_v2/snapshot.go b/consensus/XDPoS/engines/engine_v2/snapshot.go index 7c45a6a107ef..04eab2465697 100644 --- a/consensus/XDPoS/engines/engine_v2/snapshot.go +++ b/consensus/XDPoS/engines/engine_v2/snapshot.go @@ -75,7 +75,7 @@ func (s *SnapshotV2) IsCandidates(address common.Address) bool { } // snapshot retrieves the authorization snapshot at a given point in time. -func (x *XDPoS_v2) getSnapshot(chain consensus.ChainReader, number uint64, isGapNumber bool) (*SnapshotV2, error) { +func (x *XDPoS_v2) getSnapshot(chain consensus.ChainHeaderReader, number uint64, isGapNumber bool) (*SnapshotV2, error) { var gapBlockNum uint64 if isGapNumber { gapBlockNum = number diff --git a/consensus/XDPoS/engines/engine_v2/syncInfo.go b/consensus/XDPoS/engines/engine_v2/syncInfo.go index 38838e3704c4..e9707225105b 100644 --- a/consensus/XDPoS/engines/engine_v2/syncInfo.go +++ b/consensus/XDPoS/engines/engine_v2/syncInfo.go @@ -17,7 +17,7 @@ import ( ) // Verify syncInfo and trigger process QC or TC if successful -func (x *XDPoS_v2) VerifySyncInfoMessage(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) { +func (x *XDPoS_v2) VerifySyncInfoMessage(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) (bool, error) { qc := syncInfo.HighestQuorumCert tc := syncInfo.HighestTimeoutCert @@ -68,14 +68,14 @@ func (x *XDPoS_v2) VerifySyncInfoMessage(chain consensus.ChainReader, syncInfo * return true, nil } -func (x *XDPoS_v2) SyncInfoHandler(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { +func (x *XDPoS_v2) SyncInfoHandler(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) error { x.lock.Lock() defer x.lock.Unlock() x.syncInfoPool.Add(syncInfo) // Add syncInfo to the pool, in case this is valid syncInfo but chain is not sync to latest height return x.syncInfoHandler(chain, syncInfo) } -func (x *XDPoS_v2) syncInfoHandler(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { +func (x *XDPoS_v2) syncInfoHandler(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) error { qc := syncInfo.HighestQuorumCert tc := syncInfo.HighestTimeoutCert @@ -108,7 +108,7 @@ func (x *XDPoS_v2) syncInfoHandler(chain consensus.ChainReader, syncInfo *types. return nil } -func (x *XDPoS_v2) processSyncInfoPool(chain consensus.ChainReader) { +func (x *XDPoS_v2) processSyncInfoPool(chain consensus.ChainHeaderReader) { syncInfo := x.syncInfoPool.PoolObjKeysList() for _, key := range syncInfo { log.Debug("[processSyncInfoPool] Processing syncInfo message from pool", "key", key) diff --git a/consensus/XDPoS/engines/engine_v2/testing_utils.go b/consensus/XDPoS/engines/engine_v2/testing_utils.go index 36d616422091..0770a621c79a 100644 --- a/consensus/XDPoS/engines/engine_v2/testing_utils.go +++ b/consensus/XDPoS/engines/engine_v2/testing_utils.go @@ -10,7 +10,7 @@ import ( Testing tools */ -func (x *XDPoS_v2) SetNewRoundFaker(blockChainReader consensus.ChainReader, newRound types.Round, resetTimer bool) { +func (x *XDPoS_v2) SetNewRoundFaker(blockChainReader consensus.ChainHeaderReader, newRound types.Round, resetTimer bool) { x.lock.Lock() defer x.lock.Unlock() // Reset a bunch of things @@ -21,7 +21,7 @@ func (x *XDPoS_v2) SetNewRoundFaker(blockChainReader consensus.ChainReader, newR } // for test only -func (x *XDPoS_v2) ProcessQCFaker(chain consensus.ChainReader, qc *types.QuorumCert) error { +func (x *XDPoS_v2) ProcessQCFaker(chain consensus.ChainHeaderReader, qc *types.QuorumCert) error { x.lock.Lock() defer x.lock.Unlock() return x.processQC(chain, qc) diff --git a/consensus/XDPoS/engines/engine_v2/timeout.go b/consensus/XDPoS/engines/engine_v2/timeout.go index 15f3028d1d3c..e8aae2f81db4 100644 --- a/consensus/XDPoS/engines/engine_v2/timeout.go +++ b/consensus/XDPoS/engines/engine_v2/timeout.go @@ -17,7 +17,7 @@ import ( "golang.org/x/sync/errgroup" ) -func (x *XDPoS_v2) VerifyTimeoutMessage(chain consensus.ChainReader, timeoutMsg *types.Timeout) (bool, error) { +func (x *XDPoS_v2) VerifyTimeoutMessage(chain consensus.ChainHeaderReader, timeoutMsg *types.Timeout) (bool, error) { if timeoutMsg.Round < x.currentRound { log.Debug("[VerifyTimeoutMessage] Disqualified timeout message as the proposed round does not match currentRound", "timeoutHash", timeoutMsg.Hash(), "timeoutRound", timeoutMsg.Round, "currentRound", x.currentRound) return false, nil @@ -46,13 +46,13 @@ func (x *XDPoS_v2) VerifyTimeoutMessage(chain consensus.ChainReader, timeoutMsg /* Entry point for handling timeout message to process below: */ -func (x *XDPoS_v2) TimeoutHandler(blockChainReader consensus.ChainReader, timeout *types.Timeout) error { +func (x *XDPoS_v2) TimeoutHandler(blockChainReader consensus.ChainHeaderReader, timeout *types.Timeout) error { x.lock.Lock() defer x.lock.Unlock() return x.timeoutHandler(blockChainReader, timeout) } -func (x *XDPoS_v2) timeoutHandler(blockChainReader consensus.ChainReader, timeout *types.Timeout) error { +func (x *XDPoS_v2) timeoutHandler(blockChainReader consensus.ChainHeaderReader, timeout *types.Timeout) error { // checkRoundNumber if timeout.Round != x.currentRound { return &utils.ErrIncomingMessageRoundNotEqualCurrentRound{ @@ -91,7 +91,7 @@ In the engine v2, we will need to: 2. processTC() 3. generateSyncInfo() */ -func (x *XDPoS_v2) onTimeoutPoolThresholdReached(blockChainReader consensus.ChainReader, pooledTimeouts map[common.Hash]utils.PoolObj, currentTimeoutMsg utils.PoolObj, gapNumber uint64) error { +func (x *XDPoS_v2) onTimeoutPoolThresholdReached(blockChainReader consensus.ChainHeaderReader, pooledTimeouts map[common.Hash]utils.PoolObj, currentTimeoutMsg utils.PoolObj, gapNumber uint64) error { signatures := []types.Signature{} for _, v := range pooledTimeouts { signatures = append(signatures, v.(*types.Timeout).Signature) @@ -113,7 +113,7 @@ func (x *XDPoS_v2) onTimeoutPoolThresholdReached(blockChainReader consensus.Chai return nil } -func (x *XDPoS_v2) getTCEpochInfo(chain consensus.ChainReader, timeoutRound types.Round) (*types.EpochSwitchInfo, error) { +func (x *XDPoS_v2) getTCEpochInfo(chain consensus.ChainHeaderReader, timeoutRound types.Round) (*types.EpochSwitchInfo, error) { epochSwitchInfo, err := x.getEpochSwitchInfo(chain, (chain.CurrentHeader()), (chain.CurrentHeader()).Hash()) if err != nil { log.Error("[getTCEpochInfo] Error when getting epoch switch info", "error", err) @@ -149,7 +149,7 @@ func (x *XDPoS_v2) getTCEpochInfo(chain consensus.ChainReader, timeoutRound type return epochInfo, nil } -func (x *XDPoS_v2) verifyTC(chain consensus.ChainReader, timeoutCert *types.TimeoutCert) error { +func (x *XDPoS_v2) verifyTC(chain consensus.ChainHeaderReader, timeoutCert *types.TimeoutCert) error { if timeoutCert == nil || timeoutCert.Signatures == nil { log.Warn("[verifyTC] TC or TC signatures is Nil") return utils.ErrInvalidTC @@ -225,7 +225,7 @@ func (x *XDPoS_v2) verifyTC(chain consensus.ChainReader, timeoutCert *types.Time 1. Update highestTC 2. Check TC round >= node's currentRound. If yes, call setNewRound */ -func (x *XDPoS_v2) processTC(blockChainReader consensus.ChainReader, timeoutCert *types.TimeoutCert) error { +func (x *XDPoS_v2) processTC(blockChainReader consensus.ChainHeaderReader, timeoutCert *types.TimeoutCert) error { if x.highestTimeoutCert.Round < timeoutCert.Round { x.highestTimeoutCert = timeoutCert } @@ -241,7 +241,7 @@ func (x *XDPoS_v2) processTC(blockChainReader consensus.ChainReader, timeoutCert 2. Sign the signature 3. send to broadcast channel */ -func (x *XDPoS_v2) sendTimeout(chain consensus.ChainReader) error { +func (x *XDPoS_v2) sendTimeout(chain consensus.ChainHeaderReader) error { // Construct the gapNumber var gapNumber uint64 currentBlockHeader := chain.CurrentHeader() @@ -310,13 +310,13 @@ func (x *XDPoS_v2) OnCountdownTimeout(time time.Time, chain interface{}) error { defer x.lock.Unlock() // Check if we are within the master node list - allow := x.allowedToSend(chain.(consensus.ChainReader), chain.(consensus.ChainReader).CurrentHeader(), "timeout") + allow := x.allowedToSend(chain.(consensus.ChainHeaderReader), chain.(consensus.ChainHeaderReader).CurrentHeader(), "timeout") if !allow { return nil } - x.processSyncInfoPool(chain.(consensus.ChainReader)) + x.processSyncInfoPool(chain.(consensus.ChainHeaderReader)) - err := x.sendTimeout(chain.(consensus.ChainReader)) + err := x.sendTimeout(chain.(consensus.ChainHeaderReader)) if err != nil { log.Error("Error while sending out timeout message at time: ", "time", time, "err", err) return err diff --git a/consensus/XDPoS/engines/engine_v2/utils.go b/consensus/XDPoS/engines/engine_v2/utils.go index 1b4119d29df7..413201da9eaa 100644 --- a/consensus/XDPoS/engines/engine_v2/utils.go +++ b/consensus/XDPoS/engines/engine_v2/utils.go @@ -164,7 +164,7 @@ func (x *XDPoS_v2) GetRoundNumber(header *types.Header) (types.Round, error) { } } -func (x *XDPoS_v2) GetSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) { +func (x *XDPoS_v2) GetSignersFromSnapshot(chain consensus.ChainHeaderReader, header *types.Header) ([]common.Address, error) { snap, err := x.getSnapshot(chain, header.Number.Uint64(), false) if err != nil { return nil, err @@ -172,7 +172,7 @@ func (x *XDPoS_v2) GetSignersFromSnapshot(chain consensus.ChainReader, header *t return snap.NextEpochCandidates, err } -func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) { +func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainHeaderReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) { var missedRounds []utils.MissedRoundInfo switchInfo, err := x.getEpochSwitchInfo(chain, header, header.Hash()) if err != nil { @@ -229,7 +229,7 @@ func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainReader, header *t return missedRoundsMetadata, nil } -func (x *XDPoS_v2) getBlockByEpochNumberInCache(chain consensus.ChainReader, estRound types.Round) *types.BlockInfo { +func (x *XDPoS_v2) getBlockByEpochNumberInCache(chain consensus.ChainHeaderReader, estRound types.Round) *types.BlockInfo { epochSwitchInCache := make([]*types.BlockInfo, 0) for r := estRound; r < estRound+types.Round(x.config.Epoch); r++ { blockInfo, ok := x.round2epochBlockInfo.Get(r) @@ -255,7 +255,7 @@ func (x *XDPoS_v2) getBlockByEpochNumberInCache(chain consensus.ChainReader, est return nil } -func (x *XDPoS_v2) binarySearchBlockByEpochNumber(chain consensus.ChainReader, targetEpochNum uint64, start, end uint64) (*types.BlockInfo, *types.Header, error) { +func (x *XDPoS_v2) binarySearchBlockByEpochNumber(chain consensus.ChainHeaderReader, targetEpochNum uint64, start, end uint64) (*types.BlockInfo, *types.Header, error) { // `end` must be larger than the target and `start` could be the target for start < end { header := chain.GetHeaderByNumber((start + end) / 2) @@ -305,7 +305,7 @@ func (x *XDPoS_v2) binarySearchBlockByEpochNumber(chain consensus.ChainReader, t return nil, nil, errors.New("no epoch switch header in binary search (all rounds in this epoch are missed, which is very rare)") } -func (x *XDPoS_v2) GetBlockByEpochNumber(chain consensus.ChainReader, targetEpochNum uint64) (*types.BlockInfo, error) { +func (x *XDPoS_v2) GetBlockByEpochNumber(chain consensus.ChainHeaderReader, targetEpochNum uint64) (*types.BlockInfo, error) { currentHeader := chain.CurrentHeader() if currentHeader == nil { return nil, errors.New("current header is nil") diff --git a/consensus/XDPoS/engines/engine_v2/vote.go b/consensus/XDPoS/engines/engine_v2/vote.go index b43a2813774f..dd88521064f5 100644 --- a/consensus/XDPoS/engines/engine_v2/vote.go +++ b/consensus/XDPoS/engines/engine_v2/vote.go @@ -16,7 +16,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/log" ) -func (x *XDPoS_v2) VerifyVoteMessage(chain consensus.ChainReader, vote *types.Vote) (bool, error) { +func (x *XDPoS_v2) VerifyVoteMessage(chain consensus.ChainHeaderReader, vote *types.Vote) (bool, error) { if vote.ProposedBlockInfo.Round < x.currentRound { log.Debug("[VerifyVoteMessage] Disqualified vote message as the proposed round does not match currentRound", "voteHash", vote.Hash(), "voteProposedBlockInfoRound", vote.ProposedBlockInfo.Round, "currentRound", x.currentRound) return false, nil @@ -53,14 +53,14 @@ func (x *XDPoS_v2) VerifyVoteMessage(chain consensus.ChainReader, vote *types.Vo } // Consensus entry point for processing vote message to produce QC -func (x *XDPoS_v2) VoteHandler(chain consensus.ChainReader, voteMsg *types.Vote) error { +func (x *XDPoS_v2) VoteHandler(chain consensus.ChainHeaderReader, voteMsg *types.Vote) error { x.lock.Lock() defer x.lock.Unlock() return x.voteHandler(chain, voteMsg) } // Once Hot stuff voting rule has verified, this node can then send vote -func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *types.BlockInfo) error { +func (x *XDPoS_v2) sendVote(chainReader consensus.ChainHeaderReader, blockInfo *types.BlockInfo) error { // First step: Update the highest Voted round // Second step: Generate the signature by using node's private key(The signature is the blockInfo signature) // Third step: Construct the vote struct with the above signature & blockinfo struct @@ -103,7 +103,7 @@ func (x *XDPoS_v2) sendVote(chainReader consensus.ChainReader, blockInfo *types. return nil } -func (x *XDPoS_v2) voteHandler(chain consensus.ChainReader, voteMsg *types.Vote) error { +func (x *XDPoS_v2) voteHandler(chain consensus.ChainHeaderReader, voteMsg *types.Vote) error { // checkRoundNumber if (voteMsg.ProposedBlockInfo.Round != x.currentRound) && (voteMsg.ProposedBlockInfo.Round != x.currentRound+1) { return &utils.ErrIncomingMessageRoundTooFarFromCurrentRound{ @@ -167,7 +167,7 @@ func (x *XDPoS_v2) voteHandler(chain consensus.ChainReader, voteMsg *types.Vote) return nil } -func (x *XDPoS_v2) verifyVotes(chain consensus.ChainReader, votes map[common.Hash]utils.PoolObj, header *types.Header) { +func (x *XDPoS_v2) verifyVotes(chain consensus.ChainHeaderReader, votes map[common.Hash]utils.PoolObj, header *types.Header) { masternodes := x.GetMasternodes(chain, header) start := time.Now() emptySigner := common.Address{} @@ -218,7 +218,7 @@ func (x *XDPoS_v2) verifyVotes(chain consensus.ChainReader, votes map[common.Has Function that will be called by votePool when it reached threshold. In the engine v2, we will need to generate and process QC */ -func (x *XDPoS_v2) onVotePoolThresholdReached(chain consensus.ChainReader, pooledVotes map[common.Hash]utils.PoolObj, currentVoteMsg utils.PoolObj, proposedBlockHeader *types.Header) error { +func (x *XDPoS_v2) onVotePoolThresholdReached(chain consensus.ChainHeaderReader, pooledVotes map[common.Hash]utils.PoolObj, currentVoteMsg utils.PoolObj, proposedBlockHeader *types.Header) error { // The signature list may contain empty entey. we only care the ones with values var validSignatures []types.Signature emptySigner := common.Address{} @@ -256,7 +256,7 @@ func (x *XDPoS_v2) onVotePoolThresholdReached(chain consensus.ChainReader, poole } // Hot stuff rule to decide whether this node is eligible to vote for the received block -func (x *XDPoS_v2) verifyVotingRule(blockChainReader consensus.ChainReader, blockInfo *types.BlockInfo, quorumCert *types.QuorumCert) (bool, error) { +func (x *XDPoS_v2) verifyVotingRule(blockChainReader consensus.ChainHeaderReader, blockInfo *types.BlockInfo, quorumCert *types.QuorumCert) (bool, error) { // Make sure this node has not voted for this round. if x.currentRound <= x.highestVotedRound { log.Info("Failed to pass the voting rule verification, currentRound is not large then highestVoteRound", "x.currentRound", x.currentRound, "x.highestVotedRound", x.highestVotedRound) @@ -295,7 +295,7 @@ func (x *XDPoS_v2) verifyVotingRule(blockChainReader consensus.ChainReader, bloc return true, nil } -func (x *XDPoS_v2) isExtendingFromAncestor(blockChainReader consensus.ChainReader, currentBlock *types.BlockInfo, ancestorBlock *types.BlockInfo) (bool, error) { +func (x *XDPoS_v2) isExtendingFromAncestor(blockChainReader consensus.ChainHeaderReader, currentBlock *types.BlockInfo, ancestorBlock *types.BlockInfo) (bool, error) { blockNumDiff := int(big.NewInt(0).Sub(currentBlock.Number, ancestorBlock.Number).Int64()) nextBlockHash := currentBlock.Hash diff --git a/consensus/clique/api.go b/consensus/clique/api.go index a8c8aeb440f0..279a1cfc7d8a 100644 --- a/consensus/clique/api.go +++ b/consensus/clique/api.go @@ -26,7 +26,7 @@ import ( // API is a user facing RPC API to allow controlling the signer and voting // mechanisms of the proof-of-authority scheme. type API struct { - chain consensus.ChainReader + chain consensus.ChainHeaderReader clique *Clique } diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index b9f6ec343f9f..ffc7d7c429e0 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -241,14 +241,14 @@ func (c *Clique) Author(header *types.Header) (common.Address, error) { } // VerifyHeader checks whether a header conforms to the consensus rules. -func (c *Clique) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { +func (c *Clique) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error { return c.verifyHeader(chain, header, nil) } // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The // method returns a quit channel to abort the operations and a results channel to // retrieve the async verifications (the order is that of the input slice). -func (c *Clique) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { +func (c *Clique) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { abort := make(chan struct{}) results := make(chan error, len(headers)) @@ -270,7 +270,7 @@ func (c *Clique) VerifyHeaders(chain consensus.ChainReader, headers []*types.Hea // caller may optionally pass in a batch of parents (ascending order) to avoid // looking those up from the database. This is useful for concurrently verifying // a batch of new headers. -func (c *Clique) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { if header.Number == nil { return errUnknownBlock } @@ -339,7 +339,7 @@ func (c *Clique) verifyHeader(chain consensus.ChainReader, header *types.Header, // rather depend on a batch of previous headers. The caller may optionally pass // in a batch of parents (ascending order) to avoid looking those up from the // database. This is useful for concurrently verifying a batch of new headers. -func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { // The genesis block is the always valid dead-end number := header.Number.Uint64() if number == 0 { @@ -384,7 +384,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type } // snapshot retrieves the authorization snapshot at a given point in time. -func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { +func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { // Search for a snapshot in memory or on disk for checkpoints var ( headers []*types.Header @@ -471,7 +471,7 @@ func (c *Clique) VerifyUncles(chain consensus.ChainReader, block *types.Block) e // VerifySeal implements consensus.Engine, checking whether the signature contained // in the header satisfies the consensus protocol requirements. -func (c *Clique) VerifySeal(chain consensus.ChainReader, header *types.Header) error { +func (c *Clique) VerifySeal(chain consensus.ChainHeaderReader, header *types.Header) error { return c.verifySeal(chain, header, nil) } @@ -479,7 +479,7 @@ func (c *Clique) VerifySeal(chain consensus.ChainReader, header *types.Header) e // consensus protocol requirements. The method accepts an optional list of parent // headers that aren't yet part of the local blockchain to generate the snapshots // from. -func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { +func (c *Clique) verifySeal(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { // Verifying the genesis block is not supported number := header.Number.Uint64() if number == 0 { @@ -520,7 +520,7 @@ func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, p // Prepare implements consensus.Engine, preparing all the consensus fields of the // header for running the transactions on top. -func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) error { +func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { // If the block isn't a checkpoint, cast a random vote (good enough for now) header.Coinbase = common.Address{} header.Nonce = types.BlockNonce{} @@ -585,7 +585,7 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro // Finalize implements consensus.Engine, ensuring no uncles are set, nor block // rewards given, and returns the final block. -func (c *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state vm.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { +func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { // No block rewards in PoA, so the state remains as is and uncles are dropped header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) header.UncleHash = types.CalcUncleHash(nil) @@ -606,7 +606,7 @@ func (c *Clique) Authorize(signer common.Address, signFn SignerFn) { // Seal implements consensus.Engine, attempting to create a sealed block using // the local signing credentials. -func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { +func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { header := block.Header() // Sealing the genesis block is not supported @@ -672,7 +672,7 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, stop <-ch // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // that a new block should have based on the previous blocks in the chain and the // current signer. -func (c *Clique) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { +func (c *Clique) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) if err != nil { return nil @@ -692,7 +692,7 @@ func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int { // APIs implements consensus.Engine, returning the user facing RPC API to allow // controlling the signer voting. -func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API { +func (c *Clique) APIs(chain consensus.ChainHeaderReader) []rpc.API { return []rpc.API{{ Namespace: "clique", Service: &API{chain: chain, clique: c}, diff --git a/consensus/consensus.go b/consensus/consensus.go index d50479b35976..3fe8ecf9bf5a 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -28,9 +28,9 @@ import ( "github.com/XinFinOrg/XDPoSChain/rpc" ) -// ChainReader defines a small collection of methods needed to access the local +// ChainHeaderReader defines a small collection of methods needed to access the local // blockchain during header and/or uncle verification. -type ChainReader interface { +type ChainHeaderReader interface { // Config retrieves the blockchain's chain configuration. Config() *params.ChainConfig @@ -45,6 +45,12 @@ type ChainReader interface { // GetHeaderByHash retrieves a block header from the database by its hash. GetHeaderByHash(hash common.Hash) *types.Header +} + +// ChainReader defines a small collection of methods needed to access the local +// blockchain during header and/or uncle verification. +type ChainReader interface { + ChainHeaderReader // GetBlock retrieves a block from the database by hash and number. GetBlock(hash common.Hash, number uint64) *types.Block diff --git a/eth/bft/bft_handler.go b/eth/bft/bft_handler.go index 228621752315..28a1904601ee 100644 --- a/eth/bft/bft_handler.go +++ b/eth/bft/bft_handler.go @@ -22,7 +22,7 @@ type chainHeightFn func() uint64 type Bfter struct { epoch uint64 - blockChainReader consensus.ChainReader + blockChainReader consensus.ChainHeaderReader broadcastCh chan interface{} quit chan struct{} consensus ConsensusFns @@ -31,14 +31,14 @@ type Bfter struct { } type ConsensusFns struct { - verifyVote func(consensus.ChainReader, *types.Vote) (bool, error) - voteHandler func(consensus.ChainReader, *types.Vote) error + verifyVote func(consensus.ChainHeaderReader, *types.Vote) (bool, error) + voteHandler func(consensus.ChainHeaderReader, *types.Vote) error - verifyTimeout func(consensus.ChainReader, *types.Timeout) (bool, error) - timeoutHandler func(consensus.ChainReader, *types.Timeout) error + verifyTimeout func(consensus.ChainHeaderReader, *types.Timeout) (bool, error) + timeoutHandler func(consensus.ChainHeaderReader, *types.Timeout) error - verifySyncInfo func(consensus.ChainReader, *types.SyncInfo) (bool, error) - syncInfoHandler func(consensus.ChainReader, *types.SyncInfo) error + verifySyncInfo func(consensus.ChainHeaderReader, *types.SyncInfo) (bool, error) + syncInfoHandler func(consensus.ChainHeaderReader, *types.SyncInfo) error } type BroadcastFns struct { diff --git a/eth/bft/bft_handler_test.go b/eth/bft/bft_handler_test.go index e9d54130b90b..71329a2b1ffb 100644 --- a/eth/bft/bft_handler_test.go +++ b/eth/bft/bft_handler_test.go @@ -65,12 +65,12 @@ func TestSequentialVotes(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 10 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainHeaderReader, vote *types.Vote) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainHeaderReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -101,11 +101,11 @@ func TestNotBoardcastInvalidVote(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 0 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainHeaderReader, vote *types.Vote) (bool, error) { return false, errors.New("This is invalid vote") } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainHeaderReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -128,11 +128,11 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 0 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainHeaderReader, vote *types.Vote) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainHeaderReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -155,11 +155,11 @@ func TestBoardcastButNotProcessDisqualifiedTimeout(t *testing.T) { broadcastCounter := uint32(0) targetTimeout := 0 - tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainReader, timeout *types.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainHeaderReader, timeout *types.Timeout) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainHeaderReader, timeout *types.Timeout) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -182,11 +182,11 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) { broadcastCounter := uint32(0) targetSyncInfo := 0 - tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) { + tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) (bool, error) { return false, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { + tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -210,12 +210,12 @@ func TestTimeoutHandler(t *testing.T) { broadcastCounter := uint32(0) targetVotes := 1 - tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(consensus.ChainHeaderReader, *types.Timeout) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainHeaderReader, timeout *types.Timeout) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -241,11 +241,11 @@ func TestTimeoutHandler(t *testing.T) { func TestTimeoutHandlerRoundNotEqual(t *testing.T) { tester := newTester() - tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(consensus.ChainHeaderReader, *types.Timeout) (bool, error) { return true, nil } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainHeaderReader, timeout *types.Timeout) error { return &utils.ErrIncomingMessageRoundNotEqualCurrentRound{ Type: "timeout", IncomingRound: types.Round(1), @@ -268,12 +268,12 @@ func TestSyncInfoHandler(t *testing.T) { broadcastCounter := uint32(0) targetSyncInfo := 1 - tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) { + tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { + tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -298,12 +298,12 @@ func TestTooFarVotes(t *testing.T) { numberVotes := 10 targetVotes := 0 - tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) { + tester.bfter.consensus.verifyVote = func(chain consensus.ChainHeaderReader, vote *types.Vote) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error { + tester.bfter.consensus.voteHandler = func(chain consensus.ChainHeaderReader, vote *types.Vote) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -335,12 +335,12 @@ func TestTooFarTimeout(t *testing.T) { broadcastCounter := uint32(0) targetTimeout := 1 - tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) { + tester.bfter.consensus.verifyTimeout = func(consensus.ChainHeaderReader, *types.Timeout) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil } - tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error { + tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainHeaderReader, timeout *types.Timeout) error { atomic.AddUint32(&handlerCounter, 1) return nil } @@ -372,12 +372,12 @@ func TestTooFarSyncInfo(t *testing.T) { broadcastCounter := uint32(0) targetSyncInfo := 0 - tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) { + tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) (bool, error) { atomic.AddUint32(&verifyCounter, 1) return true, nil // return false but with nil in error means the message is valid but disqualified } - tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error { + tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainHeaderReader, syncInfo *types.SyncInfo) error { atomic.AddUint32(&handlerCounter, 1) return nil } diff --git a/eth/util/util.go b/eth/util/util.go index 6833b4ad8e9e..0d61c00f55da 100644 --- a/eth/util/util.go +++ b/eth/util/util.go @@ -6,7 +6,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/consensus" ) -func RewardInflation(chain consensus.ChainReader, chainReward *big.Int, number uint64, blockPerYear uint64) *big.Int { +func RewardInflation(chain consensus.ChainHeaderReader, chainReward *big.Int, number uint64, blockPerYear uint64) *big.Int { if chain != nil && chain.Config().IsTIPNoHalvingMNReward(new(big.Int).SetUint64(number)) { return chainReward } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index acbf1d631672..eb62983bb54d 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -285,11 +285,11 @@ func (s *EthereumAccountAPI) Accounts() []common.Address { // BlockChainAPI provides an API to access Ethereum blockchain data. type BlockChainAPI struct { b Backend - chainReader consensus.ChainReader + chainReader consensus.ChainHeaderReader } // NewBlockChainAPI creates a new Ethereum blockchain API. -func NewBlockChainAPI(b Backend, chainReader consensus.ChainReader) *BlockChainAPI { +func NewBlockChainAPI(b Backend, chainReader consensus.ChainHeaderReader) *BlockChainAPI { return &BlockChainAPI{ b, chainReader, diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index a50b3e88b7e5..4355a3c0fccf 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -116,7 +116,7 @@ type Backend interface { GetPeer() int } -func GetAPIs(apiBackend Backend, chainReader consensus.ChainReader) []rpc.API { +func GetAPIs(apiBackend Backend, chainReader consensus.ChainHeaderReader) []rpc.API { nonceLock := new(AddrLocker) return []rpc.API{ {