-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_util.go
82 lines (71 loc) · 2.12 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
package types
import (
"context"
"fmt"
"math/rand"
"github.com/dashpay/tenderdash/crypto"
tmrand "github.com/dashpay/tenderdash/libs/rand"
tmtime "github.com/dashpay/tenderdash/libs/time"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
)
const StateIDVersion = 1
func RandStateID() tmproto.StateID {
return tmproto.StateID{
Height: uint64(rand.Int63()), //nolint:gosec
AppHash: tmrand.Bytes(crypto.DefaultAppHashSize),
AppVersion: StateIDVersion,
CoreChainLockedHeight: rand.Uint32(), //nolint:gosec
Time: uint64(tmtime.Now().UnixMilli()),
}
}
func makeCommit(
ctx context.Context,
blockID BlockID,
height int64,
round int32,
voteSet *VoteSet,
validators []PrivValidator,
) (*Commit, error) {
// all sign
for i := 0; i < len(validators); i++ {
proTxHash, err := validators[i].GetProTxHash(ctx)
if err != nil {
return nil, fmt.Errorf("can't get proTxHash: %w", err)
}
vote := &Vote{
ValidatorProTxHash: proTxHash,
ValidatorIndex: int32(i),
Height: height,
Round: round,
Type: tmproto.PrecommitType,
BlockID: blockID,
VoteExtensions: VoteExtensionsFromProto(
&tmproto.VoteExtension{
Type: tmproto.VoteExtensionType_THRESHOLD_RECOVER_RAW,
Extension: crypto.Checksum([]byte("raw"))},
&tmproto.VoteExtension{
Type: tmproto.VoteExtensionType_THRESHOLD_RECOVER,
Extension: []byte("threshold")},
),
}
_, err = signAddVote(ctx, validators[i], vote, voteSet)
if err != nil {
return nil, err
}
}
return voteSet.MakeCommit(), nil
}
// signAddVote signs a vote using StateID configured inside voteSet, and adds it to that voteSet
func signAddVote(ctx context.Context, privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bool, err error) {
v := vote.ToProto()
err = privVal.SignVote(ctx, voteSet.ChainID(), voteSet.valSet.QuorumType, voteSet.valSet.QuorumHash,
v, nil)
if err != nil {
return false, err
}
err = vote.PopulateSignsFromProto(v)
if err != nil {
return false, err
}
return voteSet.AddVote(vote)
}