tendermint/state/priv_validator.go

186 lines
5.1 KiB
Go
Raw Normal View History

2014-12-17 01:37:13 -08:00
package state
import (
"errors"
"fmt"
"io/ioutil"
"math"
"sync"
2015-07-19 09:40:55 -07:00
acm "github.com/tendermint/tendermint/account"
2015-07-25 15:45:45 -07:00
"github.com/tendermint/tendermint/wire"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/consensus/types"
"github.com/tendermint/tendermint/types"
2015-06-09 20:17:19 -07:00
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
)
const (
stepNone = 0 // Used to distinguish the initial state
stepPropose = 1
stepPrevote = 2
stepPrecommit = 3
)
func voteToStep(vote *types.Vote) int8 {
switch vote.Type {
case types.VoteTypePrevote:
return stepPrevote
case types.VoteTypePrecommit:
return stepPrecommit
default:
2015-07-19 16:42:52 -07:00
PanicSanity("Unknown vote type")
return 0
}
}
type PrivValidator struct {
2015-07-19 09:40:55 -07:00
Address []byte `json:"address"`
PubKey acm.PubKeyEd25519 `json:"pub_key"`
PrivKey acm.PrivKeyEd25519 `json:"priv_key"`
LastHeight int `json:"last_height"`
LastRound int `json:"last_round"`
LastStep int8 `json:"last_step"`
2014-12-17 01:37:13 -08:00
// For persistence.
// Overloaded for testing.
filePath string
mtx sync.Mutex
}
// Generates a new validator with private key.
func GenPrivValidator() *PrivValidator {
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32))
pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
2015-07-19 09:40:55 -07:00
pubKey := acm.PubKeyEd25519(*pubKeyBytes)
privKey := acm.PrivKeyEd25519(*privKeyBytes)
return &PrivValidator{
Address: pubKey.Address(),
PubKey: pubKey,
PrivKey: privKey,
LastHeight: 0,
LastRound: 0,
LastStep: stepNone,
filePath: "",
}
}
func LoadPrivValidator(filePath string) *PrivValidator {
privValJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil {
2015-06-16 21:16:58 -07:00
Exit(err.Error())
}
2015-07-25 15:45:45 -07:00
privVal := wire.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
if err != nil {
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
privVal.filePath = filePath
return privVal
}
func (privVal *PrivValidator) SetFile(filePath string) {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
privVal.filePath = filePath
}
func (privVal *PrivValidator) Save() {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
privVal.save()
}
func (privVal *PrivValidator) save() {
if privVal.filePath == "" {
2015-07-19 16:42:52 -07:00
PanicSanity("Cannot save PrivValidator: filePath not set")
}
2015-07-25 15:45:45 -07:00
jsonBytes := wire.JSONBytes(privVal)
err := WriteFileAtomic(privVal.filePath, jsonBytes)
if err != nil {
// `@; BOOM!!!
2015-07-19 16:42:52 -07:00
PanicCrisis(err)
}
}
2015-05-29 14:53:57 -07:00
func (privVal *PrivValidator) SignVote(chainID string, vote *types.Vote) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
2014-12-17 01:37:13 -08:00
// If height regression, panic
if privVal.LastHeight > vote.Height {
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 {
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) {
return errors.New("Step regression in SignVote")
2014-12-17 01:37:13 -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)
privVal.save()
2014-12-17 01:37:13 -08:00
// Sign
2015-05-29 14:53:57 -07:00
privVal.SignVoteUnsafe(chainID, vote)
return nil
2014-12-17 01:37:13 -08:00
}
2015-05-29 14:53:57 -07:00
func (privVal *PrivValidator) SignVoteUnsafe(chainID string, vote *types.Vote) {
2015-07-19 09:40:55 -07:00
vote.Signature = privVal.PrivKey.Sign(acm.SignBytes(chainID, vote)).(acm.SignatureEd25519)
}
2015-05-29 14:53:57 -07:00
func (privVal *PrivValidator) SignProposal(chainID string, proposal *Proposal) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
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
privVal.save()
// Sign
2015-07-19 09:40:55 -07:00
proposal.Signature = privVal.PrivKey.Sign(acm.SignBytes(chainID, proposal)).(acm.SignatureEd25519)
return nil
} else {
return errors.New(fmt.Sprintf("Attempt of duplicate signing of proposal: Height %v, Round %v", proposal.Height, proposal.Round))
}
}
2015-05-29 14:53:57 -07:00
func (privVal *PrivValidator) SignRebondTx(chainID string, rebondTx *types.RebondTx) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
if privVal.LastHeight < rebondTx.Height {
// Persist height/round/step
2015-07-01 22:02:31 -07:00
// Prevent doing anything else for this rebondTx.Height.
privVal.LastHeight = rebondTx.Height
2015-07-01 22:02:31 -07:00
privVal.LastRound = math.MaxInt32 // MaxInt64 overflows on 32bit architectures.
privVal.LastStep = math.MaxInt8
privVal.save()
// Sign
2015-07-19 09:40:55 -07:00
rebondTx.Signature = privVal.PrivKey.Sign(acm.SignBytes(chainID, rebondTx)).(acm.SignatureEd25519)
return nil
} else {
return errors.New(fmt.Sprintf("Attempt of duplicate signing of rebondTx: Height %v", rebondTx.Height))
2014-10-07 23:11:04 -07: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)
}