github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/scheme/rolldpos/roundctx_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package rolldpos 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/golang/mock/gomock" 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/endorsement" 16 ) 17 18 func TestRoundCtx(t *testing.T) { 19 t.Parallel() 20 require := require.New(t) 21 22 ctrl := gomock.NewController(t) 23 defer ctrl.Finish() 24 now := time.Now() 25 blockHeight := uint64(8) 26 round := &roundCtx{ 27 height: blockHeight + 1, 28 roundNum: 3, 29 proposer: "delegateC", 30 roundStartTime: now.Add(-2 * time.Second), 31 nextRoundStartTime: now.Add(2 * time.Second), 32 epochNum: uint64(1), 33 epochStartHeight: uint64(1), 34 nextEpochStartHeight: uint64(33), 35 delegates: []string{ 36 "delegate1", "delegate2", "delegate3", "delegate4", 37 "delegate5", "delegate6", "delegate7", "delegate8", 38 "delegate9", "delegate0", "delegateA", "delegateB", 39 "delegateC", "delegateD", "delegateE", "delegateF", 40 }, 41 } 42 43 require.Equal(uint64(1), round.EpochNum()) 44 require.Equal(16, len(round.Delegates())) 45 require.Equal(uint64(1), round.EpochStartHeight()) 46 require.Equal(uint64(33), round.NextEpochStartHeight()) 47 require.Equal(now.Add(-2*time.Second), round.StartTime()) 48 require.Equal(now.Add(2*time.Second), round.NextRoundStartTime()) 49 require.Equal(blockHeight+1, round.Height()) 50 require.Equal(uint32(3), round.Number()) 51 require.Equal("delegateC", round.Proposer()) 52 require.False(round.IsDelegate("non-delegate")) 53 for _, d := range round.Delegates() { 54 require.True(round.IsDelegate(d)) 55 } 56 t.Run("is-stale", func(t *testing.T) { 57 blkHash := []byte("Some block hash") 58 require.True(round.IsStale(blockHeight, 2, nil)) 59 require.True(round.IsStale(blockHeight, 3, nil)) 60 require.True(round.IsStale(blockHeight, 4, nil)) 61 require.True(round.IsStale(blockHeight+1, 2, nil)) 62 require.True(round.IsStale(blockHeight+1, 2, NewEndorsedConsensusMessage( 63 blockHeight+1, 64 &blockProposal{}, 65 &endorsement.Endorsement{}, 66 ))) 67 require.True(round.IsStale(blockHeight+1, 2, NewEndorsedConsensusMessage( 68 blockHeight+1, 69 NewConsensusVote( 70 blkHash, 71 PROPOSAL, 72 ), 73 &endorsement.Endorsement{}, 74 ))) 75 require.False(round.IsStale(blockHeight+1, 2, NewEndorsedConsensusMessage( 76 blockHeight+1, 77 NewConsensusVote( 78 blkHash, 79 COMMIT, 80 ), 81 &endorsement.Endorsement{}, 82 ))) 83 require.False(round.IsStale(blockHeight+1, 3, nil)) 84 require.False(round.IsStale(blockHeight+1, 4, nil)) 85 require.False(round.IsStale(blockHeight+2, 2, nil)) 86 }) 87 t.Run("is-future", func(t *testing.T) { 88 require.False(round.IsFuture(blockHeight, 4)) 89 require.False(round.IsFuture(blockHeight+1, 2)) 90 require.False(round.IsFuture(blockHeight+1, 3)) 91 require.True(round.IsFuture(blockHeight+1, 4)) 92 require.True(round.IsFuture(blockHeight+2, 2)) 93 }) 94 // TODO: add more unit tests 95 }