summaryrefslogtreecommitdiff
path: root/cmd/netgoal/generate.go
blob: 7cd417621b236103fb10802e5b7c5d080d731564 (plain)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"encoding/json"
	"errors"
	"math/big"
	"math/rand"
	"os"
	"regexp"
	"strconv"
	"strings"

	"github.com/spf13/cobra"

	"github.com/algorand/go-algorand/gen"
	"github.com/algorand/go-algorand/netdeploy"
	"github.com/algorand/go-algorand/netdeploy/remote"
	"github.com/algorand/go-algorand/util/codecs"
)

var outputFilename string
var templateToGenerate string
var relaysToGenerate int
var nodesToGenerate int
var nodeHostsToGenerate int
var nonPartnodesHostsToGenerate int
var walletsToGenerate int
var nodeTemplatePath string
var nonParticipatingNodeTemplatePath string
var relayTemplatePath string
var sourceWallet string
var rounds uint64
var roundTxnCount uint64
var accountsCount uint64
var assetsCount uint64
var applicationCount uint64
var balRange []string

func init() {
	rootCmd.AddCommand(generateCmd)

	generateCmd.Flags().StringVarP(&outputFilename, "outputfile", "o", "", "Output filename")
	generateCmd.MarkFlagRequired("outputfile")

	generateCmd.Flags().StringVarP(&templateToGenerate, "template", "t", "", "Template to generate")

	generateCmd.Flags().IntVarP(&walletsToGenerate, "wallets", "w", -1, "Wallets to generate")
	generateCmd.Flags().IntVarP(&relaysToGenerate, "relays", "R", -1, "Relays to generate")
	generateCmd.Flags().IntVarP(&nodeHostsToGenerate, "node-hosts", "N", -1, "Node-hosts to generate, default=nodes")
	generateCmd.Flags().IntVarP(&nodesToGenerate, "nodes", "n", -1, "Nodes to generate")
	generateCmd.Flags().IntVarP(&nonPartnodesHostsToGenerate, "non-participating-nodes-hosts", "H", 0, "Non participating nodes hosts to generate")
	generateCmd.Flags().StringVarP(&nodeTemplatePath, "node-template", "", "", "json for one node")
	generateCmd.Flags().StringVarP(&nonParticipatingNodeTemplatePath, "non-participating-node-template", "", "", "json for non participating node")
	generateCmd.Flags().StringVarP(&relayTemplatePath, "relay-template", "", "", "json for a relay node")
	generateCmd.Flags().StringVarP(&sourceWallet, "wallet-name", "", "", "Source wallet name")
	generateCmd.Flags().Uint64VarP(&rounds, "rounds", "", 13, "Number of rounds")
	generateCmd.Flags().Uint64VarP(&roundTxnCount, "ntxns", "", 17, "Transaction count")
	generateCmd.Flags().Uint64VarP(&accountsCount, "naccounts", "", 31, "Account count")
	generateCmd.Flags().Uint64VarP(&assetsCount, "nassets", "", 5, "Asset count")
	generateCmd.Flags().Uint64VarP(&applicationCount, "napps", "", 7, "Application Count")
	generateCmd.Flags().StringArrayVar(&balRange, "bal", []string{}, "Application Count")

	longParts := make([]string, len(generateTemplateLines)+1)
	longParts[0] = generateCmd.Long
	copy(longParts[1:], generateTemplateLines)
	generateCmd.Long = strings.Join(longParts, "\n")
}

var generateTemplateLines = []string{
	"net => network template according to -R -N -n -w options. Suitable for 'netgoal build'",
	"goalnet => goal network template according to -R -n -w options. Suitable for 'goal network'",
	"genesis => genesis.json according to -w option",
	"otwt => OneThousandWallets network template",
	"otwg => OneThousandWallets genesis data",
	"ohwg => OneHundredWallets genesis data",
	"loadingFile => create accounts database file according to -wallet-name -rounds -ntxns -naccts -nassets -napps options",
}

