fix pool test

This commit is contained in:
Jianrong 2021-11-14 18:28:44 +11:00
parent a39612e540
commit 98014936c3
3 changed files with 79 additions and 31 deletions

View file

@ -371,16 +371,16 @@ func (x *XDPoS_v2) verifyVotingRule(header *types.Header) error {
} }
// Once Hot stuff voting rule has verified, this node can then send vote // Once Hot stuff voting rule has verified, this node can then send vote
func (x *XDPoS_v2) sendVote(blockInfo utils.BlockInfo) error { func (x *XDPoS_v2) sendVote(blockInfo *utils.BlockInfo) error {
// First step: Generate the signature by using node's private key(The signature is the blockInfo signature) // First step: Generate the signature by using node's private key(The signature is the blockInfo signature)
// Second step: Construct the vote struct with the above signature & blockinfo struct // Second step: Construct the vote struct with the above signature & blockinfo struct
// Third step: Send the vote to broadcast channel // Third step: Send the vote to broadcast channel
signedHash, err := x.signSignature(utils.VoteSigHash(&blockInfo)) signedHash, err := x.signSignature(utils.VoteSigHash(blockInfo))
if err != nil { if err != nil {
return err return err
} }
voteMsg := &utils.Vote{ voteMsg := &utils.Vote{
ProposedBlockInfo: blockInfo, ProposedBlockInfo: *blockInfo,
Signature: signedHash, Signature: signedHash,
} }
x.broadcastToBftChannel(voteMsg) x.broadcastToBftChannel(voteMsg)

View file

@ -28,22 +28,36 @@ func TestPoolWithTimeout(t *testing.T) {
timeout1 := Timeout{Round: 1, Signature: []byte{1}} timeout1 := Timeout{Round: 1, Signature: []byte{1}}
timeout2 := Timeout{Round: 1, Signature: []byte{2}} timeout2 := Timeout{Round: 1, Signature: []byte{2}}
timeout3 := Timeout{Round: 1, Signature: []byte{3}} timeout3 := Timeout{Round: 1, Signature: []byte{3}}
assert.Nil(pool.Add(&timeout1)) _, numOfItems, err := pool.Add(&timeout1)
assert.Nil(pool.Add(&timeout1)) assert.Nil(err)
assert.Equal(ret, 0) assert.Equal(1, numOfItems)
assert.Nil(pool.Add(&timeout2)) _, numOfItems, err = pool.Add(&timeout1)
assert.Equal(ret, 2) assert.Nil(err)
assert.Nil(pool.Add(&timeout3)) // Duplicates should not be added
assert.Equal(ret, 2) assert.Equal(1, numOfItems)
assert.Equal(0, ret)
_, numOfItems, err = pool.Add(&timeout2)
assert.Nil(err)
assert.Equal(2, ret)
_, numOfItems, err = pool.Add(&timeout3)
assert.Nil(err)
assert.Equal(2, ret)
pool = NewPool(3) // 3 is the cert size pool = NewPool(3) // 3 is the cert size
ret = 0 ret = 0
pool.SetOnThresholdFn(onThresholdFn) pool.SetOnThresholdFn(onThresholdFn)
assert.Nil(pool.Add(&timeout1)) _, numOfItems, err = pool.Add(&timeout1)
assert.Nil(pool.Add(&timeout2)) assert.Nil(err)
assert.Equal(1, numOfItems)
_, numOfItems, err = pool.Add(&timeout2)
assert.Nil(err)
assert.Equal(2, numOfItems)
assert.Equal(ret, 0) assert.Equal(ret, 0)
pool.Clear() pool.Clear()
assert.Nil(pool.Add(&timeout3)) _, numOfItems, err = pool.Add(&timeout3)
assert.Equal(ret, 0) assert.Nil(err)
assert.Equal(1, numOfItems)
assert.Equal(0, ret)
} }
func TestPoolWithVote(t *testing.T) { func TestPoolWithVote(t *testing.T) {
@ -68,29 +82,63 @@ func TestPoolWithVote(t *testing.T) {
vote1 := Vote{ProposedBlockInfo: blockInfo1, Signature: []byte{1}} vote1 := Vote{ProposedBlockInfo: blockInfo1, Signature: []byte{1}}
vote2 := Vote{ProposedBlockInfo: blockInfo2, Signature: []byte{2}} vote2 := Vote{ProposedBlockInfo: blockInfo2, Signature: []byte{2}}
vote3 := Vote{ProposedBlockInfo: blockInfo1, Signature: []byte{3}} vote3 := Vote{ProposedBlockInfo: blockInfo1, Signature: []byte{3}}
assert.Nil(pool.Add(&vote1)) _, numOfItems, err := pool.Add(&vote1)
assert.Nil(pool.Add(&vote1)) assert.Nil(err)
assert.Equal(1, numOfItems)
// Duplicates should not be added
_, numOfItems, err = pool.Add(&vote1)
assert.Nil(err)
assert.Equal(1, numOfItems)
assert.Equal(ret, 0) assert.Equal(ret, 0)
assert.Nil(pool.Add(&vote2))
assert.Equal(ret, 0) _, numOfItems, err = pool.Add(&vote2)
assert.Nil(pool.Add(&vote3)) assert.Nil(err)
assert.Equal(ret, 2) // vote2 is on a different blockInfo to vote1
assert.Equal(1, numOfItems)
assert.Equal(0, ret)
_, numOfItems, err = pool.Add(&vote3)
assert.Nil(err)
assert.Equal(2, numOfItems)
assert.Equal(2, ret)
pool = NewPool(3) // 3 is the cert size pool = NewPool(3) // 3 is the cert size
ret = 0 ret = 0
pool.SetOnThresholdFn(onThresholdFn) pool.SetOnThresholdFn(onThresholdFn)
assert.Nil(pool.Add(&vote1))
assert.Nil(pool.Add(&vote2)) _, numOfItems, err = pool.Add(&vote1)
assert.Nil(pool.Add(&vote3)) assert.Nil(err)
assert.Equal(ret, 0) assert.Equal(1, numOfItems)
// vote2 is on a different blockInfo to vote1
_, numOfItems, err = pool.Add(&vote2)
assert.Nil(err)
assert.Equal(1, numOfItems)
_, numOfItems, err = pool.Add(&vote3)
assert.Nil(err)
assert.Equal(2, numOfItems)
assert.Equal(0, ret)
pool.Clear() pool.Clear()
assert.Empty(pool.objList) assert.Empty(pool.objList)
pool = NewPool(2) // 2 is the cert size pool = NewPool(2) // 2 is the cert size
ret = 0 ret = 0
pool.SetOnThresholdFn(onThresholdFn) pool.SetOnThresholdFn(onThresholdFn)
assert.Nil(pool.Add(&vote1))
assert.Nil(pool.Add(&vote2)) _, numOfItems, err = pool.Add(&vote1)
assert.Nil(pool.Add(&vote3)) assert.Nil(err)
assert.Equal(len(pool.objList), 1) //vote for one hash is cleared, but another remains assert.Equal(1, numOfItems)
// vote2 is on a different blockInfo to vote1
_, numOfItems, err = pool.Add(&vote2)
assert.Nil(err)
assert.Equal(1, numOfItems)
_, numOfItems, err = pool.Add(&vote3)
assert.Nil(err)
assert.Equal(2, numOfItems)
assert.Equal(1, len(pool.objList)) //vote for one hash is cleared, but another remains
pool.Clear() pool.Clear()
assert.Empty(pool.objList) assert.Empty(pool.objList)
} }

View file

@ -100,7 +100,7 @@ func TestThrowErrorIfTimeoutMsgRoundLessThanCurrentRound(t *testing.T) {
} }
// VoteHandler // VoteHandler
func TestVoteMessageHandlerSuccessfullyGenerateTCandSyncInfo(t *testing.T) { func TestVoteMessageHandlerSuccessfullyGeneratedQC(t *testing.T) {
blockchain, _, _, _ := PrepareXDCTestBlockChain(t, 11, params.TestXDPoSMockChainConfigWithV2Engine) blockchain, _, _, _ := PrepareXDCTestBlockChain(t, 11, params.TestXDPoSMockChainConfigWithV2Engine)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2 engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
@ -129,7 +129,7 @@ func TestVoteMessageHandlerSuccessfullyGenerateTCandSyncInfo(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, utils.Round(1), engineV2.GetCurrentRound()) assert.Equal(t, utils.Round(1), engineV2.GetCurrentRound())
// Create a vote message that should trigger vite pool hook // Create a vote message that should trigger vote pool hook
voteMsg = &utils.Vote{ voteMsg = &utils.Vote{
ProposedBlockInfo: *blockInfo, ProposedBlockInfo: *blockInfo,
Signature: []byte{3}, Signature: []byte{3},
@ -202,7 +202,7 @@ func TestProcessVoteMsgThenTimeoutMsg(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, utils.Round(1), engineV2.GetCurrentRound()) assert.Equal(t, utils.Round(1), engineV2.GetCurrentRound())
// Create a vote message that should trigger vite pool hook // Create a vote message that should trigger vote pool hook
voteMsg = &utils.Vote{ voteMsg = &utils.Vote{
ProposedBlockInfo: *blockInfo, ProposedBlockInfo: *blockInfo,
Signature: []byte{3}, Signature: []byte{3},