Skip to content

Commit

Permalink
add AccountMnemonicList config option to loadgenerator (#4321)
Browse files Browse the repository at this point in the history
  • Loading branch information
cce authored Jul 29, 2022
1 parent f44c162 commit 3eff232
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/loadgenerator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const configFileName = "loadgenerator.config"
type config struct {
// AccountMnemonic is the mnemonic of the account from which we would like to spend Algos.
AccountMnemonic string
// AccountMnemonicList, if provided, is a series of mnemonics for accounts from which to spend Algos.
AccountMnemonicList []string
// ClientURL is the url ( such as http://127.0.0.1:8080 ) that would be used to communicate with a node REST endpoint
ClientURL *url.URL `json:"-"`
// APIToken is the API token used to communicate with the node.
Expand Down
35 changes: 25 additions & 10 deletions cmd/loadgenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,28 @@ func main() {
}
fmt.Printf("Configuration file loaded successfully.\n")

seed := loadMnemonic(cfg.AccountMnemonic)
privateKey := crypto.GenerateSignatureSecrets(seed)
publicKey := basics.Address(privateKey.SignatureVerifier)
var privateKeys []*crypto.SignatureSecrets
var publicKeys []basics.Address
addKey := func(mnemonic string) {
seed := loadMnemonic(mnemonic)
privateKeys = append(privateKeys, crypto.GenerateSignatureSecrets(seed))
publicKeys = append(publicKeys, basics.Address(privateKeys[0].SignatureVerifier))
}
if cfg.AccountMnemonic != "" { // one mnemonic provided
addKey(cfg.AccountMnemonic)
} else if len(cfg.AccountMnemonicList) > 0 {
for _, mnemonic := range cfg.AccountMnemonicList {
addKey(mnemonic)
}
} else {
fmt.Fprintf(os.Stderr, "no keys specified in config files")
}

fmt.Printf("Spending account public key : %v\n", publicKey.String())
for i, publicKey := range publicKeys {
fmt.Printf("Spending account public key %d: %v\n", i, publicKey.String())
}

err = spendLoop(cfg, privateKey, publicKey)
err = spendLoop(cfg, privateKeys, publicKeys)
if err != nil {
fmt.Fprintf(os.Stderr, "spend loop error : %v\n", err)
os.Exit(1)
Expand All @@ -92,7 +107,7 @@ func nextSpendRound(cfg config, round uint64) uint64 {
return ((round+cfg.RoundOffset)/cfg.RoundModulator)*cfg.RoundModulator + cfg.RoundModulator
}

func spendLoop(cfg config, privateKey *crypto.SignatureSecrets, publicKey basics.Address) (err error) {
func spendLoop(cfg config, privateKey []*crypto.SignatureSecrets, publicKey []basics.Address) (err error) {
restClient := client.MakeRestClient(*cfg.ClientURL, cfg.APIToken)
for {
waitForRound(restClient, cfg, true)
Expand Down Expand Up @@ -141,7 +156,7 @@ func waitForRound(restClient client.RestClient, cfg config, spendingRound bool)
}
}

func generateTransactions(restClient client.RestClient, cfg config, privateKey *crypto.SignatureSecrets, publicKey basics.Address) (queueFull bool) {
func generateTransactions(restClient client.RestClient, cfg config, privateKeys []*crypto.SignatureSecrets, publicKeys []basics.Address) (queueFull bool) {
var nodeStatus generatedV2.NodeStatusResponse
var err error
nodeStatus, err = restClient.Status()
Expand All @@ -162,7 +177,7 @@ func generateTransactions(restClient client.RestClient, cfg config, privateKey *
for i := range txns {
tx := transactions.Transaction{
Header: transactions.Header{
Sender: publicKey,
Sender: publicKeys[i%len(publicKeys)],
Fee: basics.MicroAlgos{Raw: cfg.Fee},
FirstValid: basics.Round(nodeStatus.LastRound),
LastValid: basics.Round(nodeStatus.LastRound + 2),
Expand All @@ -171,13 +186,13 @@ func generateTransactions(restClient client.RestClient, cfg config, privateKey *
GenesisHash: genesisHash,
},
PaymentTxnFields: transactions.PaymentTxnFields{
Receiver: publicKey,
Receiver: publicKeys[i%len(publicKeys)],
Amount: basics.MicroAlgos{Raw: 0},
},
Type: protocol.PaymentTx,
}
crypto.RandBytes(tx.Note[:])
txns[i] = tx.Sign(privateKey)
txns[i] = tx.Sign(privateKeys[i%len(privateKeys)])
}

// create multiple go-routines to send all these requests.
Expand Down

0 comments on commit 3eff232

Please sign in to comment.