Skip to content

Commit

Permalink
pingpong: add genaccountsoffset flag and tool to print test account a…
Browse files Browse the repository at this point in the history
…ddresses (#4628)
  • Loading branch information
cce authored Oct 17, 2022
1 parent a529d5d commit b29d7ff
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
17 changes: 13 additions & 4 deletions cmd/pingpong/runCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ var pidFile string
var cpuprofile string
var randSeed int64
var deterministicKeys bool
var generatedAccountsCount uint32
var generatedAccountsCount uint64
var generatedAccountsOffset uint64
var generatedAccountSampleMethod string
var configPath string

Expand Down Expand Up @@ -118,8 +119,9 @@ func init() {
runCmd.Flags().StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
runCmd.Flags().Int64Var(&randSeed, "seed", 0, "input to math/rand.Seed(), defaults to time.Now().UnixNano()")
runCmd.Flags().BoolVar(&deterministicKeys, "deterministicKeys", false, "Draw from set of netgoal-created accounts using deterministic keys")
runCmd.Flags().Uint32Var(&generatedAccountsCount, "genaccounts", 0, "The total number of accounts pre-generated by netgoal")
runCmd.Flags().StringVar(&generatedAccountSampleMethod, "gensamplemethod", "random", "The method of sampling from the total # of pre-generated accounts")
runCmd.Flags().Uint64Var(&generatedAccountsCount, "genaccounts", 0, "The total number of accounts pre-generated by netgoal")
runCmd.Flags().Uint64Var(&generatedAccountsOffset, "genaccountsoffset", 0, "The initial offset for sampling from the total # of pre-generated accounts")
runCmd.Flags().StringVar(&generatedAccountSampleMethod, "gensamplemethod", "", "The method of sampling from the total # of pre-generated accounts")
}

var runCmd = &cobra.Command{
Expand Down Expand Up @@ -378,16 +380,23 @@ var runCmd = &cobra.Command{
if !deterministicKeys && generatedAccountsCount > 0 {
reportErrorf("generatedAccountsCount requires deterministicKeys=true")
}
if deterministicKeys && numAccounts > generatedAccountsCount {
if deterministicKeys && uint64(numAccounts) > generatedAccountsCount {
reportErrorf("numAccounts must be <= generatedAccountsCount")
}
cfg.DeterministicKeys = deterministicKeys || cfg.DeterministicKeys
if generatedAccountsCount != 0 {
cfg.GeneratedAccountsCount = generatedAccountsCount
}
if generatedAccountsOffset != 0 {
cfg.GeneratedAccountsOffset = generatedAccountsOffset
}
if generatedAccountSampleMethod != "" {
cfg.GeneratedAccountSampleMethod = generatedAccountSampleMethod
}
// check if numAccounts is greater than the length of the mnemonic list, if provided
if cfg.NumPartAccounts > uint32(len(cfg.GeneratedAccountsMnemonics)) {
reportErrorf("numAccounts is greater than number of account mnemonics provided")
}

cfg.SetDefaultWeights()
err = cfg.Check()
Expand Down
20 changes: 19 additions & 1 deletion shared/pingpong/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/crypto/passphrase"
v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
algodAcct "github.com/algorand/go-algorand/data/account"
"github.com/algorand/go-algorand/data/basics"
Expand All @@ -44,14 +45,16 @@ func deterministicAccounts(initCfg PpConfig) <-chan *crypto.SignatureSecrets {
go randomDeterministicAccounts(initCfg, out)
} else if initCfg.GeneratedAccountSampleMethod == "sequential" {
go sequentialDeterministicAccounts(initCfg, out)
} else if initCfg.GeneratedAccountSampleMethod == "mnemonic" {
go mnemonicDeterministicAccounts(initCfg, out)
}
return out
}

func randomDeterministicAccounts(initCfg PpConfig, out chan *crypto.SignatureSecrets) {
numAccounts := initCfg.NumPartAccounts
totalAccounts := initCfg.GeneratedAccountsCount
if totalAccounts < numAccounts*4 {
if totalAccounts < uint64(numAccounts)*4 {
// simpler rand strategy for smaller totalAccounts
order := rand.Perm(int(totalAccounts))[:numAccounts]
for _, acct := range order {
Expand Down Expand Up @@ -86,6 +89,21 @@ func sequentialDeterministicAccounts(initCfg PpConfig, out chan *crypto.Signatur
binary.LittleEndian.PutUint64(seed[:], uint64(acct))
out <- crypto.GenerateSignatureSecrets(seed)
}
close(out)
}

func mnemonicDeterministicAccounts(initCfg PpConfig, out chan *crypto.SignatureSecrets) {
for _, mnemonic := range initCfg.GeneratedAccountsMnemonics {
seedbytes, err := passphrase.MnemonicToKey(mnemonic)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot recover key seed from mnemonic: %v\n", err)
os.Exit(1)
}
var seed crypto.Seed
copy(seed[:], seedbytes)
out <- crypto.GenerateSignatureSecrets(seed)
}
close(out)
}

// load accounts from ${ALGORAND_DATA}/${netname}-${version}/*.rootkey
Expand Down
9 changes: 6 additions & 3 deletions shared/pingpong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ type PpConfig struct {
// configuration related to using bootstrapped ledgers built by netgoal
// TODO: support generatedAssetsCount, generatedApplicationCount
DeterministicKeys bool
GeneratedAccountsCount uint32
GeneratedAccountsCount uint64
GeneratedAccountSampleMethod string
GeneratedAccountsOffset uint32
GeneratedAccountsOffset uint64
GeneratedAccountsMnemonics []string

WeightPayment float64
WeightAsset float64
Expand Down Expand Up @@ -176,6 +177,7 @@ var accountSampleMethods = []string{
"",
"random",
"sequential",
"mnemonic",
}

// Check returns an error if config is invalid.
Expand All @@ -190,8 +192,9 @@ func (cfg *PpConfig) Check() error {
if !sampleOk {
return fmt.Errorf("unknown GeneratedAccountSampleMethod: %s", cfg.GeneratedAccountSampleMethod)
}
if cfg.DeterministicKeys && (cfg.GeneratedAccountsOffset+cfg.NumPartAccounts > cfg.GeneratedAccountsCount) {
if cfg.DeterministicKeys && (cfg.GeneratedAccountsOffset+uint64(cfg.NumPartAccounts) > cfg.GeneratedAccountsCount) {
return fmt.Errorf("(GeneratedAccountsOffset %d) + (NumPartAccounts %d) > (GeneratedAccountsCount %d)", cfg.GeneratedAccountsOffset, cfg.NumPartAccounts, cfg.GeneratedAccountsCount)
}

return nil
}
45 changes: 45 additions & 0 deletions tools/debug/determaccount/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) 2019-2022 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/binary"
"flag"
"fmt"
"os"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
)

var numAccounts = flag.Uint64("numaccounts", 0, "Use this many accounts")
var offset = flag.Uint64("offset", 0, "Start at this offset")

func main() {
flag.Parse()
if *numAccounts == 0 {
flag.Usage()
os.Exit(1)
}
for i := uint64(0); i < *numAccounts; i++ {
acct := i + *offset
var seed crypto.Seed
binary.LittleEndian.PutUint64(seed[:], uint64(acct))
secrets := crypto.GenerateSignatureSecrets(seed)
fmt.Println(i, acct, basics.Address(secrets.SignatureVerifier).String())
}
}

0 comments on commit b29d7ff

Please sign in to comment.