github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/types/protobuf_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	abci "github.com/tendermint/tendermint/abci/types"
    11  	"github.com/tendermint/tendermint/crypto"
    12  	"github.com/tendermint/tendermint/crypto/ed25519"
    13  	cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
    14  )
    15  
    16  func TestABCIPubKey(t *testing.T) {
    17  	pkEd := ed25519.GenPrivKey().PubKey()
    18  	err := testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
    19  	assert.NoError(t, err)
    20  }
    21  
    22  func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
    23  	abciPubKey, err := cryptoenc.PubKeyToProto(pk)
    24  	require.NoError(t, err)
    25  	pk2, err := cryptoenc.PubKeyFromProto(abciPubKey)
    26  	require.NoError(t, err)
    27  	require.Equal(t, pk, pk2)
    28  	return nil
    29  }
    30  
    31  func TestABCIValidators(t *testing.T) {
    32  	pkEd := ed25519.GenPrivKey().PubKey()
    33  
    34  	// correct validator
    35  	tmValExpected := NewValidator(pkEd, 10)
    36  
    37  	tmVal := NewValidator(pkEd, 10)
    38  
    39  	abciVal := TM2PB.ValidatorUpdate(tmVal)
    40  	tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    41  	assert.Nil(t, err)
    42  	assert.Equal(t, tmValExpected, tmVals[0])
    43  
    44  	abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
    45  	assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
    46  
    47  	// val with address
    48  	tmVal.Address = pkEd.Address()
    49  
    50  	abciVal = TM2PB.ValidatorUpdate(tmVal)
    51  	tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    52  	assert.Nil(t, err)
    53  	assert.Equal(t, tmValExpected, tmVals[0])
    54  }
    55  
    56  func TestABCIConsensusParams(t *testing.T) {
    57  	cp := DefaultConsensusParams()
    58  	abciCP := TM2PB.ConsensusParams(cp)
    59  	cp2 := UpdateConsensusParams(*cp, abciCP)
    60  
    61  	assert.Equal(t, *cp, cp2)
    62  }
    63  
    64  func TestABCIEvidence(t *testing.T) {
    65  	val := NewMockPV()
    66  	blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
    67  	blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
    68  	const chainID = "mychain"
    69  	pubKey, err := val.GetPubKey()
    70  	require.NoError(t, err)
    71  	ev := &DuplicateVoteEvidence{
    72  		VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultVoteTime),
    73  		VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultVoteTime),
    74  	}
    75  	abciEv := TM2PB.Evidence(
    76  		ev,
    77  		NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}),
    78  		time.Now(),
    79  	)
    80  
    81  	assert.Equal(t, "duplicate/vote", abciEv.Type)
    82  }
    83  
    84  type pubKeyEddie struct{}
    85  
    86  func (pubKeyEddie) Address() Address                        { return []byte{} }
    87  func (pubKeyEddie) Bytes() []byte                           { return []byte{} }
    88  func (pubKeyEddie) VerifyBytes(msg []byte, sig []byte) bool { return false }
    89  func (pubKeyEddie) Equals(crypto.PubKey) bool               { return false }
    90  func (pubKeyEddie) String() string                          { return "" }
    91  func (pubKeyEddie) Type() string                            { return "pubKeyEddie" }
    92  
    93  func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
    94  	pubkey := ed25519.GenPrivKey().PubKey()
    95  
    96  	abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
    97  	assert.Equal(t, int64(10), abciVal.Power)
    98  
    99  	assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
   100  	assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
   101  }
   102  
   103  func TestABCIValidatorWithoutPubKey(t *testing.T) {
   104  	pkEd := ed25519.GenPrivKey().PubKey()
   105  
   106  	abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
   107  
   108  	// pubkey must be nil
   109  	tmValExpected := abci.Validator{
   110  		Address: pkEd.Address(),
   111  		Power:   10,
   112  	}
   113  
   114  	assert.Equal(t, tmValExpected, abciVal)
   115  }