2020-02-21 14:55:53 -07:00
|
|
|
use solana_clap_utils::keypair::presigner_from_pubkey_sigs;
|
2020-02-07 12:16:35 -07:00
|
|
|
use solana_cli::{
|
|
|
|
cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig},
|
2020-03-10 13:00:15 -06:00
|
|
|
nonce,
|
2020-03-11 12:14:15 -06:00
|
|
|
offline::{
|
|
|
|
blockhash_query::{self, BlockhashQuery},
|
|
|
|
parse_sign_only_reply_string,
|
|
|
|
},
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
|
|
|
use solana_client::rpc_client::RpcClient;
|
2020-02-26 12:23:54 +08:00
|
|
|
use solana_core::validator::{TestValidator, TestValidatorOptions};
|
2020-02-07 12:16:35 -07:00
|
|
|
use solana_faucet::faucet::run_local_faucet;
|
|
|
|
use solana_sdk::{
|
2020-03-10 13:00:15 -06:00
|
|
|
nonce::State as NonceState,
|
2020-02-07 12:16:35 -07:00
|
|
|
pubkey::Pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
signature::{keypair_from_seed, Keypair, Signer},
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
2020-02-24 17:03:30 -07:00
|
|
|
use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration};
|
2020-02-07 12:16:35 -07:00
|
|
|
|
|
|
|
fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) {
|
|
|
|
(0..5).for_each(|tries| {
|
|
|
|
let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap();
|
|
|
|
if balance == expected_balance {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if tries == 4 {
|
|
|
|
assert_eq!(balance, expected_balance);
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(500));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer() {
|
2020-02-26 12:23:54 +08:00
|
|
|
let TestValidator {
|
|
|
|
server,
|
|
|
|
leader_data,
|
|
|
|
alice: mint_keypair,
|
|
|
|
ledger_path,
|
|
|
|
..
|
|
|
|
} = TestValidator::run_with_options(TestValidatorOptions {
|
|
|
|
fees: 1,
|
|
|
|
bootstrap_validator_lamports: 42_000,
|
|
|
|
});
|
2020-02-07 12:16:35 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-02-24 17:03:30 -07:00
|
|
|
let default_signer = Keypair::new();
|
|
|
|
let default_offline_signer = Keypair::new();
|
|
|
|
|
2020-02-07 12:16:35 -07:00
|
|
|
let mut config = CliConfig::default();
|
|
|
|
config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-07 12:16:35 -07:00
|
|
|
|
2020-02-24 17:03:30 -07:00
|
|
|
let sender_pubkey = config.signers[0].pubkey();
|
2020-02-07 12:16:35 -07:00
|
|
|
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
|
|
|
|
|
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &sender_pubkey, 50_000).unwrap();
|
|
|
|
check_balance(50_000, &rpc_client, &sender_pubkey);
|
|
|
|
check_balance(0, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
// Plain ole transfer
|
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
sign_only: false,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::All(blockhash_query::Source::Cluster),
|
2020-02-07 12:16:35 -07:00
|
|
|
nonce_account: None,
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(49_989, &rpc_client, &sender_pubkey);
|
2020-02-07 12:16:35 -07:00
|
|
|
check_balance(10, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
let mut offline = CliConfig::default();
|
|
|
|
offline.json_rpc_url = String::default();
|
2020-02-24 17:03:30 -07:00
|
|
|
offline.signers = vec![&default_offline_signer];
|
2020-02-07 12:16:35 -07:00
|
|
|
// Verify we cannot contact the cluster
|
|
|
|
offline.command = CliCommand::ClusterVersion;
|
|
|
|
process_command(&offline).unwrap_err();
|
|
|
|
|
2020-02-24 17:03:30 -07:00
|
|
|
let offline_pubkey = offline.signers[0].pubkey();
|
2020-02-07 12:16:35 -07:00
|
|
|
request_and_confirm_airdrop(&rpc_client, &faucet_addr, &offline_pubkey, 50).unwrap();
|
|
|
|
check_balance(50, &rpc_client, &offline_pubkey);
|
|
|
|
|
|
|
|
// Offline transfer
|
|
|
|
let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap();
|
|
|
|
offline.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
sign_only: true,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::None(blockhash),
|
2020-02-07 12:16:35 -07:00
|
|
|
nonce_account: None,
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
|
|
|
let sign_only_reply = process_command(&offline).unwrap();
|
|
|
|
let (blockhash, signers) = parse_sign_only_reply_string(&sign_only_reply);
|
2020-02-21 14:55:53 -07:00
|
|
|
let offline_presigner = presigner_from_pubkey_sigs(&offline_pubkey, &signers).unwrap();
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&offline_presigner];
|
2020-02-07 12:16:35 -07:00
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
sign_only: false,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(blockhash_query::Source::Cluster, blockhash),
|
2020-02-07 12:16:35 -07:00
|
|
|
nonce_account: None,
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(39, &rpc_client, &offline_pubkey);
|
2020-02-07 12:16:35 -07:00
|
|
|
check_balance(20, &rpc_client, &recipient_pubkey);
|
|
|
|
|
|
|
|
// Create nonce account
|
|
|
|
let nonce_account = keypair_from_seed(&[3u8; 32]).unwrap();
|
|
|
|
let minimum_nonce_balance = rpc_client
|
2020-03-10 13:00:15 -06:00
|
|
|
.get_minimum_balance_for_rent_exemption(NonceState::size())
|
2020-02-07 12:16:35 -07:00
|
|
|
.unwrap();
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&default_signer, &nonce_account];
|
2020-02-07 12:16:35 -07:00
|
|
|
config.command = CliCommand::CreateNonceAccount {
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_account: 1,
|
2020-02-07 12:16:35 -07:00
|
|
|
seed: None,
|
|
|
|
nonce_authority: None,
|
|
|
|
lamports: minimum_nonce_balance,
|
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(49_987 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
2020-02-07 12:16:35 -07:00
|
|
|
|
|
|
|
// Fetch nonce hash
|
2020-03-10 13:00:15 -06:00
|
|
|
let nonce_hash = nonce::get_account(&rpc_client, &nonce_account.pubkey())
|
|
|
|
.and_then(|ref a| nonce::data_from_account(a))
|
2020-03-03 19:39:09 -07:00
|
|
|
.unwrap()
|
2020-03-10 13:00:15 -06:00
|
|
|
.blockhash;
|
2020-02-07 12:16:35 -07:00
|
|
|
|
|
|
|
// Nonced transfer
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-07 12:16:35 -07:00
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
sign_only: false,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(
|
|
|
|
blockhash_query::Source::NonceAccount(nonce_account.pubkey()),
|
|
|
|
nonce_hash,
|
|
|
|
),
|
2020-02-07 12:16:35 -07:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-07 12:16:35 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(49_976 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
2020-02-07 12:16:35 -07:00
|
|
|
check_balance(30, &rpc_client, &recipient_pubkey);
|
2020-03-10 13:00:15 -06:00
|
|
|
let new_nonce_hash = nonce::get_account(&rpc_client, &nonce_account.pubkey())
|
|
|
|
.and_then(|ref a| nonce::data_from_account(a))
|
2020-03-03 19:39:09 -07:00
|
|
|
.unwrap()
|
2020-03-10 13:00:15 -06:00
|
|
|
.blockhash;
|
2020-02-07 12:16:35 -07:00
|
|
|
assert_ne!(nonce_hash, new_nonce_hash);
|
|
|
|
|
2020-02-10 23:34:14 -07:00
|
|
|
// Assign nonce authority to offline
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&default_signer];
|
2020-02-10 23:34:14 -07:00
|
|
|
config.command = CliCommand::AuthorizeNonceAccount {
|
|
|
|
nonce_account: nonce_account.pubkey(),
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
2020-02-10 23:34:14 -07:00
|
|
|
new_authority: offline_pubkey,
|
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(49_975 - minimum_nonce_balance, &rpc_client, &sender_pubkey);
|
2020-02-10 23:34:14 -07:00
|
|
|
|
|
|
|
// Fetch nonce hash
|
2020-03-10 13:00:15 -06:00
|
|
|
let nonce_hash = nonce::get_account(&rpc_client, &nonce_account.pubkey())
|
|
|
|
.and_then(|ref a| nonce::data_from_account(a))
|
2020-03-03 19:39:09 -07:00
|
|
|
.unwrap()
|
2020-03-10 13:00:15 -06:00
|
|
|
.blockhash;
|
2020-02-10 23:34:14 -07:00
|
|
|
|
|
|
|
// Offline, nonced transfer
|
2020-02-24 17:03:30 -07:00
|
|
|
offline.signers = vec![&default_offline_signer];
|
2020-02-10 23:34:14 -07:00
|
|
|
offline.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-10 23:34:14 -07:00
|
|
|
sign_only: true,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::None(nonce_hash),
|
2020-02-10 23:34:14 -07:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-10 23:34:14 -07:00
|
|
|
};
|
|
|
|
let sign_only_reply = process_command(&offline).unwrap();
|
|
|
|
let (blockhash, signers) = parse_sign_only_reply_string(&sign_only_reply);
|
2020-02-21 14:55:53 -07:00
|
|
|
let offline_presigner = presigner_from_pubkey_sigs(&offline_pubkey, &signers).unwrap();
|
2020-02-24 17:03:30 -07:00
|
|
|
config.signers = vec![&offline_presigner];
|
2020-02-10 23:34:14 -07:00
|
|
|
config.command = CliCommand::Transfer {
|
|
|
|
lamports: 10,
|
|
|
|
to: recipient_pubkey,
|
2020-02-24 17:03:30 -07:00
|
|
|
from: 0,
|
2020-02-10 23:34:14 -07:00
|
|
|
sign_only: false,
|
2020-03-11 12:14:15 -06:00
|
|
|
blockhash_query: BlockhashQuery::FeeCalculator(
|
|
|
|
blockhash_query::Source::NonceAccount(nonce_account.pubkey()),
|
|
|
|
blockhash,
|
|
|
|
),
|
2020-02-10 23:34:14 -07:00
|
|
|
nonce_account: Some(nonce_account.pubkey()),
|
2020-02-24 17:03:30 -07:00
|
|
|
nonce_authority: 0,
|
|
|
|
fee_payer: 0,
|
2020-02-10 23:34:14 -07:00
|
|
|
};
|
|
|
|
process_command(&config).unwrap();
|
2020-02-11 22:48:04 -07:00
|
|
|
check_balance(28, &rpc_client, &offline_pubkey);
|
2020-02-10 23:34:14 -07:00
|
|
|
check_balance(40, &rpc_client, &recipient_pubkey);
|
|
|
|
|
2020-02-07 12:16:35 -07:00
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|