var generateCmd = &cobra.Command{
	Use:   "generate",
	Short: "Generate network template",
	Long: `Generate network template or genesis.json
-r is required for all netgoal commands but unused by generate

template modes for -t:`,
	Run: func(cmd *cobra.Command, args []string) {
		var err error
		baseNode := remote.NodeConfig{}
		baseRelay := remote.NodeConfig{}
		baseNonParticipatingNode := remote.NodeConfig{}
		if nodeTemplatePath != "" {
			fin, err := os.Open(nodeTemplatePath)
			if err != nil {
				reportErrorf("%s: bad node template, %s", nodeTemplatePath, err)
			}
			dec := json.NewDecoder(fin)
			err = dec.Decode(&baseNode)
			if err != nil {
				reportErrorf("%s: bad node template, %s", nodeTemplatePath, err)
			}
		}
		if nonParticipatingNodeTemplatePath != "" {
			fin, err := os.Open(nonParticipatingNodeTemplatePath)
			if err != nil {
				reportErrorf("%s: bad npnode template, %s", nonParticipatingNodeTemplatePath, err)
			}
			dec := json.NewDecoder(fin)
			err = dec.Decode(&baseNonParticipatingNode)
			if err != nil {
				reportErrorf("%s: bad node template, %s", nodeTemplatePath, err)
			}
		} else {
			baseNonParticipatingNode = baseNode
		}
		if relayTemplatePath != "" {
			fin, err := os.Open(relayTemplatePath)
			if err != nil {
				reportErrorf("%s: bad relay template, %s", relayTemplatePath, err)
			}
			dec := json.NewDecoder(fin)
			err = dec.Decode(&baseRelay)
			if err != nil {
				reportErrorf("%s: bad relay template, %s", relayTemplatePath, err)
			}
		} else {
			baseRelay = baseNode
		}
		templateType := strings.ToLower(templateToGenerate)
		switch templateType {
		case "genesis", "wallets":
			if walletsToGenerate < 0 {
				reportErrorf("must specify number of wallets with -w")
			}
			err = generateWalletGenesis(outputFilename, walletsToGenerate, nonPartnodesHostsToGenerate)
		case "net", "network", "goalnet":
			if walletsToGenerate < 0 {
				reportErrorf("must specify number of wallets with -w")
			}
			if nodesToGenerate < 0 {
				reportErrorf("must specify number of nodes with -n")
			}
			if nodeHostsToGenerate < 0 {
				nodeHostsToGenerate = nodesToGenerate
			}
			if relaysToGenerate < 0 {
				reportErrorf("must specify number of relays with -R")
			}
			if templateType == "goalnet" {
				err = generateNetworkGoalTemplate(outputFilename, walletsToGenerate, relaysToGenerate, nodesToGenerate, nonPartnodesHostsToGenerate)
			} else {
				err = generateNetworkTemplate(outputFilename, walletsToGenerate, relaysToGenerate, nodeHostsToGenerate, nodesToGenerate, nonPartnodesHostsToGenerate, baseNode, baseNonParticipatingNode, baseRelay)
			}
		case "otwt":
			err = generateNetworkTemplate(outputFilename, 1000, 10, 20, 100, 0, baseNode, baseNonParticipatingNode, baseRelay)
		case "otwg":
			err = generateWalletGenesis(outputFilename, 1000, 0)
		case "ohwg":
			err = generateWalletGenesis(outputFilename, 100, 0)
		case "loadingfile":
			if sourceWallet == "" {
				reportErrorf("must specify source wallet name with -wname.")
			}
			if len(balRange) < 2 {
				reportErrorf("must specify account balance range with --bal.")
			}
			err = generateAccountsLoadingFileTemplate(outputFilename, sourceWallet, rounds, roundTxnCount, accountsCount, assetsCount, applicationCount, balRange)
		default:
			reportInfoln("Please specify a valid template name.\nSupported templates are:")
			for _, line := range generateTemplateLines {
				reportInfof("\t%s", line)
			}
			return
		}
		if err != nil {
			reportErrorf("error generating template file: %v\n", err)
		}
	},
}

