github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/types/protobuf_test.go (about) 1 package types 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/golang/protobuf/proto" // nolint: staticcheck // still used by gogoproto 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 amino "github.com/tendermint/go-amino" 12 13 abci "github.com/tendermint/tendermint/abci/types" 14 "github.com/tendermint/tendermint/crypto" 15 "github.com/tendermint/tendermint/crypto/ed25519" 16 "github.com/tendermint/tendermint/crypto/secp256k1" 17 "github.com/tendermint/tendermint/version" 18 ) 19 20 func TestABCIPubKey(t *testing.T) { 21 pkEd := ed25519.GenPrivKey().PubKey() 22 pkSecp := secp256k1.GenPrivKey().PubKey() 23 testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519) 24 testABCIPubKey(t, pkSecp, ABCIPubKeyTypeSecp256k1) 25 } 26 27 func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) { 28 abciPubKey := TM2PB.PubKey(pk) 29 pk2, err := PB2TM.PubKey(abciPubKey) 30 assert.Nil(t, err) 31 assert.Equal(t, pk, pk2) 32 } 33 34 func TestABCIValidators(t *testing.T) { 35 pkEd := ed25519.GenPrivKey().PubKey() 36 37 // correct validator 38 tmValExpected := NewValidator(pkEd, 10) 39 40 tmVal := NewValidator(pkEd, 10) 41 42 abciVal := TM2PB.ValidatorUpdate(tmVal) 43 tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal}) 44 assert.Nil(t, err) 45 assert.Equal(t, tmValExpected, tmVals[0]) 46 47 abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals)) 48 assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals) 49 50 // val with address 51 tmVal.Address = pkEd.Address() 52 53 abciVal = TM2PB.ValidatorUpdate(tmVal) 54 tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal}) 55 assert.Nil(t, err) 56 assert.Equal(t, tmValExpected, tmVals[0]) 57 58 // val with incorrect pubkey data 59 abciVal = TM2PB.ValidatorUpdate(tmVal) 60 abciVal.PubKey.Data = []byte("incorrect!") 61 tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal}) 62 assert.NotNil(t, err) 63 assert.Nil(t, tmVals) 64 } 65 66 func TestABCIConsensusParams(t *testing.T) { 67 cp := DefaultConsensusParams() 68 abciCP := TM2PB.ConsensusParams(cp) 69 cp2 := cp.Update(abciCP) 70 71 assert.Equal(t, *cp, cp2) 72 } 73 74 func newHeader( 75 height, numTxs int64, 76 commitHash, dataHash, evidenceHash []byte, 77 ) *Header { 78 return &Header{ 79 Height: height, 80 NumTxs: numTxs, 81 LastCommitHash: commitHash, 82 DataHash: dataHash, 83 EvidenceHash: evidenceHash, 84 } 85 } 86 87 func TestABCIHeader(t *testing.T) { 88 // build a full header 89 var height int64 = 5 90 var numTxs int64 = 3 91 header := newHeader( 92 height, numTxs, 93 []byte("lastCommitHash"), []byte("dataHash"), []byte("evidenceHash"), 94 ) 95 protocolVersion := version.Consensus{Block: 7, App: 8} 96 timestamp := time.Now() 97 lastBlockID := BlockID{ 98 Hash: []byte("hash"), 99 PartsHeader: PartSetHeader{ 100 Total: 10, 101 Hash: []byte("hash"), 102 }, 103 } 104 var totalTxs int64 = 100 105 header.Populate( 106 protocolVersion, "chainID", 107 timestamp, lastBlockID, totalTxs, 108 []byte("valHash"), []byte("nextValHash"), 109 []byte("consHash"), []byte("appHash"), []byte("lastResultsHash"), 110 []byte("proposerAddress"), 111 ) 112 113 cdc := amino.NewCodec() 114 headerBz := cdc.MustMarshalBinaryBare(header) 115 116 pbHeader := TM2PB.Header(header) 117 pbHeaderBz, err := proto.Marshal(&pbHeader) 118 assert.NoError(t, err) 119 120 // assert some fields match 121 assert.EqualValues(t, protocolVersion.Block, pbHeader.Version.Block) 122 assert.EqualValues(t, protocolVersion.App, pbHeader.Version.App) 123 assert.EqualValues(t, "chainID", pbHeader.ChainID) 124 assert.EqualValues(t, height, pbHeader.Height) 125 assert.EqualValues(t, timestamp, pbHeader.Time) 126 assert.EqualValues(t, numTxs, pbHeader.NumTxs) 127 assert.EqualValues(t, totalTxs, pbHeader.TotalTxs) 128 assert.EqualValues(t, lastBlockID.Hash, pbHeader.LastBlockId.Hash) 129 assert.EqualValues(t, []byte("lastCommitHash"), pbHeader.LastCommitHash) 130 assert.Equal(t, []byte("proposerAddress"), pbHeader.ProposerAddress) 131 132 // assert the encodings match 133 // NOTE: they don't yet because Amino encodes 134 // int64 as zig-zag and we're using non-zigzag in the protobuf. 135 // See https://github.com/tendermint/tendermint/issues/2682 136 _, _ = headerBz, pbHeaderBz 137 // assert.EqualValues(t, headerBz, pbHeaderBz) 138 139 } 140 141 func TestABCIEvidence(t *testing.T) { 142 val := NewMockPV() 143 blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash")) 144 blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash")) 145 const chainID = "mychain" 146 pubKey, err := val.GetPubKey() 147 require.NoError(t, err) 148 ev := &DuplicateVoteEvidence{ 149 PubKey: pubKey, 150 VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID), 151 VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2), 152 } 153 abciEv := TM2PB.Evidence( 154 ev, 155 NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}), 156 time.Now(), 157 ) 158 159 assert.Equal(t, "duplicate/vote", abciEv.Type) 160 } 161 162 type pubKeyEddie struct{} 163 164 func (pubKeyEddie) Address() Address { return []byte{} } 165 func (pubKeyEddie) Bytes() []byte { return []byte{} } 166 func (pubKeyEddie) VerifyBytes(msg []byte, sig []byte) bool { return false } 167 func (pubKeyEddie) Equals(crypto.PubKey) bool { return false } 168 169 func TestABCIValidatorFromPubKeyAndPower(t *testing.T) { 170 pubkey := ed25519.GenPrivKey().PubKey() 171 172 abciVal := TM2PB.NewValidatorUpdate(pubkey, 10) 173 assert.Equal(t, int64(10), abciVal.Power) 174 175 assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) }) 176 assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) }) 177 } 178 179 func TestABCIValidatorWithoutPubKey(t *testing.T) { 180 pkEd := ed25519.GenPrivKey().PubKey() 181 182 abciVal := TM2PB.Validator(NewValidator(pkEd, 10)) 183 184 // pubkey must be nil 185 tmValExpected := abci.Validator{ 186 Address: pkEd.Address(), 187 Power: 10, 188 } 189 190 assert.Equal(t, tmValExpected, abciVal) 191 }