add syncInfo test

This commit is contained in:
Jianrong 2021-12-12 20:48:02 +11:00
parent 5f9fd80733
commit e0d66d4ea8
4 changed files with 52 additions and 13 deletions

View file

@ -11,7 +11,7 @@ import (
) )
func TestProcessFirstV2BlockAndSendVoteMsg(t *testing.T) { func TestProcessFirstV2BlockAndSendVoteMsg(t *testing.T) {
// Block 11 is the first v2 block with starting round of 0 // Block 11 is the first v2 block with round of 1
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0) blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2 engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
@ -33,7 +33,8 @@ func TestProcessFirstV2BlockAndSendVoteMsg(t *testing.T) {
round, _, highestQC, _ := engineV2.GetProperties() round, _, highestQC, _ := engineV2.GetProperties()
// Shoud trigger setNewRound // Shoud trigger setNewRound
assert.Equal(t, utils.Round(1), round) assert.Equal(t, utils.Round(1), round)
assert.Equal(t, extraField.QuorumCert.Signatures, highestQC.Signatures) // Should not update the highestQC
assert.Equal(t, utils.Round(0), highestQC.ProposedBlockInfo.Round)
} }

View file

@ -6,11 +6,12 @@ import (
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/params"
"github.com/stretchr/testify/assert"
) )
func TestSyncInfoForFirstV2BlockMsgWithoutQC(t *testing.T) { func TestSyncInfoShouldSuccessfullyUpdateByQC(t *testing.T) {
// Block 11 is the first v2 block with starting round of 0 // Block 11 is the first v2 block with starting round of 0
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0) blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 15, params.TestXDPoSMockChainConfigWithV2Engine, 0)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2 engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
var extraField utils.ExtraFields_v2 var extraField utils.ExtraFields_v2
@ -20,12 +21,49 @@ func TestSyncInfoForFirstV2BlockMsgWithoutQC(t *testing.T) {
} }
syncInfoMsg := &utils.SyncInfo{ syncInfoMsg := &utils.SyncInfo{
HighestQuorumCert: extraField.QuorumCert, HighestQuorumCert: extraField.QuorumCert,
HighestTimeoutCert: nil, // Initial value? HighestTimeoutCert: &utils.TimeoutCert{
Round: utils.Round(2),
Signatures: []utils.Signature{},
},
} }
err = engineV2.SyncInfoHandler(blockchain, syncInfoMsg) err = engineV2.SyncInfoHandler(blockchain, syncInfoMsg)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
round, _, highestQuorumCert, _ := engineV2.GetProperties()
// QC is parent block's qc, which is pointing at round 4, hence 4 + 1 = 5
assert.Equal(t, utils.Round(5), round)
assert.Equal(t, extraField.QuorumCert, highestQuorumCert)
}
func TestSyncInfoShouldSuccessfullyUpdateByTC(t *testing.T) {
// Block 11 is the first v2 block with starting round of 0
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 15, params.TestXDPoSMockChainConfigWithV2Engine, 0)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
var extraField utils.ExtraFields_v2
err := utils.DecodeBytesExtraFields(currentBlock.Extra(), &extraField)
if err != nil {
t.Fatal("Fail to decode extra data", err)
}
highestTC := &utils.TimeoutCert{
Round: utils.Round(6),
Signatures: []utils.Signature{},
}
syncInfoMsg := &utils.SyncInfo{
HighestQuorumCert: extraField.QuorumCert,
HighestTimeoutCert: highestTC,
}
err = engineV2.SyncInfoHandler(blockchain, syncInfoMsg)
if err != nil {
t.Fatal(err)
}
round, _, highestQuorumCert, _ := engineV2.GetProperties()
assert.Equal(t, utils.Round(7), round)
assert.Equal(t, extraField.QuorumCert, highestQuorumCert)
} }

View file