func unpackNodeConfig(base remote.NodeConfig) []remote.NodeConfig {
	out := make([]remote.NodeConfig, 1+len(base.AltConfigs))
	out[0] = base
	if len(base.AltConfigs) > 0 {
		for i, ac := range base.AltConfigs {
			out[i+1] = ac
		}
	}
	out[0].AltConfigs = nil
	return out
}

func pickNodeConfig(alt []remote.NodeConfig, name string) remote.NodeConfig {
	psum := float64(0.0)
	for _, cfg := range alt {
		if cfg.NodeNameMatchRegex != "" {
			if match, _ := regexp.MatchString(cfg.NodeNameMatchRegex, name); match {
				return cfg
			}
		}
		psum += cfg.FractionApply
	}
	if psum > 0.0 {
		if psum < 1.0 {
			// the remaining fraction will be applied to the default config at alt[0] when the sum doesn't rise above psum
			psum = 1.0
		}
		hit := rand.Float64() * psum
		sofar := float64(0.0)
		for _, cfg := range alt {
			sofar += cfg.FractionApply
			if sofar > hit {
				return cfg
			}
		}
	}
	return alt[0]
}

func generateNetworkGoalTemplate(templateFilename string, wallets, relays, nodes, npnHosts int) error {
	template := netdeploy.NetworkTemplate{}
	template.Nodes = make([]remote.NodeConfigGoal, 0, relays+nodes+npnHosts)
	template.Genesis = generateWalletGenesisData(walletsToGenerate, 0)
	for i := 0; i < relays; i++ {
		name := "relay" + strconv.Itoa(i+1)
		newNode := remote.NodeConfigGoal{
			Name:    name,
			IsRelay: true,
			Wallets: nil,
		}
		template.Nodes = append(template.Nodes, newNode)
	}

	for i := 0; i < nodes; i++ {
		name := "node" + strconv.Itoa(i+1)
		newNode := remote.NodeConfigGoal{
			Name:    name,
			Wallets: make([]remote.NodeWalletData, 0),
		}
		template.Nodes = append(template.Nodes, newNode)
	}

	for i := 0; i < npnHosts; i++ {
		name := "nonParticipatingNode" + strconv.Itoa(i+1)
		newNode := remote.NodeConfigGoal{
			Name:    name,
			Wallets: make([]remote.NodeWalletData, 0),
		}
		template.Nodes = append(template.Nodes, newNode)
	}
	walletIndex := 0
	for walletIndex < wallets {
		for nodei, node := range template.Nodes {
			if node.Name[0:4] != "node" {
				continue
			}
			wallet := remote.NodeWalletData{
				Name:              "Wallet" + strconv.Itoa(walletIndex+1),
				ParticipationOnly: false,
			}
			template.Nodes[nodei].Wallets = append(template.Nodes[nodei].Wallets, wallet)
			walletIndex++
			if walletIndex >= wallets {
				break
			}
		}
		if walletIndex >= wallets {
			break
		}
	}

	if npnHosts > 0 {
		for walletIndex < npnHosts {
			for nodei, node := range template.Nodes {
				if node.Name[0:4] != "nonP" {
					continue
				}
				wallet := remote.NodeWalletData{
					Name:              "Wallet" + strconv.Itoa(walletIndex+1),
					ParticipationOnly: false,
				}
				template.Nodes[nodei].Wallets = append(template.Nodes[nodei].Wallets, wallet)
				walletIndex++
				if walletIndex >= npnHosts {
					break
				}
			}
			if walletIndex >= npnHosts {
				break
			}
		}
	}
	return saveGoalTemplateToDisk(template, templateFilename)
}

