2020-02-07 11:16:35 -08:00
|
|
|
use solana_cli::{
|
|
|
|
cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig},
|
2020-05-14 11:24:14 -07:00
|
|
|
spend_utils::SpendAmount,
|
2020-06-17 11:18:48 -07:00
|
|
|
test_utils::{check_ready, check_recent_balance},
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
2020-09-22 19:29:32 -07:00
|
|
|
use solana_cli_output::{parse_sign_only_reply_string, OutputFormat};
|
2020-09-22 15:06:14 -07:00
|
|
|
use solana_client::{
|
|
|
|
blockhash_query::{self, BlockhashQuery},
|
|
|
|
nonce_utils,
|
|
|
|
rpc_client::RpcClient,
|
|
|
|
};
|
2020-11-16 22:27:36 -08:00
|
|
|
use solana_core::test_validator::TestValidator;
|
2020-02-07 11:16:35 -08:00
|
|
|
use solana_faucet::faucet::run_local_faucet;
|
|
|
|
use solana_sdk::{
|
2020-06-17 11:18:48 -07:00
|
|
|
commitment_config::CommitmentConfig,
|
2020-03-10 12:00:15 -07:00
|
|
|
nonce::State as NonceState,
|
2020-02-07 11:16:35 -08:00
|
|
|
pubkey::Pubkey,
|
2020-03-18 20:49:38 -07:00
|
|
|
signature::{keypair_from_seed, Keypair, NullSigner, Signer},
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer() {
|
2020-11-16 22:27:36 -08:00
|
|
|
solana_logger::setup();
|
2020-12-08 23:18:27 -08:00
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let test_validator = TestValidator::with_custom_fees(mint_keypair.pubkey(), 1);
|
2021-01-28 12:11:53 -08:00
|
|
|
let faucet_addr = run_local_faucet(mint_keypair, None);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
2021-01-19 14:33:03 -08:00
|
|
|
let rpc_client =
|
2021-01-26 11:23:07 -08:00
|
|
|
RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
|
2020-02-07 11:16:35 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let default_signer = Keypair::new();
|
|
|
|
let default_offline_signer = Keypair::new();
|
|
|
|
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut config = CliConfig::recent_for_tests();
|
2020-11-25 17:00:47 -08:00
|
|
|
config.json_rpc_url = test_validator.rpc_url();
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-07 11:16:35 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let sender_pubkey = config.signers[0].pubkey();
|
2020-02-07 11:16:35 -08:00
|
|
|
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
|
|
|
|
|
2020-05-06 21:21:48 -07:00
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &sender_pubkey, 50_000, &config)
|
|
|
|
.unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(50_000, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(0, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
check_ready(&rpc_client);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
|
|
|
// Plain ole transfer
|
|
|
|
config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-07 11:16:35 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-07 11:16:35 -08:00
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
2020-02-07 11:16:35 -08:00
|
|
|
nonce_account: None,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(49_989, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(10, &rpc_client, &recipient_pubkey);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
2020-05-14 11:24:14 -07:00
|
|
|
// Plain ole transfer, failure due to InsufficientFundsForSpendAndFee
|
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
amount: SpendAmount::Some(49_989),
|
|
|
|
to: recipient_pubkey,
|
|
|
|
from: 0,
|
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-05-14 11:24:14 -07:00
|
|
|
no_wait: false,
|
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-05-14 11:24:14 -07:00
|
|
|
};
|
|
|
|
assert!(process_command(&config).is_err());
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(49_989, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(10, &rpc_client, &recipient_pubkey);
|
2020-05-14 11:24:14 -07:00
|
|
|
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut offline = CliConfig::recent_for_tests();
|
2020-02-07 11:16:35 -08:00
|
|
|
offline.json_rpc_url = String::default();
|
2020-02-24 16:03:30 -08:00
|
|
|
offline.signers = vec![&default_offline_signer];
|
2020-02-07 11:16:35 -08:00
|
|
|
// Verify we cannot contact the cluster
|
|
|
|
offline.command = CliCommand::ClusterVersion;
|
|
|
|
process_command(&offline).unwrap_err();
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let offline_pubkey = offline.signers[0].pubkey();
|
2020-05-06 21:21:48 -07:00
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &offline_pubkey, 50, &config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(50, &rpc_client, &offline_pubkey);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
|
|
|
// Offline transfer
|
2021-01-19 14:33:03 -08:00
|
|
|
let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap();
|
2020-02-07 11:16:35 -08:00
|
|
|
offline.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-07 11:16:35 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-07 11:16:35 -08:00
|
|
|
sign_only: true,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::None(blockhash),
|
2020-02-07 11:16:35 -08:00
|
|
|
nonce_account: None,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
2020-05-06 21:21:48 -07:00
|
|
|
offline.output_format = OutputFormat::JsonCompact;
|
2020-02-07 11:16:35 -08:00
|
|
|
let sign_only_reply = process_command(&offline).unwrap();
|
2020-03-18 20:49:38 -07:00
|
|
|
let sign_only = parse_sign_only_reply_string(&sign_only_reply);
|
|
|
|
assert!(sign_only.has_all_signers());
|
|
|
|
let offline_presigner = sign_only.presigner_of(&offline_pubkey).unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&offline_presigner];
|
2020-02-07 11:16:35 -08:00
|
|
|
config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-07 11:16:35 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-07 11:16:35 -08:00
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(blockhash_query::Source::Cluster, blockhash),
|
2020-02-07 11:16:35 -08:00
|
|
|
nonce_account: None,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(39, &rpc_client, &offline_pubkey);
|
|
|
|
check_recent_balance(20, &rpc_client, &recipient_pubkey);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
|
|
|
// Create nonce account
|
|
|
|
let nonce_account = keypair_from_seed(&[3u8; 32]).unwrap();
|
|
|
|
let minimum_nonce_balance = rpc_client
|
2020-03-10 12:00:15 -07:00
|
|
|
.get_minimum_balance_for_rent_exemption(NonceState::size())
|
2020-02-07 11:16:35 -08:00
|
|
|
.unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&default_signer, &nonce_account];
|
2020-02-07 11:16:35 -08:00
|
|
|
config.command = CliCommand::CreateNonceAccount {
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: 1,
|
2020-02-07 11:16:35 -08:00
|
|
|
seed: None,
|
|
|
|
nonce_authority: None,
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(minimum_nonce_balance),
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(49_987 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
2020-02-07 11:16:35 -08:00
|
|
|
|
|
|
|
// Fetch nonce hash
|
2020-09-21 12:55:44 -07:00
|
|
|
let nonce_hash = nonce_utils::get_account_with_commitment(
|
2020-06-17 11:18:48 -07:00
|
|
|
&rpc_client,
|
|
|
|
&nonce_account.pubkey(),
|
2021-01-26 11:23:07 -08:00
|
|
|
CommitmentConfig::processed(),
|
2020-06-17 11:18:48 -07:00
|
|
|
)
|
2020-09-21 12:55:44 -07:00
|
|
|
.and_then(|ref a| nonce_utils::data_from_account(a))
|
2020-06-17 11:18:48 -07:00
|
|
|
.unwrap()
|
|
|
|
.blockhash;
|
2020-02-07 11:16:35 -08:00
|
|
|
|
|
|
|
// Nonced transfer
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-07 11:16:35 -08:00
|
|
|
config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-07 11:16:35 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-07 11:16:35 -08:00
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(
|
|
|
|
blockhash_query::Source::NonceAccount(nonce_account.pubkey()),
|
|
|
|
nonce_hash,
|
|
|
|
),
|
2020-02-07 11:16:35 -08:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-07 11:16:35 -08:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(49_976 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(30, &rpc_client, &recipient_pubkey);
|
2020-09-21 12:55:44 -07:00
|
|
|
let new_nonce_hash = nonce_utils::get_account_with_commitment(
|
2020-06-17 11:18:48 -07:00
|
|
|
&rpc_client,
|
|
|
|
&nonce_account.pubkey(),
|
2021-01-26 11:23:07 -08:00
|
|
|
CommitmentConfig::processed(),
|
2020-06-17 11:18:48 -07:00
|
|
|
)
|
2020-09-21 12:55:44 -07:00
|
|
|
.and_then(|ref a| nonce_utils::data_from_account(a))
|
2020-06-17 11:18:48 -07:00
|
|
|
.unwrap()
|
|
|
|
.blockhash;
|
2020-02-07 11:16:35 -08:00
|
|
|
assert_ne!(nonce_hash, new_nonce_hash);
|
|
|
|
|
2020-02-10 22:34:14 -08:00
|
|
|
// Assign nonce authority to offline
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-10 22:34:14 -08:00
|
|
|
config.command = CliCommand::AuthorizeNonceAccount {
|
|
|
|
nonce_account: nonce_account.pubkey(),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
2020-02-10 22:34:14 -08:00
|
|
|
new_authority: offline_pubkey,
|
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(49_975 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
2020-02-10 22:34:14 -08:00
|
|
|
|
|
|
|
// Fetch nonce hash
|
2020-09-21 12:55:44 -07:00
|
|
|
let nonce_hash = nonce_utils::get_account_with_commitment(
|
2020-06-17 11:18:48 -07:00
|
|
|
&rpc_client,
|
|
|
|
&nonce_account.pubkey(),
|
2021-01-26 11:23:07 -08:00
|
|
|
CommitmentConfig::processed(),
|
2020-06-17 11:18:48 -07:00
|
|
|
)
|
2020-09-21 12:55:44 -07:00
|
|
|
.and_then(|ref a| nonce_utils::data_from_account(a))
|
2020-06-17 11:18:48 -07:00
|
|
|
.unwrap()
|
|
|
|
.blockhash;
|
2020-02-10 22:34:14 -08:00
|
|
|
|
|
|
|
// Offline, nonced transfer
|
2020-02-24 16:03:30 -08:00
|
|
|
offline.signers = vec![&default_offline_signer];
|
2020-02-10 22:34:14 -08:00
|
|
|
offline.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-10 22:34:14 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-10 22:34:14 -08:00
|
|
|
sign_only: true,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::None(nonce_hash),
|
2020-02-10 22:34:14 -08:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-10 22:34:14 -08:00
|
|
|
};
|
|
|
|
let sign_only_reply = process_command(&offline).unwrap();
|
2020-03-18 20:49:38 -07:00
|
|
|
let sign_only = parse_sign_only_reply_string(&sign_only_reply);
|
|
|
|
assert!(sign_only.has_all_signers());
|
|
|
|
let offline_presigner = sign_only.presigner_of(&offline_pubkey).unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
config.signers = vec![&offline_presigner];
|
2020-02-10 22:34:14 -08:00
|
|
|
config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(10),
|
2020-02-10 22:34:14 -08:00
|
|
|
to: recipient_pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
from: 0,
|
2020-02-10 22:34:14 -08:00
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-11 11:14:15 -07:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(
|
|
|
|
blockhash_query::Source::NonceAccount(nonce_account.pubkey()),
|
2020-03-18 20:49:38 -07:00
|
|
|
sign_only.blockhash,
|
2020-03-11 11:14:15 -07:00
|
|
|
),
|
2020-02-10 22:34:14 -08:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-02-10 22:34:14 -08:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(28, &rpc_client, &offline_pubkey);
|
|
|
|
check_recent_balance(40, &rpc_client, &recipient_pubkey);
|
2020-02-07 11:16:35 -08:00
|
|
|
}
|
2020-03-18 20:49:38 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_multisession_signing() {
|
2020-11-16 22:27:36 -08:00
|
|
|
solana_logger::setup();
|
2020-12-08 23:18:27 -08:00
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let test_validator = TestValidator::with_custom_fees(mint_keypair.pubkey(), 1);
|
2021-01-28 12:11:53 -08:00
|
|
|
let faucet_addr = run_local_faucet(mint_keypair, None);
|
2020-03-18 20:49:38 -07:00
|
|
|
|
|
|
|
let to_pubkey = Pubkey::new(&[1u8; 32]);
|
|
|
|
let offline_from_signer = keypair_from_seed(&[2u8; 32]).unwrap();
|
|
|
|
let offline_fee_payer_signer = keypair_from_seed(&[3u8; 32]).unwrap();
|
|
|
|
let from_null_signer = NullSigner::new(&offline_from_signer.pubkey());
|
2020-06-17 11:18:48 -07:00
|
|
|
let config = CliConfig::recent_for_tests();
|
2020-03-18 20:49:38 -07:00
|
|
|
|
|
|
|
// Setup accounts
|
2021-01-19 14:33:03 -08:00
|
|
|
let rpc_client =
|
2021-01-26 11:23:07 -08:00
|
|
|
RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
|
2020-05-06 21:21:48 -07:00
|
|
|
request_and_confirm_airdrop(
|
|
|
|
&rpc_client,
|
|
|
|
&faucet_addr,
|
|
|
|
&offline_from_signer.pubkey(),
|
|
|
|
43,
|
|
|
|
&config,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-03-18 20:49:38 -07:00
|
|
|
request_and_confirm_airdrop(
|
|
|
|
&rpc_client,
|
|
|
|
&faucet_addr,
|
|
|
|
&offline_fee_payer_signer.pubkey(),
|
|
|
|
3,
|
2020-05-06 21:21:48 -07:00
|
|
|
&config,
|
2020-03-18 20:49:38 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(43, &rpc_client, &offline_from_signer.pubkey());
|
|
|
|
check_recent_balance(3, &rpc_client, &offline_fee_payer_signer.pubkey());
|
|
|
|
check_recent_balance(0, &rpc_client, &to_pubkey);
|
2020-03-18 20:49:38 -07:00
|
|
|
|
2020-06-17 11:18:48 -07:00
|
|
|
check_ready(&rpc_client);
|
|
|
|
|
2021-01-19 14:33:03 -08:00
|
|
|
let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap();
|
2020-03-18 20:49:38 -07:00
|
|
|
|
|
|
|
// Offline fee-payer signs first
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut fee_payer_config = CliConfig::recent_for_tests();
|
2020-03-18 20:49:38 -07:00
|
|
|
fee_payer_config.json_rpc_url = String::default();
|
|
|
|
fee_payer_config.signers = vec![&offline_fee_payer_signer, &from_null_signer];
|
|
|
|
// Verify we cannot contact the cluster
|
|
|
|
fee_payer_config.command = CliCommand::ClusterVersion;
|
|
|
|
process_command(&fee_payer_config).unwrap_err();
|
|
|
|
fee_payer_config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(42),
|
2020-03-18 20:49:38 -07:00
|
|
|
to: to_pubkey,
|
|
|
|
from: 1,
|
|
|
|
sign_only: true,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-18 20:49:38 -07:00
|
|
|
blockhash_query: BlockhashQuery::None(blockhash),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-03-18 20:49:38 -07:00
|
|
|
};
|
2020-05-06 21:21:48 -07:00
|
|
|
fee_payer_config.output_format = OutputFormat::JsonCompact;
|
2020-03-18 20:49:38 -07:00
|
|
|
let sign_only_reply = process_command(&fee_payer_config).unwrap();
|
|
|
|
let sign_only = parse_sign_only_reply_string(&sign_only_reply);
|
|
|
|
assert!(!sign_only.has_all_signers());
|
|
|
|
let fee_payer_presigner = sign_only
|
|
|
|
.presigner_of(&offline_fee_payer_signer.pubkey())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Now the offline fund source
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut from_config = CliConfig::recent_for_tests();
|
2020-03-18 20:49:38 -07:00
|
|
|
from_config.json_rpc_url = String::default();
|
|
|
|
from_config.signers = vec![&fee_payer_presigner, &offline_from_signer];
|
|
|
|
// Verify we cannot contact the cluster
|
|
|
|
from_config.command = CliCommand::ClusterVersion;
|
|
|
|
process_command(&from_config).unwrap_err();
|
|
|
|
from_config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(42),
|
2020-03-18 20:49:38 -07:00
|
|
|
to: to_pubkey,
|
|
|
|
from: 1,
|
|
|
|
sign_only: true,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-18 20:49:38 -07:00
|
|
|
blockhash_query: BlockhashQuery::None(blockhash),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-03-18 20:49:38 -07:00
|
|
|
};
|
2020-05-06 21:21:48 -07:00
|
|
|
from_config.output_format = OutputFormat::JsonCompact;
|
2020-03-18 20:49:38 -07:00
|
|
|
let sign_only_reply = process_command(&from_config).unwrap();
|
|
|
|
let sign_only = parse_sign_only_reply_string(&sign_only_reply);
|
|
|
|
assert!(sign_only.has_all_signers());
|
|
|
|
let from_presigner = sign_only
|
|
|
|
.presigner_of(&offline_from_signer.pubkey())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Finally submit to the cluster
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut config = CliConfig::recent_for_tests();
|
2020-11-25 17:00:47 -08:00
|
|
|
config.json_rpc_url = test_validator.rpc_url();
|
2020-03-18 20:49:38 -07:00
|
|
|
config.signers = vec![&fee_payer_presigner, &from_presigner];
|
|
|
|
config.command = CliCommand::Transfer {
|
2020-05-14 11:24:14 -07:00
|
|
|
amount: SpendAmount::Some(42),
|
2020-03-18 20:49:38 -07:00
|
|
|
to: to_pubkey,
|
|
|
|
from: 1,
|
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-04-08 22:46:19 -07:00
|
|
|
no_wait: false,
|
2020-03-18 20:49:38 -07:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(blockhash_query::Source::Cluster, blockhash),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-03-18 20:49:38 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
|
|
|
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(1, &rpc_client, &offline_from_signer.pubkey());
|
|
|
|
check_recent_balance(1, &rpc_client, &offline_fee_payer_signer.pubkey());
|
|
|
|
check_recent_balance(42, &rpc_client, &to_pubkey);
|
2020-03-18 20:49:38 -07:00
|
|
|
}
|
2020-05-14 11:24:14 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_all() {
|
2020-11-16 22:27:36 -08:00
|
|
|
solana_logger::setup();
|
2020-12-08 23:18:27 -08:00
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let test_validator = TestValidator::with_custom_fees(mint_keypair.pubkey(), 1);
|
2021-01-28 12:11:53 -08:00
|
|
|
let faucet_addr = run_local_faucet(mint_keypair, None);
|
2020-05-14 11:24:14 -07:00
|
|
|
|
2021-01-19 14:33:03 -08:00
|
|
|
let rpc_client =
|
2021-01-26 11:23:07 -08:00
|
|
|
RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
|
2020-05-14 11:24:14 -07:00
|
|
|
|
|
|
|
let default_signer = Keypair::new();
|
|
|
|
|
2020-06-17 11:18:48 -07:00
|
|
|
let mut config = CliConfig::recent_for_tests();
|
2020-11-25 17:00:47 -08:00
|
|
|
config.json_rpc_url = test_validator.rpc_url();
|
2020-05-14 11:24:14 -07:00
|
|
|
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();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(50_000, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(0, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
check_ready(&rpc_client);
|
2020-05-14 11:24:14 -07:00
|
|
|
|
|
|
|
// Plain ole transfer
|
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
amount: SpendAmount::All,
|
|
|
|
to: recipient_pubkey,
|
|
|
|
from: 0,
|
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2020-05-14 11:24:14 -07:00
|
|
|
no_wait: false,
|
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2021-02-17 23:08:12 -08:00
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
2020-05-14 11:24:14 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-06-17 11:18:48 -07:00
|
|
|
check_recent_balance(0, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(49_999, &rpc_client, &recipient_pubkey);
|
2020-05-14 11:24:14 -07:00
|
|
|
}
|
2021-02-17 23:08:12 -08:00
|
|
|
|
2021-03-22 11:10:44 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_unfunded_recipient() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let test_validator = TestValidator::with_custom_fees(mint_keypair.pubkey(), 1);
|
|
|
|
let faucet_addr = run_local_faucet(mint_keypair, None);
|
|
|
|
|
|
|
|
let rpc_client =
|
|
|
|
RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
|
|
|
|
|
|
|
|
let default_signer = Keypair::new();
|
|
|
|
|
|
|
|
let mut config = CliConfig::recent_for_tests();
|
|
|
|
config.json_rpc_url = test_validator.rpc_url();
|
|
|
|
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_recent_balance(50_000, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(0, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
check_ready(&rpc_client);
|
|
|
|
|
|
|
|
// Plain ole transfer
|
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
amount: SpendAmount::All,
|
|
|
|
to: recipient_pubkey,
|
|
|
|
from: 0,
|
|
|
|
sign_only: false,
|
|
|
|
dump_transaction_message: false,
|
|
|
|
allow_unfunded_recipient: false,
|
|
|
|
no_wait: false,
|
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
|
|
|
derived_address_seed: None,
|
|
|
|
derived_address_program_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Expect failure due to unfunded recipient and the lack of the `allow_unfunded_recipient` flag
|
|
|
|
process_command(&config).unwrap_err();
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:08:12 -08:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_with_seed() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let test_validator = TestValidator::with_custom_fees(mint_keypair.pubkey(), 1);
|
|
|
|
let faucet_addr = run_local_faucet(mint_keypair, None);
|
|
|
|
|
|
|
|
let rpc_client =
|
|
|
|
RpcClient::new_with_commitment(test_validator.rpc_url(), CommitmentConfig::processed());
|
|
|
|
|
|
|
|
let default_signer = Keypair::new();
|
|
|
|
|
|
|
|
let mut config = CliConfig::recent_for_tests();
|
|
|
|
config.json_rpc_url = test_validator.rpc_url();
|
|
|
|
config.signers = vec![&default_signer];
|
|
|
|
|
|
|
|
let sender_pubkey = config.signers[0].pubkey();
|
|
|
|
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
|
|
|
|
let derived_address_seed = "seed".to_string();
|
|
|
|
let derived_address_program_id = solana_stake_program::id();
|
|
|
|
let derived_address = Pubkey::create_with_seed(
|
|
|
|
&sender_pubkey,
|
|
|
|
&derived_address_seed,
|
|
|
|
&derived_address_program_id,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &sender_pubkey, 1, &config).unwrap();
|
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &derived_address, 50_000, &config)
|
|
|
|
.unwrap();
|
|
|
|
check_recent_balance(1, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(50_000, &rpc_client, &derived_address);
|
|
|
|
check_recent_balance(0, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
check_ready(&rpc_client);
|
|
|
|
|
|
|
|
// Transfer with seed
|
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
amount: SpendAmount::Some(50_000),
|
|
|
|
to: recipient_pubkey,
|
|
|
|
from: 0,
|
|
|
|
sign_only: false,
|
2021-03-12 18:37:39 -08:00
|
|
|
dump_transaction_message: false,
|
2021-03-22 11:10:44 -07:00
|
|
|
allow_unfunded_recipient: true,
|
2021-02-17 23:08:12 -08:00
|
|
|
no_wait: false,
|
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
|
|
|
nonce_account: None,
|
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
|
|
|
derived_address_seed: Some(derived_address_seed),
|
|
|
|
derived_address_program_id: Some(derived_address_program_id),
|
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
|
|
|
check_recent_balance(0, &rpc_client, &sender_pubkey);
|
|
|
|
check_recent_balance(50_000, &rpc_client, &recipient_pubkey);
|
|
|
|
check_recent_balance(0, &rpc_client, &derived_address);
|
|
|
|
}
|