summaryrefslogtreecommitdiff
path: root/tools/block-generator/generator/config.go
blob: 55097b9ee1be5f24e5115c5e0bf3f401c1b13b7a (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
// Copyright (C) 2019-2024 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 generator

import (
	"fmt"
	"os"
	"strings"

	"gopkg.in/yaml.v3"
)

// ---- types ----

// TxTypeID is the transaction type.
type TxTypeID string

const (
	genesis TxTypeID = "genesis"

	// TX Distribution / ID's
	paymentTx     TxTypeID = "pay"
	assetTx       TxTypeID = "asset"
	applicationTx TxTypeID = "appl"
	//keyRegistrationTx TxTypeID = "keyreg"

	// Payment TX Distribution / ID's
	paymentAcctCreateTx TxTypeID = "pay_create"
	paymentPayTx        TxTypeID = "pay_pay"

	// Asset TX Distribution / ID's
	assetCreate  TxTypeID = "asset_create"
	assetDestroy TxTypeID = "asset_destroy"
	assetOptin   TxTypeID = "asset_optin"
	assetXfer    TxTypeID = "asset_xfer"
	assetClose   TxTypeID = "asset_close"

	// App kind TX Distribution / ID's don't exist because these are flattened
	// into weights across (app kinds) X (app tx type)

	// App Swap TX Distribution / ID's
	appSwapCreate TxTypeID = "app_swap_create"
	appSwapUpdate TxTypeID = "app_swap_update"
	appSwapDelete TxTypeID = "app_swap_delete"
	appSwapOptin  TxTypeID = "app_swap_optin"
	appSwapCall   TxTypeID = "app_swap_call"
	appSwapClose  TxTypeID = "app_swap_close"
	appSwapClear  TxTypeID = "app_swap_clear"

	// App Boxes TX Distribution / ID's
	appBoxesCreate TxTypeID = "app_boxes_create"
	appBoxesUpdate TxTypeID = "app_boxes_update"
	appBoxesDelete TxTypeID = "app_boxes_delete"
	appBoxesOptin  TxTypeID = "app_boxes_optin"
	appBoxesCall   TxTypeID = "app_boxes_call"
	appBoxesClose  TxTypeID = "app_boxes_close"
	appBoxesClear  TxTypeID = "app_boxes_clear"

	// For reporting side-effects of higher level transactions
	effectPaymentTxSibling = "effect_payment_sibling"
	effectInnerTx          = "effect_inner_tx"

	// Defaults
	defaultGenesisAccountsCount         uint64 = 1000
	defaultGenesisAccountInitialBalance uint64 = 1_000_000_000000 // 1 million algos per account

	assetTotal uint64 = 100_000_000_000_000_000 // 100 billion units per asset

	consensusTimeMilli int64 = 3300
)

type appKind uint8

const (
	appKindSwap appKind = iota
	appKindBoxes
)

func (a appKind) String() string {
	switch a {
	case appKindSwap:
		return "swap"
	case appKindBoxes:
		return "boxes"
	default:
		// Return a default value for unknown kinds.
		return "Unknown"
	}
}

type appTxType uint8

const (
	appTxTypeCreate appTxType = iota
	appTxTypeUpdate
	appTxTypeDelete
	appTxTypeOptin
	appTxTypeCall
	appTxTypeClose
	appTxTypeClear
)

func (a appTxType) String() string {
	switch a {
	case appTxTypeCreate:
		return "create"
	case appTxTypeUpdate:
		return "update"
	case appTxTypeDelete:
		return "delete"
	case appTxTypeOptin:
		return "optin"
	case appTxTypeCall:
		return "call"
	case appTxTypeClose:
		return "close"
	case appTxTypeClear:
		return "clear"
	default:
		// Return a default value for unknown types.
		return "Unknown"
	}
}

func parseAppTxType(txType TxTypeID) (isApp bool, kind appKind, tx appTxType, err error) {
	parts := strings.Split(string(txType), "_")

	if len(parts) != 3 {
		err = fmt.Errorf("invalid tx type for parsing")
		return
	}

	if len(parts) > 1 && strings.HasPrefix(parts[0], "app") {
		isApp = true
		// Setting the app kind
		switch parts[1] {
		case "swap":
			kind = appKindSwap
		case "boxes":
			kind = appKindBoxes
		default:
			err = fmt.Errorf("invalid app kind")
			return
		}

		switch parts[2] {
		case "create":
			tx = appTxTypeCreate
		case "update":
			tx = appTxTypeUpdate
		case "delete":
			tx = appTxTypeDelete
		case "optin":
			tx = appTxTypeOptin
		case "call":
			tx = appTxTypeCall
		case "close":
			tx = appTxTypeClose
		case "clear":
			tx = appTxTypeClear
		default:
			err = fmt.Errorf("invalid app tx type")
			return
		}
	} else {
		err = fmt.Errorf("not an app type")
		return
	}

	return
}

func getAppTxType(kind appKind, appType appTxType) TxTypeID {
	return TxTypeID(fmt.Sprintf("app_%s_%s", kind, appType))
}

// GenerationConfig defines the tunable parameters for block generation.
type GenerationConfig struct {
	Name                         string `yaml:"name"`
	NumGenesisAccounts           uint64 `yaml:"genesis_accounts"`
	GenesisAccountInitialBalance uint64 `yaml:"genesis_account_balance"`

	// Block generation
	TxnPerBlock uint64 `yaml:"tx_per_block"`

	// TX Distribution
	PaymentTransactionFraction float32 `yaml:"tx_pay_fraction"`
	AssetTransactionFraction   float32 `yaml:"tx_asset_fraction"`
	AppTransactionFraction     float32 `yaml:"tx_app_fraction"`

	// Payment TX Distribution
	PaymentNewAccountFraction float32 `yaml:"pay_acct_create_fraction"`
	PaymentFraction           float32 `yaml:"pay_xfer_fraction"`

	// Asset TX Distribution
	AssetCreateFraction  float32 `yaml:"asset_create_fraction"`
	AssetDestroyFraction float32 `yaml:"asset_destroy_fraction"`
	AssetOptinFraction   float32 `yaml:"asset_optin_fraction"`
	AssetXferFraction    float32 `yaml:"asset_xfer_fraction"`
	AssetCloseFraction   float32 `yaml:"asset_close_fraction"`

	// App kind TX Distribution
	AppSwapFraction  float32 `yaml:"app_swap_fraction"`
	AppBoxesFraction float32 `yaml:"app_boxes_fraction"`

	// App Swap TX Distribution
	AppSwapCreateFraction float32 `yaml:"app_swap_create_fraction"`
	AppSwapUpdateFraction float32 `yaml:"app_swap_update_fraction"`
	AppSwapDeleteFraction float32 `yaml:"app_swap_delete_fraction"`
	AppSwapOptinFraction  float32 `yaml:"app_swap_optin_fraction"`
	AppSwapCallFraction   float32 `yaml:"app_swap_call_fraction"`
	AppSwapCloseFraction  float32 `yaml:"app_swap_close_fraction"`
	AppSwapClearFraction  float32 `yaml:"app_swap_clear_fraction"`

	// App Boxes TX Distribution
	AppBoxesCreateFraction float32 `yaml:"app_boxes_create_fraction"`
	AppBoxesUpdateFraction float32 `yaml:"app_boxes_update_fraction"`
	AppBoxesDeleteFraction float32 `yaml:"app_boxes_delete_fraction"`
	AppBoxesOptinFraction  float32 `yaml:"app_boxes_optin_fraction"`
	AppBoxesCallFraction   float32 `yaml:"app_boxes_call_fraction"`
	AppBoxesCloseFraction  float32 `yaml:"app_boxes_close_fraction"`
	AppBoxesClearFraction  float32 `yaml:"app_boxes_clear_fraction"`
}

// ---- construction and validation ----

// initializeConfigFile reads the config file and validates its parameters. Certain missing
// parameters are defaulted to a reasonable value when missing, or when an entire associated
// group is missing.
func initializeConfigFile(configFile string) (config GenerationConfig, err error) {
	var data []byte
	data, err = os.ReadFile(configFile)
	if err != nil {
		return
	}
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		return
	}

	err = config.validateWithDefaults(true)
	return
}

