|
|
@ -35,12 +35,15 @@ func voteToStep(vote *Vote) int8 { |
|
|
|
} |
|
|
|
|
|
|
|
type PrivValidator struct { |
|
|
|
Address []byte `json:"address"` |
|
|
|
PubKey crypto.PubKeyEd25519 `json:"pub_key"` |
|
|
|
PrivKey crypto.PrivKeyEd25519 `json:"priv_key"` |
|
|
|
LastHeight int `json:"last_height"` |
|
|
|
LastRound int `json:"last_round"` |
|
|
|
LastStep int8 `json:"last_step"` |
|
|
|
Address []byte `json:"address"` |
|
|
|
PubKey crypto.PubKeyEd25519 `json:"pub_key"` |
|
|
|
LastHeight int `json:"last_height"` |
|
|
|
LastRound int `json:"last_round"` |
|
|
|
LastStep int8 `json:"last_step"` |
|
|
|
|
|
|
|
// PrivKey should be empty if a Signer other than the default is being used.
|
|
|
|
PrivKey crypto.PrivKeyEd25519 `json:"priv_key"` |
|
|
|
signer Signer |
|
|
|
|
|
|
|
// For persistence.
|
|
|
|
// Overloaded for testing.
|
|
|
@ -48,6 +51,32 @@ type PrivValidator struct { |
|
|
|
mtx sync.Mutex |
|
|
|
} |
|
|
|
|
|
|
|
// XXX: This is used to sign votes.
|
|
|
|
// 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 { |
|
|
|
Sign(msg []byte) crypto.SignatureEd25519 |
|
|
|
} |
|
|
|
|
|
|
|
// Implements Signer
|
|
|
|
type DefaultSigner struct { |
|
|
|
priv crypto.PrivKeyEd25519 |
|
|
|
} |
|
|
|
|
|
|
|
func NewDefaultSigner(priv crypto.PrivKeyEd25519) *DefaultSigner { |
|
|
|
return &DefaultSigner{priv: priv} |
|
|
|
} |
|
|
|
|
|
|
|
// Implements Signer
|
|
|
|
func (ds *DefaultSigner) Sign(msg []byte) crypto.SignatureEd25519 { |
|
|
|
return ds.priv.Sign(msg).(crypto.SignatureEd25519) |
|
|
|
} |
|
|
|
|
|
|
|
func (privVal *PrivValidator) SetSigner(s Signer) { |
|
|
|
privVal.signer = s |
|
|
|
} |
|
|
|
|
|
|
|
// Generates a new validator with private key.
|
|
|
|
func GenPrivValidator() *PrivValidator { |
|
|
|
privKeyBytes := new([64]byte) |
|
|
@ -63,6 +92,7 @@ func GenPrivValidator() *PrivValidator { |
|
|
|
LastRound: 0, |
|
|
|
LastStep: stepNone, |
|
|
|
filePath: "", |
|
|
|
signer: NewDefaultSigner(privKey), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -76,6 +106,7 @@ func LoadPrivValidator(filePath string) *PrivValidator { |
|
|
|
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) |
|
|
|
} |
|
|
|
privVal.filePath = filePath |
|
|
|
privVal.signer = NewDefaultSigner(privVal.PrivKey) |
|
|
|
return privVal |
|
|
|
} |
|
|
|
|
|
|
@ -145,14 +176,10 @@ func (privVal *PrivValidator) SignVote(chainID string, vote *Vote) error { |
|
|
|
privVal.save() |
|
|
|
|
|
|
|
// Sign
|
|
|
|
privVal.SignVoteUnsafe(chainID, vote) |
|
|
|
vote.Signature = privVal.signer.Sign(SignBytes(chainID, vote)) |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func (privVal *PrivValidator) SignVoteUnsafe(chainID string, vote *Vote) { |
|
|
|
vote.Signature = privVal.PrivKey.Sign(SignBytes(chainID, vote)).(crypto.SignatureEd25519) |
|
|
|
} |
|
|
|
|
|
|
|
func (privVal *PrivValidator) SignProposal(chainID string, proposal *Proposal) error { |
|
|
|
privVal.mtx.Lock() |
|
|
|
defer privVal.mtx.Unlock() |
|
|
@ -167,7 +194,7 @@ func (privVal *PrivValidator) SignProposal(chainID string, proposal *Proposal) e |
|
|
|
privVal.save() |
|
|
|
|
|
|
|
// Sign
|
|
|
|
proposal.Signature = privVal.PrivKey.Sign(SignBytes(chainID, proposal)).(crypto.SignatureEd25519) |
|
|
|
proposal.Signature = privVal.signer.Sign(SignBytes(chainID, proposal)) |
|
|
|
return nil |
|
|
|
} else { |
|
|
|
return errors.New(fmt.Sprintf("Attempt of duplicate signing of proposal: Height %v, Round %v", proposal.Height, proposal.Round)) |
|
|
|