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.

176 lines
5.2 KiB

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