// validateWithDefaults validates the config parameters. When defaults is true
// certain missing parameters are defaulted to reasonable values.
// When defaults is false, validate only without attempting to set defaults.
func (cfg *GenerationConfig) validateWithDefaults(defaults bool) error {
	if cfg.Name == "" {
		return fmt.Errorf("scenario name must be set")
	}

	if cfg.NumGenesisAccounts == 0 {
		if defaults {
			cfg.NumGenesisAccounts = defaultGenesisAccountsCount
		} else {
			return fmt.Errorf("number of genesis accounts must be > 0")
		}
	}

	if cfg.GenesisAccountInitialBalance == 0 {
		if defaults {
			cfg.GenesisAccountInitialBalance = defaultGenesisAccountInitialBalance
		} else {
			return fmt.Errorf("genesis account initial balance must be > 0")
		}
	}

	var weights []*float32

	weights = []*float32{&cfg.PaymentTransactionFraction, &cfg.AssetTransactionFraction, &cfg.AppTransactionFraction}
	if eTxnTypes := sumIsCloseToOneWithDefault(defaults, weights...); eTxnTypes != nil {
		return fmt.Errorf("transaction distribution ratios sum should equal 1: %w", eTxnTypes)
	}

	weights = []*float32{&cfg.PaymentNewAccountFraction, &cfg.PaymentFraction}
	if ePymtTypes := sumIsCloseToOneWithDefault(defaults, weights...); ePymtTypes != nil {
		return fmt.Errorf("payment configuration ratios sum should equal 1: %w", ePymtTypes)
	}

	weights = []*float32{&cfg.AssetCreateFraction, &cfg.AssetDestroyFraction, &cfg.AssetOptinFraction, &cfg.AssetCloseFraction, &cfg.AssetXferFraction}
	if eAssetTypes := sumIsCloseToOneWithDefault(defaults, weights...); eAssetTypes != nil {
		return fmt.Errorf("asset configuration ratios sum should equal 1: %w", eAssetTypes)
	}

	weights = []*float32{&cfg.AppSwapFraction, &cfg.AppBoxesFraction}
	if eAppTypes := sumIsCloseToOneWithDefault(defaults, weights...); eAppTypes != nil {
		return fmt.Errorf("app configuration ratios sum should equal 1: %w", eAppTypes)
	}

	weights = []*float32{&cfg.AppSwapCreateFraction, &cfg.AppSwapUpdateFraction, &cfg.AppSwapDeleteFraction, &cfg.AppSwapOptinFraction, &cfg.AppSwapCallFraction, &cfg.AppSwapCloseFraction, &cfg.AppSwapClearFraction}
	if eAppSwapTypes := sumIsCloseToOneWithDefault(defaults, weights...); eAppSwapTypes != nil {
		return fmt.Errorf("app swap configuration ratios sum should equal 1: %w", eAppSwapTypes)
	}

	weights = []*float32{&cfg.AppBoxesCreateFraction, &cfg.AppBoxesUpdateFraction, &cfg.AppBoxesDeleteFraction, &cfg.AppBoxesOptinFraction, &cfg.AppBoxesCallFraction, &cfg.AppBoxesCloseFraction, &cfg.AppBoxesClearFraction}
	if eAppBoxesTypes := sumIsCloseToOneWithDefault(defaults, weights...); eAppBoxesTypes != nil {
		return fmt.Errorf("app boxes configuration ratios sum should equal 1: %w", eAppBoxesTypes)

	}

	return nil
}

