github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/types/priv_validator.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/tendermint/tendermint/crypto"
     9  	"github.com/tendermint/tendermint/crypto/ed25519"
    10  )
    11  
    12  // PrivValidator defines the functionality of a local Tendermint validator
    13  // that signs votes and proposals, and never double signs.
    14  type PrivValidator interface {
    15  	GetPubKey() (crypto.PubKey, error)
    16  
    17  	SignVote(chainID string, vote *Vote) error
    18  	SignProposal(chainID string, proposal *Proposal) error
    19  }
    20  
    21  type PrivValidators interface {
    22  	GetPubKeys() ([]crypto.PubKey, error)
    23  
    24  	SignVote(chainID string, vote *Vote, publicKey crypto.PubKey) error
    25  	SignProposal(chainID string, proposal *Proposal, publicKey crypto.PubKey) error
    26  }
    27  
    28  //----------------------------------------
    29  // Misc.
    30  
    31  type PrivValidatorsByAddress []PrivValidator
    32  
    33  func (pvs PrivValidatorsByAddress) Len() int {
    34  	return len(pvs)
    35  }
    36  
    37  func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
    38  	pvi, err := pvs[i].GetPubKey()
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	pvj, err := pvs[j].GetPubKey()
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  
    47  	return bytes.Compare(pvi.Address(), pvj.Address()) == -1
    48  }
    49  
    50  func (pvs PrivValidatorsByAddress) Swap(i, j int) {
    51  	it := pvs[i]
    52  	pvs[i] = pvs[j]
    53  	pvs[j] = it
    54  }
    55  
    56  //----------------------------------------
    57  // MockPV
    58  
    59  // MockPV implements PrivValidator without any safety or persistence.
    60  // Only use it for testing.
    61  type MockPV struct {
    62  	PrivKey              crypto.PrivKey
    63  	breakProposalSigning bool
    64  	breakVoteSigning     bool
    65  }
    66  
    67  func NewMockPV() MockPV {
    68  	return MockPV{ed25519.GenPrivKey(), false, false}
    69  }
    70  
    71  // NewMockPVWithParams allows one to create a MockPV instance, but with finer
    72  // grained control over the operation of the mock validator. This is useful for
    73  // mocking test failures.
    74  func NewMockPVWithParams(privKey crypto.PrivKey, breakProposalSigning, breakVoteSigning bool) MockPV {
    75  	return MockPV{privKey, breakProposalSigning, breakVoteSigning}
    76  }
    77  
    78  // Implements PrivValidator.
    79  func (pv MockPV) GetPubKey() (crypto.PubKey, error) {
    80  	return pv.PrivKey.PubKey(), nil
    81  }
    82  
    83  // Implements PrivValidator.
    84  func (pv MockPV) SignVote(chainID string, vote *Vote) error {
    85  	useChainID := chainID
    86  	if pv.breakVoteSigning {
    87  		useChainID = "incorrect-chain-id"
    88  	}
    89  	signBytes := vote.SignBytes(useChainID)
    90  	sig, err := pv.PrivKey.Sign(signBytes)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	vote.Signature = sig
    95  	return nil
    96  }
    97  
    98  // Implements PrivValidator.
    99  func (pv MockPV) SignProposal(chainID string, proposal *Proposal) error {
   100  	useChainID := chainID
   101  	if pv.breakProposalSigning {
   102  		useChainID = "incorrect-chain-id"
   103  	}
   104  	signBytes := proposal.SignBytes(useChainID)
   105  	sig, err := pv.PrivKey.Sign(signBytes)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	proposal.Signature = sig
   110  	return nil
   111  }
   112  
   113  // String returns a string representation of the MockPV.
   114  func (pv MockPV) String() string {
   115  	mpv, _ := pv.GetPubKey() // mockPV will never return an error, ignored here
   116  	return fmt.Sprintf("MockPV{%v}", mpv.Address())
   117  }
   118  
   119  // XXX: Implement.
   120  func (pv MockPV) DisableChecks() {
   121  	// Currently this does nothing,
   122  	// as MockPV has no safety checks at all.
   123  }
   124  
   125  type ErroringMockPV struct {
   126  	MockPV
   127  }
   128  
   129  var ErroringMockPVErr = errors.New("erroringMockPV always returns an error")
   130  
   131  // Implements PrivValidator.
   132  func (pv *ErroringMockPV) SignVote(chainID string, vote *Vote) error {
   133  	return ErroringMockPVErr
   134  }
   135  
   136  // Implements PrivValidator.
   137  func (pv *ErroringMockPV) SignProposal(chainID string, proposal *Proposal) error {
   138  	return ErroringMockPVErr
   139  }
   140  
   141  // NewErroringMockPV returns a MockPV that fails on each signing request. Again, for testing only.
   142  
   143  func NewErroringMockPV() *ErroringMockPV {
   144  	return &ErroringMockPV{MockPV{ed25519.GenPrivKey(), false, false}}
   145  }