@@ -11,40 +11,48 @@ import (
1111
1212 "github.com/ava-labs/subnet-evm/cmd/simulator/config"
1313 "github.com/ava-labs/subnet-evm/cmd/simulator/key"
14+ "github.com/ava-labs/subnet-evm/cmd/simulator/txs"
1415 "github.com/ava-labs/subnet-evm/core/types"
1516 "github.com/ava-labs/subnet-evm/ethclient"
1617 "github.com/ava-labs/subnet-evm/params"
1718 "github.com/ethereum/go-ethereum/common"
1819 ethcrypto "github.com/ethereum/go-ethereum/crypto"
1920 "github.com/ethereum/go-ethereum/log"
21+ "golang.org/x/sync/errgroup"
2022)
2123
22- // CreateLoader creates a WorkerGroup from [config] to perform the specified simulation.
23- func CreateLoader (ctx context.Context , config config.Config ) (* WorkerGroup , error ) {
24+ // ExecuteLoader creates txSequences from [config] and has txAgents execute the specified simulation.
25+ func ExecuteLoader (ctx context.Context , config config.Config ) error {
26+ if config .Timeout > 0 {
27+ var cancel context.CancelFunc
28+ ctx , cancel = context .WithTimeout (ctx , config .Timeout )
29+ defer cancel ()
30+ }
31+
2432 // Construct the arguments for the load simulator
2533 clients := make ([]ethclient.Client , 0 , len (config .Endpoints ))
2634 for i := 0 ; i < config .Workers ; i ++ {
2735 clientURI := config .Endpoints [i % len (config .Endpoints )]
2836 client , err := ethclient .Dial (clientURI )
2937 if err != nil {
30- return nil , fmt .Errorf ("failed to dial client at %s: %w" , clientURI , err )
38+ return fmt .Errorf ("failed to dial client at %s: %w" , clientURI , err )
3139 }
3240 clients = append (clients , client )
3341 }
3442
3543 keys , err := key .LoadAll (ctx , config .KeyDir )
3644 if err != nil {
37- return nil , err
45+ return err
3846 }
3947 // Ensure there are at least [config.Workers] keys and save any newly generated ones.
4048 if len (keys ) < config .Workers {
4149 for i := 0 ; len (keys ) < config .Workers ; i ++ {
4250 newKey , err := key .Generate ()
4351 if err != nil {
44- return nil , fmt .Errorf ("failed to generate %d new key: %w" , i , err )
52+ return fmt .Errorf ("failed to generate %d new key: %w" , i , err )
4553 }
4654 if err := newKey .Save (config .KeyDir ); err != nil {
47- return nil , fmt .Errorf ("failed to save %d new key: %w" , i , err )
55+ return fmt .Errorf ("failed to save %d new key: %w" , i , err )
4856 }
4957 keys = append (keys , newKey )
5058 }
@@ -57,8 +65,9 @@ func CreateLoader(ctx context.Context, config config.Config) (*WorkerGroup, erro
5765 log .Info ("Distributing funds" , "numTxsPerWorker" , config .TxsPerWorker , "minFunds" , minFundsPerAddr )
5866 keys , err = DistributeFunds (ctx , clients [0 ], keys , config .Workers , minFundsPerAddr )
5967 if err != nil {
60- return nil , err
68+ return err
6169 }
70+ log .Info ("Distributed funds successfully" )
6271
6372 pks := make ([]* ecdsa.PrivateKey , 0 , len (keys ))
6473 senders := make ([]common.Address , 0 , len (keys ))
@@ -73,10 +82,11 @@ func CreateLoader(ctx context.Context, config config.Config) (*WorkerGroup, erro
7382 client := clients [0 ]
7483 chainID , err := client .ChainID (ctx )
7584 if err != nil {
76- return nil , fmt .Errorf ("failed to fetch chainID: %w" , err )
85+ return fmt .Errorf ("failed to fetch chainID: %w" , err )
7786 }
7887 signer := types .LatestSignerForChainID (chainID )
7988
89+ log .Info ("Creating transaction sequences..." )
8090 txGenerator := func (key * ecdsa.PrivateKey , nonce uint64 ) (* types.Transaction , error ) {
8191 addr := ethcrypto .PubkeyToAddress (key .PublicKey )
8292 tx , err := types .SignNewTx (key , signer , & types.DynamicFeeTx {
@@ -94,26 +104,30 @@ func CreateLoader(ctx context.Context, config config.Config) (*WorkerGroup, erro
94104 }
95105 return tx , nil
96106 }
97- txSequences , err := GenerateTxSequences (ctx , txGenerator , clients [0 ], pks , config .TxsPerWorker )
107+ txSequences , err := txs . GenerateTxSequences (ctx , txGenerator , clients [0 ], pks , config .TxsPerWorker )
98108 if err != nil {
99- return nil , err
109+ return err
100110 }
101111
102- wg := NewWorkerGroup (clients [:config .Workers ], senders [:config .Workers ], txSequences [:config .Workers ])
103- return wg , nil
104- }
112+ log .Info ("Constructing tx agents..." , "numAgents" , config .Workers )
113+ agents := make ([]txs.Agent [* types.Transaction ], 0 , config .Workers )
114+ for i := 0 ; i < config .Workers ; i ++ {
115+ agents = append (agents , txs .NewIssueNAgent [* types.Transaction ](txSequences [i ], NewSingleAddressTxWorker (ctx , clients [i ], senders [i ]), config .BatchSize ))
116+ }
105117
106- // ExecuteLoader runs the load simulation specified by config.
107- func ExecuteLoader (ctx context.Context , config config.Config ) error {
108- if config .Timeout > 0 {
109- var cancel context.CancelFunc
110- ctx , cancel = context .WithTimeout (ctx , config .Timeout )
111- defer cancel ()
118+ log .Info ("Starting tx agents..." )
119+ eg := errgroup.Group {}
120+ for _ , agent := range agents {
121+ agent := agent
122+ eg .Go (func () error {
123+ return agent .Execute (ctx )
124+ })
112125 }
113126
114- loader , err := CreateLoader ( ctx , config )
115- if err != nil {
127+ log . Info ( "Waiting for tx agents..." )
128+ if err := eg . Wait (); err != nil {
116129 return err
117130 }
118- return loader .Execute (ctx )
131+ log .Info ("Tx agents completed successfully." )
132+ return nil
119133}
0 commit comments