33
44from stests import chain
55from stests .core import factory
6+ from stests .core .types .chain import Account
67from stests .core .types .chain import AccountType
8+ from stests .core .types .infra import Network
79from stests .core .utils import args_validator
810from stests .core .utils import cli as utils
911from stests .core .utils import env
5153 default = int (25e8 )
5254 )
5355
56+ # CLI argument: # transfers to dispatch.
57+ ARGS .add_argument (
58+ "--accounts" ,
59+ help = "Number of target accounts to create on the fly. If set to 0 then each target account is unique, otherwise a single target account is created." ,
60+ dest = "accounts" ,
61+ type = int ,
62+ default = 1
63+ )
64+
65+ # Map: account index <-> account.
66+ _ACCOUNTS = dict ()
67+
5468
5569def main (args ):
5670 """Entry point.
@@ -60,30 +74,55 @@ def main(args):
6074 """
6175 utils .log (f"Dispatching { args .transfers } wasm transfers:" )
6276
63- network , nodeset = get_network_nodeset_by_node (args )
77+ network , nodeset = get_network_nodeset_by_node (args )
78+ nodeset = sorted (nodeset , key = lambda n : n .index )
6479 cp1 = network .faucet
65- cp2 = factory .create_account (network .name , AccountType .OTHER , index = 2 )
6680
6781 utils .log (f"... node : { nodeset [0 ].address if len (nodeset ) == 1 else 'any' } " )
6882 utils .log (f"... amount / transfer : { args .amount } " )
6983 utils .log (f"... counter-party 1 : { cp1 .account_key } " )
70- utils .log (f"... counter-party 2 : { cp2 .account_key } " )
7184
7285 with Timer () as timer :
73- for idx in range (1 , args .transfers + 1 ):
86+ for deploy_idx in range (1 , args .transfers + 1 ):
7487 chain .set_transfer_wasm (
7588 chain .DeployDispatchInfo (cp1 , network , random .choice (nodeset )),
76- cp2 ,
89+ _get_account_for_cp2 ( network , args . accounts , deploy_idx ) ,
7790 args .amount ,
7891 verbose = False ,
7992 )
8093
8194 utils .log (f"Dispatch complete" )
95+ utils .log (f"... total transfers : { args .transfers } " )
96+ utils .log (f"... total accounts : { min (args .accounts if args .accounts != 0 else args .transfers , args .transfers )} " )
8297 utils .log (f"... total amount : { args .amount * args .transfers } " )
8398 utils .log (f"... total time : { timer .elapsed :.2f} seconds" )
8499 utils .log (f"... dispatch rate : { (args .transfers / timer .elapsed ):.2f} / second" )
85100
86101
102+ def _get_account_for_cp2 (network : Network , accounts : int , deploy_idx : int ) -> Account :
103+ """Returns counter-party 2 account.
104+
105+ """
106+ if accounts != 0 :
107+ account_idx = _get_account_idx_for_deploy (accounts , deploy_idx )
108+ if not account_idx in _ACCOUNTS :
109+ _ACCOUNTS [account_idx ] = factory .create_account (network .name , AccountType .OTHER , index = account_idx )
110+ return _ACCOUNTS [account_idx ]
111+ return factory .create_account (network .name , AccountType .OTHER , index = deploy_idx )
112+
113+
114+ def _get_account_idx_for_deploy (accounts : int , deploy_idx : int ) -> int :
115+ """Returns account index to use for a particular transfer.
116+
117+ :param accounts: Number of accounts within batch.
118+ :param deploy_idx: Index of deploy within batch.
119+ :returns: Ordinal index of account used to dispatch deploy.
120+
121+ """
122+ return deploy_idx if accounts == 0 else \
123+ deploy_idx % accounts or accounts
124+
125+
87126# Entry point.
88127if __name__ == '__main__' :
89128 main (ARGS .parse_args ())
0 commit comments