@ -12,7 +12,6 @@ import (
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
// TODO: type ?
@ -35,30 +34,6 @@ func voteToStep(vote *Vote) int8 {
}
}
// PrivValidator implements the functionality for signing blocks.
type PrivValidator struct {
Address data . Bytes ` json:"address" `
PubKey crypto . PubKey ` json:"pub_key" `
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
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey crypto . PrivKey ` json:"priv_key" `
Signer ` json:"-" `
// For persistence.
// Overloaded for testing.
filePath string
mtx sync . Mutex
}
type SignerGenerator func ( pk crypto . PrivKey ) ( Signer )
// This is used to sign votes.
// It is the caller's duty to verify the msg before calling Sign,
// eg. to avoid double signing.
@ -90,38 +65,39 @@ func (ds *DefaultSigner) PubKey() crypto.PubKey {
return ds . priv . PubKey ( )
}
func ( privVal * PrivValidator ) SetSigner ( s Signer ) {
privVal . Signer = s
privVal . setPubKeyAndAddress ( )
}
// PrivValidator implements the functionality for signing blocks.
type PrivValidator struct {
Address data . Bytes ` json:"address" `
PubKey crypto . PubKey ` json:"pub_key" `
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
// Overwrite address and pubkey for convenience
func ( privVal * PrivValidator ) setPubKeyAndAddress ( ) {
privVal . PubKey = privVal . Signer . PubKey ( )
privVal . Address = privVal . PubKey . Address ( )
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey crypto . PrivKey ` json:"priv_key" `
Signer ` json:"-" `
// For persistence.
// Overloaded for testing.
filePath string
mtx sync . Mutex
}
// Generates a new validator with private key.
func GenPrivValidator ( ) * PrivValidator {
privKey := crypto . GenPrivKeyEd25519 ( ) . Wrap ( )
pubKey := privKey . PubKey ( )
return & PrivValidator {
Address : pubKey . Address ( ) ,
PubKey : pubKey ,
PrivKey : privKey ,
LastStep : stepNone ,
filePath : "" ,
Signer : NewDefaultSigner ( privKey ) ,
func LoadOrGenPrivValidator ( filePath string ) * PrivValidator {
var privValidator * PrivValidator
if _ , err := os . Stat ( filePath ) ; err == nil {
privValidator = LoadPrivValidator ( filePath )
} else {
privValidator = GenPrivValidator ( )
privValidator . SetFile ( filePath )
privValidator . Save ( )
}
return privValidator
}
func LoadPrivValidator ( filePath string ) * PrivValidator {
return LoadPrivValidatorWithSigner ( filePath , func ( pk crypto . PrivKey ) Signer {
return NewDefaultSigner ( pk )
} )
}
func LoadPrivValidatorWithSigner ( filePath string , generator SignerGenerator ) * PrivValidator {
privValJSONBytes , err := ioutil . ReadFile ( filePath )
if err != nil {
Exit ( err . Error ( ) )
@ -133,25 +109,44 @@ func LoadPrivValidatorWithSigner(filePath string, generator SignerGenerator) *Pr
}
privVal . filePath = filePath
privVal . Signer = generator ( privVal . PrivKey )
privVal . Signer = NewDefaultSigner ( privVal . PrivKey )
privVal . setPubKeyAndAddress ( )
return & privVal
}
func LoadOrGenPrivValidator ( filePath string , logger log . Logger ) * PrivValidator {
var privValidator * PrivValidator
if _ , err := os . Stat ( filePath ) ; err == nil {
privValidator = LoadPrivValidator ( filePath )
logger . Info ( "Loaded PrivValidator" ,
"file" , filePath , "privValidator" , privValidator )
} else {
privValidator = GenPrivValidator ( )
privValidator . SetFile ( filePath )
privValidator . Save ( )
logger . Info ( "Generated PrivValidator" , "file" , filePath )
// Generates a new validator with private key.
func GenPrivValidator ( ) * PrivValidator {
privKey := crypto . GenPrivKeyEd25519 ( ) . Wrap ( )
pubKey := privKey . PubKey ( )
return & PrivValidator {
Address : pubKey . Address ( ) ,
PubKey : pubKey ,
PrivKey : privKey ,
LastStep : stepNone ,
filePath : "" ,
Signer : NewDefaultSigner ( privKey ) ,
}
return privValidator
}
func LoadPrivValidatorWithSigner ( signer Signer ) * PrivValidator {
return & PrivValidator {
Address : signer . PubKey ( ) . Address ( ) ,
PubKey : signer . PubKey ( ) ,
LastStep : stepNone ,
filePath : "" ,
Signer : signer ,
}
}
func ( privVal * PrivValidator ) SetSigner ( s Signer ) {
privVal . Signer = s
privVal . setPubKeyAndAddress ( )
}
// Overwrite address and pubkey for convenience
func ( privVal * PrivValidator ) setPubKeyAndAddress ( ) {
privVal . PubKey = privVal . Signer . PubKey ( )
privVal . Address = privVal . PubKey . Address ( )
}
func ( privVal * PrivValidator ) SetFile ( filePath string ) {