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.

105 lines
2.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/tendermint/tendermint/crypto"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. )
  8. // PrivValidator defines the functionality of a local Tendermint validator
  9. // that signs votes, proposals, and heartbeats, and never double signs.
  10. type PrivValidator interface {
  11. GetAddress() Address // redundant since .PubKey().Address()
  12. GetPubKey() crypto.PubKey
  13. SignVote(chainID string, vote *Vote) error
  14. SignProposal(chainID string, proposal *Proposal) error
  15. SignHeartbeat(chainID string, heartbeat *Heartbeat) error
  16. }
  17. //----------------------------------------
  18. // Misc.
  19. type PrivValidatorsByAddress []PrivValidator
  20. func (pvs PrivValidatorsByAddress) Len() int {
  21. return len(pvs)
  22. }
  23. func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
  24. return bytes.Compare(pvs[i].GetAddress(), pvs[j].GetAddress()) == -1
  25. }
  26. func (pvs PrivValidatorsByAddress) Swap(i, j int) {
  27. it := pvs[i]
  28. pvs[i] = pvs[j]
  29. pvs[j] = it
  30. }
  31. //----------------------------------------
  32. // MockPV
  33. // MockPV implements PrivValidator without any safety or persistence.
  34. // Only use it for testing.
  35. type MockPV struct {
  36. privKey crypto.PrivKey
  37. }
  38. func NewMockPV() *MockPV {
  39. return &MockPV{ed25519.GenPrivKey()}
  40. }
  41. // Implements PrivValidator.
  42. func (pv *MockPV) GetAddress() Address {
  43. return pv.privKey.PubKey().Address()
  44. }
  45. // Implements PrivValidator.
  46. func (pv *MockPV) GetPubKey() crypto.PubKey {
  47. return pv.privKey.PubKey()
  48. }
  49. // Implements PrivValidator.
  50. func (pv *MockPV) SignVote(chainID string, vote *Vote) error {
  51. signBytes := vote.SignBytes(chainID)
  52. sig, err := pv.privKey.Sign(signBytes)
  53. if err != nil {
  54. return err
  55. }
  56. vote.Signature = sig
  57. return nil
  58. }
  59. // Implements PrivValidator.
  60. func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
  61. signBytes := proposal.SignBytes(chainID)
  62. sig, err := pv.privKey.Sign(signBytes)
  63. if err != nil {
  64. return err
  65. }
  66. proposal.Signature = sig
  67. return nil
  68. }
  69. // signHeartbeat signs the heartbeat without any checking.
  70. func (pv *MockPV) SignHeartbeat(chainID string, heartbeat *Heartbeat) error {
  71. sig, err := pv.privKey.Sign(heartbeat.SignBytes(chainID))
  72. if err != nil {
  73. return err
  74. }
  75. heartbeat.Signature = sig
  76. return nil
  77. }
  78. // String returns a string representation of the MockPV.
  79. func (pv *MockPV) String() string {
  80. return fmt.Sprintf("MockPV{%v}", pv.GetAddress())
  81. }
  82. // XXX: Implement.
  83. func (pv *MockPV) DisableChecks() {
  84. // Currently this does nothing,
  85. // as MockPV has no safety checks at all.
  86. }