func generateNetworkTemplate(templateFilename string, wallets, relays, nodeHosts, nodes, npnHosts int, baseNode, baseNonPartNode, baseRelay remote.NodeConfig) error {
	network := remote.DeployedNetworkConfig{}

	relayTemplates := unpackNodeConfig(baseRelay)
	leafTemplates := unpackNodeConfig(baseNode)
	npnTemplates := unpackNodeConfig(baseNonPartNode)

	for i := 0; i < relays; i++ {
		indexID := strconv.Itoa(i + 1)
		host := remote.HostConfig{
			Name: "R" + indexID,
		}
		name := "relay" + indexID
		newNode := pickNodeConfig(relayTemplates, name)
		newNode.NodeNameMatchRegex = ""
		newNode.FractionApply = 0.0
		newNode.Name = name
		if newNode.NetAddress == "" {
			return errors.New("relay template did not set NetAddress")
		}
		newNode.Wallets = nil
		host.Nodes = append(host.Nodes, newNode)
		network.Hosts = append(network.Hosts, host)
	}

	for i := 0; i < nodeHosts; i++ {
		indexID := strconv.Itoa(i + 1)
		host := remote.HostConfig{
			Name: "N" + indexID,
		}
		network.Hosts = append(network.Hosts, host)
	}

	nodeIndex := 0
	for nodeIndex < nodes {
		for hosti, host := range network.Hosts {
			if host.Name[0] == 'R' {
				// don't assign user nodes to relay hosts
				continue
			}
			name := "node" + strconv.Itoa(nodeIndex+1)
			node := pickNodeConfig(leafTemplates, name)
			node.NodeNameMatchRegex = ""
			node.FractionApply = 0.0
			node.Name = name
			network.Hosts[hosti].Nodes = append(network.Hosts[hosti].Nodes, node)
			nodeIndex++
			if nodeIndex >= nodes {
				break
			}
		}
	}

	for i := 0; i < npnHosts; i++ {
		indexID := strconv.Itoa(i + 1)

		name := "nonParticipatingNode" + strconv.Itoa(i+1)
		node := pickNodeConfig(npnTemplates, name)
		node.NodeNameMatchRegex = ""
		node.FractionApply = 0.0
		node.Name = name
		host := remote.HostConfig{
			Name:  "NPN" + indexID,
			Nodes: []remote.NodeConfig{node},
		}
		network.Hosts = append(network.Hosts, host)
	}

	walletIndex := 0
	for walletIndex < wallets {
		for hosti := range network.Hosts {
			for nodei, node := range network.Hosts[hosti].Nodes {
				if node.Name[0:4] != "node" {
					continue
				}
				wallet := remote.NodeWalletData{
					Name:              "Wallet" + strconv.Itoa(walletIndex+1),
					ParticipationOnly: false,
				}
				network.Hosts[hosti].Nodes[nodei].Wallets = append(network.Hosts[hosti].Nodes[nodei].Wallets, wallet)
				walletIndex++
				if walletIndex >= wallets {
					break
				}
			}
			if walletIndex >= wallets {
				break
			}
		}
	}

	// one wallet per NPN host to concentrate stake
	if npnHosts > 0 {
		walletIndex := 0
		for walletIndex < npnHosts {
			for hosti := range network.Hosts {
				for nodei, node := range network.Hosts[hosti].Nodes {
					if node.Name[0:4] != "nonP" {
						continue
					}
					wallet := remote.NodeWalletData{
						Name:              "Wallet" + strconv.Itoa(wallets+walletIndex+1),
						ParticipationOnly: false,
					}
					network.Hosts[hosti].Nodes[nodei].Wallets = append(network.Hosts[hosti].Nodes[nodei].Wallets, wallet)
					walletIndex++
					if walletIndex >= npnHosts {
						break
					}
				}
				if walletIndex >= npnHosts {
					break
				}
			}
		}
	}

	// ensure that at most one node per host claims any APIEndpoint port
	for hosti, host := range network.Hosts {
		seenAPIEndpoint := make(map[string]bool, 4)
		for nodei, node := range host.Nodes {
			if node.APIEndpoint != "" {
				if seenAPIEndpoint[node.APIEndpoint] {
					// squash dup
					network.Hosts[hosti].Nodes[nodei].APIEndpoint = ""
				} else {
					seenAPIEndpoint[node.APIEndpoint] = true
				}
			}
		}
	}

	return saveTemplateToDisk(network, templateFilename)
}

