7
7
input_validators:: { is_amount, is_parsable, is_valid_pubkey} ,
8
8
} ,
9
9
solana_client:: {
10
- rpc_client:: RpcClient ,
11
- tpu_client:: { TpuClient , TpuClientConfig } ,
10
+ nonblocking :: rpc_client:: RpcClient , rpc_client :: RpcClient as BlockingRpcClient ,
11
+ tpu_client:: TpuClient , tpu_client :: TpuClientConfig ,
12
12
} ,
13
13
solana_remote_wallet:: remote_wallet:: RemoteWalletManager ,
14
14
solana_sdk:: {
15
15
message:: Message , native_token:: Sol , program_pack:: Pack , pubkey:: Pubkey , signature:: Signer ,
16
16
system_instruction,
17
17
} ,
18
18
spl_associated_token_account:: * ,
19
+ spl_token:: {
20
+ instruction,
21
+ state:: { Account , Mint } ,
22
+ } ,
19
23
std:: { sync:: Arc , time:: Instant } ,
20
24
} ;
21
25
@@ -160,9 +164,9 @@ impl BenchSubCommand for App<'_, '_> {
160
164
}
161
165
}
162
166
163
- pub ( crate ) fn bench_process_command (
167
+ pub ( crate ) async fn bench_process_command (
164
168
matches : & ArgMatches < ' _ > ,
165
- config : & Config ,
169
+ config : & Config < ' _ > ,
166
170
mut signers : Vec < Box < dyn Signer > > ,
167
171
wallet_manager : & mut Option < Arc < RemoteWalletManager > > ,
168
172
) -> CommandResult {
@@ -179,7 +183,7 @@ pub(crate) fn bench_process_command(
179
183
config. signer_or_default ( arg_matches, "owner" , wallet_manager) ;
180
184
signers. push ( owner_signer) ;
181
185
182
- command_create_accounts ( config, signers, & token, n, & owner) ?;
186
+ command_create_accounts ( config, signers, & token, n, & owner) . await ?;
183
187
}
184
188
( "close-accounts" , Some ( arg_matches) ) => {
185
189
let token = pubkey_of_signer ( arg_matches, "token" , wallet_manager)
@@ -190,7 +194,7 @@ pub(crate) fn bench_process_command(
190
194
config. signer_or_default ( arg_matches, "owner" , wallet_manager) ;
191
195
signers. push ( owner_signer) ;
192
196
193
- command_close_accounts ( config, signers, & token, n, & owner) ?;
197
+ command_close_accounts ( config, signers, & token, n, & owner) . await ?;
194
198
}
195
199
( "deposit-into" , Some ( arg_matches) ) => {
196
200
let token = pubkey_of_signer ( arg_matches, "token" , wallet_manager)
@@ -209,7 +213,8 @@ pub(crate) fn bench_process_command(
209
213
210
214
command_deposit_into_or_withdraw_from (
211
215
config, signers, & token, n, & owner, ui_amount, & from, true ,
212
- ) ?;
216
+ )
217
+ . await ?;
213
218
}
214
219
( "withdraw-from" , Some ( arg_matches) ) => {
215
220
let token = pubkey_of_signer ( arg_matches, "token" , wallet_manager)
@@ -228,7 +233,8 @@ pub(crate) fn bench_process_command(
228
233
229
234
command_deposit_into_or_withdraw_from (
230
235
config, signers, & token, n, & owner, ui_amount, & to, false ,
231
- ) ?;
236
+ )
237
+ . await ?;
232
238
}
233
239
_ => unreachable ! ( ) ,
234
240
}
@@ -260,18 +266,19 @@ fn get_token_addresses_with_seed(
260
266
. collect ( )
261
267
}
262
268
263
- fn is_valid_token ( rpc_client : & RpcClient , token : & Pubkey ) -> Result < ( ) , Error > {
264
- let mint_account_data = rpc_client
269
+ async fn is_valid_token ( rpc_client : & RpcClient , token : & Pubkey ) -> Result < ( ) , Error > {
270
+ let mint_account = rpc_client
265
271
. get_account_data ( token)
272
+ . await
266
273
. map_err ( |err| format ! ( "Token mint {} does not exist: {}" , token, err) ) ?;
267
274
268
- spl_token :: state :: Mint :: unpack ( & mint_account_data )
275
+ Mint :: unpack ( & mint_account )
269
276
. map ( |_| ( ) )
270
277
. map_err ( |err| format ! ( "Invalid token mint {}: {}" , token, err) . into ( ) )
271
278
}
272
279
273
- fn command_create_accounts (
274
- config : & Config ,
280
+ async fn command_create_accounts (
281
+ config : & Config < ' _ > ,
275
282
signers : Vec < Box < dyn Signer > > ,
276
283
token : & Pubkey ,
277
284
n : usize ,
@@ -280,10 +287,11 @@ fn command_create_accounts(
280
287
let rpc_client = & config. rpc_client ;
281
288
282
289
println ! ( "Scanning accounts..." ) ;
283
- is_valid_token ( rpc_client, token) ?;
290
+ is_valid_token ( rpc_client, token) . await ?;
284
291
285
292
let minimum_balance_for_rent_exemption = rpc_client
286
- . get_minimum_balance_for_rent_exemption ( spl_token:: state:: Account :: get_packed_len ( ) ) ?;
293
+ . get_minimum_balance_for_rent_exemption ( Account :: get_packed_len ( ) )
294
+ . await ?;
287
295
288
296
let mut lamports_required = 0 ;
289
297
@@ -292,7 +300,8 @@ fn command_create_accounts(
292
300
let mut messages = vec ! [ ] ;
293
301
for address_chunk in token_addresses_with_seed. chunks ( 100 ) {
294
302
let accounts_chunk = rpc_client
295
- . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) ) ?;
303
+ . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) )
304
+ . await ?;
296
305
297
306
for ( account, ( address, seed) ) in accounts_chunk. iter ( ) . zip ( address_chunk) {
298
307
if account. is_none ( ) {
@@ -305,27 +314,22 @@ fn command_create_accounts(
305
314
owner,
306
315
seed,
307
316
minimum_balance_for_rent_exemption,
308
- spl_token :: state :: Account :: get_packed_len ( ) as u64 ,
317
+ Account :: get_packed_len ( ) as u64 ,
309
318
& config. program_id ,
310
319
) ,
311
- spl_token:: instruction:: initialize_account (
312
- & config. program_id ,
313
- address,
314
- token,
315
- owner,
316
- ) ?,
320
+ instruction:: initialize_account ( & config. program_id , address, token, owner) ?,
317
321
] ,
318
322
Some ( & config. fee_payer ) ,
319
323
) ) ;
320
324
}
321
325
}
322
326
}
323
327
324
- send_messages ( config, & messages, lamports_required, signers)
328
+ send_messages ( config, & messages, lamports_required, signers) . await
325
329
}
326
330
327
- fn command_close_accounts (
328
- config : & Config ,
331
+ async fn command_close_accounts (
332
+ config : & Config < ' _ > ,
329
333
signers : Vec < Box < dyn Signer > > ,
330
334
token : & Pubkey ,
331
335
n : usize ,
@@ -334,18 +338,19 @@ fn command_close_accounts(
334
338
let rpc_client = & config. rpc_client ;
335
339
336
340
println ! ( "Scanning accounts..." ) ;
337
- is_valid_token ( rpc_client, token) ?;
341
+ is_valid_token ( rpc_client, token) . await ?;
338
342
339
343
let token_addresses_with_seed =
340
344
get_token_addresses_with_seed ( & config. program_id , token, owner, n) ;
341
345
let mut messages = vec ! [ ] ;
342
346
for address_chunk in token_addresses_with_seed. chunks ( 100 ) {
343
347
let accounts_chunk = rpc_client
344
- . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) ) ?;
348
+ . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) )
349
+ . await ?;
345
350
346
351
for ( account, ( address, _seed) ) in accounts_chunk. iter ( ) . zip ( address_chunk) {
347
352
if let Some ( account) = account {
348
- match spl_token :: state :: Account :: unpack ( & account. data ) {
353
+ match Account :: unpack ( & account. data ) {
349
354
Ok ( token_account) => {
350
355
if token_account. amount != 0 {
351
356
eprintln ! (
@@ -354,7 +359,7 @@ fn command_close_accounts(
354
359
) ;
355
360
} else {
356
361
messages. push ( Message :: new (
357
- & [ spl_token :: instruction:: close_account (
362
+ & [ instruction:: close_account (
358
363
& config. program_id ,
359
364
address,
360
365
owner,
@@ -373,12 +378,12 @@ fn command_close_accounts(
373
378
}
374
379
}
375
380
376
- send_messages ( config, & messages, 0 , signers)
381
+ send_messages ( config, & messages, 0 , signers) . await
377
382
}
378
383
379
384
#[ allow( clippy:: too_many_arguments) ]
380
- fn command_deposit_into_or_withdraw_from (
381
- config : & Config ,
385
+ async fn command_deposit_into_or_withdraw_from (
386
+ config : & Config < ' _ > ,
382
387
signers : Vec < Box < dyn Signer > > ,
383
388
token : & Pubkey ,
384
389
n : usize ,
@@ -390,9 +395,10 @@ fn command_deposit_into_or_withdraw_from(
390
395
let rpc_client = & config. rpc_client ;
391
396
392
397
println ! ( "Scanning accounts..." ) ;
393
- is_valid_token ( rpc_client, token) ?;
398
+ is_valid_token ( rpc_client, token) . await ?;
394
399
395
- let ( mint_pubkey, decimals) = crate :: resolve_mint_info ( config, from_or_to, Some ( * token) , None ) ?;
400
+ let ( mint_pubkey, decimals) =
401
+ crate :: resolve_mint_info ( config, from_or_to, Some ( * token) , None ) . await ?;
396
402
if mint_pubkey != * token {
397
403
return Err ( format ! ( "Source account {} is not a {} token" , from_or_to, token) . into ( ) ) ;
398
404
}
@@ -403,12 +409,13 @@ fn command_deposit_into_or_withdraw_from(
403
409
let mut messages = vec ! [ ] ;
404
410
for address_chunk in token_addresses_with_seed. chunks ( 100 ) {
405
411
let accounts_chunk = rpc_client
406
- . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) ) ?;
412
+ . get_multiple_accounts ( & address_chunk. iter ( ) . map ( |x| x. 0 ) . collect :: < Vec < _ > > ( ) )
413
+ . await ?;
407
414
408
415
for ( account, ( address, _seed) ) in accounts_chunk. iter ( ) . zip ( address_chunk) {
409
416
if account. is_some ( ) {
410
417
messages. push ( Message :: new (
411
- & [ spl_token :: instruction:: transfer_checked (
418
+ & [ instruction:: transfer_checked (
412
419
& config. program_id ,
413
420
if deposit_into { from_or_to } else { address } ,
414
421
token,
@@ -426,11 +433,11 @@ fn command_deposit_into_or_withdraw_from(
426
433
}
427
434
}
428
435
429
- send_messages ( config, & messages, 0 , signers)
436
+ send_messages ( config, & messages, 0 , signers) . await
430
437
}
431
438
432
- fn send_messages (
433
- config : & Config ,
439
+ async fn send_messages (
440
+ config : & Config < ' _ > ,
434
441
messages : & [ Message ] ,
435
442
mut lamports_required : u64 ,
436
443
signers : Vec < Box < dyn Signer > > ,
@@ -442,7 +449,8 @@ fn send_messages(
442
449
443
450
let ( _blockhash, fee_calculator, _last_valid_block_height) = config
444
451
. rpc_client
445
- . get_recent_blockhash_with_commitment ( config. rpc_client . commitment ( ) ) ?
452
+ . get_recent_blockhash_with_commitment ( config. rpc_client . commitment ( ) )
453
+ . await ?
446
454
. value ;
447
455
448
456
lamports_required += messages
@@ -456,17 +464,18 @@ fn send_messages(
456
464
Sol ( lamports_required)
457
465
) ;
458
466
459
- crate :: check_fee_payer_balance ( config, lamports_required) ?;
467
+ crate :: check_fee_payer_balance ( config, lamports_required) . await ?;
460
468
469
+ // TODO use async tpu client once it's available in 1.11
461
470
let start = Instant :: now ( ) ;
471
+ let rpc_client = BlockingRpcClient :: new ( config. rpc_client . url ( ) ) ;
462
472
let tpu_client = TpuClient :: new (
463
- config . rpc_client . clone ( ) ,
473
+ Arc :: new ( rpc_client ) ,
464
474
& config. websocket_url ,
465
475
TpuClientConfig :: default ( ) ,
466
476
) ?;
467
477
let transaction_errors =
468
478
tpu_client. send_and_confirm_messages_with_spinner ( messages, & signers) ?;
469
-
470
479
for ( i, transaction_error) in transaction_errors. into_iter ( ) . enumerate ( ) {
471
480
if let Some ( transaction_error) = transaction_error {
472
481
println ! ( "Message {} failed with {:?}" , i, transaction_error) ;
0 commit comments