2015-08-10 20:38:45 -07:00
|
|
|
package types
|
2014-09-04 03:32:38 -07:00
|
|
|
|
|
|
|
import (
|
2015-08-10 20:38:45 -07:00
|
|
|
"bytes"
|
2014-12-09 18:49:04 -08:00
|
|
|
"fmt"
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
"github.com/tendermint/go-crypto"
|
2014-12-09 18:49:04 -08:00
|
|
|
)
|
|
|
|
|
2017-09-21 09:19:21 -07:00
|
|
|
// PrivValidator defines the functionality of a local Tendermint validator
|
|
|
|
// that signs votes, proposals, and heartbeats, and never double signs.
|
2017-09-18 15:12:31 -07:00
|
|
|
type PrivValidator interface {
|
2018-01-05 22:26:51 -08:00
|
|
|
GetAddress() Address // redundant since .PubKey().Address()
|
2017-09-21 13:32:02 -07:00
|
|
|
GetPubKey() crypto.PubKey
|
2017-09-18 15:12:31 -07:00
|
|
|
|
|
|
|
SignVote(chainID string, vote *Vote) error
|
|
|
|
SignProposal(chainID string, proposal *Proposal) error
|
|
|
|
SignHeartbeat(chainID string, heartbeat *Heartbeat) error
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
//----------------------------------------
|
|
|
|
// Misc.
|
2017-09-18 19:05:33 -07:00
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
type PrivValidatorsByAddress []PrivValidator
|
2017-09-18 15:12:31 -07:00
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
func (pvs PrivValidatorsByAddress) Len() int {
|
|
|
|
return len(pvs)
|
2017-05-25 10:39:36 -07:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(pvs[i].GetAddress(), pvs[j].GetAddress()) == -1
|
2017-09-21 13:58:18 -07:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
|
|
|
|
it := pvs[i]
|
|
|
|
pvs[i] = pvs[j]
|
|
|
|
pvs[j] = it
|
2017-09-21 13:58:18 -07:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
//----------------------------------------
|
|
|
|
// MockPV
|
2017-09-21 13:58:18 -07:00
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// MockPV implements PrivValidator without any safety or persistence.
|
|
|
|
// Only use it for testing.
|
|
|
|
type MockPV struct {
|
|
|
|
privKey crypto.PrivKey
|
2017-09-21 13:58:18 -07:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
func NewMockPV() *MockPV {
|
|
|
|
return &MockPV{crypto.GenPrivKeyEd25519()}
|
2014-10-20 19:02:10 -07:00
|
|
|
}
|
|
|
|
|
2017-09-21 09:19:21 -07:00
|
|
|
// Implements PrivValidator.
|
2018-03-30 15:18:43 -07:00
|
|
|
func (pv *MockPV) GetAddress() Address {
|
|
|
|
return pv.privKey.PubKey().Address()
|
2014-12-28 00:44:56 -08:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// Implements PrivValidator.
|
|
|
|
func (pv *MockPV) GetPubKey() crypto.PubKey {
|
|
|
|
return pv.privKey.PubKey()
|
2017-12-21 12:37:27 -08:00
|
|
|
}
|
2017-09-18 20:16:14 -07:00
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// Implements PrivValidator.
|
|
|
|
func (pv *MockPV) SignVote(chainID string, vote *Vote) error {
|
2018-01-14 16:05:22 -08:00
|
|
|
signBytes := vote.SignBytes(chainID)
|
2018-03-30 15:18:43 -07:00
|
|
|
sig := pv.privKey.Sign(signBytes)
|
2018-01-04 10:52:41 -08:00
|
|
|
vote.Signature = sig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// Implements PrivValidator.
|
|
|
|
func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
|
2018-01-14 16:05:22 -08:00
|
|
|
signBytes := proposal.SignBytes(chainID)
|
2018-03-30 15:18:43 -07:00
|
|
|
sig := pv.privKey.Sign(signBytes)
|
2018-01-04 10:52:41 -08:00
|
|
|
proposal.Signature = sig
|
|
|
|
return nil
|
2017-12-21 12:37:27 -08:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// signHeartbeat signs the heartbeat without any checking.
|
|
|
|
func (pv *MockPV) SignHeartbeat(chainID string, heartbeat *Heartbeat) error {
|
|
|
|
sig := pv.privKey.Sign(heartbeat.SignBytes(chainID))
|
|
|
|
heartbeat.Signature = sig
|
|
|
|
return nil
|
2017-12-21 12:37:27 -08:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:18:43 -07:00
|
|
|
// String returns a string representation of the MockPV.
|
|
|
|
func (pv *MockPV) String() string {
|
|
|
|
return fmt.Sprintf("MockPV{%v}", pv.GetAddress())
|
2017-12-21 12:37:27 -08:00
|
|
|
}
|
2018-04-05 07:05:45 -07:00
|
|
|
|
|
|
|
// XXX: Implement.
|
|
|
|
func (pv *MockPV) DisableChecks() {
|
|
|
|
// Currently this does nothing,
|
|
|
|
// as MockPV has no safety checks at all.
|
|
|
|
return
|
|
|
|
}
|