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"
|
2017-03-21 14:46:15 -07:00
|
|
|
"encoding/json"
|
2014-12-31 16:14:26 -08:00
|
|
|
"errors"
|
2014-12-09 18:49:04 -08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-10-18 07:20:05 -07:00
|
|
|
"os"
|
2014-12-22 18:49:37 -08:00
|
|
|
"sync"
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2017-05-02 00:53:32 -07:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2017-04-21 15:13:25 -07:00
|
|
|
data "github.com/tendermint/go-wire/data"
|
2017-05-02 00:53:32 -07:00
|
|
|
. "github.com/tendermint/tmlibs/common"
|
2014-12-09 18:49:04 -08:00
|
|
|
)
|
|
|
|
|
2017-09-11 12:45:12 -07:00
|
|
|
// TODO: type ?
|
2014-12-09 18:49:04 -08:00
|
|
|
const (
|
|
|
|
stepNone = 0 // Used to distinguish the initial state
|
|
|
|
stepPropose = 1
|
|
|
|
stepPrevote = 2
|
|
|
|
stepPrecommit = 3
|
2014-09-04 03:32:38 -07:00
|
|
|
)
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func voteToStep(vote *Vote) int8 {
|
2014-12-09 18:49:04 -08:00
|
|
|
switch vote.Type {
|
2015-08-10 20:38:45 -07:00
|
|
|
case VoteTypePrevote:
|
2014-12-09 18:49:04 -08:00
|
|
|
return stepPrevote
|
2015-08-10 20:38:45 -07:00
|
|
|
case VoteTypePrecommit:
|
2014-12-09 18:49:04 -08:00
|
|
|
return stepPrecommit
|
|
|
|
default:
|
2015-07-19 16:42:52 -07:00
|
|
|
PanicSanity("Unknown vote type")
|
|
|
|
return 0
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 03:32:38 -07:00
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// PrivValidator defines the functionality of a local Tendermint validator.
|
2017-09-18 15:12:31 -07:00
|
|
|
type PrivValidator interface {
|
|
|
|
Address() data.Bytes // redundant since .PubKey().Address()
|
|
|
|
PubKey() crypto.PubKey
|
|
|
|
|
|
|
|
SignVote(chainID string, vote *Vote) error
|
|
|
|
SignProposal(chainID string, proposal *Proposal) error
|
|
|
|
SignHeartbeat(chainID string, heartbeat *Heartbeat) error
|
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// PrivValidatorFS implements PrivValidator using data persisted to disk
|
|
|
|
// to prevent double signing. The Signer itself can be mutated to use
|
|
|
|
// something besides the default, for instance a hardware signer.
|
|
|
|
type PrivValidatorFS struct {
|
|
|
|
ID ValidatorID `json:"id"`
|
|
|
|
Signer Signer `json:"signer"`
|
|
|
|
|
|
|
|
// mutable state to be persisted to disk
|
|
|
|
// after each signature to prevent double signing
|
|
|
|
mtx sync.Mutex
|
|
|
|
Info LastSignedInfo `json:"info"`
|
2017-09-18 15:12:31 -07:00
|
|
|
|
|
|
|
// For persistence.
|
|
|
|
// Overloaded for testing.
|
|
|
|
filePath string
|
2017-05-25 10:39:36 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// LoadOrGenPrivValidatorFS loads a PrivValidatorFS from the given filePath
|
|
|
|
// or else generates a new one and saves it to the filePath.
|
|
|
|
func LoadOrGenPrivValidatorFS(filePath string) *PrivValidatorFS {
|
|
|
|
var PrivValidatorFS *PrivValidatorFS
|
2017-09-12 15:18:07 -07:00
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
2017-09-18 19:05:33 -07:00
|
|
|
PrivValidatorFS = LoadPrivValidatorFS(filePath)
|
2017-09-12 15:18:07 -07:00
|
|
|
} else {
|
2017-09-18 19:05:33 -07:00
|
|
|
PrivValidatorFS = GenPrivValidatorFS(filePath)
|
|
|
|
PrivValidatorFS.Save()
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2017-09-18 19:05:33 -07:00
|
|
|
return PrivValidatorFS
|
2014-10-20 19:02:10 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// LoadPrivValidatorFS loads a PrivValidatorFS from the filePath.
|
|
|
|
func LoadPrivValidatorFS(filePath string) *PrivValidatorFS {
|
2015-05-15 18:05:09 -07:00
|
|
|
privValJSONBytes, err := ioutil.ReadFile(filePath)
|
2014-12-09 18:49:04 -08:00
|
|
|
if err != nil {
|
2015-06-16 21:16:58 -07:00
|
|
|
Exit(err.Error())
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2017-09-18 19:05:33 -07:00
|
|
|
privVal := PrivValidatorFS{}
|
2017-03-21 14:46:15 -07:00
|
|
|
err = json.Unmarshal(privValJSONBytes, &privVal)
|
2014-12-09 18:49:04 -08:00
|
|
|
if err != nil {
|
2015-05-15 18:05:09 -07:00
|
|
|
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2017-05-25 10:39:36 -07:00
|
|
|
|
2015-05-15 18:05:09 -07:00
|
|
|
privVal.filePath = filePath
|
2017-03-21 14:46:15 -07:00
|
|
|
return &privVal
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// GenPrivValidatorFS generates a new validator with randomly generated private key
|
|
|
|
// and sets the filePath, but does not call Save().
|
|
|
|
func GenPrivValidatorFS(filePath string) *PrivValidatorFS {
|
2017-09-12 15:18:07 -07:00
|
|
|
privKey := crypto.GenPrivKeyEd25519().Wrap()
|
2017-09-18 19:05:33 -07:00
|
|
|
return &PrivValidatorFS{
|
|
|
|
ID: ValidatorID{privKey.PubKey().Address(), privKey.PubKey()},
|
|
|
|
Info: LastSignedInfo{
|
2017-09-18 15:12:31 -07:00
|
|
|
LastStep: stepNone,
|
|
|
|
},
|
2017-09-12 15:18:07 -07:00
|
|
|
Signer: NewDefaultSigner(privKey),
|
2017-09-18 19:05:33 -07:00
|
|
|
filePath: filePath,
|
2015-10-18 07:20:05 -07:00
|
|
|
}
|
2017-09-12 15:18:07 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// LoadPrivValidatorWithSigner loads a PrivValidatorFS with a custom
|
|
|
|
// signer object. The PrivValidatorFS handles double signing prevention by persisting
|
|
|
|
// data to the filePath, while the Signer handles the signing.
|
|
|
|
// If the filePath does not exist, the PrivValidatorFS must be created manually and saved.
|
|
|
|
func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(ValidatorID) Signer) *PrivValidatorFS {
|
|
|
|
privValJSONBytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
Exit(err.Error())
|
2017-09-12 15:18:07 -07:00
|
|
|
}
|
2017-09-18 19:05:33 -07:00
|
|
|
privVal := PrivValidatorFS{}
|
|
|
|
err = json.Unmarshal(privValJSONBytes, &privVal)
|
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
privVal.filePath = filePath
|
|
|
|
privVal.Signer = signerFunc(privVal.ID)
|
|
|
|
return &privVal
|
2017-09-12 15:18:07 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// Address returns the address of the validator.
|
|
|
|
func (pv *PrivValidatorFS) Address() data.Bytes {
|
|
|
|
return pv.ID.Address
|
|
|
|
}
|
2015-10-18 07:20:05 -07:00
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// PubKey returns the public key of the validator.
|
|
|
|
func (pv *PrivValidatorFS) PubKey() crypto.PubKey {
|
|
|
|
return pv.ID.PubKey
|
2015-04-03 16:15:52 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// Save persists the PrivValidatorFS to disk.
|
|
|
|
func (privVal *PrivValidatorFS) Save() {
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
|
|
|
privVal.save()
|
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
func (privVal *PrivValidatorFS) save() {
|
2015-05-15 18:05:09 -07:00
|
|
|
if privVal.filePath == "" {
|
2015-07-19 16:42:52 -07:00
|
|
|
PanicSanity("Cannot save PrivValidator: filePath not set")
|
2015-05-15 18:05:09 -07:00
|
|
|
}
|
2017-03-21 14:46:15 -07:00
|
|
|
jsonBytes, err := json.Marshal(privVal)
|
|
|
|
if err != nil {
|
|
|
|
// `@; BOOM!!!
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
err = WriteFileAtomic(privVal.filePath, jsonBytes, 0600)
|
2014-12-28 00:44:56 -08:00
|
|
|
if err != nil {
|
2015-04-18 12:48:14 -07:00
|
|
|
// `@; BOOM!!!
|
2015-07-19 16:42:52 -07:00
|
|
|
PanicCrisis(err)
|
2014-12-28 00:44:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// Reset resets all fields in the PrivValidatorFS.Info.
|
2016-08-14 09:31:24 -07:00
|
|
|
// NOTE: Unsafe!
|
2017-09-18 19:05:33 -07:00
|
|
|
func (privVal *PrivValidatorFS) Reset() {
|
2017-09-18 15:12:31 -07:00
|
|
|
privVal.Info.LastHeight = 0
|
|
|
|
privVal.Info.LastRound = 0
|
|
|
|
privVal.Info.LastStep = 0
|
|
|
|
privVal.Info.LastSignature = crypto.Signature{}
|
|
|
|
privVal.Info.LastSignBytes = nil
|
2016-08-14 09:31:24 -07:00
|
|
|
privVal.Save()
|
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// SignVote signs a canonical representation of the vote, along with the chainID.
|
|
|
|
func (privVal *PrivValidatorFS) SignVote(chainID string, vote *Vote) error {
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
2016-08-09 14:18:29 -07:00
|
|
|
signature, err := privVal.signBytesHRS(vote.Height, vote.Round, voteToStep(vote), SignBytes(chainID, vote))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Error signing vote: %v", err))
|
|
|
|
}
|
2016-12-17 21:10:14 -08:00
|
|
|
vote.Signature = signature
|
2016-08-09 14:18:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// SignProposal signs a canonical representation of the proposal, along with the chainID.
|
|
|
|
func (privVal *PrivValidatorFS) SignProposal(chainID string, proposal *Proposal) error {
|
2016-08-09 14:18:29 -07:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
|
|
|
signature, err := privVal.signBytesHRS(proposal.Height, proposal.Round, stepPropose, SignBytes(chainID, proposal))
|
|
|
|
if err != nil {
|
2017-05-12 14:07:53 -07:00
|
|
|
return fmt.Errorf("Error signing proposal: %v", err)
|
2016-08-09 14:18:29 -07:00
|
|
|
}
|
2016-12-17 21:10:14 -08:00
|
|
|
proposal.Signature = signature
|
2016-08-09 14:18:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.
|
|
|
|
func (privVal *PrivValidatorFS) SignHeartbeat(chainID string, heartbeat *Heartbeat) error {
|
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
|
|
|
var err error
|
|
|
|
heartbeat.Signature, err = privVal.Signer.Sign(SignBytes(chainID, heartbeat))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-09 14:18:29 -07:00
|
|
|
// check if there's a regression. Else sign and write the hrs+signature to disk
|
2017-09-18 19:05:33 -07:00
|
|
|
func (privVal *PrivValidatorFS) signBytesHRS(height, round int, step int8, signBytes []byte) (crypto.Signature, error) {
|
2017-03-21 14:12:02 -07:00
|
|
|
sig := crypto.Signature{}
|
2017-09-18 15:12:31 -07:00
|
|
|
info := privVal.Info
|
2016-08-09 14:18:29 -07:00
|
|
|
// If height regression, err
|
2017-09-18 15:12:31 -07:00
|
|
|
if info.LastHeight > height {
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, errors.New("Height regression")
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
|
|
|
// More cases for when the height matches
|
2017-09-18 15:12:31 -07:00
|
|
|
if info.LastHeight == height {
|
2016-08-09 14:18:29 -07:00
|
|
|
// If round regression, err
|
2017-09-18 15:12:31 -07:00
|
|
|
if info.LastRound > round {
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, errors.New("Round regression")
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
2016-08-09 14:18:29 -07:00
|
|
|
// If step regression, err
|
2017-09-18 15:12:31 -07:00
|
|
|
if info.LastRound == round {
|
|
|
|
if info.LastStep > step {
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, errors.New("Step regression")
|
2017-09-18 15:12:31 -07:00
|
|
|
} else if info.LastStep == step {
|
|
|
|
if info.LastSignBytes != nil {
|
|
|
|
if info.LastSignature.Empty() {
|
2016-08-14 09:31:24 -07:00
|
|
|
PanicSanity("privVal: LastSignature is nil but LastSignBytes is not!")
|
|
|
|
}
|
|
|
|
// so we dont sign a conflicting vote or proposal
|
|
|
|
// NOTE: proposals are non-deterministic (include time),
|
|
|
|
// so we can actually lose them, but will still never sign conflicting ones
|
2017-09-18 15:12:31 -07:00
|
|
|
if bytes.Equal(info.LastSignBytes, signBytes) {
|
|
|
|
// log.Notice("Using info.LastSignature", "sig", info.LastSignature)
|
|
|
|
return info.LastSignature, nil
|
2016-08-14 09:31:24 -07:00
|
|
|
}
|
2016-08-09 14:18:29 -07:00
|
|
|
}
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, errors.New("Step regression")
|
2016-08-09 14:18:29 -07:00
|
|
|
}
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2014-12-17 01:37:13 -08:00
|
|
|
|
2016-08-09 14:18:29 -07:00
|
|
|
// Sign
|
2017-09-18 15:12:31 -07:00
|
|
|
sig, err := privVal.Signer.Sign(signBytes)
|
2017-09-15 22:07:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return sig, err
|
|
|
|
}
|
2016-08-09 14:18:29 -07:00
|
|
|
|
2014-12-17 01:37:13 -08:00
|
|
|
// Persist height/round/step
|
2017-09-18 15:12:31 -07:00
|
|
|
privVal.Info.LastHeight = height
|
|
|
|
privVal.Info.LastRound = round
|
|
|
|
privVal.Info.LastStep = step
|
|
|
|
privVal.Info.LastSignature = sig
|
|
|
|
privVal.Info.LastSignBytes = signBytes
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.save()
|
2014-12-17 01:37:13 -08:00
|
|
|
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, nil
|
2017-09-18 19:05:33 -07:00
|
|
|
}
|
2016-06-26 12:33:11 -07:00
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// String returns a string representation of the PrivValidatorFS.
|
|
|
|
func (privVal *PrivValidatorFS) String() string {
|
|
|
|
info := privVal.Info
|
|
|
|
return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", privVal.Address(), info.LastHeight, info.LastRound, info.LastStep)
|
2014-12-16 05:40:17 -08:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
// ValidatorID contains the identity of the validator.
|
|
|
|
type ValidatorID struct {
|
|
|
|
Address data.Bytes `json:"address"`
|
|
|
|
PubKey crypto.PubKey `json:"pub_key"`
|
2017-07-29 11:15:10 -07:00
|
|
|
}
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
// LastSignedInfo contains information about the latest
|
|
|
|
// data signed by a validator to help prevent double signing.
|
|
|
|
type LastSignedInfo struct {
|
|
|
|
LastHeight int `json:"last_height"`
|
|
|
|
LastRound int `json:"last_round"`
|
|
|
|
LastStep int8 `json:"last_step"`
|
|
|
|
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
|
|
|
|
LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signer is an interface that defines how to sign messages.
|
|
|
|
// It is the caller's duty to verify the msg before calling Sign,
|
|
|
|
// eg. to avoid double signing.
|
|
|
|
// Currently, the only callers are SignVote, SignProposal, and SignHeartbeat.
|
|
|
|
type Signer interface {
|
|
|
|
Sign(msg []byte) (crypto.Signature, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultSigner implements Signer.
|
|
|
|
// It uses a standard, unencrypted crypto.PrivKey.
|
|
|
|
type DefaultSigner struct {
|
|
|
|
PrivKey crypto.PrivKey `json:"priv_key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultSigner returns an instance of DefaultSigner.
|
|
|
|
func NewDefaultSigner(priv crypto.PrivKey) *DefaultSigner {
|
|
|
|
return &DefaultSigner{
|
|
|
|
PrivKey: priv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign implements Signer. It signs the byte slice with a private key.
|
|
|
|
func (ds *DefaultSigner) Sign(msg []byte) (crypto.Signature, error) {
|
|
|
|
return ds.PrivKey.Sign(msg), nil
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
2015-08-10 20:38:45 -07:00
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2017-09-18 19:05:33 -07:00
|
|
|
type PrivValidatorsByAddress []*PrivValidatorFS
|
2015-08-10 20:38:45 -07:00
|
|
|
|
|
|
|
func (pvs PrivValidatorsByAddress) Len() int {
|
|
|
|
return len(pvs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
|
2017-09-18 19:05:33 -07:00
|
|
|
return bytes.Compare(pvs[i].Address(), pvs[j].Address()) == -1
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
|
|
|
|
it := pvs[i]
|
|
|
|
pvs[i] = pvs[j]
|
|
|
|
pvs[j] = it
|
|
|
|
}
|