func asPtrSlice(weights []float32) []*float32 {
	ptrs := make([]*float32, len(weights))
	for i := range weights {
		weight := weights[i]
		ptrs[i] = &weight
	}
	return ptrs
}

// sumIsCloseToOneWithDefault returns no error if the sum of the params is close to 1.
// It returns an error if any of the params are negative.
// Finally, in the case that all the params are zero, it sets the first param to 1 and returns no error.
func sumIsCloseToOneWithDefault(defaults bool, params ...*float32) error {
	if len(params) == 0 {
		return fmt.Errorf("no params provided")
	}

	sum, valid, err := validateSumCloseToOne(params)
	if valid || err != nil {
		return err
	}

	if sum == 0 && defaults {
		*params[0] = 1
		return nil
	}

	return fmt.Errorf("sum of params is not close to 1: %f", sum)
}

// validateSumCloseToOne returns the sum of the params, whether the sum is close to 1, and any error encountered.
// In the case that err is not nil, the value of valid is undefined.
func validateSumCloseToOne(params []*float32) (sum float32, valid bool, err error) {
	for i, num := range params {
		if *num < 0 {
			return *num, false, fmt.Errorf("param at index %d is negative: %f", i, *num)
		}
		sum += *num
	}
	if 0.99 < sum && sum < 1.01 {
		return sum, true, nil
	}
	return sum, false, nil
}