tendermint/types/priv_validator.go

364 lines
10 KiB
Go
Raw Normal View History

package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"sync"
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-09-20 15:40:41 -07:00
cmn "github.com/tendermint/tmlibs/common"
)
2017-09-11 12:45:12 -07:00
// TODO: type ?
const (
stepNone = 0 // Used to distinguish the initial state
stepPropose = 1
stepPrevote = 2
stepPrecommit = 3
)
func voteToStep(vote *Vote) int8 {
switch vote.Type {
case VoteTypePrevote:
return stepPrevote
case VoteTypePrecommit:
return stepPrecommit
default:
2017-09-20 15:40:41 -07:00
cmn.PanicSanity("Unknown vote type")
2015-07-19 16:42:52 -07:00
return 0
}
}
// 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 {
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
}
// Address returns the address of the validator.
// Implements PrivValidator.
func (pv *PrivValidatorFS) Address() data.Bytes {
return pv.ID.Address
}
// PubKey returns the public key of the validator.
// Implements PrivValidator.
func (pv *PrivValidatorFS) PubKey() crypto.PubKey {
return pv.ID.PubKey
}
// SignVote signs a canonical representation of the vote, along with the chainID.
// Implements PrivValidator.
func (privVal *PrivValidatorFS) SignVote(chainID string, vote *Vote) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
signature, err := privVal.Info.SignBytesHRS(privVal.Signer,
vote.Height, vote.Round, voteToStep(vote), SignBytes(chainID, vote))
if err != nil {
return errors.New(cmn.Fmt("Error signing vote: %v", err))
}
privVal.save()
vote.Signature = signature
return nil
}
// SignProposal signs a canonical representation of the proposal, along with the chainID.
// Implements PrivValidator.
func (privVal *PrivValidatorFS) SignProposal(chainID string, proposal *Proposal) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
signature, err := privVal.Info.SignBytesHRS(privVal.Signer,
proposal.Height, proposal.Round, stepPropose, SignBytes(chainID, proposal))
2017-09-18 19:05:33 -07:00
if err != nil {
return fmt.Errorf("Error signing proposal: %v", err)
2017-09-18 19:05:33 -07:00
}
privVal.save()
proposal.Signature = signature
return nil
2017-09-18 19:05:33 -07:00
}
// SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.
// Implements PrivValidator.
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
}
2017-09-18 19:05:33 -07:00
// Save persists the PrivValidatorFS to disk.
func (privVal *PrivValidatorFS) Save() {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
privVal.save()
}
2017-09-18 19:05:33 -07:00
func (privVal *PrivValidatorFS) save() {
if privVal.filePath == "" {
2017-09-20 15:40:41 -07:00
cmn.PanicSanity("Cannot save PrivValidator: filePath not set")
}
jsonBytes, err := json.Marshal(privVal)
if err != nil {
// `@; BOOM!!!
2017-09-20 15:40:41 -07:00
cmn.PanicCrisis(err)
}
2017-09-20 15:40:41 -07:00
err = cmn.WriteFileAtomic(privVal.filePath, jsonBytes, 0600)
if err != nil {
// `@; BOOM!!!
2017-09-20 15:40:41 -07:00
cmn.PanicCrisis(err)
}
}
2017-09-18 20:16:14 -07:00
// UnmarshalJSON unmarshals the given jsonString
// into a PrivValidatorFS using a DefaultSigner.
func (pv *PrivValidatorFS) UnmarshalJSON(jsonString []byte) error {
idAndInfo := &struct {
ID ValidatorID `json:"id"`
Info LastSignedInfo `json:"info"`
}{}
if err := json.Unmarshal(jsonString, idAndInfo); err != nil {
return err
}
signer := &struct {
Signer *DefaultSigner `json:"signer"`
}{}
if err := json.Unmarshal(jsonString, signer); err != nil {
return err
}
pv.ID = idAndInfo.ID
pv.Info = idAndInfo.Info
pv.Signer = signer.Signer
return nil
}
2017-09-18 19:05:33 -07:00
// Reset resets all fields in the PrivValidatorFS.Info.
// 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
privVal.Save()
}
// 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)
}
// 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
if _, err := os.Stat(filePath); err == nil {
PrivValidatorFS = LoadPrivValidatorFS(filePath)
} else {
PrivValidatorFS = GenPrivValidatorFS(filePath)
PrivValidatorFS.Save()
}
return PrivValidatorFS
}
// LoadPrivValidatorFS loads a PrivValidatorFS from the filePath.
func LoadPrivValidatorFS(filePath string) *PrivValidatorFS {
privValJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil {
cmn.Exit(err.Error())
}
privVal := PrivValidatorFS{}
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil {
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
privVal.filePath = filePath
return &privVal
}
// GenPrivValidatorFS generates a new validator with randomly generated private key
// and sets the filePath, but does not call Save().
func GenPrivValidatorFS(filePath string) *PrivValidatorFS {
privKey := crypto.GenPrivKeyEd25519().Wrap()
return &PrivValidatorFS{
ID: ValidatorID{privKey.PubKey().Address(), privKey.PubKey()},
Info: LastSignedInfo{
LastStep: stepNone,
},
Signer: NewDefaultSigner(privKey),
filePath: filePath,
}
}
// 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 {
cmn.Exit(err.Error())
}
privVal := PrivValidatorFS{}
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil {
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
privVal.filePath = filePath
privVal.Signer = signerFunc(privVal.ID)
return &privVal
}
//-------------------------------------
// ValidatorID contains the identity of the validator.
type ValidatorID struct {
Address data.Bytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
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
}
// SignBytesHRS signs the given signBytes with the signer if the height/round/step (HRS)
// are greater than the latest state of the LastSignedInfo. If the HRS are equal,
// it returns the LastSignedInfo.LastSignature.
func (info *LastSignedInfo) SignBytesHRS(signer Signer,
height, round int, step int8, signBytes []byte) (crypto.Signature, error) {
2017-03-21 14:12:02 -07:00
sig := crypto.Signature{}
// 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 {
// 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
}
// 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() {
2017-09-20 15:40:41 -07:00
cmn.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
}
}
2017-03-21 14:12:02 -07:00
return sig, errors.New("Step regression")
}
2014-12-17 01:37:13 -08:00
}
}
2014-12-17 01:37:13 -08:00
// Sign
sig, err := signer.Sign(signBytes)
2017-09-15 22:07:04 -07:00
if err != nil {
return sig, err
}
2014-12-17 01:37:13 -08:00
// Persist height/round/step
info.LastHeight = height
info.LastRound = round
info.LastStep = step
info.LastSignature = sig
info.LastSignBytes = signBytes
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
//-------------------------------------
// 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
}
//-------------------------------------
2017-09-18 19:05:33 -07:00
type PrivValidatorsByAddress []*PrivValidatorFS
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
}
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
it := pvs[i]
pvs[i] = pvs[j]
pvs[j] = it
}