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-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
|
|
|
|
2015-10-22 17:39:06 -07:00
|
|
|
. "github.com/tendermint/go-common"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2015-10-22 17:39:06 -07:00
|
|
|
"github.com/tendermint/go-wire"
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-10-22 17:39:06 -07:00
|
|
|
"github.com/tendermint/ed25519"
|
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 {
|
2016-01-12 15:58:48 -08:00
|
|
|
Address []byte `json:"address"`
|
|
|
|
PubKey crypto.PubKey `json:"pub_key"`
|
|
|
|
LastHeight int `json:"last_height"`
|
|
|
|
LastRound int `json:"last_round"`
|
|
|
|
LastStep int8 `json:"last_step"`
|
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 {
|
2015-01-18 06:34:01 -08:00
|
|
|
privKeyBytes := new([64]byte)
|
2016-03-13 10:01:32 -07:00
|
|
|
copy(privKeyBytes[:32], crypto.CRandBytes(32))
|
2015-01-18 06:34:01 -08:00
|
|
|
pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
|
2015-11-01 11:34:08 -08:00
|
|
|
pubKey := crypto.PubKeyEd25519(*pubKeyBytes)
|
|
|
|
privKey := crypto.PrivKeyEd25519(*privKeyBytes)
|
2014-12-09 18:49:04 -08:00
|
|
|
return &PrivValidator{
|
|
|
|
Address: pubKey.Address(),
|
|
|
|
PubKey: pubKey,
|
|
|
|
PrivKey: privKey,
|
|
|
|
LastHeight: 0,
|
|
|
|
LastRound: 0,
|
|
|
|
LastStep: stepNone,
|
2015-05-15 18:05:09 -07:00
|
|
|
filePath: "",
|
2016-01-12 15:58:48 -08:00
|
|
|
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
|
|
|
}
|
2015-07-25 15:45:45 -07:00
|
|
|
privVal := wire.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
|
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)
|
2014-12-09 18:49:04 -08:00
|
|
|
return privVal
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-03-19 16:58:15 -07:00
|
|
|
jsonBytes := wire.JSONBytesPretty(privVal)
|
2015-12-04 00:03:09 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2014-12-17 01:37:13 -08:00
|
|
|
// If height regression, panic
|
|
|
|
if privVal.LastHeight > vote.Height {
|
2014-12-31 16:14:26 -08:00
|
|
|
return errors.New("Height regression in SignVote")
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
|
|
|
// More cases for when the height matches
|
|
|
|
if privVal.LastHeight == vote.Height {
|
|
|
|
// If round regression, panic
|
|
|
|
if privVal.LastRound > vote.Round {
|
2014-12-31 16:14:26 -08:00
|
|
|
return errors.New("Round regression in SignVote")
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
|
|
|
// If step regression, panic
|
|
|
|
if privVal.LastRound == vote.Round && privVal.LastStep > voteToStep(vote) {
|
2014-12-31 16:14:26 -08:00
|
|
|
return errors.New("Step regression in SignVote")
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2014-12-17 01:37:13 -08:00
|
|
|
|
|
|
|
// Persist height/round/step
|
|
|
|
privVal.LastHeight = vote.Height
|
|
|
|
privVal.LastRound = vote.Round
|
|
|
|
privVal.LastStep = voteToStep(vote)
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.save()
|
2014-12-17 01:37:13 -08:00
|
|
|
|
|
|
|
// Sign
|
2016-01-12 15:58:48 -08:00
|
|
|
vote.Signature = privVal.Sign(SignBytes(chainID, vote)).(crypto.SignatureEd25519)
|
2014-12-31 16:14:26 -08:00
|
|
|
return nil
|
2014-12-17 01:37:13 -08:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:53:57 -07:00
|
|
|
func (privVal *PrivValidator) SignProposal(chainID string, proposal *Proposal) error {
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.mtx.Lock()
|
|
|
|
defer privVal.mtx.Unlock()
|
2014-12-09 18:49:04 -08:00
|
|
|
if privVal.LastHeight < proposal.Height ||
|
|
|
|
privVal.LastHeight == proposal.Height && privVal.LastRound < proposal.Round ||
|
|
|
|
privVal.LastHeight == 0 && privVal.LastRound == 0 && privVal.LastStep == stepNone {
|
|
|
|
|
|
|
|
// Persist height/round/step
|
|
|
|
privVal.LastHeight = proposal.Height
|
|
|
|
privVal.LastRound = proposal.Round
|
|
|
|
privVal.LastStep = stepPropose
|
2014-12-22 18:49:37 -08:00
|
|
|
privVal.save()
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
// Sign
|
2016-01-12 15:58:48 -08:00
|
|
|
proposal.Signature = privVal.Sign(SignBytes(chainID, proposal)).(crypto.SignatureEd25519)
|
2014-12-31 16:14:26 -08:00
|
|
|
return nil
|
2014-12-09 18:49:04 -08:00
|
|
|
} else {
|
2014-12-31 16:14:26 -08:00
|
|
|
return errors.New(fmt.Sprintf("Attempt of duplicate signing of proposal: Height %v, Round %v", proposal.Height, proposal.Round))
|
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
|
|
|
|
}
|