func saveTemplateToDisk(template remote.DeployedNetworkConfig, filename string) error {
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		defer f.Close()

		enc := codecs.NewFormattedJSONEncoder(f)
		err = enc.Encode(template)
	}
	return err
}

func saveGoalTemplateToDisk(template netdeploy.NetworkTemplate, filename string) error {
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		defer f.Close()

		enc := codecs.NewFormattedJSONEncoder(f)
		err = enc.Encode(template)
	}
	return err
}

func generateWalletGenesisData(wallets, npnHosts int) gen.GenesisData {
	ratZero := big.NewRat(int64(0), int64(1))
	ratHundred := big.NewRat(int64(100), int64(1))
	data := gen.DefaultGenesis
	totalWallets := wallets + npnHosts
	data.Wallets = make([]gen.WalletData, totalWallets)
	participatingNodeStake := big.NewRat(int64(100), int64(wallets))
	nonParticipatingNodeStake := ratZero
	if npnHosts > 0 {
		// split participating an non participating stake evenly
		participatingNodeStake = big.NewRat(int64(50), int64(wallets))
		nonParticipatingNodeStake = big.NewRat(int64(50), int64(npnHosts))
	}

	stake := ratZero
	stakeSum := new(big.Rat).Set(ratZero)
	for i := 0; i < totalWallets; i++ {

		if i < wallets {
			stake = participatingNodeStake
		} else {
			stake = nonParticipatingNodeStake
		}
		if i == (totalWallets - 1) {
			// use the last wallet to workaround roundoff and get back to 1.0
			stake = stake.Sub(new(big.Rat).Set(ratHundred), stakeSum)
		}
		floatStake, _ := stake.Float64()
		w := gen.WalletData{
			Name:  "Wallet" + strconv.Itoa(i+1), // Wallet names are 1-based for this template
			Stake: floatStake,
		}
		if i < wallets {
			w.Online = true
		}
		stakeSum = stakeSum.Add(stakeSum, stake)
		data.Wallets[i] = w
	}
	return data
}

func generateWalletGenesis(filename string, wallets, npnHosts int) error {
	data := generateWalletGenesisData(wallets, npnHosts)
	return saveGenesisDataToDisk(data, filename)
}

func saveGenesisDataToDisk(genesisData gen.GenesisData, filename string) error {
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		defer f.Close()

		enc := codecs.NewFormattedJSONEncoder(f)
		err = enc.Encode(genesisData)
	}
	return err
}

func generateAccountsLoadingFileTemplate(templateFilename, sourceWallet string, rounds, roundTxnCount, accountsCount, assetsCount, applicationCount uint64, balRange []string) error {

	min, err := strconv.ParseInt(balRange[0], 0, 64)
	if err != nil {
		return err
	}
	max, err := strconv.ParseInt(balRange[1], 0, 64)
	if err != nil {
		return err
	}

	var data = remote.BootstrappedNetwork{
		NumRounds:                 rounds,
		RoundTransactionsCount:    roundTxnCount,
		GeneratedAccountsCount:    accountsCount,
		GeneratedAssetsCount:      assetsCount,
		GeneratedApplicationCount: applicationCount,
		SourceWalletName:          sourceWallet,
		BalanceRange:              []int64{min, max},
	}
	return saveLoadingFileDataToDisk(data, templateFilename)
}

func saveLoadingFileDataToDisk(data remote.BootstrappedNetwork, filename string) error {
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return err
	}
	defer f.Close()

	enc := codecs.NewFormattedJSONEncoder(f)
	return enc.Encode(data)
}