github.com/aakash4dev/cometbft@v0.38.2/types/test_util.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 cmtproto "github.com/aakash4dev/cometbft/proto/tendermint/types" 9 cmtversion "github.com/aakash4dev/cometbft/proto/tendermint/version" 10 "github.com/aakash4dev/cometbft/version" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func MakeExtCommit(blockID BlockID, height int64, round int32, 15 voteSet *VoteSet, validators []PrivValidator, now time.Time, extEnabled bool) (*ExtendedCommit, error) { 16 17 // all sign 18 for i := 0; i < len(validators); i++ { 19 pubKey, err := validators[i].GetPubKey() 20 if err != nil { 21 return nil, fmt.Errorf("can't get pubkey: %w", err) 22 } 23 vote := &Vote{ 24 ValidatorAddress: pubKey.Address(), 25 ValidatorIndex: int32(i), 26 Height: height, 27 Round: round, 28 Type: cmtproto.PrecommitType, 29 BlockID: blockID, 30 Timestamp: now, 31 } 32 33 _, err = signAddVote(validators[i], vote, voteSet) 34 if err != nil { 35 return nil, err 36 } 37 } 38 39 var enableHeight int64 40 if extEnabled { 41 enableHeight = height 42 } 43 44 return voteSet.MakeExtendedCommit(ABCIParams{VoteExtensionsEnableHeight: enableHeight}), nil 45 } 46 47 func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (bool, error) { 48 if vote.Type != voteSet.signedMsgType { 49 return false, fmt.Errorf("vote and voteset are of different types; %d != %d", vote.Type, voteSet.signedMsgType) 50 } 51 if _, err := SignAndCheckVote(vote, privVal, voteSet.ChainID(), voteSet.extensionsEnabled); err != nil { 52 return false, err 53 } 54 return voteSet.AddVote(vote) 55 } 56 57 func MakeVote( 58 val PrivValidator, 59 chainID string, 60 valIndex int32, 61 height int64, 62 round int32, 63 step cmtproto.SignedMsgType, 64 blockID BlockID, 65 time time.Time, 66 ) (*Vote, error) { 67 pubKey, err := val.GetPubKey() 68 if err != nil { 69 return nil, err 70 } 71 72 vote := &Vote{ 73 ValidatorAddress: pubKey.Address(), 74 ValidatorIndex: valIndex, 75 Height: height, 76 Round: round, 77 Type: step, 78 BlockID: blockID, 79 Timestamp: time, 80 } 81 82 extensionsEnabled := step == cmtproto.PrecommitType 83 if _, err := SignAndCheckVote(vote, val, chainID, extensionsEnabled); err != nil { 84 return nil, err 85 } 86 87 return vote, nil 88 } 89 90 func MakeVoteNoError( 91 t *testing.T, 92 val PrivValidator, 93 chainID string, 94 valIndex int32, 95 height int64, 96 round int32, 97 step cmtproto.SignedMsgType, 98 blockID BlockID, 99 time time.Time, 100 ) *Vote { 101 vote, err := MakeVote(val, chainID, valIndex, height, round, step, blockID, time) 102 require.NoError(t, err) 103 return vote 104 } 105 106 // MakeBlock returns a new block with an empty header, except what can be 107 // computed from itself. 108 // It populates the same set of fields validated by ValidateBasic. 109 func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block { 110 block := &Block{ 111 Header: Header{ 112 Version: cmtversion.Consensus{Block: version.BlockProtocol, App: 0}, 113 Height: height, 114 }, 115 Data: Data{ 116 Txs: txs, 117 }, 118 Evidence: EvidenceData{Evidence: evidence}, 119 LastCommit: lastCommit, 120 } 121 block.fillHeader() 122 return block 123 }