/
utils.go
242 lines (215 loc) · 6.05 KB
/
utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package e2e
import (
"errors"
"fmt"
"io"
"math/rand"
"strconv"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/dash/llmq"
)
// proTxHashGenerator generates pseudorandom proTxHash based on a seed.
type proTxHashGenerator struct {
random *rand.Rand
}
func newProTxHashGenerator(seed int64) *proTxHashGenerator {
return &proTxHashGenerator{
random: rand.New(rand.NewSource(seed)), //nolint: gosec
}
}
func (g *proTxHashGenerator) generate() crypto.ProTxHash {
seed := make([]byte, crypto.DefaultHashSize)
_, err := io.ReadFull(g.random, seed)
if err != nil {
panic(err) // this shouldn't happen
}
return seed
}
// quorumHashGenerator generates pseudorandom quorumHash based on a seed.
type quorumHashGenerator struct {
random *rand.Rand
}
func newQuorumHashGenerator(seed int64) *quorumHashGenerator {
return &quorumHashGenerator{
random: rand.New(rand.NewSource(seed)), //nolint: gosec
}
}
func (g *quorumHashGenerator) generate() crypto.QuorumHash {
seed := make([]byte, crypto.DefaultHashSize)
_, err := io.ReadFull(g.random, seed)
if err != nil {
panic(err) // this shouldn't happen
}
return seed
}
type initNodeFunc func(*Node) error
type initValidatorFunc func(*Node, crypto.ProTxHash, crypto.QuorumKeys) error
func initValidator(iter *llmq.Iter, quorumHash crypto.QuorumHash, handlers ...initValidatorFunc) initNodeFunc {
return func(node *Node) error {
if !iter.Next() {
return errors.New("unable to move iterator to next an element")
}
if node.PrivvalKeys == nil {
node.PrivvalKeys = make(map[string]crypto.QuorumKeys)
}
proTxHash, qks := iter.Value()
node.PrivvalKeys[quorumHash.String()] = qks
for _, handler := range handlers {
err := handler(node, proTxHash, qks)
if err != nil {
return err
}
}
return nil
}
}
func updateProTxHash() initValidatorFunc {
return func(node *Node, proTxHash crypto.ProTxHash, qks crypto.QuorumKeys) error {
node.ProTxHash = proTxHash
fmt.Printf("Setting validator %s proTxHash to %s\n", node.Name, node.ProTxHash.ShortString())
return nil
}
}
func updateGenesisValidators(testnet *Testnet) initValidatorFunc {
return func(node *Node, proTxHash crypto.ProTxHash, qks crypto.QuorumKeys) error {
vu, err := node.validatorUpdate(qks.PubKey)
if err != nil {
return err
}
testnet.Validators[node] = ValidatorConfig{vu}
return nil
}
}
func updatePrivvalUpdateHeights(height int, quorumHash crypto.QuorumHash) initNodeFunc {
return func(node *Node) error {
if node.PrivvalUpdateHeights == nil {
node.PrivvalUpdateHeights = make(map[string]crypto.QuorumHash)
}
node.PrivvalUpdateHeights[strconv.Itoa(height)] = quorumHash
return nil
}
}
func updateValidatorUpdate(valUpdate ValidatorsMap) initValidatorFunc {
return func(node *Node, proTxHash crypto.ProTxHash, qks crypto.QuorumKeys) error {
vu, err := node.validatorUpdate(qks.PubKey)
if err != nil {
return err
}
valUpdate[node] = ValidatorConfig{vu}
return nil
}
}
func initAnyNode(thresholdPublicKey crypto.PubKey, quorumHash crypto.QuorumHash) initNodeFunc {
return func(node *Node) error {
quorumKeys := crypto.QuorumKeys{
ThresholdPublicKey: thresholdPublicKey,
}
if node.PrivvalKeys == nil {
node.PrivvalKeys = make(map[string]crypto.QuorumKeys)
}
node.PrivvalKeys[quorumHash.String()] = quorumKeys
return nil
}
}
func printInitValidatorInfo(height int) initValidatorFunc {
return func(node *Node, proTxHash crypto.ProTxHash, qks crypto.QuorumKeys) error {
if height == 0 {
fmt.Printf("Set validator %s/%X (at genesis) pubkey to %X\n", node.Name,
node.ProTxHash, qks.PubKey.Bytes())
return nil
}
fmt.Printf("Set validator %s/%X (at height %d (+ 2)) pubkey to %X\n", node.Name,
node.ProTxHash, height, qks.PubKey.Bytes())
return nil
}
}
func genProTxHashes(proTxHashGen *proTxHashGenerator, n int) []crypto.ProTxHash {
proTxHashes := make([]crypto.ProTxHash, n)
for i := 0; i < n; i++ {
proTxHashes[i] = proTxHashGen.generate()
if proTxHashes[i] == nil || len(proTxHashes[i]) != crypto.ProTxHashSize {
panic("the proTxHash must be 32 bytes")
}
}
return proTxHashes
}
func countValidators(nodes map[string]*ManifestNode) int {
cnt := 0
for _, node := range nodes {
nodeManifest := node
if nodeManifest.Mode == "" || Mode(nodeManifest.Mode) == ModeValidator {
cnt++
}
}
return cnt
}
func updateNodeParams(nodes []*Node, initFuncs ...initNodeFunc) error {
// Set up genesis validators. If not specified explicitly, use all validator nodes.
for _, node := range nodes {
for _, initFunc := range initFuncs {
err := initFunc(node)
if err != nil {
return err
}
}
}
return nil
}
func makeProTxHashMap(proTxHashes []crypto.ProTxHash) map[string]struct{} {
m := make(map[string]struct{})
for _, proTxHash := range proTxHashes {
m[proTxHash.String()] = struct{}{}
}
return m
}
func modifyHeight(height int) int {
if height > 0 {
return height + 2
}
return height
}
// filter
func nodesFilter(nodes []*Node, cond func(node *Node) bool) []*Node {
var res []*Node
for _, node := range nodes {
if cond(node) {
res = append(res, node)
}
}
return res
}
func shouldNotBeValidator() func(node *Node) bool {
return func(node *Node) bool {
return node.Mode != ModeValidator
}
}
func shouldHaveName(allowed map[string]int64) func(node *Node) bool {
return func(node *Node) bool {
if node.Mode == ModeValidator && len(allowed) == 0 {
return true
}
_, ok := allowed[node.Name]
return ok
}
}
func proTxHashShouldNotBeIn(proTxHashes []crypto.ProTxHash) func(node *Node) bool {
proTxHashesMap := makeProTxHashMap(proTxHashes)
return func(node *Node) bool {
flag := false
if node.ProTxHash != nil {
_, flag = proTxHashesMap[node.ProTxHash.String()]
}
return !flag
}
}
func lookupNodesByProTxHash(testnet *Testnet, proTxHashes ...crypto.ProTxHash) []*Node {
res := make([]*Node, 0, len(proTxHashes))
nodeMap := make(map[string]*Node)
for _, node := range testnet.Nodes {
nodeMap[node.ProTxHash.String()] = node
}
for _, proTxHash := range proTxHashes {
res = append(res, nodeMap[proTxHash.String()])
}
return res
}