Skip to content

Commit e0d66d4

Browse files
committed
add syncInfo test
1 parent 5f9fd80 commit e0d66d4

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

consensus/tests/proposed_block_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestProcessFirstV2BlockAndSendVoteMsg(t *testing.T) {
14-
// Block 11 is the first v2 block with starting round of 0
14+
// Block 11 is the first v2 block with round of 1
1515
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0)
1616
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
1717

@@ -33,7 +33,8 @@ func TestProcessFirstV2BlockAndSendVoteMsg(t *testing.T) {
3333
round, _, highestQC, _ := engineV2.GetProperties()
3434
// Shoud trigger setNewRound
3535
assert.Equal(t, utils.Round(1), round)
36-
assert.Equal(t, extraField.QuorumCert.Signatures, highestQC.Signatures)
36+
// Should not update the highestQC
37+
assert.Equal(t, utils.Round(0), highestQC.ProposedBlockInfo.Round)
3738

3839
}
3940

consensus/tests/sync_info_test.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
77
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
88
"github.com/XinFinOrg/XDPoSChain/params"
9+
"github.com/stretchr/testify/assert"
910
)
1011

11-
func TestSyncInfoForFirstV2BlockMsgWithoutQC(t *testing.T) {
12+
func TestSyncInfoShouldSuccessfullyUpdateByQC(t *testing.T) {
1213
// Block 11 is the first v2 block with starting round of 0
13-
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfigWithV2Engine, 0)
14+
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 15, params.TestXDPoSMockChainConfigWithV2Engine, 0)
1415
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
1516

1617
var extraField utils.ExtraFields_v2
@@ -19,13 +20,50 @@ func TestSyncInfoForFirstV2BlockMsgWithoutQC(t *testing.T) {
1920
t.Fatal("Fail to decode extra data", err)
2021
}
2122

23+
syncInfoMsg := &utils.SyncInfo{
24+
HighestQuorumCert: extraField.QuorumCert,
25+
HighestTimeoutCert: &utils.TimeoutCert{
26+
Round: utils.Round(2),
27+
Signatures: []utils.Signature{},
28+
},
29+
}
30+
31+
err = engineV2.SyncInfoHandler(blockchain, syncInfoMsg)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
round, _, highestQuorumCert, _ := engineV2.GetProperties()
36+
// QC is parent block's qc, which is pointing at round 4, hence 4 + 1 = 5
37+
assert.Equal(t, utils.Round(5), round)
38+
assert.Equal(t, extraField.QuorumCert, highestQuorumCert)
39+
}
40+
41+
func TestSyncInfoShouldSuccessfullyUpdateByTC(t *testing.T) {
42+
// Block 11 is the first v2 block with starting round of 0
43+
blockchain, _, currentBlock, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 15, params.TestXDPoSMockChainConfigWithV2Engine, 0)
44+
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
45+
46+
var extraField utils.ExtraFields_v2
47+
err := utils.DecodeBytesExtraFields(currentBlock.Extra(), &extraField)
48+
if err != nil {
49+
t.Fatal("Fail to decode extra data", err)
50+
}
51+
52+
highestTC := &utils.TimeoutCert{
53+
Round: utils.Round(6),
54+
Signatures: []utils.Signature{},
55+
}
56+
2257
syncInfoMsg := &utils.SyncInfo{
2358
HighestQuorumCert: extraField.QuorumCert,
24-
HighestTimeoutCert: nil, // Initial value?
59+
HighestTimeoutCert: highestTC,
2560
}
2661

2762
err = engineV2.SyncInfoHandler(blockchain, syncInfoMsg)
2863
if err != nil {
2964
t.Fatal(err)
3065
}
66+
round, _, highestQuorumCert, _ := engineV2.GetProperties()
67+
assert.Equal(t, utils.Round(7), round)
68+
assert.Equal(t, extraField.QuorumCert, highestQuorumCert)
3169
}

consensus/tests/timeout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestTimeoutMessageHandlerSuccessfullyGenerateTCandSyncInfo(t *testing.T) {
5151

5252
// Shouldn't have QC, however, we did not inilise it, hence will show default empty value
5353
qc := syncInfoMsg.(*utils.SyncInfo).HighestQuorumCert
54-
assert.Nil(t, qc)
54+
assert.Equal(t, utils.Round(0), qc.ProposedBlockInfo.Round)
5555

5656
tc := syncInfoMsg.(*utils.SyncInfo).HighestTimeoutCert
5757
assert.NotNil(t, tc)

consensus/tests/vote_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
3535
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
3636
// initialised with nil and 0 round
3737
assert.Nil(t, lockQuorumCert)
38-
assert.Nil(t, highestQuorumCert)
38+
assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
3939
assert.Equal(t, utils.Round(1), currentRound)
4040
voteMsg = &utils.Vote{
4141
ProposedBlockInfo: blockInfo,
@@ -46,7 +46,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
4646
currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties()
4747
// Still using the initlised value because we did not yet go to the next round
4848
assert.Nil(t, lockQuorumCert)
49-
assert.Nil(t, highestQuorumCert)
49+
assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
5050

5151
assert.Equal(t, utils.Round(1), currentRound)
5252

@@ -63,7 +63,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQCForFistV2Round(t *te
6363
assert.Equal(t, utils.Round(0), lockQuorumCert.ProposedBlockInfo.Round)
6464
// The highestQC proposedBlockInfo shall be the same as the one from its votes
6565
assert.Equal(t, highestQuorumCert.ProposedBlockInfo, voteMsg.ProposedBlockInfo)
66-
// Check round has now changed from 5 to 6
66+
// Check round has now changed from 1 to 2
6767
assert.Equal(t, utils.Round(2), currentRound)
6868
}
6969

@@ -90,7 +90,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
9090
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
9191
// initialised with nil and 0 round
9292
assert.Nil(t, lockQuorumCert)
93-
assert.Nil(t, highestQuorumCert)
93+
assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
9494
assert.Equal(t, utils.Round(5), currentRound)
9595
voteMsg = &utils.Vote{
9696
ProposedBlockInfo: blockInfo,
@@ -101,7 +101,7 @@ func TestVoteMessageHandlerSuccessfullyGeneratedAndProcessQC(t *testing.T) {
101101
currentRound, lockQuorumCert, highestQuorumCert, _ = engineV2.GetProperties()
102102
// Still using the initlised value because we did not yet go to the next round
103103
assert.Nil(t, lockQuorumCert)
104-
assert.Nil(t, highestQuorumCert)
104+
assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
105105

106106
assert.Equal(t, utils.Round(5), currentRound)
107107

@@ -176,7 +176,7 @@ func TestProcessVoteMsgThenTimeoutMsg(t *testing.T) {
176176
currentRound, lockQuorumCert, highestQuorumCert, _ := engineV2.GetProperties()
177177
// initialised with nil and 0 round
178178
assert.Nil(t, lockQuorumCert)
179-
assert.Nil(t, highestQuorumCert)
179+
assert.Equal(t, utils.Round(0), highestQuorumCert.ProposedBlockInfo.Round)
180180

181181
assert.Equal(t, utils.Round(5), currentRound)
182182
voteMsg = &utils.Vote{

0 commit comments

Comments
 (0)