|
|
@ -67,6 +67,32 @@ type PrivValidatorFS struct { |
|
|
|
mtx sync.Mutex |
|
|
|
} |
|
|
|
|
|
|
|
// 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 |
|
|
|
} |
|
|
|
|
|
|
|
// GetAddress returns the address of the validator.
|
|
|
|
// Implements PrivValidator.
|
|
|
|
func (pv *PrivValidatorFS) GetAddress() data.Bytes { |
|
|
@ -79,40 +105,68 @@ func (pv *PrivValidatorFS) GetPubKey() crypto.PubKey { |
|
|
|
return pv.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.signBytesHRS(vote.Height, vote.Round, voteToStep(vote), SignBytes(chainID, vote)) |
|
|
|
if err != nil { |
|
|
|
return errors.New(cmn.Fmt("Error signing vote: %v", err)) |
|
|
|
// 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{ |
|
|
|
Address: privKey.PubKey().Address(), |
|
|
|
PubKey: privKey.PubKey(), |
|
|
|
PrivKey: privKey, |
|
|
|
LastStep: stepNone, |
|
|
|
Signer: NewDefaultSigner(privKey), |
|
|
|
filePath: filePath, |
|
|
|
} |
|
|
|
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.signBytesHRS(proposal.Height, proposal.Round, stepPropose, SignBytes(chainID, proposal)) |
|
|
|
// LoadPrivValidatorFS loads a PrivValidatorFS from the filePath.
|
|
|
|
func LoadPrivValidatorFS(filePath string) *PrivValidatorFS { |
|
|
|
privValJSONBytes, err := ioutil.ReadFile(filePath) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Error signing proposal: %v", err) |
|
|
|
cmn.Exit(err.Error()) |
|
|
|
} |
|
|
|
proposal.Signature = signature |
|
|
|
return nil |
|
|
|
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 = NewDefaultSigner(privVal.PrivKey) |
|
|
|
return &privVal |
|
|
|
} |
|
|
|
|
|
|
|
// 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 |
|
|
|
// 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 |
|
|
|
} |
|
|
|
|
|
|
|
// 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(crypto.PubKey) 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.PubKey) |
|
|
|
return &privVal |
|
|
|
} |
|
|
|
|
|
|
|
// Save persists the PrivValidatorFS to disk.
|
|
|
@ -138,6 +192,43 @@ func (privVal *PrivValidatorFS) save() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Reset resets all fields in the PrivValidatorFS.
|
|
|
|
// NOTE: Unsafe!
|
|
|
|
func (privVal *PrivValidatorFS) Reset() { |
|
|
|
privVal.LastHeight = 0 |
|
|
|
privVal.LastRound = 0 |
|
|
|
privVal.LastStep = 0 |
|
|
|
privVal.LastSignature = crypto.Signature{} |
|
|
|
privVal.LastSignBytes = nil |
|
|
|
privVal.Save() |
|
|
|
} |
|
|
|
|
|
|
|
// 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.signBytesHRS(vote.Height, vote.Round, voteToStep(vote), SignBytes(chainID, vote)) |
|
|
|
if err != nil { |
|
|
|
return errors.New(cmn.Fmt("Error signing vote: %v", err)) |
|
|
|
} |
|
|
|
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.signBytesHRS(proposal.Height, proposal.Round, stepPropose, SignBytes(chainID, proposal)) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Error signing proposal: %v", err) |
|
|
|
} |
|
|
|
proposal.Signature = signature |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// signBytesHRS signs the given signBytes if the height/round/step (HRS)
|
|
|
|
// are greater than the latest state. If the HRS are equal,
|
|
|
|
// it returns the privValidator.LastSignature.
|
|
|
@ -193,15 +284,14 @@ func (privVal *PrivValidatorFS) signBytesHRS(height, round int, step int8, signB |
|
|
|
return sig, nil |
|
|
|
} |
|
|
|
|
|
|
|
// Reset resets all fields in the PrivValidatorFS.
|
|
|
|
// NOTE: Unsafe!
|
|
|
|
func (privVal *PrivValidatorFS) Reset() { |
|
|
|
privVal.LastHeight = 0 |
|
|
|
privVal.LastRound = 0 |
|
|
|
privVal.LastStep = 0 |
|
|
|
privVal.LastSignature = crypto.Signature{} |
|
|
|
privVal.LastSignBytes = nil |
|
|
|
privVal.Save() |
|
|
|
// 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 |
|
|
|
} |
|
|
|
|
|
|
|
// String returns a string representation of the PrivValidatorFS.
|
|
|
@ -209,100 +299,6 @@ func (privVal *PrivValidatorFS) String() string { |
|
|
|
return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", privVal.GetAddress(), privVal.LastHeight, privVal.LastRound, privVal.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 |
|
|
|
privVal.Signer = NewDefaultSigner(privVal.PrivKey) |
|
|
|
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{ |
|
|
|
Address: privKey.PubKey().Address(), |
|
|
|
PubKey: privKey.PubKey(), |
|
|
|
PrivKey: privKey, |
|
|
|
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(crypto.PubKey) 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.PubKey) |
|
|
|
return &privVal |
|
|
|
} |
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
// 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 |
|
|
|
} |
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type PrivValidatorsByAddress []*PrivValidatorFS |
|
|
|