This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
priv_validator_memory.go
53 lines (44 loc) · 1.66 KB
/
priv_validator_memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package tendermint
import (
"github.com/hyperledger/burrow/crypto"
tmCrypto "github.com/tendermint/tendermint/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
type privValidatorMemory struct {
crypto.Addressable
signer func(msg []byte) []byte
lastSignedInfo *LastSignedInfo
}
var _ types.PrivValidator = &privValidatorMemory{}
// Create a PrivValidator with in-memory state that takes an addressable representing the validator identity
// and a signer providing private signing for that identity.
func NewPrivValidatorMemory(addressable crypto.Addressable, signer crypto.Signer) *privValidatorMemory {
return &privValidatorMemory{
Addressable: addressable,
signer: asTendermintSigner(signer),
lastSignedInfo: NewLastSignedInfo(),
}
}
func asTendermintSigner(signer crypto.Signer) func(msg []byte) []byte {
return func(msg []byte) []byte {
sig, err := signer.Sign(msg)
if err != nil {
return nil
}
return sig.TendermintSignature()
}
}
func (pvm *privValidatorMemory) GetAddress() types.Address {
return pvm.Addressable.GetAddress().Bytes()
}
func (pvm *privValidatorMemory) GetPubKey() (tmCrypto.PubKey, error) {
return pvm.GetPublicKey().TendermintPubKey(), nil
}
// TODO: consider persistence to disk/database to avoid double signing after a crash
func (pvm *privValidatorMemory) SignVote(chainID string, vote *tmproto.Vote) error {
return pvm.lastSignedInfo.SignVote(pvm.signer, chainID, vote)
}
func (pvm *privValidatorMemory) SignProposal(chainID string, proposal *tmproto.Proposal) error {
return pvm.lastSignedInfo.SignProposal(pvm.signer, chainID, proposal)
}