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.

86 lines
2.5 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  10. )
  11. func TestABCIPubKey(t *testing.T) {
  12. pkEd := ed25519.GenPrivKey().PubKey()
  13. err := testABCIPubKey(t, pkEd, ABCIPubKeyTypeEd25519)
  14. assert.NoError(t, err)
  15. }
  16. func testABCIPubKey(t *testing.T, pk crypto.PubKey, typeStr string) error {
  17. abciPubKey, err := cryptoenc.PubKeyToProto(pk)
  18. require.NoError(t, err)
  19. pk2, err := cryptoenc.PubKeyFromProto(abciPubKey)
  20. require.NoError(t, err)
  21. require.Equal(t, pk, pk2)
  22. return nil
  23. }
  24. func TestABCIValidators(t *testing.T) {
  25. pkEd := ed25519.GenPrivKey().PubKey()
  26. // correct validator
  27. tmValExpected := NewValidator(pkEd, 10)
  28. tmVal := NewValidator(pkEd, 10)
  29. abciVal := TM2PB.ValidatorUpdate(tmVal)
  30. tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
  31. assert.Nil(t, err)
  32. assert.Equal(t, tmValExpected, tmVals[0])
  33. abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
  34. assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
  35. // val with address
  36. tmVal.Address = pkEd.Address()
  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. }
  42. type pubKeyEddie struct{}
  43. func (pubKeyEddie) Address() Address { return []byte{} }
  44. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  45. func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
  46. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  47. func (pubKeyEddie) String() string { return "" }
  48. func (pubKeyEddie) Type() string { return "pubKeyEddie" }
  49. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  50. pubkey := ed25519.GenPrivKey().PubKey()
  51. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  52. assert.Equal(t, int64(10), abciVal.Power)
  53. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  54. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  55. }
  56. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  57. pkEd := ed25519.GenPrivKey().PubKey()
  58. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  59. // pubkey must be nil
  60. tmValExpected := abci.Validator{
  61. Address: pkEd.Address(),
  62. Power: 10,
  63. }
  64. assert.Equal(t, tmValExpected, abciVal)
  65. }