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.

145 lines
3.9 KiB

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