github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/types/test_util.go (about)

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