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