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.

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