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.

143 lines
3.9 KiB

6 years ago
  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. "github.com/tendermint/tendermint/crypto/secp256k1"
  10. tmtime "github.com/tendermint/tendermint/types/time"
  11. )
  12. func TestABCIPubKey(t *testing.T) {
  13. pkEd := ed25519.GenPrivKey().PubKey()
  14. pkSecp := secp256k1.GenPrivKey().PubKey()
  15. testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
  16. testABCIPubKey(t, pkSecp, ABCIPubKeyTypeSecp256k1)
  17. }
  18. func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) {
  19. abciPubKey := TM2PB.PubKey(pk)
  20. pk2, err := PB2TM.PubKey(abciPubKey)
  21. assert.Nil(t, err)
  22. assert.Equal(t, pk, pk2)
  23. }
  24. func TestABCIValidators(t *testing.T) {
  25. pkEd := ed25519.GenPrivKey().PubKey()
  26. // correct validator
  27. tmValExpected := &Validator{
  28. Address: pkEd.Address(),
  29. PubKey: pkEd,
  30. VotingPower: 10,
  31. }
  32. tmVal := &Validator{
  33. Address: pkEd.Address(),
  34. PubKey: pkEd,
  35. VotingPower: 10,
  36. }
  37. abciVal := TM2PB.ValidatorUpdate(tmVal)
  38. tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  39. assert.Nil(t, err)
  40. assert.Equal(t, tmValExpected, tmVals[0])
  41. abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
  42. assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
  43. // val with address
  44. tmVal.Address = pkEd.Address()
  45. abciVal = TM2PB.ValidatorUpdate(tmVal)
  46. tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  47. assert.Nil(t, err)
  48. assert.Equal(t, tmValExpected, tmVals[0])
  49. // val with incorrect pubkey data
  50. abciVal = TM2PB.ValidatorUpdate(tmVal)
  51. abciVal.PubKey.Data = []byte("incorrect!")
  52. tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  53. assert.NotNil(t, err)
  54. assert.Nil(t, tmVals)
  55. }
  56. func TestABCIConsensusParams(t *testing.T) {
  57. cp := DefaultConsensusParams()
  58. cp.EvidenceParams.MaxAge = 0 // TODO add this to ABCI
  59. abciCP := TM2PB.ConsensusParams(cp)
  60. cp2 := PB2TM.ConsensusParams(abciCP)
  61. assert.Equal(t, *cp, cp2)
  62. }
  63. func TestABCIHeader(t *testing.T) {
  64. header := &Header{
  65. Height: int64(3),
  66. Time: tmtime.Now(),
  67. NumTxs: int64(10),
  68. ProposerAddress: []byte("cloak"),
  69. }
  70. abciHeader := TM2PB.Header(header)
  71. assert.Equal(t, int64(3), abciHeader.Height)
  72. assert.Equal(t, []byte("cloak"), abciHeader.ProposerAddress)
  73. }
  74. func TestABCIEvidence(t *testing.T) {
  75. val := NewMockPV()
  76. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  77. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  78. const chainID = "mychain"
  79. ev := &DuplicateVoteEvidence{
  80. PubKey: val.GetPubKey(),
  81. VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
  82. VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
  83. }
  84. abciEv := TM2PB.Evidence(
  85. ev,
  86. NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
  87. time.Now(),
  88. )
  89. assert.Equal(t, "duplicate/vote", abciEv.Type)
  90. }
  91. type pubKeyEddie struct{}
  92. func (pubKeyEddie) Address() Address { return []byte{} }
  93. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  94. func (pubKeyEddie) VerifyBytes(msg []byte, sig []byte) bool { return false }
  95. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  96. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  97. pubkey := ed25519.GenPrivKey().PubKey()
  98. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  99. assert.Equal(t, int64(10), abciVal.Power)
  100. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  101. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  102. }
  103. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  104. pkEd := ed25519.GenPrivKey().PubKey()
  105. abciVal := TM2PB.Validator(&Validator{
  106. Address: pkEd.Address(),
  107. PubKey: pkEd,
  108. VotingPower: 10,
  109. })
  110. // pubkey must be nil
  111. tmValExpected := abci.Validator{
  112. Address: pkEd.Address(),
  113. Power: 10,
  114. }
  115. assert.Equal(t, tmValExpected, abciVal)
  116. }