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