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-04-21 15:12:54 -07:00
|
|
|
. "github.com/tendermint/tmlibs/common"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2017-04-21 15:13:25 -07:00
|
|
|
data "github.com/tendermint/go-wire/data"
|
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
|
|
|
|
|
|
|
type PrivValidator struct {
|
2017-03-22 10:16:22 -07:00
|
|
|
Address data.Bytes `json:"address"`
|
2016-08-09 14:18:29 -07:00
|
|
|
PubKey crypto.PubKey `json:"pub_key"`
|
|
|
|
LastHeight int `json:"last_height"`
|
|
|
|
LastRound int `json:"last_round"`
|
|
|
|
LastStep int8 `json:"last_step"`
|
2017-03-22 10:16:22 -07:00
|
|
|
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
|
|
|
|
LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
|
2015-11-04 16:09:43 -08:00
|
|
|
|
|
|
|
// PrivKey should be empty if a Signer other than the default is being used.
|
2016-01-12 15:58:48 -08:00
|
|
|
PrivKey crypto.PrivKey `json:"priv_key"`
|
2016-01-14 08:06:02 -08:00
|
|
|
Signer `json:"-"`
|
2014-12-17 01:37:13 -08:00
|
|
|
|
|
|
|
// For persistence.
|
|
|
|
// Overloaded for testing.
|
2015-05-15 18:05:09 -07:00
|
|
|
filePath string
|
2014-12-22 18:49:37 -08:00
|
|
|
mtx sync.Mutex
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:58:48 -08:00
|
|
|
// This is used to sign votes.
|
2015-11-04 16:09:43 -08:00
|
|
|
// It is the caller's duty to verify the msg before calling Sign,
|
|
|
|
// eg. to avoid double signing.
|
|
|
|
// Currently, the only callers are SignVote and SignProposal
|
|
|
|
type Signer interface {
|
2016-01-12 15:58:48 -08:00
|
|
|
Sign(msg []byte) crypto.Signature
|
2015-11-04 16:09:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Signer
|
|
|
|
type DefaultSigner struct {
|
2016-01-12 15:58:48 -08:00
|
|
|
priv crypto.PrivKey
|
2015-11-04 16:09:43 -08:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:58:48 -08:00
|
|
|
func NewDefaultSigner(priv crypto.PrivKey) *DefaultSigner {
|
2015-11-04 16:09:43 -08:00
|
|
|
return &DefaultSigner{priv: priv}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Signer
|
2016-01-12 15:58:48 -08:00
|
|
|
func (ds *DefaultSigner) Sign(msg []byte) crypto.Signature {
|
|
|
|
return ds.priv.Sign(msg)
|
2015-11-04 16:09:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (privVal *PrivValidator) SetSigner(s Signer) {
|
2016-01-12 15:58:48 -08:00
|
|
|
privVal.Signer = s
|
2015-11-04 16:09:43 -08:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
// Generates a new validator with private key.
|
|
|
|
func GenPrivValidator() *PrivValidator {
|
2017-04-10 07:15:41 -07:00
|
|
|
privKey := crypto.GenPrivKeyEd25519().Wrap()
|
2017-03-21 14:12:02 -07:00
|
|
|
pubKey := privKey.PubKey()
|
2014-12-09 18:49:04 -08:00
|
|
|
return &PrivValidator{
|
2017-03-21 14:12:02 -07:00
|
|
|
Address: pubKey.Address(),
|
|
|
|
PubKey: pubKey,
|
|
|
|
PrivKey: privKey,
|
|
|
|
LastStep: stepNone,
|
|
|
|
filePath: "",
|
|
|
|
Signer: NewDefaultSigner(privKey),
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2014-10-20 19:02:10 -07:00
|
|
|
}
|
|
|
|
|
2015-05-15 18:05:09 -07:00
|
|
|
func LoadPrivValidator(filePath string) *PrivValidator {
|
|
|
|
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-03-21 14:46:15 -07:00
|
|
|
privVal := PrivValidator{}
|
|
|
|
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
|
|
|
}
|
2015-05-15 18:05:09 -07:00
|
|
|
privVal.filePath = filePath
|
2016-01-12 15:58:48 -08:00
|
|
|
privVal.Signer = NewDefaultSigner(privVal.PrivKey)
|
2017-03-21 14:46:15 -07:00
|
|
|
return &privVal
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2015-10-18 07:20:05 -07:00
|
|
|
func LoadOrGenPrivValidator(filePath string) *PrivValidator {
|
|
|
|
var privValidator *PrivValidator
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
privValidator = LoadPrivValidator(filePath)
|
|
|
|
log.Notice("Loaded PrivValidator",
|
|
|
|
"file", filePath, "privValidator", privValidator)
|
|
|
|
} else {
|
|
|
|
privValidator = GenPrivValidator()
|
|
|
|
privValidator.SetFile(filePath)
|
|
|
|
privValidator.Save()
|
|
|
|
log.Notice("Generated PrivValidator", "file", filePath)
|
|
|
|
}
|
|
|
|
return privValidator
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:05:09 -07:00
|
|
|
func (privVal *PrivValidator) SetFile(filePath string) {
|
2015-04-03 16:15:52 -07:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
2015-05-15 18:05:09 -07:00
|
|
|
privVal.filePath = filePath
|
2015-04-03 16:15:52 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (privVal *PrivValidator) Save() {
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
|
|
|
privVal.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (privVal *PrivValidator) 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 09:31:24 -07:00
|
|
|
// NOTE: Unsafe!
|
|
|
|
func (privVal *PrivValidator) Reset() {
|
|
|
|
privVal.LastHeight = 0
|
|
|
|
privVal.LastRound = 0
|
|
|
|
privVal.LastStep = 0
|
2017-03-21 14:12:02 -07:00
|
|
|
privVal.LastSignature = crypto.Signature{}
|
2016-08-14 09:31:24 -07:00
|
|
|
privVal.LastSignBytes = nil
|
|
|
|
privVal.Save()
|
|
|
|
}
|
|
|
|
|
2016-06-26 12:33:11 -07:00
|
|
|
func (privVal *PrivValidator) GetAddress() []byte {
|
|
|
|
return privVal.Address
|
|
|
|
}
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func (privVal *PrivValidator) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (privVal *PrivValidator) SignProposal(chainID string, proposal *Proposal) error {
|
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
|
|
|
signature, err := privVal.signBytesHRS(proposal.Height, proposal.Round, stepPropose, SignBytes(chainID, proposal))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(Fmt("Error signing proposal: %v", err))
|
|
|
|
}
|
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
|
|
|
|
2016-08-09 14:18:29 -07:00
|
|
|
// check if there's a regression. Else sign and write the hrs+signature to disk
|
|
|
|
func (privVal *PrivValidator) signBytesHRS(height, round int, step int8, signBytes []byte) (crypto.Signature, error) {
|
2017-03-21 14:12:02 -07:00
|
|
|
sig := crypto.Signature{}
|
2016-08-09 14:18:29 -07:00
|
|
|
// If height regression, err
|
|
|
|
if privVal.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
|
2016-08-09 14:18:29 -07:00
|
|
|
if privVal.LastHeight == height {
|
|
|
|
// If round regression, err
|
|
|
|
if privVal.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
|
|
|
|
if privVal.LastRound == round {
|
|
|
|
if privVal.LastStep > step {
|
2017-03-21 14:12:02 -07:00
|
|
|
return sig, errors.New("Step regression")
|
2016-08-09 14:18:29 -07:00
|
|
|
} else if privVal.LastStep == step {
|
2016-08-14 09:31:24 -07:00
|
|
|
if privVal.LastSignBytes != nil {
|
2017-03-21 14:12:02 -07:00
|
|
|
if privVal.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
|
|
|
|
if bytes.Equal(privVal.LastSignBytes, signBytes) {
|
|
|
|
log.Notice("Using privVal.LastSignature", "sig", privVal.LastSignature)
|
|
|
|
return privVal.LastSignature, nil
|
|
|
|
}
|
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-03-21 14:12:02 -07:00
|
|
|
sig = privVal.Sign(signBytes)
|
2016-08-09 14:18:29 -07:00
|
|
|
|
2014-12-17 01:37:13 -08:00
|
|
|
// Persist height/round/step
|
2016-08-09 14:18:29 -07:00
|
|
|
privVal.LastHeight = height
|
|
|
|
privVal.LastRound = round
|
|
|
|
privVal.LastStep = step
|
2017-03-21 14:12:02 -07:00
|
|
|
privVal.LastSignature = sig
|
2016-08-14 09:31:24 -07:00
|
|
|
privVal.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
|
2016-06-26 12:33:11 -07:00
|
|
|
|
2014-12-16 05:40:17 -08:00
|
|
|
}
|
|
|
|
|
2014-12-17 01:37:13 -08:00
|
|
|
func (privVal *PrivValidator) String() string {
|
|
|
|
return fmt.Sprintf("PrivValidator{%X LH:%v, LR:%v, LS:%v}", privVal.Address, privVal.LastHeight, privVal.LastRound, privVal.LastStep)
|
|
|
|
}
|
2015-08-10 20:38:45 -07:00
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type PrivValidatorsByAddress []*PrivValidator
|
|
|
|
|
|
|
|
func (pvs PrivValidatorsByAddress) Len() int {
|
|
|
|
return len(pvs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
|
|
|
|
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
|
|
|
|
}
|