Cli: transfer ALL; check spend+fee in client (#10012)

* lamports->SOL in user-facing error msg

* Check for sufficient balance for spend and fee

* Add ALL option to solana transfer

* Rework TransferAmount to check for sign_only in parse

* Refactor TransferAmount & fee-check handling to be more general

* Add addl checks mechanism

* Move checks out of cli.rs

* Rename to SpendAmount to be more general & move

* Impl ALL/spend helpers for create-nonce-account

* Impl spend helpers for create-vote-account

* Impl ALL/spend helpers for create-stake-account

* Impl spend helpers for ping

* Impl ALL/spend helpers for pay

* Impl spend helpers for validator-info

* Remove unused fns

* Remove retry_get_balance

* Add a couple unit tests

* Rework send_util fn signatures
This commit is contained in:
Tyera Eulberg
2020-05-14 12:24:14 -06:00
committed by GitHub
parent 4e4a21f9b7
commit ed53a70b5c
21 changed files with 929 additions and 443 deletions

View File

@ -7,6 +7,7 @@ use solana_cli::{
blockhash_query::{self, BlockhashQuery},
parse_sign_only_reply_string,
},
spend_utils::SpendAmount,
};
use solana_client::rpc_client::RpcClient;
use solana_core::contact_info::ContactInfo;
@ -126,7 +127,7 @@ fn full_battery_tests(
nonce_account: 1,
seed,
nonce_authority: optional_authority,
lamports: 1000,
amount: SpendAmount::Some(1000),
};
process_command(&config_payer).unwrap();
@ -289,7 +290,7 @@ fn test_create_account_with_seed() {
nonce_account: 0,
seed: Some(seed),
nonce_authority: Some(authority_pubkey),
lamports: 241,
amount: SpendAmount::Some(241),
};
process_command(&creator_config).unwrap();
check_balance(241, &rpc_client, &nonce_address);
@ -311,7 +312,7 @@ fn test_create_account_with_seed() {
authority_config.command = CliCommand::ClusterVersion;
process_command(&authority_config).unwrap_err();
authority_config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: to_address,
from: 0,
sign_only: true,
@ -333,7 +334,7 @@ fn test_create_account_with_seed() {
format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
submit_config.signers = vec![&authority_presigner];
submit_config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: to_address,
from: 0,
sign_only: false,

View File

@ -9,6 +9,7 @@ use solana_cli::{
blockhash_query::{self, BlockhashQuery},
parse_sign_only_reply_string,
},
spend_utils::SpendAmount,
};
use solana_client::rpc_client::RpcClient;
use solana_core::validator::TestValidator;
@ -76,7 +77,7 @@ fn test_cli_timestamp_tx() {
let date_string = "\"2018-09-19T17:30:59Z\"";
let dt: DateTime<Utc> = serde_json::from_str(&date_string).unwrap();
config_payer.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(config_witness.signers[0].pubkey()),
@ -159,7 +160,7 @@ fn test_cli_witness_tx() {
// Make transaction (from config_payer to bob_pubkey) requiring witness signature from config_witness
config_payer.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
witnesses: Some(vec![config_witness.signers[0].pubkey()]),
..PayCommand::default()
@ -233,7 +234,7 @@ fn test_cli_cancel_tx() {
// Make transaction (from config_payer to bob_pubkey) requiring witness signature from config_witness
config_payer.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
witnesses: Some(vec![config_witness.signers[0].pubkey()]),
cancelable: true,
@ -318,7 +319,7 @@ fn test_offline_pay_tx() {
let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap();
config_offline.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
blockhash_query: BlockhashQuery::None(blockhash),
sign_only: true,
@ -339,7 +340,7 @@ fn test_offline_pay_tx() {
let online_pubkey = config_online.signers[0].pubkey();
config_online.signers = vec![&offline_presigner];
config_online.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(blockhash_query::Source::Cluster, blockhash),
..PayCommand::default()
@ -400,7 +401,7 @@ fn test_nonced_pay_tx() {
nonce_account: 1,
seed: None,
nonce_authority: Some(config.signers[0].pubkey()),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
config.signers.push(&nonce_account);
process_command(&config).unwrap();
@ -417,7 +418,7 @@ fn test_nonced_pay_tx() {
let bob_pubkey = Pubkey::new_rand();
config.signers = vec![&default_signer];
config.command = CliCommand::Pay(PayCommand {
lamports: 10,
amount: SpendAmount::Some(10),
to: bob_pubkey,
blockhash_query: BlockhashQuery::FeeCalculator(
blockhash_query::Source::NonceAccount(nonce_account.pubkey()),

View File

@ -35,8 +35,7 @@ fn test_cli_request_airdrop() {
let rpc_client = RpcClient::new_socket(leader_data.rpc);
let balance = rpc_client
.retry_get_balance(&bob_config.signers[0].pubkey(), 1)
.unwrap()
.get_balance(&bob_config.signers[0].pubkey())
.unwrap();
assert_eq!(balance, 50);

View File

@ -7,6 +7,7 @@ use solana_cli::{
blockhash_query::{self, BlockhashQuery},
parse_sign_only_reply_string,
},
spend_utils::SpendAmount,
};
use solana_client::rpc_client::RpcClient;
use solana_core::validator::{TestValidator, TestValidatorOptions};
@ -73,7 +74,7 @@ fn test_stake_delegation_force() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -165,7 +166,7 @@ fn test_seed_stake_delegation_and_deactivation() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -249,7 +250,7 @@ fn test_stake_delegation_and_deactivation() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -356,7 +357,7 @@ fn test_offline_stake_delegation_and_deactivation() {
staker: Some(config_offline.signers[0].pubkey().into()),
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -478,7 +479,7 @@ fn test_nonced_stake_delegation_and_deactivation() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -495,7 +496,7 @@ fn test_nonced_stake_delegation_and_deactivation() {
nonce_account: 1,
seed: None,
nonce_authority: Some(config.signers[0].pubkey()),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
@ -607,7 +608,7 @@ fn test_stake_authorize() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -735,7 +736,7 @@ fn test_stake_authorize() {
nonce_account: 1,
seed: None,
nonce_authority: Some(offline_authority_pubkey),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
@ -862,7 +863,7 @@ fn test_stake_authorize_with_fee_payer() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -991,7 +992,7 @@ fn test_stake_split() {
staker: Some(offline_pubkey),
withdrawer: Some(offline_pubkey),
lockup: Lockup::default(),
lamports: 10 * minimum_stake_balance,
amount: SpendAmount::Some(10 * minimum_stake_balance),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -1016,7 +1017,7 @@ fn test_stake_split() {
nonce_account: 1,
seed: None,
nonce_authority: Some(offline_pubkey),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
check_balance(minimum_nonce_balance, &rpc_client, &nonce_account.pubkey());
@ -1147,7 +1148,7 @@ fn test_stake_set_lockup() {
staker: Some(offline_pubkey),
withdrawer: Some(offline_pubkey),
lockup,
lamports: 10 * minimum_stake_balance,
amount: SpendAmount::Some(10 * minimum_stake_balance),
sign_only: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
@ -1273,7 +1274,7 @@ fn test_stake_set_lockup() {
nonce_account: 1,
seed: None,
nonce_authority: Some(offline_pubkey),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
check_balance(minimum_nonce_balance, &rpc_client, &nonce_account_pubkey);
@ -1393,7 +1394,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
nonce_account: 1,
seed: None,
nonce_authority: Some(offline_pubkey),
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
@ -1413,7 +1414,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: true,
blockhash_query: BlockhashQuery::None(nonce_hash),
nonce_account: Some(nonce_pubkey),
@ -1434,7 +1435,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
staker: Some(offline_pubkey),
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::FeeCalculator(
blockhash_query::Source::NonceAccount(nonce_pubkey),
@ -1507,7 +1508,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
staker: None,
withdrawer: None,
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: true,
blockhash_query: BlockhashQuery::None(nonce_hash),
nonce_account: Some(nonce_pubkey),
@ -1526,7 +1527,7 @@ fn test_offline_nonced_create_stake_account_and_withdraw() {
staker: Some(offline_pubkey.into()),
withdrawer: Some(offline_pubkey.into()),
lockup: Lockup::default(),
lamports: 50_000,
amount: SpendAmount::Some(50_000),
sign_only: false,
blockhash_query: BlockhashQuery::FeeCalculator(
blockhash_query::Source::NonceAccount(nonce_pubkey),

View File

@ -7,6 +7,7 @@ use solana_cli::{
blockhash_query::{self, BlockhashQuery},
parse_sign_only_reply_string,
},
spend_utils::SpendAmount,
};
use solana_client::rpc_client::RpcClient;
use solana_core::validator::{TestValidator, TestValidatorOptions};
@ -55,7 +56,7 @@ fn test_transfer() {
// Plain ole transfer
config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: false,
@ -69,6 +70,22 @@ fn test_transfer() {
check_balance(49_989, &rpc_client, &sender_pubkey);
check_balance(10, &rpc_client, &recipient_pubkey);
// Plain ole transfer, failure due to InsufficientFundsForSpendAndFee
config.command = CliCommand::Transfer {
amount: SpendAmount::Some(49_989),
to: recipient_pubkey,
from: 0,
sign_only: false,
no_wait: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
nonce_authority: 0,
fee_payer: 0,
};
assert!(process_command(&config).is_err());
check_balance(49_989, &rpc_client, &sender_pubkey);
check_balance(10, &rpc_client, &recipient_pubkey);
let mut offline = CliConfig::default();
offline.json_rpc_url = String::default();
offline.signers = vec![&default_offline_signer];
@ -83,7 +100,7 @@ fn test_transfer() {
// Offline transfer
let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap();
offline.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: true,
@ -100,7 +117,7 @@ fn test_transfer() {
let offline_presigner = sign_only.presigner_of(&offline_pubkey).unwrap();
config.signers = vec![&offline_presigner];
config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: false,
@ -124,7 +141,7 @@ fn test_transfer() {
nonce_account: 1,
seed: None,
nonce_authority: None,
lamports: minimum_nonce_balance,
amount: SpendAmount::Some(minimum_nonce_balance),
};
process_command(&config).unwrap();
check_balance(49_987 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
@ -138,7 +155,7 @@ fn test_transfer() {
// Nonced transfer
config.signers = vec![&default_signer];
config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: false,
@ -179,7 +196,7 @@ fn test_transfer() {
// Offline, nonced transfer
offline.signers = vec![&default_offline_signer];
offline.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: true,
@ -195,7 +212,7 @@ fn test_transfer() {
let offline_presigner = sign_only.presigner_of(&offline_pubkey).unwrap();
config.signers = vec![&offline_presigner];
config.command = CliCommand::Transfer {
lamports: 10,
amount: SpendAmount::Some(10),
to: recipient_pubkey,
from: 0,
sign_only: false,
@ -272,7 +289,7 @@ fn test_transfer_multisession_signing() {
fee_payer_config.command = CliCommand::ClusterVersion;
process_command(&fee_payer_config).unwrap_err();
fee_payer_config.command = CliCommand::Transfer {
lamports: 42,
amount: SpendAmount::Some(42),
to: to_pubkey,
from: 1,
sign_only: true,
@ -298,7 +315,7 @@ fn test_transfer_multisession_signing() {
from_config.command = CliCommand::ClusterVersion;
process_command(&from_config).unwrap_err();
from_config.command = CliCommand::Transfer {
lamports: 42,
amount: SpendAmount::Some(42),
to: to_pubkey,
from: 1,
sign_only: true,
@ -321,7 +338,7 @@ fn test_transfer_multisession_signing() {
config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
config.signers = vec![&fee_payer_presigner, &from_presigner];
config.command = CliCommand::Transfer {
lamports: 42,
amount: SpendAmount::Some(42),
to: to_pubkey,
from: 1,
sign_only: false,
@ -340,3 +357,57 @@ fn test_transfer_multisession_signing() {
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();
}
#[test]
fn test_transfer_all() {
let TestValidator {
server,
leader_data,
alice: mint_keypair,
ledger_path,
..
} = TestValidator::run_with_options(TestValidatorOptions {
fees: 1,
bootstrap_validator_lamports: 42_000,
..TestValidatorOptions::default()
});
let (sender, receiver) = channel();
run_local_faucet(mint_keypair, sender, None);
let faucet_addr = receiver.recv().unwrap();
let rpc_client = RpcClient::new_socket(leader_data.rpc);
let default_signer = Keypair::new();
let mut config = CliConfig::default();
config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
config.signers = vec![&default_signer];
let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &sender_pubkey, 50_000, &config)
.unwrap();
check_balance(50_000, &rpc_client, &sender_pubkey);
check_balance(0, &rpc_client, &recipient_pubkey);
// Plain ole transfer
config.command = CliCommand::Transfer {
amount: SpendAmount::All,
to: recipient_pubkey,
from: 0,
sign_only: false,
no_wait: false,
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
nonce_account: None,
nonce_authority: 0,
fee_payer: 0,
};
process_command(&config).unwrap();
check_balance(0, &rpc_client, &sender_pubkey);
check_balance(49_999, &rpc_client, &recipient_pubkey);
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();
}