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.

189 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. cp.EvidenceParams.MaxAge = 0 // TODO add this to ABCI
  53. abciCP := TM2PB.ConsensusParams(cp)
  54. cp2 := PB2TM.ConsensusParams(abciCP)
  55. assert.Equal(t, *cp, cp2)
  56. }
  57. func newHeader(
  58. height, numTxs int64,
  59. commitHash, dataHash, evidenceHash []byte,
  60. ) *Header {
  61. return &Header{
  62. Height: height,
  63. NumTxs: numTxs,
  64. LastCommitHash: commitHash,
  65. DataHash: dataHash,
  66. EvidenceHash: evidenceHash,
  67. }
  68. }
  69. func TestABCIHeader(t *testing.T) {
  70. // build a full header
  71. var height int64 = 5
  72. var numTxs int64 = 3
  73. header := newHeader(
  74. height, numTxs,
  75. []byte("lastCommitHash"), []byte("dataHash"), []byte("evidenceHash"),
  76. )
  77. protocolVersion := version.Consensus{7, 8}
  78. timestamp := time.Now()
  79. lastBlockID := BlockID{
  80. Hash: []byte("hash"),
  81. PartsHeader: PartSetHeader{
  82. Total: 10,
  83. Hash: []byte("hash"),
  84. },
  85. }
  86. var totalTxs int64 = 100
  87. header.Populate(
  88. protocolVersion, "chainID",
  89. timestamp, lastBlockID, totalTxs,
  90. []byte("valHash"), []byte("nextValHash"),
  91. []byte("consHash"), []byte("appHash"), []byte("lastResultsHash"),
  92. []byte("proposerAddress"),
  93. )
  94. cdc := amino.NewCodec()
  95. headerBz := cdc.MustMarshalBinaryBare(header)
  96. pbHeader := TM2PB.Header(header)
  97. pbHeaderBz, err := proto.Marshal(&pbHeader)
  98. assert.NoError(t, err)
  99. // assert some fields match
  100. assert.EqualValues(t, protocolVersion.Block, pbHeader.Version.Block)
  101. assert.EqualValues(t, protocolVersion.App, pbHeader.Version.App)
  102. assert.EqualValues(t, "chainID", pbHeader.ChainID)
  103. assert.EqualValues(t, height, pbHeader.Height)
  104. assert.EqualValues(t, timestamp, pbHeader.Time)
  105. assert.EqualValues(t, numTxs, pbHeader.NumTxs)
  106. assert.EqualValues(t, totalTxs, pbHeader.TotalTxs)
  107. assert.EqualValues(t, lastBlockID.Hash, pbHeader.LastBlockId.Hash)
  108. assert.EqualValues(t, []byte("lastCommitHash"), pbHeader.LastCommitHash)
  109. assert.Equal(t, []byte("proposerAddress"), pbHeader.ProposerAddress)
  110. // assert the encodings match
  111. // NOTE: they don't yet because Amino encodes
  112. // int64 as zig-zag and we're using non-zigzag in the protobuf.
  113. // See https://github.com/tendermint/tendermint/issues/2682
  114. _, _ = headerBz, pbHeaderBz
  115. // assert.EqualValues(t, headerBz, pbHeaderBz)
  116. }
  117. func TestABCIEvidence(t *testing.T) {
  118. val := NewMockPV()
  119. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  120. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  121. const chainID = "mychain"
  122. ev := &DuplicateVoteEvidence{
  123. PubKey: val.GetPubKey(),
  124. VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
  125. VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
  126. }
  127. abciEv := TM2PB.Evidence(
  128. ev,
  129. NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
  130. time.Now(),
  131. )
  132. assert.Equal(t, "duplicate/vote", abciEv.Type)
  133. }
  134. type pubKeyEddie struct{}
  135. func (pubKeyEddie) Address() Address { return []byte{} }
  136. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  137. func (pubKeyEddie) VerifyBytes(msg []byte, sig []byte) bool { return false }
  138. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  139. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  140. pubkey := ed25519.GenPrivKey().PubKey()
  141. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  142. assert.Equal(t, int64(10), abciVal.Power)
  143. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  144. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  145. }
  146. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  147. pkEd := ed25519.GenPrivKey().PubKey()
  148. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  149. // pubkey must be nil
  150. tmValExpected := abci.Validator{
  151. Address: pkEd.Address(),
  152. Power: 10,
  153. }
  154. assert.Equal(t, tmValExpected, abciVal)
  155. }