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.

95 lines
2.3 KiB

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