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