github.com/Team-Kujira/tendermint@v0.34.24-indexer/consensus/types/height_vote_set_test.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 cfg "github.com/tendermint/tendermint/config" 9 "github.com/tendermint/tendermint/crypto/tmhash" 10 tmrand "github.com/tendermint/tendermint/libs/rand" 11 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 12 "github.com/tendermint/tendermint/types" 13 tmtime "github.com/tendermint/tendermint/types/time" 14 ) 15 16 var config *cfg.Config // NOTE: must be reset for each _test.go file 17 18 func TestMain(m *testing.M) { 19 config = cfg.ResetTestRoot("consensus_height_vote_set_test") 20 code := m.Run() 21 os.RemoveAll(config.RootDir) 22 os.Exit(code) 23 } 24 25 func TestPeerCatchupRounds(t *testing.T) { 26 valSet, privVals := types.RandValidatorSet(10, 1) 27 28 hvs := NewHeightVoteSet(config.ChainID(), 1, valSet) 29 30 vote999_0 := makeVoteHR(t, 1, 0, 999, privVals) 31 added, err := hvs.AddVote(vote999_0, "peer1") 32 if !added || err != nil { 33 t.Error("Expected to successfully add vote from peer", added, err) 34 } 35 36 vote1000_0 := makeVoteHR(t, 1, 0, 1000, privVals) 37 added, err = hvs.AddVote(vote1000_0, "peer1") 38 if !added || err != nil { 39 t.Error("Expected to successfully add vote from peer", added, err) 40 } 41 42 vote1001_0 := makeVoteHR(t, 1, 0, 1001, privVals) 43 added, err = hvs.AddVote(vote1001_0, "peer1") 44 if err != ErrGotVoteFromUnwantedRound { 45 t.Errorf("expected GotVoteFromUnwantedRoundError, but got %v", err) 46 } 47 if added { 48 t.Error("Expected to *not* add vote from peer, too many catchup rounds.") 49 } 50 51 added, err = hvs.AddVote(vote1001_0, "peer2") 52 if !added || err != nil { 53 t.Error("Expected to successfully add vote from another peer") 54 } 55 56 } 57 58 func makeVoteHR(t *testing.T, height int64, valIndex, round int32, privVals []types.PrivValidator) *types.Vote { 59 privVal := privVals[valIndex] 60 pubKey, err := privVal.GetPubKey() 61 if err != nil { 62 panic(err) 63 } 64 65 randBytes := tmrand.Bytes(tmhash.Size) 66 67 vote := &types.Vote{ 68 ValidatorAddress: pubKey.Address(), 69 ValidatorIndex: valIndex, 70 Height: height, 71 Round: round, 72 Timestamp: tmtime.Now(), 73 Type: tmproto.PrecommitType, 74 BlockID: types.BlockID{Hash: randBytes, PartSetHeader: types.PartSetHeader{}}, 75 } 76 chainID := config.ChainID() 77 78 v := vote.ToProto() 79 err = privVal.SignVote(chainID, v) 80 if err != nil { 81 panic(fmt.Sprintf("Error signing vote: %v", err)) 82 } 83 84 vote.Signature = v.Signature 85 86 return vote 87 }