You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.3 KiB

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