package tests import ( "testing" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils" "github.com/XinFinOrg/XDPoSChain/params" "github.com/stretchr/testify/assert" ) // Timeout handler func TestTimeoutMessageHandlerSuccessfullyGenerateTCandSyncInfo(t *testing.T) { blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfig, 0) engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2 // Set round to 1 engineV2.SetNewRoundFaker(blockchain, utils.Round(1), false) // Create two timeout message which will not reach timeout pool threshold timeoutMsg := &utils.Timeout{ Round: utils.Round(1), Signature: []byte{1}, GapNumber: 450, } err := engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.Nil(t, err) currentRound, _, _, _, _ := engineV2.GetProperties() assert.Equal(t, utils.Round(1), currentRound) timeoutMsg = &utils.Timeout{ Round: utils.Round(1), Signature: []byte{2}, GapNumber: 450, } err = engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.Nil(t, err) currentRound, _, _, _, _ = engineV2.GetProperties() assert.Equal(t, utils.Round(1), currentRound) // Send a timeout with different gap number, it shall not trigger timeout pool hook timeoutMsg = &utils.Timeout{ Round: utils.Round(1), Signature: []byte{3}, GapNumber: 1350, } err = engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.Nil(t, err) currentRound, _, _, _, _ = engineV2.GetProperties() assert.Equal(t, utils.Round(1), currentRound) // Create a timeout message that should trigger timeout pool hook timeoutMsg = &utils.Timeout{ Round: utils.Round(1), Signature: []byte{4}, GapNumber: 450, } err = engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.Nil(t, err) syncInfoMsg := <-engineV2.BroadcastCh currentRound, _, _, _, _ = engineV2.GetProperties() assert.NotNil(t, syncInfoMsg) // Shouldn't have QC, however, we did not inilise it, hence will show default empty value qc := syncInfoMsg.(*utils.SyncInfo).HighestQuorumCert assert.Equal(t, utils.Round(0), qc.ProposedBlockInfo.Round) tc := syncInfoMsg.(*utils.SyncInfo).HighestTimeoutCert assert.NotNil(t, tc) assert.Equal(t, tc.Round, utils.Round(1)) assert.Equal(t, uint64(450), tc.GapNumber) // The signatures shall not include the byte{3} from a different gap number sigatures := []utils.Signature{[]byte{1}, []byte{2}, []byte{4}} assert.ElementsMatch(t, tc.Signatures, sigatures) assert.Equal(t, utils.Round(2), currentRound) } func TestThrowErrorIfTimeoutMsgRoundNotEqualToCurrentRound(t *testing.T) { blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 11, params.TestXDPoSMockChainConfig, 0) engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2 // Set round to 3 engineV2.SetNewRoundFaker(blockchain, utils.Round(3), false) timeoutMsg := &utils.Timeout{ Round: utils.Round(2), Signature: []byte{1}, } err := engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.NotNil(t, err) // Timeout msg round > currentRound assert.Equal(t, "timeout message round number: 2 does not match currentRound: 3", err.Error()) // Set round to 1 engineV2.SetNewRoundFaker(blockchain, utils.Round(1), false) err = engineV2.TimeoutHandler(blockchain, timeoutMsg) assert.NotNil(t, err) // Timeout msg round < currentRound assert.Equal(t, "timeout message round number: 2 does not match currentRound: 1", err.Error()) }