-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
generate.go
465 lines (409 loc) · 12.5 KB
/
generate.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package main
import (
"fmt"
"math/rand"
"sort"
"strings"
e2e "github.com/tendermint/tendermint/test/e2e/pkg"
"github.com/tendermint/tendermint/types"
)
var (
// testnetCombinations defines global testnet options, where we generate a
// separate testnet for each combination (Cartesian product) of options.
testnetCombinations = map[string][]interface{}{
"topology": {"single", "quad", "large"},
"p2p": {NewP2PMode, LegacyP2PMode, HybridP2PMode},
"queueType": {"priority"}, // "fifo", "wdrr"
"initialState": {
map[string]string{},
map[string]string{"initial01": "a", "initial02": "b", "initial03": "c"},
},
"validators": {"genesis", "initchain"},
"txSize": {1024, 2048, 4096, 8192},
}
initalHeights = uniformChoice{0, 1000}
// The following specify randomly chosen values for testnet nodes.
nodeDatabases = weightedChoice{
"goleveldb": 35,
"badgerdb": 35,
"boltdb": 15,
"rocksdb": 10,
"cleveldb": 5,
}
nodeABCIProtocols = weightedChoice{
"builtin": 50,
"tcp": 20,
"grpc": 20,
"unix": 10,
}
nodePrivvalProtocols = weightedChoice{
"file": 50,
"grpc": 20,
"tcp": 20,
"unix": 10,
}
nodeMempools = uniformChoice{"v0", "v1"}
nodeStateSyncs = weightedChoice{
e2e.StateSyncDisabled: 10,
e2e.StateSyncP2P: 45,
e2e.StateSyncRPC: 45,
}
nodePersistIntervals = uniformChoice{0, 1, 5}
nodeSnapshotIntervals = uniformChoice{0, 5}
nodeRetainBlocks = uniformChoice{0, 2 * int(e2e.EvidenceAgeHeight), 4 * int(e2e.EvidenceAgeHeight)}
nodePerturbations = probSetChoice{
"disconnect": 0.1,
"pause": 0.1,
"kill": 0.1,
"restart": 0.1,
}
evidence = uniformChoice{0, 1, 10}
ipv6 = uniformChoice{false, true}
keyType = uniformChoice{types.ABCIPubKeyTypeEd25519, types.ABCIPubKeyTypeSecp256k1}
)
// Generate generates random testnets using the given RNG.
func Generate(r *rand.Rand, opts Options) ([]e2e.Manifest, error) {
manifests := []e2e.Manifest{}
switch opts.P2P {
case NewP2PMode, LegacyP2PMode, HybridP2PMode:
defer func() {
// avoid modifying the global state.
original := make([]interface{}, len(testnetCombinations["p2p"]))
copy(original, testnetCombinations["p2p"])
testnetCombinations["p2p"] = original
}()
testnetCombinations["p2p"] = []interface{}{opts.P2P}
case MixedP2PMode:
testnetCombinations["p2p"] = []interface{}{NewP2PMode, LegacyP2PMode, HybridP2PMode}
}
for _, opt := range combinations(testnetCombinations) {
manifest, err := generateTestnet(r, opt)
if err != nil {
return nil, err
}
if len(manifest.Nodes) < opts.MinNetworkSize {
continue
}
if len(manifest.Nodes) == 1 {
if opt["p2p"] == HybridP2PMode {
continue
}
if opt["txSize"].(int) != 2048 {
continue
}
}
if opts.MaxNetworkSize > 0 && len(manifest.Nodes) >= opts.MaxNetworkSize {
continue
}
if len(manifest.Nodes) > 4 {
if opt["txSize"].(int) == 8192 {
continue
}
}
if opt["p2p"] == HybridP2PMode {
numLegacy := 0
for _, n := range manifest.Nodes {
if n.UseLegacyP2P {
numLegacy++
}
}
if numLegacy == len(manifest.Nodes) {
continue
}
if numLegacy == 0 {
continue
}
}
manifests = append(manifests, manifest)
}
return manifests, nil
}
type Options struct {
MinNetworkSize int
MaxNetworkSize int
NumGroups int
Directory string
P2P P2PMode
Reverse bool
}
type P2PMode string
const (
NewP2PMode P2PMode = "new"
LegacyP2PMode P2PMode = "legacy"
HybridP2PMode P2PMode = "hybrid"
// mixed means that all combination are generated
MixedP2PMode P2PMode = "mixed"
)
// generateTestnet generates a single testnet with the given options.
func generateTestnet(r *rand.Rand, opt map[string]interface{}) (e2e.Manifest, error) {
manifest := e2e.Manifest{
IPv6: ipv6.Choose(r).(bool),
ABCIProtocol: nodeABCIProtocols.Choose(r),
InitialHeight: int64(initalHeights.Choose(r).(int)),
InitialState: opt["initialState"].(map[string]string),
Validators: &map[string]int64{},
ValidatorUpdates: map[string]map[string]int64{},
Nodes: map[string]*e2e.ManifestNode{},
KeyType: keyType.Choose(r).(string),
Evidence: evidence.Choose(r).(int),
QueueType: opt["queueType"].(string),
TxSize: opt["txSize"].(int),
}
p2pMode := opt["p2p"].(P2PMode)
switch p2pMode {
case NewP2PMode, LegacyP2PMode, HybridP2PMode:
default:
return manifest, fmt.Errorf("unknown p2p mode %s", p2pMode)
}
var numSeeds, numValidators, numFulls, numLightClients int
switch opt["topology"].(string) {
case "single":
numValidators = 1
case "quad":
numValidators = 4
case "large":
// FIXME Networks are kept small since large ones use too much CPU.
numSeeds = r.Intn(1)
numLightClients = r.Intn(2)
numValidators = 4 + r.Intn(4)
numFulls = r.Intn(4)
default:
return manifest, fmt.Errorf("unknown topology %q", opt["topology"])
}
const legacyP2PFactor float64 = 0.5
// First we generate seed nodes, starting at the initial height.
for i := 1; i <= numSeeds; i++ {
node := generateNode(r, manifest, e2e.ModeSeed, 0, false)
switch p2pMode {
case LegacyP2PMode:
node.UseLegacyP2P = true
case HybridP2PMode:
node.UseLegacyP2P = r.Float64() < legacyP2PFactor
}
manifest.Nodes[fmt.Sprintf("seed%02d", i)] = node
}
var (
numSyncingNodes = 0
hybridNumNew = 0
hybridNumLegacy = 0
)
// Next, we generate validators. We make sure a BFT quorum of validators start
// at the initial height, and that we have two archive nodes. We also set up
// the initial validator set, and validator set updates for delayed nodes.
nextStartAt := manifest.InitialHeight + 5
quorum := numValidators*2/3 + 1
for i := 1; i <= numValidators; i++ {
startAt := int64(0)
if i > quorum && numSyncingNodes < 2 && r.Float64() >= 0.25 {
numSyncingNodes++
startAt = nextStartAt
nextStartAt += 5
}
name := fmt.Sprintf("validator%02d", i)
node := generateNode(r, manifest, e2e.ModeValidator, startAt, i <= 2)
switch p2pMode {
case LegacyP2PMode:
node.UseLegacyP2P = true
case HybridP2PMode:
node.UseLegacyP2P = r.Float64() < legacyP2PFactor
if node.UseLegacyP2P {
hybridNumLegacy++
if hybridNumNew == 0 {
hybridNumNew++
hybridNumLegacy--
node.UseLegacyP2P = false
}
if node.StartAt > manifest.InitialHeight {
hybridNumLegacy--
node.UseLegacyP2P = false
}
} else {
hybridNumNew++
if hybridNumLegacy == 0 {
hybridNumNew--
hybridNumLegacy++
node.UseLegacyP2P = true
}
}
}
manifest.Nodes[name] = node
if startAt == 0 {
(*manifest.Validators)[name] = int64(30 + r.Intn(71))
} else {
manifest.ValidatorUpdates[fmt.Sprint(startAt+5)] = map[string]int64{
name: int64(30 + r.Intn(71)),
}
}
}
// Move validators to InitChain if specified.
switch opt["validators"].(string) {
case "genesis":
case "initchain":
manifest.ValidatorUpdates["0"] = *manifest.Validators
manifest.Validators = &map[string]int64{}
default:
return manifest, fmt.Errorf("invalid validators option %q", opt["validators"])
}
// Finally, we generate random full nodes.
for i := 1; i <= numFulls; i++ {
startAt := int64(0)
if numSyncingNodes < 2 && r.Float64() >= 0.5 {
numSyncingNodes++
startAt = nextStartAt
nextStartAt += 5
}
node := generateNode(r, manifest, e2e.ModeFull, startAt, false)
switch p2pMode {
case LegacyP2PMode:
node.UseLegacyP2P = true
case HybridP2PMode:
node.UseLegacyP2P = r.Float64() > legacyP2PFactor
}
manifest.Nodes[fmt.Sprintf("full%02d", i)] = node
}
// We now set up peer discovery for nodes. Seed nodes are fully meshed with
// each other, while non-seed nodes either use a set of random seeds or a
// set of random peers that start before themselves.
var seedNames, peerNames, lightProviders []string
for name, node := range manifest.Nodes {
if node.Mode == string(e2e.ModeSeed) {
seedNames = append(seedNames, name)
} else {
// if the full node or validator is an ideal candidate, it is added as a light provider.
// There are at least two archive nodes so there should be at least two ideal candidates
if (node.StartAt == 0 || node.StartAt == manifest.InitialHeight) && node.RetainBlocks == 0 {
lightProviders = append(lightProviders, name)
}
peerNames = append(peerNames, name)
}
}
for _, name := range seedNames {
for _, otherName := range seedNames {
if name != otherName {
manifest.Nodes[name].Seeds = append(manifest.Nodes[name].Seeds, otherName)
}
}
}
sort.Slice(peerNames, func(i, j int) bool {
iName, jName := peerNames[i], peerNames[j]
switch {
case manifest.Nodes[iName].StartAt < manifest.Nodes[jName].StartAt:
return true
case manifest.Nodes[iName].StartAt > manifest.Nodes[jName].StartAt:
return false
default:
return strings.Compare(iName, jName) == -1
}
})
for i, name := range peerNames {
// there are seeds, statesync is disabled, and it's
// either the first peer by the sort order, and
// (randomly half of the remaining peers use a seed
// node; otherwise, choose some remaining set of the
// peers.
if len(seedNames) > 0 &&
manifest.Nodes[name].StateSync == e2e.StateSyncDisabled &&
(i == 0 || r.Float64() >= 0.5) {
// choose one of the seeds
manifest.Nodes[name].Seeds = uniformSetChoice(seedNames).Choose(r)
} else if i > 0 {
peers := uniformSetChoice(peerNames[:i])
if manifest.Nodes[name].StateSync == e2e.StateSyncP2P {
manifest.Nodes[name].PersistentPeers = peers.ChooseAtLeast(r, 2)
} else {
manifest.Nodes[name].PersistentPeers = peers.Choose(r)
}
}
}
// lastly, set up the light clients
for i := 1; i <= numLightClients; i++ {
startAt := manifest.InitialHeight + 5
node := generateLightNode(
r, startAt+(5*int64(i)), lightProviders,
)
switch p2pMode {
case LegacyP2PMode:
node.UseLegacyP2P = true
case HybridP2PMode:
node.UseLegacyP2P = r.Float64() < legacyP2PFactor
}
manifest.Nodes[fmt.Sprintf("light%02d", i)] = node
}
return manifest, nil
}
// generateNode randomly generates a node, with some constraints to avoid
// generating invalid configurations. We do not set Seeds or PersistentPeers
// here, since we need to know the overall network topology and startup
// sequencing.
func generateNode(
r *rand.Rand,
manifest e2e.Manifest,
mode e2e.Mode,
startAt int64,
forceArchive bool,
) *e2e.ManifestNode {
node := e2e.ManifestNode{
Mode: string(mode),
StartAt: startAt,
Database: nodeDatabases.Choose(r),
PrivvalProtocol: nodePrivvalProtocols.Choose(r),
BlockSync: "v0",
Mempool: nodeMempools.Choose(r).(string),
StateSync: e2e.StateSyncDisabled,
PersistInterval: ptrUint64(uint64(nodePersistIntervals.Choose(r).(int))),
SnapshotInterval: uint64(nodeSnapshotIntervals.Choose(r).(int)),
RetainBlocks: uint64(nodeRetainBlocks.Choose(r).(int)),
Perturb: nodePerturbations.Choose(r),
}
if startAt > 0 {
node.StateSync = nodeStateSyncs.Choose(r)
if manifest.InitialHeight-startAt <= 5 && node.StateSync == e2e.StateSyncDisabled {
// avoid needing to blocsync more than five total blocks.
node.StateSync = uniformSetChoice([]string{
e2e.StateSyncP2P,
e2e.StateSyncRPC,
}).Choose(r)[0]
}
}
// If this node is forced to be an archive node, retain all blocks and
// enable state sync snapshotting.
if forceArchive {
node.RetainBlocks = 0
node.SnapshotInterval = 3
}
// If a node which does not persist state also does not retain blocks, randomly
// choose to either persist state or retain all blocks.
if node.PersistInterval != nil && *node.PersistInterval == 0 && node.RetainBlocks > 0 {
if r.Float64() > 0.5 {
node.RetainBlocks = 0
} else {
node.PersistInterval = ptrUint64(node.RetainBlocks)
}
}
// If either PersistInterval or SnapshotInterval are greater than RetainBlocks,
// expand the block retention time.
if node.RetainBlocks > 0 {
if node.PersistInterval != nil && node.RetainBlocks < *node.PersistInterval {
node.RetainBlocks = *node.PersistInterval
}
if node.RetainBlocks < node.SnapshotInterval {
node.RetainBlocks = node.SnapshotInterval
}
}
if node.StateSync != e2e.StateSyncDisabled {
node.BlockSync = "v0"
}
return &node
}
func generateLightNode(r *rand.Rand, startAt int64, providers []string) *e2e.ManifestNode {
return &e2e.ManifestNode{
Mode: string(e2e.ModeLight),
StartAt: startAt,
Database: nodeDatabases.Choose(r),
PersistInterval: ptrUint64(0),
PersistentPeers: providers,
}
}
func ptrUint64(i uint64) *uint64 {
return &i
}