github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/types/protobuf_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	abci "github.com/tendermint/tendermint/abci/types"
    10  	"github.com/tendermint/tendermint/crypto"
    11  	"github.com/tendermint/tendermint/crypto/ed25519"
    12  	cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
    13  )
    14  
    15  func TestABCIPubKey(t *testing.T) {
    16  	pkEd := ed25519.GenPrivKey().PubKey()
    17  	err := testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
    18  	assert.NoError(t, err)
    19  }
    20  
    21  func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
    22  	abciPubKey, err := cryptoenc.PubKeyToProto(pk)
    23  	require.NoError(t, err)
    24  	pk2, err := cryptoenc.PubKeyFromProto(abciPubKey)
    25  	require.NoError(t, err)
    26  	require.Equal(t, pk, pk2)
    27  	return nil
    28  }
    29  
    30  func TestABCIValidators(t *testing.T) {
    31  	pkEd := ed25519.GenPrivKey().PubKey()
    32  
    33  	// correct validator
    34  	tmValExpected := NewValidator(pkEd, 10)
    35  
    36  	tmVal := NewValidator(pkEd, 10)
    37  
    38  	abciVal := TM2PB.ValidatorUpdate(tmVal)
    39  	tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    40  	assert.Nil(t, err)
    41  	assert.Equal(t, tmValExpected, tmVals[0])
    42  
    43  	abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
    44  	assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
    45  
    46  	// val with address
    47  	tmVal.Address = pkEd.Address()
    48  
    49  	abciVal = TM2PB.ValidatorUpdate(tmVal)
    50  	tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
    51  	assert.Nil(t, err)
    52  	assert.Equal(t, tmValExpected, tmVals[0])
    53  }
    54  
    55  func TestABCIConsensusParams(t *testing.T) {
    56  	cp := DefaultConsensusParams()
    57  	abciCP := TM2PB.ConsensusParams(cp)
    58  	cp2 := UpdateConsensusParams(*cp, abciCP)
    59  
    60  	assert.Equal(t, *cp, cp2)
    61  }
    62  
    63  type pubKeyEddie struct{}
    64  
    65  func (pubKeyEddie) Address() Address                            { return []byte{} }
    66  func (pubKeyEddie) Bytes() []byte                               { return []byte{} }
    67  func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
    68  func (pubKeyEddie) Equals(crypto.PubKey) bool                   { return false }
    69  func (pubKeyEddie) String() string                              { return "" }
    70  func (pubKeyEddie) Type() string                                { return "pubKeyEddie" }
    71  
    72  func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
    73  	pubkey := ed25519.GenPrivKey().PubKey()
    74  
    75  	abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
    76  	assert.Equal(t, int64(10), abciVal.Power)
    77  
    78  	assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
    79  	assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
    80  }
    81  
    82  func TestABCIValidatorWithoutPubKey(t *testing.T) {
    83  	pkEd := ed25519.GenPrivKey().PubKey()
    84  
    85  	abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
    86  
    87  	// pubkey must be nil
    88  	tmValExpected := abci.Validator{
    89  		Address: pkEd.Address(),
    90  		Power:   10,
    91  	}
    92  
    93  	assert.Equal(t, tmValExpected, abciVal)
    94  }