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.

116 lines
3.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. 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. func TestABCIEvidence(t *testing.T) {
  49. val := NewMockPV()
  50. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  51. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  52. const chainID = "mychain"
  53. pubKey, err := val.GetPubKey()
  54. require.NoError(t, err)
  55. ev := &DuplicateVoteEvidence{
  56. VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultVoteTime),
  57. VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultVoteTime),
  58. }
  59. abciEv := TM2PB.Evidence(
  60. ev,
  61. NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}),
  62. )
  63. assert.Equal(t, abci.EvidenceType_DUPLICATE_VOTE, abciEv.Type)
  64. assert.Equal(t, ev.Time(), abciEv.GetTime())
  65. assert.Equal(t, ev.Address(), abciEv.Validator.GetAddress())
  66. assert.Equal(t, ev.Height(), abciEv.GetHeight())
  67. }
  68. type pubKeyEddie struct{}
  69. func (pubKeyEddie) Address() Address { return []byte{} }
  70. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  71. func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
  72. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  73. func (pubKeyEddie) String() string { return "" }
  74. func (pubKeyEddie) Type() string { return "pubKeyEddie" }
  75. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  76. pubkey := ed25519.GenPrivKey().PubKey()
  77. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  78. assert.Equal(t, int64(10), abciVal.Power)
  79. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  80. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  81. }
  82. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  83. pkEd := ed25519.GenPrivKey().PubKey()
  84. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  85. // pubkey must be nil
  86. tmValExpected := abci.Validator{
  87. Address: pkEd.Address(),
  88. Power: 10,
  89. }
  90. assert.Equal(t, tmValExpected, abciVal)
  91. }