Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c94eeec

Browse files
authored
token-cli: Refactor tests out of main.rs (#5804)
* Refactor command processor and clap app * Move tests to a separate file * Allow multiple options in update confidential mint * Unserialize tests (for testing) * Fixup amounts to use decimals * Update tests, fix clippy warnings * Revert "Unserialize tests (for testing)" This reverts commit 8ceecf9.
1 parent 6ac9c28 commit c94eeec

File tree

9 files changed

+10288
-10238
lines changed

9 files changed

+10288
-10238
lines changed

token/cli/src/bench.rs

+34-154
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
/// The `bench` subcommand
22
use {
3-
crate::{config::Config, owner_address_arg, CommandResult, Error},
4-
clap::{value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand},
5-
solana_clap_utils::{
6-
input_parsers::pubkey_of_signer,
7-
input_validators::{is_amount, is_parsable, is_valid_pubkey},
8-
},
3+
crate::{clap_app::Error, command::CommandResult, config::Config},
4+
clap::{value_t_or_exit, ArgMatches},
5+
solana_clap_utils::input_parsers::pubkey_of_signer,
96
solana_client::{
107
nonblocking::rpc_client::RpcClient, rpc_client::RpcClient as BlockingRpcClient,
118
tpu_client::TpuClient, tpu_client::TpuClientConfig,
129
},
1310
solana_remote_wallet::remote_wallet::RemoteWalletManager,
1411
solana_sdk::{
15-
message::Message, native_token::Sol, program_pack::Pack, pubkey::Pubkey, signature::Signer,
16-
system_instruction,
12+
message::Message, native_token::lamports_to_sol, native_token::Sol, program_pack::Pack,
13+
pubkey::Pubkey, signature::Signer, system_instruction,
1714
},
1815
spl_associated_token_account::*,
1916
spl_token_2022::{
@@ -24,147 +21,6 @@ use {
2421
std::{rc::Rc, sync::Arc, time::Instant},
2522
};
2623

27-
pub(crate) trait BenchSubCommand {
28-
fn bench_subcommand(self) -> Self;
29-
}
30-
31-
impl BenchSubCommand for App<'_, '_> {
32-
fn bench_subcommand(self) -> Self {
33-
self.subcommand(
34-
SubCommand::with_name("bench")
35-
.about("Token benchmarking facilities")
36-
.setting(AppSettings::InferSubcommands)
37-
.setting(AppSettings::SubcommandRequiredElseHelp)
38-
.subcommand(
39-
SubCommand::with_name("create-accounts")
40-
.about("Create multiple token accounts for benchmarking")
41-
.arg(
42-
Arg::with_name("token")
43-
.validator(is_valid_pubkey)
44-
.value_name("TOKEN_ADDRESS")
45-
.takes_value(true)
46-
.index(1)
47-
.required(true)
48-
.help("The token that the accounts will hold"),
49-
)
50-
.arg(
51-
Arg::with_name("n")
52-
.validator(is_parsable::<usize>)
53-
.value_name("N")
54-
.takes_value(true)
55-
.index(2)
56-
.required(true)
57-
.help("The number of accounts to create"),
58-
)
59-
.arg(owner_address_arg()),
60-
)
61-
.subcommand(
62-
SubCommand::with_name("close-accounts")
63-
.about("Close multiple token accounts used for benchmarking")
64-
.arg(
65-
Arg::with_name("token")
66-
.validator(is_valid_pubkey)
67-
.value_name("TOKEN_ADDRESS")
68-
.takes_value(true)
69-
.index(1)
70-
.required(true)
71-
.help("The token that the accounts held"),
72-
)
73-
.arg(
74-
Arg::with_name("n")
75-
.validator(is_parsable::<usize>)
76-
.value_name("N")
77-
.takes_value(true)
78-
.index(2)
79-
.required(true)
80-
.help("The number of accounts to close"),
81-
)
82-
.arg(owner_address_arg()),
83-
)
84-
.subcommand(
85-
SubCommand::with_name("deposit-into")
86-
.about("Deposit tokens into multiple accounts")
87-
.arg(
88-
Arg::with_name("token")
89-
.validator(is_valid_pubkey)
90-
.value_name("TOKEN_ADDRESS")
91-
.takes_value(true)
92-
.index(1)
93-
.required(true)
94-
.help("The token that the accounts will hold"),
95-
)
96-
.arg(
97-
Arg::with_name("n")
98-
.validator(is_parsable::<usize>)
99-
.value_name("N")
100-
.takes_value(true)
101-
.index(2)
102-
.required(true)
103-
.help("The number of accounts to deposit into"),
104-
)
105-
.arg(
106-
Arg::with_name("amount")
107-
.validator(is_amount)
108-
.value_name("TOKEN_AMOUNT")
109-
.takes_value(true)
110-
.index(3)
111-
.required(true)
112-
.help("Amount to deposit into each account, in tokens"),
113-
)
114-
.arg(
115-
Arg::with_name("from")
116-
.long("from")
117-
.validator(is_valid_pubkey)
118-
.value_name("SOURCE_TOKEN_ACCOUNT_ADDRESS")
119-
.takes_value(true)
120-
.help("The source token account address [default: associated token account for --owner]")
121-
)
122-
.arg(owner_address_arg()),
123-
)
124-
.subcommand(
125-
SubCommand::with_name("withdraw-from")
126-
.about("Withdraw tokens from multiple accounts")
127-
.arg(
128-
Arg::with_name("token")
129-
.validator(is_valid_pubkey)
130-
.value_name("TOKEN_ADDRESS")
131-
.takes_value(true)
132-
.index(1)
133-
.required(true)
134-
.help("The token that the accounts hold"),
135-
)
136-
.arg(
137-
Arg::with_name("n")
138-
.validator(is_parsable::<usize>)
139-
.value_name("N")
140-
.takes_value(true)
141-
.index(2)
142-
.required(true)
143-
.help("The number of accounts to withdraw from"),
144-
)
145-
.arg(
146-
Arg::with_name("amount")
147-
.validator(is_amount)
148-
.value_name("TOKEN_AMOUNT")
149-
.takes_value(true)
150-
.index(3)
151-
.required(true)
152-
.help("Amount to withdraw from each account, in tokens"),
153-
)
154-
.arg(
155-
Arg::with_name("to")
156-
.long("to")
157-
.validator(is_valid_pubkey)
158-
.value_name("RECIPIENT_TOKEN_ACCOUNT_ADDRESS")
159-
.takes_value(true)
160-
.help("The recipient token account address [default: associated token account for --owner]")
161-
)
162-
.arg(owner_address_arg()),
163-
),
164-
)
165-
}
166-
}
167-
16824
pub(crate) async fn bench_process_command(
16925
matches: &ArgMatches<'_>,
17026
config: &Config<'_>,
@@ -287,7 +143,7 @@ async fn command_create_accounts(
287143
.get_minimum_balance_for_rent_exemption(Account::get_packed_len())
288144
.await?;
289145

290-
let mut lamports_required = 0;
146+
let mut lamports_required: u64 = 0;
291147

292148
let token_addresses_with_seed = get_token_addresses_with_seed(&program_id, token, owner, n);
293149
let mut messages = vec![];
@@ -298,7 +154,8 @@ async fn command_create_accounts(
298154

299155
for (account, (address, seed)) in accounts_chunk.iter().zip(address_chunk) {
300156
if account.is_none() {
301-
lamports_required += minimum_balance_for_rent_exemption;
157+
lamports_required =
158+
lamports_required.saturating_add(minimum_balance_for_rent_exemption);
302159
messages.push(Message::new(
303160
&[
304161
system_instruction::create_account_with_seed(
@@ -440,16 +297,21 @@ async fn send_messages(
440297
let blockhash = config.rpc_client.get_latest_blockhash().await?;
441298
let mut message = messages[0].clone();
442299
message.recent_blockhash = blockhash;
443-
lamports_required +=
444-
config.rpc_client.get_fee_for_message(&message).await? * messages.len() as u64;
300+
lamports_required = lamports_required.saturating_add(
301+
config
302+
.rpc_client
303+
.get_fee_for_message(&message)
304+
.await?
305+
.saturating_mul(messages.len() as u64),
306+
);
445307

446308
println!(
447309
"Sending {:?} messages for ~{}",
448310
messages.len(),
449311
Sol(lamports_required)
450312
);
451313

452-
crate::check_fee_payer_balance(config, lamports_required).await?;
314+
check_fee_payer_balance(config, lamports_required).await?;
453315

454316
// TODO use async tpu client once it's available in 1.11
455317
let start = Instant::now();
@@ -489,3 +351,21 @@ async fn send_messages(
489351

490352
Ok(())
491353
}
354+
355+
async fn check_fee_payer_balance(config: &Config<'_>, required_balance: u64) -> Result<(), Error> {
356+
let balance = config
357+
.rpc_client
358+
.get_balance(&config.fee_payer()?.pubkey())
359+
.await?;
360+
if balance < required_balance {
361+
Err(format!(
362+
"Fee payer, {}, has insufficient balance: {} required, {} available",
363+
config.fee_payer()?.pubkey(),
364+
lamports_to_sol(required_balance),
365+
lamports_to_sol(balance)
366+
)
367+
.into())
368+
} else {
369+
Ok(())
370+
}
371+
}

0 commit comments

Comments
 (0)