github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/consensus/tendermint/priv_validator_memory.go (about)

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