2020-02-21 13:55:53 -08:00
|
|
|
use solana_cli::cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig};
|
2019-12-10 00:24:44 -08:00
|
|
|
use solana_client::rpc_client::RpcClient;
|
2020-02-25 20:23:54 -08:00
|
|
|
use solana_core::validator::TestValidator;
|
2019-12-16 13:05:17 -08:00
|
|
|
use solana_faucet::faucet::run_local_faucet;
|
2019-12-10 00:24:44 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
hash::Hash,
|
|
|
|
pubkey::Pubkey,
|
2020-02-24 16:03:30 -08:00
|
|
|
signature::{keypair_from_seed, Keypair, Signer},
|
2020-01-09 15:22:48 -08:00
|
|
|
system_instruction::create_address_with_seed,
|
|
|
|
system_program,
|
2019-12-10 00:24:44 -08:00
|
|
|
};
|
2020-02-24 16:03:30 -08:00
|
|
|
use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration};
|
2019-12-10 00:24:44 -08: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_nonce() {
|
2020-02-25 20:23:54 -08:00
|
|
|
let TestValidator {
|
|
|
|
server,
|
|
|
|
leader_data,
|
|
|
|
alice,
|
|
|
|
ledger_path,
|
|
|
|
..
|
|
|
|
} = TestValidator::run();
|
2019-12-10 00:24:44 -08:00
|
|
|
let (sender, receiver) = channel();
|
2019-12-16 13:05:17 -08:00
|
|
|
run_local_faucet(alice, sender, None);
|
|
|
|
let faucet_addr = receiver.recv().unwrap();
|
2019-12-10 00:24:44 -08:00
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_socket(leader_data.rpc);
|
2020-02-24 16:03:30 -08:00
|
|
|
let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
|
2019-12-10 00:24:44 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
full_battery_tests(&rpc_client, &faucet_addr, json_rpc_url, None, false);
|
2020-01-09 15:22:48 -08:00
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonce_with_seed() {
|
2020-02-25 20:23:54 -08:00
|
|
|
let TestValidator {
|
|
|
|
server,
|
|
|
|
leader_data,
|
|
|
|
alice,
|
|
|
|
ledger_path,
|
|
|
|
..
|
|
|
|
} = TestValidator::run();
|
2020-01-09 15:22:48 -08:00
|
|
|
let (sender, receiver) = channel();
|
|
|
|
run_local_faucet(alice, sender, None);
|
|
|
|
let faucet_addr = receiver.recv().unwrap();
|
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_socket(leader_data.rpc);
|
2020-02-24 16:03:30 -08:00
|
|
|
let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
|
2020-01-09 15:22:48 -08:00
|
|
|
|
|
|
|
full_battery_tests(
|
|
|
|
&rpc_client,
|
|
|
|
&faucet_addr,
|
2020-02-24 16:03:30 -08:00
|
|
|
json_rpc_url,
|
2020-01-09 15:22:48 -08:00
|
|
|
Some(String::from("seed")),
|
2020-02-24 16:03:30 -08:00
|
|
|
false,
|
2019-12-17 06:34:21 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonce_with_authority() {
|
2020-02-25 20:23:54 -08:00
|
|
|
let TestValidator {
|
|
|
|
server,
|
|
|
|
leader_data,
|
|
|
|
alice,
|
|
|
|
ledger_path,
|
|
|
|
..
|
|
|
|
} = TestValidator::run();
|
2019-12-17 06:34:21 -08:00
|
|
|
let (sender, receiver) = channel();
|
2019-12-17 09:26:13 -08:00
|
|
|
run_local_faucet(alice, sender, None);
|
|
|
|
let faucet_addr = receiver.recv().unwrap();
|
2019-12-17 06:34:21 -08:00
|
|
|
|
|
|
|
let rpc_client = RpcClient::new_socket(leader_data.rpc);
|
2020-02-24 16:03:30 -08:00
|
|
|
let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port());
|
2019-12-17 06:34:21 -08:00
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
full_battery_tests(&rpc_client, &faucet_addr, json_rpc_url, None, true);
|
2019-12-17 06:34:21 -08:00
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_battery_tests(
|
|
|
|
rpc_client: &RpcClient,
|
2019-12-17 09:26:13 -08:00
|
|
|
faucet_addr: &std::net::SocketAddr,
|
2020-02-24 16:03:30 -08:00
|
|
|
json_rpc_url: String,
|
2020-01-09 15:22:48 -08:00
|
|
|
seed: Option<String>,
|
2020-02-24 16:03:30 -08:00
|
|
|
use_nonce_authority: bool,
|
2019-12-17 06:34:21 -08:00
|
|
|
) {
|
2020-02-24 16:03:30 -08:00
|
|
|
let mut config_payer = CliConfig::default();
|
|
|
|
config_payer.json_rpc_url = json_rpc_url.clone();
|
|
|
|
let payer = Keypair::new();
|
|
|
|
config_payer.signers = vec![&payer];
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
request_and_confirm_airdrop(
|
|
|
|
&rpc_client,
|
2019-12-16 13:05:17 -08:00
|
|
|
&faucet_addr,
|
2020-02-24 16:03:30 -08:00
|
|
|
&config_payer.signers[0].pubkey(),
|
2019-12-10 00:24:44 -08:00
|
|
|
2000,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
check_balance(2000, &rpc_client, &config_payer.signers[0].pubkey());
|
|
|
|
|
|
|
|
let mut config_nonce = CliConfig::default();
|
|
|
|
config_nonce.json_rpc_url = json_rpc_url;
|
|
|
|
let nonce_keypair = keypair_from_seed(&[0u8; 32]).unwrap();
|
|
|
|
config_nonce.signers = vec![&nonce_keypair];
|
2019-12-10 00:24:44 -08:00
|
|
|
|
2020-01-09 15:22:48 -08:00
|
|
|
let nonce_account = if let Some(seed) = seed.as_ref() {
|
2020-02-24 16:03:30 -08:00
|
|
|
create_address_with_seed(
|
|
|
|
&config_nonce.signers[0].pubkey(),
|
|
|
|
seed,
|
|
|
|
&system_program::id(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
} else {
|
|
|
|
nonce_keypair.pubkey()
|
|
|
|
};
|
|
|
|
|
|
|
|
let nonce_authority = Keypair::new();
|
|
|
|
let optional_authority = if use_nonce_authority {
|
|
|
|
Some(nonce_authority.pubkey())
|
2020-01-09 15:22:48 -08:00
|
|
|
} else {
|
2020-02-24 16:03:30 -08:00
|
|
|
None
|
2020-01-09 15:22:48 -08:00
|
|
|
};
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
// Create nonce account
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers.push(&nonce_keypair);
|
2019-12-10 00:24:44 -08:00
|
|
|
config_payer.command = CliCommand::CreateNonceAccount {
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_account: 1,
|
2020-01-09 15:22:48 -08:00
|
|
|
seed,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: optional_authority,
|
2019-12-10 00:24:44 -08:00
|
|
|
lamports: 1000,
|
|
|
|
};
|
2020-01-09 15:22:48 -08:00
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
process_command(&config_payer).unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
check_balance(1000, &rpc_client, &config_payer.signers[0].pubkey());
|
2020-01-09 15:22:48 -08:00
|
|
|
check_balance(1000, &rpc_client, &nonce_account);
|
2019-12-10 00:24:44 -08:00
|
|
|
|
|
|
|
// Get nonce
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers.pop();
|
2020-01-09 15:22:48 -08:00
|
|
|
config_payer.command = CliCommand::GetNonce(nonce_account);
|
2019-12-10 00:24:44 -08:00
|
|
|
let first_nonce_string = process_command(&config_payer).unwrap();
|
|
|
|
let first_nonce = first_nonce_string.parse::<Hash>().unwrap();
|
|
|
|
|
|
|
|
// Get nonce
|
2020-01-09 15:22:48 -08:00
|
|
|
config_payer.command = CliCommand::GetNonce(nonce_account);
|
2019-12-10 00:24:44 -08:00
|
|
|
let second_nonce_string = process_command(&config_payer).unwrap();
|
|
|
|
let second_nonce = second_nonce_string.parse::<Hash>().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(first_nonce, second_nonce);
|
|
|
|
|
2020-02-24 16:03:30 -08:00
|
|
|
let mut authorized_signers: Vec<&dyn Signer> = vec![&payer];
|
|
|
|
let index = if use_nonce_authority {
|
|
|
|
authorized_signers.push(&nonce_authority);
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-12-10 00:24:44 -08:00
|
|
|
// New nonce
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers = authorized_signers.clone();
|
2019-12-17 06:34:21 -08:00
|
|
|
config_payer.command = CliCommand::NewNonce {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: index,
|
2019-12-17 06:34:21 -08:00
|
|
|
};
|
2019-12-10 00:24:44 -08:00
|
|
|
process_command(&config_payer).unwrap();
|
|
|
|
|
|
|
|
// Get nonce
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers = vec![&payer];
|
2020-01-09 15:22:48 -08:00
|
|
|
config_payer.command = CliCommand::GetNonce(nonce_account);
|
2019-12-10 00:24:44 -08:00
|
|
|
let third_nonce_string = process_command(&config_payer).unwrap();
|
|
|
|
let third_nonce = third_nonce_string.parse::<Hash>().unwrap();
|
|
|
|
|
|
|
|
assert_ne!(first_nonce, third_nonce);
|
|
|
|
|
|
|
|
// Withdraw from nonce account
|
|
|
|
let payee_pubkey = Pubkey::new_rand();
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers = authorized_signers;
|
2019-12-10 00:24:44 -08:00
|
|
|
config_payer.command = CliCommand::WithdrawFromNonceAccount {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: index,
|
2019-12-10 00:24:44 -08:00
|
|
|
destination_account_pubkey: payee_pubkey,
|
|
|
|
lamports: 100,
|
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
check_balance(1000, &rpc_client, &config_payer.signers[0].pubkey());
|
2020-01-09 15:22:48 -08:00
|
|
|
check_balance(900, &rpc_client, &nonce_account);
|
2019-12-10 00:24:44 -08:00
|
|
|
check_balance(100, &rpc_client, &payee_pubkey);
|
|
|
|
|
|
|
|
// Show nonce account
|
|
|
|
config_payer.command = CliCommand::ShowNonceAccount {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account_pubkey: nonce_account,
|
2019-12-10 00:24:44 -08:00
|
|
|
use_lamports_unit: true,
|
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap();
|
2019-12-19 16:13:01 -08:00
|
|
|
|
|
|
|
// Set new authority
|
|
|
|
let new_authority = Keypair::new();
|
|
|
|
config_payer.command = CliCommand::AuthorizeNonceAccount {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: index,
|
|
|
|
new_authority: new_authority.pubkey(),
|
2019-12-19 16:13:01 -08:00
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap();
|
|
|
|
|
|
|
|
// Old authority fails now
|
|
|
|
config_payer.command = CliCommand::NewNonce {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: index,
|
2019-12-19 16:13:01 -08:00
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap_err();
|
|
|
|
|
|
|
|
// New authority can advance nonce
|
2020-02-24 16:03:30 -08:00
|
|
|
config_payer.signers = vec![&payer, &new_authority];
|
2019-12-19 16:13:01 -08:00
|
|
|
config_payer.command = CliCommand::NewNonce {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 1,
|
2019-12-19 16:13:01 -08:00
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap();
|
|
|
|
|
|
|
|
// New authority can withdraw from nonce account
|
|
|
|
config_payer.command = CliCommand::WithdrawFromNonceAccount {
|
2020-01-09 15:22:48 -08:00
|
|
|
nonce_account,
|
2020-02-24 16:03:30 -08:00
|
|
|
nonce_authority: 1,
|
2019-12-19 16:13:01 -08:00
|
|
|
destination_account_pubkey: payee_pubkey,
|
|
|
|
lamports: 100,
|
|
|
|
};
|
|
|
|
process_command(&config_payer).unwrap();
|
2020-02-24 16:03:30 -08:00
|
|
|
check_balance(1000, &rpc_client, &config_payer.signers[0].pubkey());
|
2020-01-09 15:22:48 -08:00
|
|
|
check_balance(800, &rpc_client, &nonce_account);
|
2019-12-19 16:13:01 -08:00
|
|
|
check_balance(200, &rpc_client, &payee_pubkey);
|
2019-12-10 00:24:44 -08:00
|
|
|
}
|