Skip to content

Commit a856030

Browse files
benjamin202410liam.lai
andauthored
fix vote test and optimize log (#750)
Co-authored-by: liam.lai <liam.lai@us>
1 parent 4278930 commit a856030

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

consensus/XDPoS/engines/engine_v2/vote.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ func (x *XDPoS_v2) voteHandler(chain consensus.ChainReader, voteMsg *types.Vote)
7878

7979
epochInfo, err := x.getEpochSwitchInfo(chain, nil, voteMsg.ProposedBlockInfo.Hash)
8080
if err != nil {
81-
log.Error("[voteHandler] Error when getting epoch switch Info", "error", err)
82-
return errors.New("fail on voteHandler due to failure in getting epoch switch info")
81+
return &utils.ErrIncomingMessageBlockNotFound{
82+
Type: "vote",
83+
IncomingBlockHash: voteMsg.ProposedBlockInfo.Hash,
84+
IncomingBlockNumber: voteMsg.ProposedBlockInfo.Number,
85+
Err: err,
86+
}
8387
}
8488

8589
certThreshold := x.config.V2.Config(uint64(voteMsg.ProposedBlockInfo.Round)).CertThreshold

consensus/XDPoS/utils/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package utils
33
import (
44
"errors"
55
"fmt"
6+
"math/big"
67

8+
"github.com/XinFinOrg/XDPoSChain/common"
79
"github.com/XinFinOrg/XDPoSChain/core/types"
810
)
911

@@ -120,3 +122,14 @@ type ErrIncomingMessageRoundTooFarFromCurrentRound struct {
120122
func (e *ErrIncomingMessageRoundTooFarFromCurrentRound) Error() string {
121123
return fmt.Sprintf("%s message round number: %v is too far away from currentRound: %v", e.Type, e.IncomingRound, e.CurrentRound)
122124
}
125+
126+
type ErrIncomingMessageBlockNotFound struct {
127+
Type string
128+
IncomingBlockHash common.Hash
129+
IncomingBlockNumber *big.Int
130+
Err error
131+
}
132+
133+
func (e *ErrIncomingMessageBlockNotFound) Error() string {
134+
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)
135+
}

consensus/tests/engine_v2_tests/vote_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/XinFinOrg/XDPoSChain/accounts"
1212
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends"
13-
"github.com/XinFinOrg/XDPoSChain/common"
1413
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
1514
"github.com/XinFinOrg/XDPoSChain/core/types"
1615
"github.com/XinFinOrg/XDPoSChain/params"
@@ -195,11 +194,11 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
195194
}
196195

197196
func TestThrowErrorIfVoteMsgRoundIsMoreThanOneRoundAwayFromCurrentRound(t *testing.T) {
198-
blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
197+
blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 905, params.TestXDPoSMockChainConfig, nil)
199198
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
200199

201200
blockInfo := &types.BlockInfo{
202-
Hash: common.HexToHash("0x1"),
201+
Hash: currentBlock.Hash(),
203202
Round: types.Round(6),
204203
Number: big.NewInt(999),
205204
}
@@ -397,15 +396,15 @@ func TestVoteMessageShallNotThrowErrorIfBlockNotYetExist(t *testing.T) {
397396
}
398397

399398
err := engineV2.VoteHandler(blockchain, voteMsg)
400-
assert.Nil(t, err)
399+
assert.Contains(t, err.Error(), "proposed block is not found")
401400

402401
voteMsg = &types.Vote{
403402
ProposedBlockInfo: blockInfo,
404403
Signature: SignHashByPK(acc2Key, voteSigningHash.Bytes()),
405404
GapNumber: 450,
406405
}
407406
err = engineV2.VoteHandler(blockchain, voteMsg)
408-
assert.Nil(t, err)
407+
assert.Contains(t, err.Error(), "proposed block is not found")
409408

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

417416
err = engineV2.VoteHandler(blockchain, voteMsg)
418-
assert.Nil(t, err)
417+
assert.Contains(t, err.Error(), "proposed block is not found")
419418
currentRound, lockQuorumCert, highestQuorumCert, _, _, _ := engineV2.GetPropertiesFaker()
420419
// Still using the initlised value because we did not yet go to the next round
421420
assert.Nil(t, lockQuorumCert)

eth/bft/bft_handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (b *Bfter) Vote(peer string, vote *types.Vote) error {
101101
log.Debug("vote round not equal", "error", err, "vote", vote.Hash())
102102
return err
103103
}
104+
if _, ok := err.(*utils.ErrIncomingMessageBlockNotFound); ok {
105+
log.Debug("vote proposed block not found", "error", err, "vote", vote.Hash())
106+
return err
107+
}
104108
log.Error("handle BFT Vote", "error", err)
105109
return err
106110
}

eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
854854
if !exist {
855855
go pm.bft.Vote(p.id, &vote)
856856
} else {
857-
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)
857+
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)
858858
}
859859

860860
case msg.Code == TimeoutMsg:

eth/hooks/engine_v2_hooks.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func AttachConsensusV2Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
6464
parentNumber--
6565
parentHash = parentHeader.ParentHash
6666
listBlockHash = append(listBlockHash, parentHash)
67-
log.Debug("[HookPenalty] listBlockHash", "i", i, "len", len(listBlockHash), "parentHash", parentHash, "parentNumber", parentNumber)
6867
}
6968

7069
// add list not miner to penalties

0 commit comments

Comments
 (0)