github.com/franono/tendermint@v0.32.2-0.20200527150959-749313264ce9/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/franono/tendermint/config"
     9  	"github.com/franono/tendermint/types"
    10  	tmtime "github.com/franono/tendermint/types/time"
    11  )
    12  
    13  var config *cfg.Config // NOTE: must be reset for each _test.go file
    14  
    15  func TestMain(m *testing.M) {
    16  	config = cfg.ResetTestRoot("consensus_height_vote_set_test")
    17  	code := m.Run()
    18  	os.RemoveAll(config.RootDir)
    19  	os.Exit(code)
    20  }
    21  
    22  func TestPeerCatchupRounds(t *testing.T) {
    23  	valSet, privVals := types.RandValidatorSet(10, 1)
    24  
    25  	hvs := NewHeightVoteSet(config.ChainID(), 1, valSet)
    26  
    27  	vote999_0 := makeVoteHR(t, 1, 999, privVals, 0)
    28  	added, err := hvs.AddVote(vote999_0, "peer1")
    29  	if !added || err != nil {
    30  		t.Error("Expected to successfully add vote from peer", added, err)
    31  	}
    32  
    33  	vote1000_0 := makeVoteHR(t, 1, 1000, privVals, 0)
    34  	added, err = hvs.AddVote(vote1000_0, "peer1")
    35  	if !added || err != nil {
    36  		t.Error("Expected to successfully add vote from peer", added, err)
    37  	}
    38  
    39  	vote1001_0 := makeVoteHR(t, 1, 1001, privVals, 0)
    40  	added, err = hvs.AddVote(vote1001_0, "peer1")
    41  	if err != ErrGotVoteFromUnwantedRound {
    42  		t.Errorf("expected GotVoteFromUnwantedRoundError, but got %v", err)
    43  	}
    44  	if added {
    45  		t.Error("Expected to *not* add vote from peer, too many catchup rounds.")
    46  	}
    47  
    48  	added, err = hvs.AddVote(vote1001_0, "peer2")
    49  	if !added || err != nil {
    50  		t.Error("Expected to successfully add vote from another peer")
    51  	}
    52  
    53  }
    54  
    55  func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivValidator, valIndex int) *types.Vote {
    56  	privVal := privVals[valIndex]
    57  	pubKey, err := privVal.GetPubKey()
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  	vote := &types.Vote{
    63  		ValidatorAddress: pubKey.Address(),
    64  		ValidatorIndex:   valIndex,
    65  		Height:           height,
    66  		Round:            round,
    67  		Timestamp:        tmtime.Now(),
    68  		Type:             types.PrecommitType,
    69  		BlockID:          types.BlockID{Hash: []byte("fakehash"), PartsHeader: types.PartSetHeader{}},
    70  	}
    71  	chainID := config.ChainID()
    72  	err = privVal.SignVote(chainID, vote)
    73  	if err != nil {
    74  		panic(fmt.Sprintf("Error signing vote: %v", err))
    75  	}
    76  	return vote
    77  }