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.

115 lines
3.3 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  11. )
  12. func TestABCIPubKey(t *testing.T) {
  13. pkEd := ed25519.GenPrivKey().PubKey()
  14. err := testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
  15. assert.NoError(t, err)
  16. }
  17. func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
  18. abciPubKey, err := cryptoenc.PubKeyToProto(pk)
  19. require.NoError(t, err)
  20. pk2, err := cryptoenc.PubKeyFromProto(abciPubKey)
  21. require.NoError(t, err)
  22. require.Equal(t, pk, pk2)
  23. return nil
  24. }
  25. func TestABCIValidators(t *testing.T) {
  26. pkEd := ed25519.GenPrivKey().PubKey()
  27. // correct validator
  28. tmValExpected := NewValidator(pkEd, 10)
  29. tmVal := NewValidator(pkEd, 10)
  30. abciVal := TM2PB.ValidatorUpdate(tmVal)
  31. tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  32. assert.Nil(t, err)
  33. assert.Equal(t, tmValExpected, tmVals[0])
  34. abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
  35. assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
  36. // val with address
  37. tmVal.Address = pkEd.Address()
  38. abciVal = TM2PB.ValidatorUpdate(tmVal)
  39. tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  40. assert.Nil(t, err)
  41. assert.Equal(t, tmValExpected, tmVals[0])
  42. }
  43. func TestABCIConsensusParams(t *testing.T) {
  44. cp := DefaultConsensusParams()
  45. abciCP := TM2PB.ConsensusParams(cp)
  46. cp2 := UpdateConsensusParams(*cp, abciCP)
  47. assert.Equal(t, *cp, cp2)
  48. }
  49. func TestABCIEvidence(t *testing.T) {
  50. val := NewMockPV()
  51. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  52. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  53. const chainID = "mychain"
  54. pubKey, err := val.GetPubKey()
  55. require.NoError(t, err)
  56. ev := &DuplicateVoteEvidence{
  57. VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultVoteTime),
  58. VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultVoteTime),
  59. }
  60. abciEv := TM2PB.Evidence(
  61. ev,
  62. NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}),
  63. time.Now(),
  64. )
  65. assert.Equal(t, "duplicate/vote", abciEv.Type)
  66. }
  67. type pubKeyEddie struct{}
  68. func (pubKeyEddie) Address() Address { return []byte{} }
  69. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  70. func (pubKeyEddie) VerifyBytes(msg []byte, sig []byte) bool { return false }
  71. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  72. func (pubKeyEddie) String() string { return "" }
  73. func (pubKeyEddie) Type() string { return "pubKeyEddie" }
  74. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  75. pubkey := ed25519.GenPrivKey().PubKey()
  76. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  77. assert.Equal(t, int64(10), abciVal.Power)
  78. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  79. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  80. }
  81. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  82. pkEd := ed25519.GenPrivKey().PubKey()
  83. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  84. // pubkey must be nil
  85. tmValExpected := abci.Validator{
  86. Address: pkEd.Address(),
  87. Power: 10,
  88. }
  89. assert.Equal(t, tmValExpected, abciVal)
  90. }