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.

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