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.

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