@ -51,7 +51,7 @@ func TestTimeoutMessageHandlerSuccessfullyGenerateTCandSyncInfo(t *testing.T) {
// Shouldn't have QC, however, we did not inilise it, hence will show default empty value // Shouldn't have QC, however, we did not inilise it, hence will show default empty value
qc := syncInfoMsg.(*utils.SyncInfo).HighestQuorumCert qc := syncInfoMsg.(*utils.SyncInfo).HighestQuorumCert
assert.Nil(t, qc) assert.Equal(t, utils.Round(0), qc.ProposedBlockInfo.Round)
tc := syncInfoMsg.(*utils.SyncInfo).HighestTimeoutCert tc := syncInfoMsg.(*utils.SyncInfo).HighestTimeoutCert
assert.NotNil(t, tc) assert.NotNil(t, tc)

View file

@ -35,7 +35,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties() currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
// initialised with nil and 0 round // initialised with nil and 0 round
assert.Nil(t, lockQuorumCert) assert.Nil(t, lockQuorumCert)
assert.Nil(t, highestQuorumCert) assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
assert.Equal(t, utils.Round(1), currentRound) assert.Equal(t, utils.Round(1), currentRound)
voteMsg = &utils.Vote{ voteMsg = &utils.Vote{
ProposedBlockInfo: blockInfo, ProposedBlockInfo: blockInfo,
@ -46,7 +46,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties() currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties()
// Still using the initlised value because we did not yet go to the next round // Still using the initlised value because we did not yet go to the next round
assert.Nil(t, lockQuorumCert) assert.Nil(t, lockQuorumCert)
assert.Nil(t, highestQuorumCert) assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
assert.Equal(t, utils.Round(1), currentRound) assert.Equal(t, utils.Round(1), currentRound)
@ -63,7 +63,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
assert.Equal(t, utils.Round(0), lockQuorumCert.ProposedBlockInfo.Round) assert.Equal(t, utils.Round(0), lockQuorumCert.ProposedBlockInfo.Round)
// The highestQC proposedBlockInfo shall be the same as the one from its votes // The highestQC proposedBlockInfo shall be the same as the one from its votes
assert.Equal(t, highestQuorumCert.ProposedBlockInfo, voteMsg.ProposedBlockInfo) assert.Equal(t, highestQuorumCert.ProposedBlockInfo, voteMsg.ProposedBlockInfo)
// Check round has now changed from 5 to 6 // Check round has now changed from 1 to 2
assert.Equal(t, utils.Round(2), currentRound) assert.Equal(t, utils.Round(2), currentRound)
} }
@ -90,7 +90,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties() currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
// initialised with nil and 0 round // initialised with nil and 0 round
assert.Nil(t, lockQuorumCert) assert.Nil(t, lockQuorumCert)
assert.Nil(t, highestQuorumCert) assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
assert.Equal(t, utils.Round(5), currentRound) assert.Equal(t, utils.Round(5), currentRound)
voteMsg = &utils.Vote{ voteMsg = &utils.Vote{
ProposedBlockInfo: blockInfo, ProposedBlockInfo: blockInfo,
@ -101,7 +101,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties() currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties()
// Still using the initlised value because we did not yet go to the next round // Still using the initlised value because we did not yet go to the next round
assert.Nil(t, lockQuorumCert) assert.Nil(t, lockQuorumCert)
assert.Nil(t, highestQuorumCert) assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
assert.Equal(t, utils.Round(5), currentRound) assert.Equal(t, utils.Round(5), currentRound)
@ -176,7 +176,7 @@ func TestProcessVoteMsgThenTimeoutMsg(t *testing.T) {
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties() currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
// initialised with nil and 0 round // initialised with nil and 0 round
assert.Nil(t, lockQuorumCert) assert.Nil(t, lockQuorumCert)
assert.Nil(t, highestQuorumCert) assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
assert.Equal(t, utils.Round(5), currentRound) assert.Equal(t, utils.Round(5), currentRound)
voteMsg = &utils.Vote{ voteMsg = &utils.Vote{