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.

122 lines
3.3 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. }
  68. abciHeader := TM2PB.Header(header)
  69. assert.Equal(t, int64(3), abciHeader.Height)
  70. }
  71. func TestABCIEvidence(t *testing.T) {
  72. val := NewMockPV()
  73. blockID := makeBlockID("blockhash", 1000, "partshash")
  74. blockID2 := makeBlockID("blockhash2", 1000, "partshash")
  75. const chainID = "mychain"
  76. ev := &DuplicateVoteEvidence{
  77. PubKey: val.GetPubKey(),
  78. VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
  79. VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
  80. }
  81. abciEv := TM2PB.Evidence(
  82. ev,
  83. NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
  84. time.Now(),
  85. )
  86. assert.Equal(t, "duplicate/vote", abciEv.Type)
  87. }
  88. type pubKeyEddie struct{}
  89. func (pubKeyEddie) Address() Address { return []byte{} }
  90. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  91. func (pubKeyEddie) VerifyBytes(msg []byte, sig crypto.Signature) bool { return false }
  92. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  93. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  94. pubkey := ed25519.GenPrivKey().PubKey()
  95. abciVal := TM2PB.ValidatorFromPubKeyAndPower(pubkey, 10)
  96. assert.Equal(t, int64(10), abciVal.Power)
  97. assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(nil, 10) })
  98. assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(pubKeyEddie{}, 10) })
  99. }