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.

114 lines
3.4 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.Height(), abciEv.GetHeight())
  65. }
  66. type pubKeyEddie struct{}
  67. func (pubKeyEddie) Address() Address { return []byte{} }
  68. func (pubKeyEddie) Bytes() []byte { return []byte{} }
  69. func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
  70. func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
  71. func (pubKeyEddie) String() string { return "" }
  72. func (pubKeyEddie) Type() string { return "pubKeyEddie" }
  73. func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
  74. pubkey := ed25519.GenPrivKey().PubKey()
  75. abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
  76. assert.Equal(t, int64(10), abciVal.Power)
  77. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
  78. assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
  79. }
  80. func TestABCIValidatorWithoutPubKey(t *testing.T) {
  81. pkEd := ed25519.GenPrivKey().PubKey()
  82. abciVal := TM2PB.Validator(NewValidator(pkEd, 10))
  83. // pubkey must be nil
  84. tmValExpected := abci.Validator{
  85. Address: pkEd.Address(),
  86. Power: 10,
  87. }
  88. assert.Equal(t, tmValExpected, abciVal)
  89. }