@@ -18,95 +18,58 @@ package pingpong
1818
1919import (
2020 "fmt"
21- "io/ioutil"
2221 "math"
2322 "math/rand"
2423 "os"
25- "path/filepath"
2624 "sort"
2725 "strings"
2826 "time"
2927
3028 "github.com/algorand/go-algorand/config"
3129 "github.com/algorand/go-algorand/crypto"
3230 v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
33- algodAcct "github.com/algorand/go-algorand/data/account"
3431 "github.com/algorand/go-algorand/data/basics"
3532 "github.com/algorand/go-algorand/data/transactions"
3633 "github.com/algorand/go-algorand/data/transactions/logic"
3734 "github.com/algorand/go-algorand/libgoal"
3835 "github.com/algorand/go-algorand/protocol"
39- "github.com/algorand/go-algorand/util/db"
4036)
4137
42- func ( pps * WorkerState ) ensureAccounts (ac libgoal.Client , initCfg PpConfig ) (accounts map [string ]* pingPongAccount , cfg PpConfig , err error ) {
43- accounts = make (map [string ]* pingPongAccount )
38+ func ensureAccounts (ac libgoal.Client , initCfg PpConfig ) (accounts map [string ]uint64 , cfg PpConfig , err error ) {
39+ accounts = make (map [string ]uint64 )
4440 cfg = initCfg
4541
46- genID , err2 := ac .GenesisID ()
47- if err2 != nil {
48- err = err2
49- return
50- }
51- genesisDir := filepath .Join (ac .DataDir (), genID )
52- files , err2 := ioutil .ReadDir (genesisDir )
53- if err2 != nil {
54- err = err2
55- return
56- }
42+ wallet , err := ac .GetUnencryptedWalletHandle ()
5743
5844 var srcAcctPresent bool
5945 var richestAccount string
6046 var richestBalance uint64
6147
62- for _ , info := range files {
63- var handle db.Accessor
48+ addresses , err := ac .ListAddresses (wallet )
6449
65- // If it can't be a participation key database, skip it
66- if ! config .IsRootKeyFilename (info .Name ()) {
67- continue
68- }
69-
70- // Fetch a handle to this database
71- handle , err = db .MakeErasableAccessor (filepath .Join (genesisDir , info .Name ()))
72- if err != nil {
73- // Couldn't open it, skip it
74- continue
75- }
76-
77- // Fetch an account.Participation from the database
78- root , err := algodAcct .RestoreRoot (handle )
79- handle .Close ()
80- if err != nil {
81- // Couldn't read it, skip it
82- continue
83- }
84-
85- publicKey := root .Secrets ().SignatureVerifier
86- accountAddress := basics .Address (publicKey )
50+ if err != nil {
51+ return nil , PpConfig {}, err
52+ }
8753
88- if accountAddress .String () == cfg .SrcAccount {
54+ // find either srcAccount or the richest account
55+ for _ , addr := range addresses {
56+ if addr == cfg .SrcAccount {
8957 srcAcctPresent = true
9058 }
9159
92- amt , err := ac .GetBalance (accountAddress . String () )
60+ amount , err := ac .GetBalance (addr )
9361 if err != nil {
9462 return nil , PpConfig {}, err
9563 }
9664
65+ amt := amount
9766 if ! srcAcctPresent && amt > richestBalance {
98- richestAccount = accountAddress . String ()
67+ richestAccount = addr
9968 richestBalance = amt
10069 }
101-
70+ accounts [ addr ] = amt
10271 if ! initCfg .Quiet {
103- fmt .Printf ("Found local account: %s -> %v\n " , accountAddress .String (), amt )
104- }
105-
106- accounts [accountAddress .String ()] = & pingPongAccount {
107- balance : amt ,
108- sk : root .Secrets (),
109- pk : accountAddress ,
72+ fmt .Printf ("Found local account: %s -> %v\n " , addr , amt )
11073 }
11174 }
11275
@@ -141,7 +104,7 @@ func (pps *WorkerState) ensureAccounts(ac libgoal.Client, initCfg PpConfig) (acc
141104 if len (accounts ) != int (cfg .NumPartAccounts + 1 ) {
142105 fmt .Printf ("Not enough accounts - creating %d more\n " , int (cfg .NumPartAccounts + 1 )- len (accounts ))
143106 }
144- accounts , err = generateAccounts (ac , accounts , cfg .NumPartAccounts )
107+ accounts , err = generateAccounts (ac , accounts , cfg .NumPartAccounts , wallet )
145108 if err != nil {
146109 return
147110 }
@@ -166,7 +129,7 @@ func throttleTransactionRate(startTime time.Time, cfg PpConfig, totalSent uint64
166129// Step 1) Create X assets for each of the participant accounts
167130// Step 2) For each participant account, opt-in to assets of all other participant accounts
168131// Step 3) Evenly distribute the assets across all participant accounts
169- func (pps * WorkerState ) prepareAssets (assetAccounts map [string ]* pingPongAccount , client libgoal.Client ) (resultAssetMaps map [uint64 ]v1.AssetParams , optIns map [uint64 ][]string , err error ) {
132+ func (pps * WorkerState ) prepareAssets (assetAccounts map [string ]uint64 , client libgoal.Client ) (resultAssetMaps map [uint64 ]v1.AssetParams , optIns map [uint64 ][]string , err error ) {
170133 accounts := assetAccounts
171134 cfg := pps .cfg
172135 proto , err := getProto (client )
@@ -391,14 +354,14 @@ func (pps *WorkerState) prepareAssets(assetAccounts map[string]*pingPongAccount,
391354 fmt .Printf ("Distributing assets from %v to %v \n " , creator , addr )
392355 }
393356
394- tx , signer , sendErr := pps .constructTxn (creator , addr , cfg .MaxFee , assetAmt , k , client )
357+ tx , sendErr := pps .constructTxn (creator , addr , cfg .MaxFee , assetAmt , k , client )
395358 if sendErr != nil {
396359 fmt .Printf ("Cannot transfer asset %v from account %v\n " , k , creator )
397360 err = sendErr
398361 return
399362 }
400363
401- _ , err = signAndBroadcastTransaction (accounts , signer , tx , client , cfg )
364+ _ , err = signAndBroadcastTransaction (accounts , creator , tx , client , cfg )
402365 if err != nil {
403366 _ , _ = fmt .Fprintf (os .Stderr , "signing and broadcasting asset distribution failed with error %v\n " , err )
404367 return
@@ -445,9 +408,9 @@ func (pps *WorkerState) prepareAssets(assetAccounts map[string]*pingPongAccount,
445408 return
446409}
447410
448- func signAndBroadcastTransaction (accounts map [string ]* pingPongAccount , sender string , tx transactions.Transaction , client libgoal.Client , cfg PpConfig ) (txID string , err error ) {
411+ func signAndBroadcastTransaction (accounts map [string ]uint64 , sender string , tx transactions.Transaction , client libgoal.Client , cfg PpConfig ) (txID string , err error ) {
449412 var signedTx transactions.SignedTxn
450- signedTx , err = signTxn (sender , tx , accounts , cfg )
413+ signedTx , err = signTxn (sender , tx , client , cfg )
451414 if err != nil {
452415 fmt .Printf ("Cannot sign trx %+v with account %v\n error %v\n " , tx , sender , err )
453416 return
@@ -460,7 +423,7 @@ func signAndBroadcastTransaction(accounts map[string]*pingPongAccount, sender st
460423 if ! cfg .Quiet {
461424 fmt .Printf ("Broadcast transaction %v\n " , txID )
462425 }
463- accounts [sender ]. balance -= tx .Fee .Raw
426+ accounts [sender ] -= tx .Fee .Raw
464427
465428 return
466429}
@@ -622,7 +585,7 @@ func genAppProgram(numOps uint32, numHashes uint32, hashSize string, numGlobalKe
622585 return ops .Program , progAsm
623586}
624587
625- func ( pps * WorkerState ) sendAsGroup (txgroup []transactions.Transaction , client libgoal.Client , senders []string ) (err error ) {
588+ func sendAsGroup (txgroup []transactions.Transaction , client libgoal.Client , h []byte ) (err error ) {
626589 if len (txgroup ) == 0 {
627590 err = fmt .Errorf ("sendAsGroup: empty group" )
628591 return
@@ -633,15 +596,9 @@ func (pps *WorkerState) sendAsGroup(txgroup []transactions.Transaction, client l
633596 return
634597 }
635598 var stxgroup []transactions.SignedTxn
636- for i , txn := range txgroup {
599+ for _ , txn := range txgroup {
637600 txn .Group = gid
638- //signedTxn, signErr := client.SignTransactionWithWallet(h, nil, txn)
639- signedTxn , signErr := signTxn (senders [i ], txn , pps .accounts , pps .cfg )
640- if err != nil {
641- fmt .Printf ("Cannot sign trx %+v with account %v\n error %v\n " , txn , senders [i ], err )
642- return
643- }
644-
601+ signedTxn , signErr := client .SignTransactionWithWallet (h , nil , txn )
645602 if signErr != nil {
646603 fmt .Printf ("Cannot sign app creation txn\n " )
647604 err = signErr
@@ -678,7 +635,7 @@ func getProto(client libgoal.Client) (config.ConsensusParams, error) {
678635 return * proto , nil
679636}
680637
681- func ( pps * WorkerState ) prepareApps (accounts map [string ]* pingPongAccount , client libgoal.Client , cfg PpConfig ) (appParams map [uint64 ]v1.AppParams , optIns map [uint64 ][]string , err error ) {
638+ func prepareApps (accounts map [string ]uint64 , client libgoal.Client , cfg PpConfig ) (appParams map [uint64 ]v1.AppParams , optIns map [uint64 ][]string , err error ) {
682639 proto , err := getProto (client )
683640 if err != nil {
684641 return
@@ -729,6 +686,13 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
729686 }
730687 }
731688
689+ // Get wallet handle token
690+ var h []byte
691+ h , err = client .GetUnencryptedWalletHandle ()
692+ if err != nil {
693+ return
694+ }
695+
732696 // create apps
733697 for idx , appAccount := range appAccounts {
734698 begin := idx * appsPerAcct
@@ -737,7 +701,6 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
737701 end = toCreate
738702 }
739703 var txgroup []transactions.Transaction
740- var senders []string
741704 for i := begin ; i < end ; i ++ {
742705 var tx transactions.Transaction
743706
@@ -767,11 +730,10 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
767730 tx .Note = note [:]
768731
769732 txgroup = append (txgroup , tx )
770- accounts [appAccount .Address ].balance -= tx .Fee .Raw
771- senders = append (senders , appAccount .Address )
733+ accounts [appAccount .Address ] -= tx .Fee .Raw
772734 }
773735
774- err = pps . sendAsGroup (txgroup , client , senders )
736+ err = sendAsGroup (txgroup , client , h )
775737 if err != nil {
776738 return
777739 }
@@ -814,7 +776,6 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
814776 optIns = make (map [uint64 ][]string )
815777 for addr := range accounts {
816778 var txgroup []transactions.Transaction
817- var senders []string
818779 permAppIndices := rand .Perm (len (aidxs ))
819780 for i := uint32 (0 ); i < cfg .NumAppOptIn ; i ++ {
820781 j := permAppIndices [i ]
@@ -840,9 +801,8 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
840801 optIns [aidx ] = append (optIns [aidx ], addr )
841802
842803 txgroup = append (txgroup , tx )
843- senders = append (senders , addr )
844804 if len (txgroup ) == groupSize {
845- err = pps . sendAsGroup (txgroup , client , senders )
805+ err = sendAsGroup (txgroup , client , h )
846806 if err != nil {
847807 return
848808 }
@@ -851,7 +811,7 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
851811 }
852812 // broadcast leftovers
853813 if len (txgroup ) > 0 {
854- err = pps . sendAsGroup (txgroup , client , senders )
814+ err = sendAsGroup (txgroup , client , h )
855815 if err != nil {
856816 return
857817 }
@@ -862,7 +822,7 @@ func (pps *WorkerState) prepareApps(accounts map[string]*pingPongAccount, client
862822 return
863823}
864824
865- func takeTopAccounts (allAccounts map [string ]* pingPongAccount , numAccounts uint32 , srcAccount string ) (accounts map [string ]* pingPongAccount ) {
825+ func takeTopAccounts (allAccounts map [string ]uint64 , numAccounts uint32 , srcAccount string ) (accounts map [string ]uint64 ) {
866826 allAddrs := make ([]string , len (allAccounts ))
867827 var i int
868828 for addr := range allAccounts {
@@ -873,12 +833,12 @@ func takeTopAccounts(allAccounts map[string]*pingPongAccount, numAccounts uint32
873833 sort .SliceStable (allAddrs , func (i , j int ) bool {
874834 amt1 := allAccounts [allAddrs [i ]]
875835 amt2 := allAccounts [allAddrs [j ]]
876- return amt1 . balance > amt2 . balance
836+ return amt1 > amt2
877837 })
878838
879839 // Now populate a new map with just the accounts needed
880840 accountsRequired := int (numAccounts + 1 ) // Participating and Src
881- accounts = make (map [string ]* pingPongAccount )
841+ accounts = make (map [string ]uint64 )
882842 accounts [srcAccount ] = allAccounts [srcAccount ]
883843 for _ , addr := range allAddrs {
884844 accounts [addr ] = allAccounts [addr ]
@@ -889,23 +849,18 @@ func takeTopAccounts(allAccounts map[string]*pingPongAccount, numAccounts uint32
889849 return
890850}
891851
892- func generateAccounts (client libgoal.Client , allAccounts map [string ]* pingPongAccount , numAccounts uint32 ) (map [string ]* pingPongAccount , error ) {
852+ func generateAccounts (client libgoal.Client , allAccounts map [string ]uint64 , numAccounts uint32 , wallet [] byte ) (map [string ]uint64 , error ) {
893853 // Compute the number of accounts to generate
894854 accountsRequired := int (numAccounts + 1 ) - len (allAccounts )
895855
896- var seed crypto.Seed
897-
898856 for accountsRequired > 0 {
899857 accountsRequired --
900-
901- crypto .RandBytes (seed [:])
902- privateKey := crypto .GenerateSignatureSecrets (seed )
903- publicKey := basics .Address (privateKey .SignatureVerifier )
904-
905- allAccounts [publicKey .String ()] = & pingPongAccount {
906- sk : privateKey ,
907- pk : publicKey ,
858+ addr , err := client .GenerateAddress (wallet )
859+ if err != nil {
860+ return allAccounts , err
908861 }
862+
863+ allAccounts [addr ] = 0
909864 }
910865
911866 return allAccounts , nil
0 commit comments