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

     1  package types
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  func MakeCommit(blockID BlockID, height int64, round int,
    10  	voteSet *VoteSet, validators []PrivValidator, now time.Time) (*Commit, error) {
    11  
    12  	// all sign
    13  	for i := 0; i < len(validators); i++ {
    14  		pubKey, err := validators[i].GetPubKey()
    15  		if err != nil {
    16  			return nil, errors.Wrap(err, "can't get pubkey")
    17  		}
    18  		vote := &Vote{
    19  			ValidatorAddress: pubKey.Address(),
    20  			ValidatorIndex:   i,
    21  			Height:           height,
    22  			Round:            round,
    23  			Type:             PrecommitType,
    24  			BlockID:          blockID,
    25  			Timestamp:        now,
    26  		}
    27  
    28  		_, err = signAddVote(validators[i], vote, voteSet)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  	}
    33  
    34  	return voteSet.MakeCommit(), nil
    35  }
    36  
    37  func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bool, err error) {
    38  	err = privVal.SignVote(voteSet.ChainID(), vote)
    39  	if err != nil {
    40  		return false, err
    41  	}
    42  	return voteSet.AddVote(vote)
    43  }
    44  
    45  func MakeVote(
    46  	height int64,
    47  	blockID BlockID,
    48  	valSet *ValidatorSet,
    49  	privVal PrivValidator,
    50  	chainID string,
    51  	now time.Time,
    52  ) (*Vote, error) {
    53  	pubKey, err := privVal.GetPubKey()
    54  	if err != nil {
    55  		return nil, errors.Wrap(err, "can't get pubkey")
    56  	}
    57  	addr := pubKey.Address()
    58  	idx, _ := valSet.GetByAddress(addr)
    59  	vote := &Vote{
    60  		ValidatorAddress: addr,
    61  		ValidatorIndex:   idx,
    62  		Height:           height,
    63  		Round:            0,
    64  		Timestamp:        now,
    65  		Type:             PrecommitType,
    66  		BlockID:          blockID,
    67  	}
    68  	if err := privVal.SignVote(chainID, vote); err != nil {
    69  		return nil, err
    70  	}
    71  	return vote, nil
    72  }
    73  
    74  // MakeBlock returns a new block with an empty header, except what can be
    75  // computed from itself.
    76  // It populates the same set of fields validated by ValidateBasic.
    77  func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
    78  	block := &Block{
    79  		Header: Header{
    80  			Height: height,
    81  			NumTxs: int64(len(txs)),
    82  		},
    83  		Data: Data{
    84  			Txs: txs,
    85  		},
    86  		Evidence:   EvidenceData{Evidence: evidence},
    87  		LastCommit: lastCommit,
    88  	}
    89  	block.fillHeader()
    90  	return block
    91  }