types: privVal.Sign returns an error

This commit is contained in:
Ethan Buchman
2017-09-16 01:07:04 -04:00
parent 8963bf08aa
commit 90c0267bc1
4 changed files with 29 additions and 21 deletions

View File

@@ -275,30 +275,30 @@ func (privVal *ByzantinePrivValidator) GetAddress() []byte {
return privVal.Address
}
func (privVal *ByzantinePrivValidator) SignVote(chainID string, vote *types.Vote) error {
func (privVal *ByzantinePrivValidator) SignVote(chainID string, vote *types.Vote) (err error) {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
// Sign
vote.Signature = privVal.Sign(types.SignBytes(chainID, vote))
vote.Signature, err = privVal.Sign(types.SignBytes(chainID, vote))
return err
}
func (privVal *ByzantinePrivValidator) SignProposal(chainID string, proposal *types.Proposal) (err error) {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
// Sign
proposal.Signature, err = privVal.Sign(types.SignBytes(chainID, proposal))
return nil
}
func (privVal *ByzantinePrivValidator) SignProposal(chainID string, proposal *types.Proposal) error {
func (privVal *ByzantinePrivValidator) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) (err error) {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
// Sign
proposal.Signature = privVal.Sign(types.SignBytes(chainID, proposal))
return nil
}
func (privVal *ByzantinePrivValidator) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
// Sign
heartbeat.Signature = privVal.Sign(types.SignBytes(chainID, heartbeat))
heartbeat.Signature, err = privVal.Sign(types.SignBytes(chainID, heartbeat))
return nil
}

View File

@@ -59,7 +59,7 @@ type PrivValidator struct {
// Currently, the only callers are SignVote and SignProposal
type Signer interface {
PubKey() crypto.PubKey
Sign(msg []byte) crypto.Signature
Sign(msg []byte) (crypto.Signature, error)
}
// Implements Signer
@@ -72,8 +72,8 @@ func NewDefaultSigner(priv crypto.PrivKey) *DefaultSigner {
}
// Implements Signer
func (ds *DefaultSigner) Sign(msg []byte) crypto.Signature {
return ds.priv.Sign(msg)
func (ds *DefaultSigner) Sign(msg []byte) (crypto.Signature, error) {
return ds.priv.Sign(msg), nil
}
// Implements Signer
@@ -238,7 +238,10 @@ func (privVal *PrivValidator) signBytesHRS(height, round int, step int8, signByt
}
// Sign
sig = privVal.Sign(signBytes)
sig, err := privVal.Sign(signBytes)
if err != nil {
return sig, err
}
// Persist height/round/step
privVal.LastHeight = height
@@ -255,8 +258,9 @@ func (privVal *PrivValidator) signBytesHRS(height, round int, step int8, signByt
func (privVal *PrivValidator) SignHeartbeat(chainID string, heartbeat *Heartbeat) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
heartbeat.Signature = privVal.Sign(SignBytes(chainID, heartbeat))
return nil
var err error
heartbeat.Signature, err = privVal.Sign(SignBytes(chainID, heartbeat))
return err
}
func (privVal *PrivValidator) String() string {

View File

@@ -37,7 +37,7 @@ func BenchmarkProposalSign(b *testing.B) {
func BenchmarkProposalVerifySignature(b *testing.B) {
signBytes := SignBytes("test_chain_id", testProposal)
privVal := GenPrivValidator()
signature := privVal.Sign(signBytes)
signature, _ := privVal.Sign(signBytes)
pubKey := privVal.PubKey
for i := 0; i < b.N; i++ {

View File

@@ -60,7 +60,11 @@ func withBlockPartsHeader(vote *Vote, blockPartsHeader PartSetHeader) *Vote {
}
func signAddVote(privVal *PrivValidator, vote *Vote, voteSet *VoteSet) (bool, error) {
vote.Signature = privVal.Sign(SignBytes(voteSet.ChainID(), vote))
var err error
vote.Signature, err = privVal.Sign(SignBytes(voteSet.ChainID(), vote))
if err != nil {
return false, err
}
added, err := voteSet.AddVote(vote)
return added, err
}