github.com/vipernet-xyz/tm@v0.34.24/types/priv_validator.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/vipernet-xyz/tm/crypto"
     9  	"github.com/vipernet-xyz/tm/crypto/ed25519"
    10  	tmproto "github.com/vipernet-xyz/tm/proto/tendermint/types"
    11  )
    12  
    13  // PrivValidator defines the functionality of a local Tendermint validator
    14  // that signs votes and proposals, and never double signs.
    15  type PrivValidator interface {
    16  	GetPubKey() (crypto.PubKey, error)
    17  
    18  	SignVote(chainID string, vote *tmproto.Vote) error
    19  	SignProposal(chainID string, proposal *tmproto.Proposal) error
    20  }
    21  
    22  type PrivValidatorsByAddress []PrivValidator
    23  
    24  func (pvs PrivValidatorsByAddress) Len() int {
    25  	return len(pvs)
    26  }
    27  
    28  func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
    29  	pvi, err := pvs[i].GetPubKey()
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	pvj, err := pvs[j].GetPubKey()
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	return bytes.Compare(pvi.Address(), pvj.Address()) == -1
    39  }
    40  
    41  func (pvs PrivValidatorsByAddress) Swap(i, j int) {
    42  	pvs[i], pvs[j] = pvs[j], pvs[i]
    43  }
    44  
    45  //----------------------------------------
    46  // MockPV
    47  
    48  // MockPV implements PrivValidator without any safety or persistence.
    49  // Only use it for testing.
    50  type MockPV struct {
    51  	PrivKey              crypto.PrivKey
    52  	breakProposalSigning bool
    53  	breakVoteSigning     bool
    54  }
    55  
    56  func NewMockPV() MockPV {
    57  	return MockPV{ed25519.GenPrivKey(), false, false}
    58  }
    59  
    60  // NewMockPVWithParams allows one to create a MockPV instance, but with finer
    61  // grained control over the operation of the mock validator. This is useful for
    62  // mocking test failures.
    63  func NewMockPVWithParams(privKey crypto.PrivKey, breakProposalSigning, breakVoteSigning bool) MockPV {
    64  	return MockPV{privKey, breakProposalSigning, breakVoteSigning}
    65  }
    66  
    67  // Implements PrivValidator.
    68  func (pv MockPV) GetPubKey() (crypto.PubKey, error) {
    69  	return pv.PrivKey.PubKey(), nil
    70  }
    71  
    72  // Implements PrivValidator.
    73  func (pv MockPV) SignVote(chainID string, vote *tmproto.Vote) error {
    74  	useChainID := chainID
    75  	if pv.breakVoteSigning {
    76  		useChainID = "incorrect-chain-id"
    77  	}
    78  
    79  	signBytes := VoteSignBytes(useChainID, vote)
    80  	sig, err := pv.PrivKey.Sign(signBytes)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	vote.Signature = sig
    85  	return nil
    86  }
    87  
    88  // Implements PrivValidator.
    89  func (pv MockPV) SignProposal(chainID string, proposal *tmproto.Proposal) error {
    90  	useChainID := chainID
    91  	if pv.breakProposalSigning {
    92  		useChainID = "incorrect-chain-id"
    93  	}
    94  
    95  	signBytes := ProposalSignBytes(useChainID, proposal)
    96  	sig, err := pv.PrivKey.Sign(signBytes)
    97  	if err != nil {
    98  		return err
    99  	}
   100  	proposal.Signature = sig
   101  	return nil
   102  }
   103  
   104  func (pv MockPV) ExtractIntoValidator(votingPower int64) *Validator {
   105  	pubKey, _ := pv.GetPubKey()
   106  	return &Validator{
   107  		Address:     pubKey.Address(),
   108  		PubKey:      pubKey,
   109  		VotingPower: votingPower,
   110  	}
   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 *tmproto.Vote) error {
   133  	return ErroringMockPVErr
   134  }
   135  
   136  // Implements PrivValidator.
   137  func (pv *ErroringMockPV) SignProposal(chainID string, proposal *tmproto.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  }