Skip to content

Commit

Permalink
fix test and update log level
Browse files Browse the repository at this point in the history
  • Loading branch information
liam.lai committed Nov 25, 2024
1 parent 2172918 commit b5ca27b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
8 changes: 6 additions & 2 deletions consensus/XDPoS/engines/engine_v2/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func (x *XDPoS_v2) voteHandler(chain consensus.ChainReader, voteMsg *types.Vote)

epochInfo, err := x.getEpochSwitchInfo(chain, nil, voteMsg.ProposedBlockInfo.Hash)
if err != nil {
log.Error("[voteHandler] Error when getting epoch switch Info", "error", err)
return errors.New("Fail on voteHandler due to failure in getting epoch switch info")
return &utils.ErrIncomingMessageBlockNotFound{
Type: "vote",
IncomingBlockHash: voteMsg.ProposedBlockInfo.Hash,
IncomingBlockNumber: voteMsg.ProposedBlockInfo.Number,
Err: err,
}
}

certThreshold := x.config.V2.Config(uint64(voteMsg.ProposedBlockInfo.Round)).CertThreshold
Expand Down
13 changes: 13 additions & 0 deletions consensus/XDPoS/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package utils
import (
"errors"
"fmt"
"math/big"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
)

Expand Down Expand Up @@ -120,3 +122,14 @@ type ErrIncomingMessageRoundTooFarFromCurrentRound struct {
func (e *ErrIncomingMessageRoundTooFarFromCurrentRound) Error() string {
return fmt.Sprintf("%s message round number: %v is too far away from currentRound: %v", e.Type, e.IncomingRound, e.CurrentRound)
}

type ErrIncomingMessageBlockNotFound struct {
Type string
IncomingBlockHash common.Hash
IncomingBlockNumber *big.Int
Err error
}

func (e *ErrIncomingMessageBlockNotFound) Error() string {
return fmt.Sprintf("%s proposed block is not found hash: %v, block number: %v, error: %s", e.Type, e.IncomingBlockHash.Hex(), e.IncomingBlockNumber, e.Err)
}
4 changes: 2 additions & 2 deletions consensus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (

ErrNotReadyToPropose = errors.New("not ready to propose, QC is not ready")

ErrNotReadyToMine = errors.New("Not ready to mine, it's not your turn")
ErrNotReadyToMine = errors.New("not ready to mine, it's not your turn")

ErrCoinbaseMismatch = errors.New("Block Coinbase address does not match its wallte address")
ErrCoinbaseMismatch = errors.New("block Coinbase address does not match its wallte address")
)
11 changes: 5 additions & 6 deletions consensus/tests/engine_v2_tests/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/XinFinOrg/XDPoSChain/accounts"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/params"
Expand Down Expand Up @@ -195,11 +194,11 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
}

func TestThrowErrorIfVoteMsgRoundIsMoreThanOneRoundAwayFromCurrentRound(t *testing.T) {
blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2

blockInfo := &types.BlockInfo{
Hash: common.HexToHash("0x1"),
Hash: currentBlock.Hash(),
Round: types.Round(6),
Number: big.NewInt(999),
}
Expand Down Expand Up @@ -397,15 +396,15 @@ func TestVoteMessageShallNotThrowErrorIfBlockNotYetExist(t *testing.T) {
}

err := engineV2.VoteHandler(blockchain, voteMsg)
assert.Nil(t, err)
assert.Contains(t, err.Error(), "proposed block is not found")

voteMsg = &types.Vote{
ProposedBlockInfo: blockInfo,
Signature: SignHashByPK(acc2Key, voteSigningHash.Bytes()),
GapNumber: 450,
}
err = engineV2.VoteHandler(blockchain, voteMsg)
assert.Nil(t, err)
assert.Contains(t, err.Error(), "proposed block is not found")

// Create a vote message that should trigger vote pool hook, but it shall not produce any QC yet
voteMsg = &types.Vote{
Expand All @@ -415,7 +414,7 @@ func TestVoteMessageShallNotThrowErrorIfBlockNotYetExist(t *testing.T) {
}

err = engineV2.VoteHandler(blockchain, voteMsg)
assert.Nil(t, err)
assert.Contains(t, err.Error(), "proposed block is not found")
currentRound, lockQuorumCert, highestQuorumCert, _, _, _ := engineV2.GetPropertiesFaker()
// Still using the initlised value because we did not yet go to the next round
assert.Nil(t, lockQuorumCert)
Expand Down
6 changes: 3 additions & 3 deletions core/types/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BlockInfo struct {

// Vote message in XDPoS 2.0
type Vote struct {
signer common.Address //field not exported
signer common.Address //field not exported
ProposedBlockInfo *BlockInfo `json:"proposedBlockInfo"`
Signature Signature `json:"signature"`
GapNumber uint64 `json:"gapNumber"`
Expand Down Expand Up @@ -81,9 +81,9 @@ func (s *SyncInfo) Hash() common.Hash {

// Quorum Certificate struct in XDPoS 2.0
type QuorumCert struct {
ProposedBlockInfo *BlockInfo `json:"proposedBlockInfo"`
ProposedBlockInfo *BlockInfo `json:"proposedBlockInfo"`
Signatures []Signature `json:"signatures"`
GapNumber uint64 `json:"gapNumber"`
GapNumber uint64 `json:"gapNumber"`
}

// Timeout Certificate struct in XDPoS 2.0
Expand Down
4 changes: 4 additions & 0 deletions eth/bft/bft_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (b *Bfter) Vote(peer string, vote *types.Vote) error {
log.Debug("vote round not equal", "error", err, "vote", vote.Hash())
return err
}
if _, ok := err.(*utils.ErrIncomingMessageBlockNotFound); ok {
log.Debug("vote proposed block not found", "error", err, "vote", vote.Hash())
return err
}
log.Error("handle BFT Vote", "error", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if !exist {
go pm.bft.Vote(p.id, &vote)
} else {
log.Debug("Discarded vote, known vote", "vote hash", vote.Hash(), "voted block hash", vote.ProposedBlockInfo.Hash.Hex(), "number", vote.ProposedBlockInfo.Number, "round", vote.ProposedBlockInfo.Round)
log.Trace("Discarded vote, known vote", "vote hash", vote.Hash(), "voted block hash", vote.ProposedBlockInfo.Hash.Hex(), "number", vote.ProposedBlockInfo.Number, "round", vote.ProposedBlockInfo.Round)
}

case msg.Code == TimeoutMsg:
Expand Down
1 change: 0 additions & 1 deletion eth/hooks/engine_v2_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
parentNumber--
parentHash = parentHeader.ParentHash
listBlockHash = append(listBlockHash, parentHash)
log.Debug("[HookPenalty] listBlockHash", "i", i, "len", len(listBlockHash), "parentHash", parentHash, "parentNumber", parentNumber)
}

// add list not miner to penalties
Expand Down

0 comments on commit b5ca27b

Please sign in to comment.