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.

94 lines
2.7 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. func TestABCIConsensusParams(t *testing.T) {
  43. cp := DefaultConsensusParams()
  44. abciCP := TM2PB.ConsensusParams(cp)
  45. cp2 := UpdateConsensusParams(*cp, abciCP)
  46. assert.Equal(t, *cp, cp2)
  47. }
  48. type pubKeyEddie struct{}
  49. func (pubKeyEddie) Address() Address { return []byte{} }
  50. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  51. func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
  52. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  53. func (pubKeyEddie) String() string { return "" }
  54. func (pubKeyEddie) Type() string { return "pubKeyEddie" }
  55. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  56. pubkey := ed25519.GenPrivKey().PubKey()
  57. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  58. assert.Equal(t, int64(10), abciVal.Power)
  59. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  60. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  61. }
  62. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  63. pkEd := ed25519.GenPrivKey().PubKey()
  64. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  65. // pubkey must be nil
  66. tmValExpected := abci.Validator{
  67. Address: pkEd.Address(),
  68. Power: 10,
  69. }
  70. assert.Equal(t, tmValExpected, abciVal)
  71. }