Browse Source

add tests for protobuf

Refs #693
pull/1936/head
Anton Kaliaev 6 years ago
parent
commit
715ec19c96
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 52 additions and 1 deletions
  1. +1
    -1
      types/protobuf.go
  2. +51
    -0
      types/protobuf_test.go

+ 1
- 1
types/protobuf.go View File

@ -78,7 +78,7 @@ func (tm2pb) PubKey(pubKey crypto.PubKey) abci.PubKey {
// XXX: panics on nil or unknown pubkey type
func (tm2pb) Validators(vals *ValidatorSet) []abci.Validator {
validators := make([]abci.Validator, len(vals.Validators))
validators := make([]abci.Validator, vals.Size())
for i, val := range vals.Validators {
validators[i] = TM2PB.Validator(val)
}


+ 51
- 0
types/protobuf_test.go View File

@ -2,6 +2,7 @@ package types
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/tendermint/abci/types"
@ -43,6 +44,9 @@ func TestABCIValidators(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, tmValExpected, tmVals[0])
abciVals := TM2PB.Validators(NewValidatorSet(tmVals))
assert.Equal(t, []abci.Validator{abciVal}, abciVals)
// val with address
tmVal.Address = pkEd.Address()
@ -67,3 +71,50 @@ func TestABCIConsensusParams(t *testing.T) {
assert.Equal(t, *cp, cp2)
}
func TestABCIHeader(t *testing.T) {
header := &Header{
Height: int64(3),
Time: time.Now(),
NumTxs: int64(10),
}
abciHeader := TM2PB.Header(header)
assert.Equal(t, int64(3), abciHeader.Height)
}
func TestABCIEvidence(t *testing.T) {
val := NewMockPV()
blockID := makeBlockID("blockhash", 1000, "partshash")
blockID2 := makeBlockID("blockhash2", 1000, "partshash")
const chainID = "mychain"
ev := &DuplicateVoteEvidence{
PubKey: val.GetPubKey(),
VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
}
abciEv := TM2PB.Evidence(
ev,
NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
time.Now(),
)
assert.Equal(t, "duplicate/vote", abciEv.Type)
}
type pubKeyEddie struct{}
func (pubKeyEddie) Address() Address { return []byte{} }
func (pubKeyEddie) Bytes() []byte { return []byte{} }
func (pubKeyEddie) VerifyBytes(msg []byte, sig crypto.Signature) bool { return false }
func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
pubkey := crypto.GenPrivKeyEd25519().PubKey()
abciVal := TM2PB.ValidatorFromPubKeyAndPower(pubkey, 10)
assert.Equal(t, int64(10), abciVal.Power)
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(nil, 10) })
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(pubKeyEddie{}, 10) })
}

Loading…
Cancel
Save