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