solana/cli/src/cli.rs

1944 lines
68 KiB
Rust
Raw Normal View History

use crate::{
cluster_query::*, display::println_name_value, input_parsers::*, input_validators::*, stake::*,
storage::*, validator_info::*, vote::*,
};
2018-09-19 12:44:16 -07:00
use chrono::prelude::*;
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use log::*;
2019-04-25 10:29:44 -07:00
use num_traits::FromPrimitive;
use serde_json::{self, json};
use solana_budget_api::budget_instruction::{self, BudgetError};
use solana_client::{client_error::ClientError, rpc_client::RpcClient};
#[cfg(not(test))]
use solana_drone::drone::request_airdrop_transaction;
#[cfg(test)]
use solana_drone::drone_mock::request_airdrop_transaction;
use solana_sdk::{
bpf_loader,
fee_calculator::FeeCalculator,
hash::Hash,
instruction::InstructionError,
instruction_processor_utils::DecodeError,
loader_instruction,
message::Message,
2019-10-01 10:53:28 -07:00
native_token::lamports_to_sol,
pubkey::Pubkey,
signature::{Keypair, KeypairUtil, Signature},
system_instruction::SystemError,
system_transaction,
transaction::{Transaction, TransactionError},
};
use solana_stake_api::stake_state::{Authorized, Lockup, StakeAuthorize};
use solana_storage_api::storage_instruction::StorageAccountType;
use solana_vote_api::vote_state::{VoteAuthorize, VoteInit};
use std::{
fs::File,
io::{Read, Write},
net::{IpAddr, SocketAddr},
thread::sleep,
time::Duration,
{error, fmt},
};
const USERDATA_CHUNK_SIZE: usize = 229; // Keep program chunks under PACKET_DATA_SIZE
2018-10-22 21:21:33 -07:00
2018-09-14 01:59:09 -07:00
#[derive(Debug, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum CliCommand {
// Cluster Query Commands
ClusterVersion,
Fees,
GetEpochInfo,
GetGenesisBlockhash,
GetSlot,
GetTransactionCount,
Ping {
interval: Duration,
count: Option<u64>,
timeout: Duration,
},
// Program Deployment
Deploy(String),
// Stake Commands
CreateStakeAccount(Pubkey, Authorized, Lockup, u64),
DeactivateStake(Pubkey),
DelegateStake(Pubkey, Pubkey, bool),
RedeemVoteCredits(Pubkey, Pubkey),
ShowStakeAccount {
pubkey: Pubkey,
use_lamports_unit: bool,
},
StakeAuthorize(Pubkey, Pubkey, StakeAuthorize),
WithdrawStake(Pubkey, Pubkey, u64),
// Storage Commands
CreateStorageAccount {
account_owner: Pubkey,
storage_account_pubkey: Pubkey,
account_type: StorageAccountType,
},
ClaimStorageReward {
node_account_pubkey: Pubkey,
storage_account_pubkey: Pubkey,
},
ShowStorageAccount(Pubkey),
// Validator Info Commands
GetValidatorInfo(Option<Pubkey>),
SetValidatorInfo(ValidatorInfo, Option<Pubkey>),
// Vote Commands
CreateVoteAccount(Pubkey, VoteInit),
ShowVoteAccount {
pubkey: Pubkey,
use_lamports_unit: bool,
},
Uptime {
pubkey: Pubkey,
aggregate: bool,
span: Option<u64>,
},
VoteAuthorize(Pubkey, Pubkey, VoteAuthorize),
// Wallet Commands
Address,
Airdrop {
drone_host: Option<IpAddr>,
drone_port: u16,
lamports: u64,
use_lamports_unit: bool,
},
Balance {
pubkey: Pubkey,
use_lamports_unit: bool,
},
Cancel(Pubkey),
Confirm(Signature),
Pay {
lamports: u64,
to: Pubkey,
timestamp: Option<DateTime<Utc>>,
timestamp_pubkey: Option<Pubkey>,
witnesses: Option<Vec<Pubkey>>,
cancelable: Option<Pubkey>,
},
ShowAccount {
pubkey: Pubkey,
output_file: Option<String>,
use_lamports_unit: bool,
},
TimeElapsed(Pubkey, Pubkey, DateTime<Utc>), // TimeElapsed(to, process_id, timestamp)
Witness(Pubkey, Pubkey), // Witness(to, process_id)
}
#[derive(Debug, Clone)]
pub enum CliError {
BadParameter(String),
CommandNotRecognized(String),
InsufficientFundsForFee,
2018-10-22 21:21:33 -07:00
DynamicProgramError(String),
2018-09-20 22:27:06 -07:00
RpcRequestError(String),
KeypairFileNotFound(String),
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid")
}
}
impl error::Error for CliError {
fn description(&self) -> &str {
"invalid"
}
fn cause(&self) -> Option<&dyn error::Error> {
// Generic error, underlying cause isn't tracked.
None
}
}
pub struct CliConfig {
pub command: CliCommand,
pub json_rpc_url: String,
pub keypair: Keypair,
2019-09-02 12:53:13 -07:00
pub keypair_path: String,
pub rpc_client: Option<RpcClient>,
}
impl Default for CliConfig {
fn default() -> CliConfig {
let mut keypair_path = dirs::home_dir().expect("home directory");
keypair_path.extend(&[".config", "solana", "id.json"]);
CliConfig {
command: CliCommand::Balance {
pubkey: Pubkey::default(),
use_lamports_unit: false,
},
json_rpc_url: "http://127.0.0.1:8899".to_string(),
2019-04-02 06:08:11 -07:00
keypair: Keypair::new(),
keypair_path: keypair_path.to_str().unwrap().to_string(),
rpc_client: None,
}
}
}
pub fn parse_command(
pubkey: &Pubkey,
matches: &ArgMatches<'_>,
) -> Result<CliCommand, Box<dyn error::Error>> {
let response = match matches.subcommand() {
// Cluster Query Commands
("cluster-version", Some(_matches)) => Ok(CliCommand::ClusterVersion),
("fees", Some(_fees_matches)) => Ok(CliCommand::Fees),
("get-epoch-info", Some(_matches)) => Ok(CliCommand::GetEpochInfo),
("get-genesis-blockhash", Some(_matches)) => Ok(CliCommand::GetGenesisBlockhash),
("get-slot", Some(_matches)) => Ok(CliCommand::GetSlot),
("get-transaction-count", Some(_matches)) => Ok(CliCommand::GetTransactionCount),
("ping", Some(matches)) => parse_cluster_ping(matches),
// Program Deployment
("deploy", Some(deploy_matches)) => Ok(CliCommand::Deploy(
deploy_matches
.value_of("program_location")
.unwrap()
.to_string(),
)),
// Stake Commands
("create-stake-account", Some(matches)) => parse_stake_create_account(pubkey, matches),
("delegate-stake", Some(matches)) => parse_stake_delegate_stake(matches),
("withdraw-stake", Some(matches)) => parse_stake_withdraw_stake(matches),
("deactivate-stake", Some(matches)) => parse_stake_deactivate_stake(matches),
("stake-authorize-staker", Some(matches)) => {
parse_stake_authorize(matches, StakeAuthorize::Staker)
}
("stake-authorize-withdrawer", Some(matches)) => {
parse_stake_authorize(matches, StakeAuthorize::Withdrawer)
}
("redeem-vote-credits", Some(matches)) => parse_redeem_vote_credits(matches),
("show-stake-account", Some(matches)) => parse_show_stake_account(matches),
// Storage Commands
("create-replicator-storage-account", Some(matches)) => {
parse_storage_create_replicator_account(matches)
}
("create-validator-storage-account", Some(matches)) => {
parse_storage_create_validator_account(matches)
}
("claim-storage-reward", Some(matches)) => parse_storage_claim_reward(matches),
("show-storage-account", Some(matches)) => parse_storage_get_account_command(matches),
// Validator Info Commands
("validator-info", Some(matches)) => match matches.subcommand() {
("publish", Some(matches)) => parse_validator_info_command(matches, pubkey),
("get", Some(matches)) => parse_get_validator_info_command(matches),
("", None) => {
eprintln!("{}", matches.usage());
Err(CliError::CommandNotRecognized(
"no validator-info subcommand given".to_string(),
))
}
_ => unreachable!(),
},
// Vote Commands
("create-vote-account", Some(matches)) => parse_vote_create_account(pubkey, matches),
("vote-authorize-voter", Some(matches)) => {
parse_vote_authorize(matches, VoteAuthorize::Voter)
}
("vote-authorize-withdrawer", Some(matches)) => {
parse_vote_authorize(matches, VoteAuthorize::Withdrawer)
}
("show-vote-account", Some(matches)) => parse_vote_get_account_command(matches),
("uptime", Some(matches)) => parse_vote_uptime_command(matches),
// Wallet Commands
("address", Some(_address_matches)) => Ok(CliCommand::Address),
("airdrop", Some(airdrop_matches)) => {
let drone_port = airdrop_matches
.value_of("drone_port")
.unwrap()
.parse()
.or_else(|err| {
Err(CliError::BadParameter(format!(
"Invalid drone port: {:?}",
err
)))
})?;
let drone_host = if let Some(drone_host) = matches.value_of("drone_host") {
Some(solana_netutil::parse_host(drone_host).or_else(|err| {
Err(CliError::BadParameter(format!(
"Invalid drone host: {:?}",
err
)))
})?)
} else {
None
};
let lamports = amount_of(airdrop_matches, "amount", "unit").expect("Invalid amount");
let use_lamports_unit = airdrop_matches.value_of("unit").is_some()
&& airdrop_matches.value_of("unit").unwrap() == "lamports";
Ok(CliCommand::Airdrop {
drone_host,
drone_port,
lamports,
use_lamports_unit,
})
}
2019-03-20 09:44:16 -07:00
("balance", Some(balance_matches)) => {
let pubkey = pubkey_of(&balance_matches, "pubkey").unwrap_or(*pubkey);
let use_lamports_unit = balance_matches.is_present("lamports");
Ok(CliCommand::Balance {
pubkey,
use_lamports_unit,
})
2019-03-20 09:44:16 -07:00
}
2018-09-19 12:44:16 -07:00
("cancel", Some(cancel_matches)) => {
let process_id = value_of(cancel_matches, "process_id").unwrap();
Ok(CliCommand::Cancel(process_id))
2018-09-19 12:44:16 -07:00
}
2018-09-20 22:27:06 -07:00
("confirm", Some(confirm_matches)) => {
match confirm_matches.value_of("signature").unwrap().parse() {
Ok(signature) => Ok(CliCommand::Confirm(signature)),
_ => {
eprintln!("{}", confirm_matches.usage());
Err(CliError::BadParameter("Invalid signature".to_string()))
}
2018-09-20 22:27:06 -07:00
}
}
("pay", Some(pay_matches)) => {
let lamports = amount_of(pay_matches, "amount", "unit").expect("Invalid amount");
let to = value_of(&pay_matches, "to").unwrap_or(*pubkey);
2018-09-19 12:44:16 -07:00
let timestamp = if pay_matches.is_present("timestamp") {
// Parse input for serde_json
2018-09-21 20:48:46 -07:00
let date_string = if !pay_matches.value_of("timestamp").unwrap().contains('Z') {
2018-09-19 12:44:16 -07:00
format!("\"{}Z\"", pay_matches.value_of("timestamp").unwrap())
} else {
format!("\"{}\"", pay_matches.value_of("timestamp").unwrap())
};
Some(serde_json::from_str(&date_string)?)
} else {
None
};
let timestamp_pubkey = value_of(&pay_matches, "timestamp_pubkey");
let witnesses = values_of(&pay_matches, "witness");
2018-09-19 12:44:16 -07:00
let cancelable = if pay_matches.is_present("cancelable") {
Some(*pubkey)
2018-09-19 12:44:16 -07:00
} else {
None
};
Ok(CliCommand::Pay {
2019-03-05 17:22:46 -08:00
lamports,
2018-09-19 12:44:16 -07:00
to,
timestamp,
timestamp_pubkey,
witnesses,
2018-09-19 12:44:16 -07:00
cancelable,
})
2018-09-19 12:44:16 -07:00
}
("show-account", Some(matches)) => {
let account_pubkey = pubkey_of(matches, "account_pubkey").unwrap();
let output_file = matches.value_of("output_file");
let use_lamports_unit = matches.is_present("lamports");
Ok(CliCommand::ShowAccount {
pubkey: account_pubkey,
output_file: output_file.map(ToString::to_string),
use_lamports_unit,
2019-08-12 21:33:13 -07:00
})
}
2018-09-19 12:44:16 -07:00
("send-signature", Some(sig_matches)) => {
let to = value_of(&sig_matches, "to").unwrap();
let process_id = value_of(&sig_matches, "process_id").unwrap();
Ok(CliCommand::Witness(to, process_id))
2018-09-19 12:44:16 -07:00
}
("send-timestamp", Some(timestamp_matches)) => {
let to = value_of(&timestamp_matches, "to").unwrap();
let process_id = value_of(&timestamp_matches, "process_id").unwrap();
let dt = if timestamp_matches.is_present("datetime") {
2018-09-19 12:44:16 -07:00
// Parse input for serde_json
let date_string = if !timestamp_matches
.value_of("datetime")
.unwrap()
2018-09-21 20:48:46 -07:00
.contains('Z')
2018-09-19 12:44:16 -07:00
{
format!("\"{}Z\"", timestamp_matches.value_of("datetime").unwrap())
} else {
format!("\"{}\"", timestamp_matches.value_of("datetime").unwrap())
};
serde_json::from_str(&date_string)?
} else {
Utc::now()
};
Ok(CliCommand::TimeElapsed(to, process_id, dt))
}
("", None) => {
2018-09-24 09:23:16 -07:00
eprintln!("{}", matches.usage());
Err(CliError::CommandNotRecognized(
"no subcommand given".to_string(),
))
}
_ => unreachable!(),
}?;
Ok(response)
}
pub type ProcessResult = Result<String, Box<dyn std::error::Error>>;
pub fn check_account_for_fee(
rpc_client: &RpcClient,
config: &CliConfig,
fee_calculator: &FeeCalculator,
message: &Message,
) -> Result<(), Box<dyn error::Error>> {
check_account_for_multiple_fees(rpc_client, config, fee_calculator, &[message])
}
fn check_account_for_multiple_fees(
rpc_client: &RpcClient,
config: &CliConfig,
fee_calculator: &FeeCalculator,
messages: &[&Message],
) -> Result<(), Box<dyn error::Error>> {
let balance = rpc_client.retry_get_balance(&config.keypair.pubkey(), 5)?;
if let Some(lamports) = balance {
if lamports
>= messages
.iter()
.map(|message| fee_calculator.calculate_fee(message))
.sum()
{
return Ok(());
}
}
Err(CliError::InsufficientFundsForFee.into())
}
pub fn check_unique_pubkeys(
pubkey0: (&Pubkey, String),
pubkey1: (&Pubkey, String),
) -> Result<(), CliError> {
if pubkey0.0 == pubkey1.0 {
Err(CliError::BadParameter(format!(
"Identical pubkeys found: `{}` and `{}` must be unique",
pubkey0.1, pubkey1.1
)))
} else {
Ok(())
}
}
fn process_airdrop(
rpc_client: &RpcClient,
config: &CliConfig,
drone_addr: &SocketAddr,
2019-03-05 17:22:46 -08:00
lamports: u64,
use_lamports_unit: bool,
) -> ProcessResult {
println!(
"Requesting airdrop of {} from {}",
build_balance_message(lamports, use_lamports_unit),
drone_addr
);
2019-04-02 06:08:11 -07:00
let previous_balance = match rpc_client.retry_get_balance(&config.keypair.pubkey(), 5)? {
2019-03-05 17:22:46 -08:00
Some(lamports) => lamports,
2019-10-02 18:33:01 -07:00
None => {
return Err(CliError::RpcRequestError(
2019-10-02 18:33:01 -07:00
"Received result of an unexpected type".to_string(),
)
.into())
}
};
request_and_confirm_airdrop(&rpc_client, drone_addr, &config.keypair.pubkey(), lamports)?;
let current_balance = rpc_client
2019-04-02 06:08:11 -07:00
.retry_get_balance(&config.keypair.pubkey(), 5)?
.unwrap_or(previous_balance);
Ok(build_balance_message(current_balance, use_lamports_unit))
}
fn process_balance(
pubkey: &Pubkey,
rpc_client: &RpcClient,
use_lamports_unit: bool,
) -> ProcessResult {
2019-03-20 09:44:16 -07:00
let balance = rpc_client.retry_get_balance(pubkey, 5)?;
match balance {
Some(lamports) => Ok(build_balance_message(lamports, use_lamports_unit)),
None => Err(
CliError::RpcRequestError("Received result of an unexpected type".to_string()).into(),
),
}
}
fn process_confirm(rpc_client: &RpcClient, signature: &Signature) -> ProcessResult {
2019-03-17 00:40:45 -07:00
match rpc_client.get_signature_status(&signature.to_string()) {
Ok(status) => {
2019-04-05 19:56:17 -07:00
if let Some(result) = status {
match result {
Ok(_) => Ok("Confirmed".to_string()),
Err(err) => Ok(format!("Transaction failed with error {:?}", err)),
}
} else {
Ok("Not found".to_string())
}
}
Err(err) => Err(CliError::RpcRequestError(format!("Unable to confirm: {:?}", err)).into()),
}
}
2019-08-09 22:48:57 -07:00
fn process_show_account(
rpc_client: &RpcClient,
_config: &CliConfig,
2019-08-09 22:48:57 -07:00
account_pubkey: &Pubkey,
output_file: &Option<String>,
use_lamports_unit: bool,
2019-08-09 22:48:57 -07:00
) -> ProcessResult {
let account = rpc_client.get_account(account_pubkey)?;
println!();
println!("Public Key: {}", account_pubkey);
println!(
"{:?}",
build_balance_message(account.lamports, use_lamports_unit)
);
2019-08-09 22:48:57 -07:00
println!("Owner: {}", account.owner);
println!("Executable: {}", account.executable);
if let Some(output_file) = output_file {
let mut f = File::create(output_file)?;
f.write_all(&account.data)?;
println!();
println!("Wrote account data to {}", output_file);
} else {
use pretty_hex::*;
println!("{:?}", account.data.hex_dump());
}
Ok("".to_string())
}
fn process_deploy(
rpc_client: &RpcClient,
config: &CliConfig,
program_location: &str,
) -> ProcessResult {
let program_id = Keypair::new();
let mut file = File::open(program_location).map_err(|err| {
CliError::DynamicProgramError(format!("Unable to open program file: {}", err).to_string())
})?;
let mut program_data = Vec::new();
file.read_to_end(&mut program_data).map_err(|err| {
CliError::DynamicProgramError(format!("Unable to read program file: {}", err).to_string())
})?;
// Build transactions to calculate fees
let mut messages: Vec<&Message> = Vec::new();
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(program_data.len())?;
let mut create_account_tx = system_transaction::create_account(
2019-04-02 06:08:11 -07:00
&config.keypair,
&program_id.pubkey(),
2019-03-02 10:25:16 -08:00
blockhash,
minimum_balance,
program_data.len() as u64,
&bpf_loader::id(),
);
messages.push(&create_account_tx.message);
let signers = [&config.keypair, &program_id];
let write_transactions: Vec<_> = program_data
.chunks(USERDATA_CHUNK_SIZE)
.zip(0..)
.map(|(chunk, i)| {
let instruction = loader_instruction::write(
2019-03-21 07:14:14 -07:00
&program_id.pubkey(),
&bpf_loader::id(),
(i * USERDATA_CHUNK_SIZE) as u32,
chunk.to_vec(),
2019-03-21 07:14:14 -07:00
);
let message = Message::new_with_payer(vec![instruction], Some(&signers[0].pubkey()));
Transaction::new(&signers, message, blockhash)
})
.collect();
for transaction in write_transactions.iter() {
messages.push(&transaction.message);
}
let instruction = loader_instruction::finalize(&program_id.pubkey(), &bpf_loader::id());
let message = Message::new_with_payer(vec![instruction], Some(&signers[0].pubkey()));
let mut finalize_tx = Transaction::new(&signers, message, blockhash);
messages.push(&finalize_tx.message);
check_account_for_multiple_fees(rpc_client, config, &fee_calculator, &messages)?;
trace!("Creating program account");
let result =
rpc_client.send_and_confirm_transaction(&mut create_account_tx, &[&config.keypair]);
log_instruction_custom_error::<SystemError>(result)
.map_err(|_| CliError::DynamicProgramError("Program allocate space failed".to_string()))?;
trace!("Writing program data");
rpc_client.send_and_confirm_transactions(write_transactions, &signers)?;
trace!("Finalizing program account");
rpc_client
.send_and_confirm_transaction(&mut finalize_tx, &signers)
.map_err(|_| {
CliError::DynamicProgramError("Program finalize transaction failed".to_string())
})?;
Ok(json!({
"programId": format!("{}", program_id.pubkey()),
})
.to_string())
}
fn process_pay(
rpc_client: &RpcClient,
config: &CliConfig,
2019-03-05 17:22:46 -08:00
lamports: u64,
to: &Pubkey,
timestamp: Option<DateTime<Utc>>,
timestamp_pubkey: Option<Pubkey>,
witnesses: &Option<Vec<Pubkey>>,
cancelable: Option<Pubkey>,
) -> ProcessResult {
check_unique_pubkeys(
(&config.keypair.pubkey(), "cli keypair".to_string()),
(to, "to".to_string()),
)?;
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
if timestamp == None && *witnesses == None {
let mut tx = system_transaction::transfer(&config.keypair, to, lamports, blockhash);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
log_instruction_custom_error::<SystemError>(result)
} else if *witnesses == None {
let dt = timestamp.unwrap();
let dt_pubkey = match timestamp_pubkey {
Some(pubkey) => pubkey,
2019-04-02 06:08:11 -07:00
None => config.keypair.pubkey(),
};
let contract_state = Keypair::new();
// Initializing contract
let ixs = budget_instruction::on_date(
2019-04-02 06:08:11 -07:00
&config.keypair.pubkey(),
to,
&contract_state.pubkey(),
dt,
&dt_pubkey,
cancelable,
2019-03-05 17:22:46 -08:00
lamports,
);
2019-04-02 06:08:11 -07:00
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, blockhash);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
2019-04-25 10:29:44 -07:00
let signature_str = log_instruction_custom_error::<BudgetError>(result)?;
Ok(json!({
"signature": signature_str,
"processId": format!("{}", contract_state.pubkey()),
})
.to_string())
} else if timestamp == None {
let (blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
let witness = if let Some(ref witness_vec) = *witnesses {
witness_vec[0]
} else {
return Err(CliError::BadParameter(
"Could not parse required signature pubkey(s)".to_string(),
2019-10-02 18:33:01 -07:00
)
.into());
};
let contract_state = Keypair::new();
// Initializing contract
let ixs = budget_instruction::when_signed(
2019-04-02 06:08:11 -07:00
&config.keypair.pubkey(),
to,
&contract_state.pubkey(),
&witness,
cancelable,
2019-03-05 17:22:46 -08:00
lamports,
);
2019-04-02 06:08:11 -07:00
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], ixs, blockhash);
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
2019-04-25 10:29:44 -07:00
let signature_str = log_instruction_custom_error::<BudgetError>(result)?;
Ok(json!({
"signature": signature_str,
"processId": format!("{}", contract_state.pubkey()),
})
.to_string())
} else {
Ok("Combo transactions not yet handled".to_string())
}
}
fn process_cancel(rpc_client: &RpcClient, config: &CliConfig, pubkey: &Pubkey) -> ProcessResult {
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let ix = budget_instruction::apply_signature(
2019-04-02 06:08:11 -07:00
&config.keypair.pubkey(),
pubkey,
&config.keypair.pubkey(),
);
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], vec![ix], blockhash);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
log_instruction_custom_error::<BudgetError>(result)
}
fn process_time_elapsed(
rpc_client: &RpcClient,
config: &CliConfig,
to: &Pubkey,
pubkey: &Pubkey,
dt: DateTime<Utc>,
) -> ProcessResult {
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let ix = budget_instruction::apply_timestamp(&config.keypair.pubkey(), pubkey, to, dt);
2019-04-02 06:08:11 -07:00
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], vec![ix], blockhash);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
log_instruction_custom_error::<BudgetError>(result)
}
fn process_witness(
rpc_client: &RpcClient,
config: &CliConfig,
to: &Pubkey,
pubkey: &Pubkey,
) -> ProcessResult {
let (blockhash, fee_calculator) = rpc_client.get_recent_blockhash()?;
let ix = budget_instruction::apply_signature(&config.keypair.pubkey(), pubkey, to);
2019-04-02 06:08:11 -07:00
let mut tx = Transaction::new_signed_instructions(&[&config.keypair], vec![ix], blockhash);
check_account_for_fee(rpc_client, config, &fee_calculator, &tx.message)?;
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&config.keypair]);
log_instruction_custom_error::<BudgetError>(result)
}
pub fn process_command(config: &CliConfig) -> ProcessResult {
2019-09-02 12:53:13 -07:00
println_name_value("Keypair:", &config.keypair_path);
if let CliCommand::Address = config.command {
// Get address of this client
2019-04-02 06:08:11 -07:00
return Ok(format!("{}", config.keypair.pubkey()));
}
2019-09-02 12:53:13 -07:00
println_name_value("RPC Endpoint:", &config.json_rpc_url);
let mut _rpc_client;
let rpc_client = if config.rpc_client.is_none() {
_rpc_client = RpcClient::new(config.json_rpc_url.to_string());
&_rpc_client
} else {
// Primarily for testing
config.rpc_client.as_ref().unwrap()
};
match &config.command {
// Cluster Query Commands
// Return software version of solana-cli and cluster entrypoint node
CliCommand::ClusterVersion => process_cluster_version(&rpc_client, config),
CliCommand::Fees => process_fees(&rpc_client),
CliCommand::GetGenesisBlockhash => process_get_genesis_blockhash(&rpc_client),
CliCommand::GetSlot => process_get_slot(&rpc_client),
CliCommand::GetEpochInfo => process_get_epoch_info(&rpc_client),
CliCommand::GetTransactionCount => process_get_transaction_count(&rpc_client),
CliCommand::Ping {
interval,
count,
timeout,
} => process_ping(&rpc_client, config, interval, count, timeout),
// Program Deployment
// Deploy a custom program to the chain
CliCommand::Deploy(ref program_location) => {
process_deploy(&rpc_client, config, program_location)
2019-03-05 17:22:46 -08:00
}
// Stake Commands
// Create stake account
CliCommand::CreateStakeAccount(stake_account_pubkey, authorized, lockup, lamports) => {
process_create_stake_account(
&rpc_client,
config,
&stake_account_pubkey,
&authorized,
lockup,
*lamports,
)
}
// Deactivate stake account
CliCommand::DeactivateStake(stake_account_pubkey) => {
process_deactivate_stake_account(&rpc_client, config, &stake_account_pubkey)
}
CliCommand::DelegateStake(stake_account_pubkey, vote_account_pubkey, force) => {
process_delegate_stake(
&rpc_client,
config,
&stake_account_pubkey,
&vote_account_pubkey,
*force,
)
}
CliCommand::RedeemVoteCredits(stake_account_pubkey, vote_account_pubkey) => {
process_redeem_vote_credits(
&rpc_client,
config,
&stake_account_pubkey,
&vote_account_pubkey,
)
}
CliCommand::ShowStakeAccount {
pubkey: stake_account_pubkey,
use_lamports_unit,
} => process_show_stake_account(
&rpc_client,
config,
&stake_account_pubkey,
*use_lamports_unit,
),
CliCommand::StakeAuthorize(
stake_account_pubkey,
new_authorized_pubkey,
stake_authorize,
) => process_stake_authorize(
&rpc_client,
config,
&stake_account_pubkey,
&new_authorized_pubkey,
*stake_authorize,
),
CliCommand::WithdrawStake(stake_account_pubkey, destination_account_pubkey, lamports) => {
process_withdraw_stake(
&rpc_client,
config,
&stake_account_pubkey,
&destination_account_pubkey,
*lamports,
)
}
// Storage Commands
2019-04-17 07:45:07 -07:00
// Create storage account
CliCommand::CreateStorageAccount {
account_owner,
storage_account_pubkey,
account_type,
} => process_create_storage_account(
&rpc_client,
config,
&account_owner,
&storage_account_pubkey,
*account_type,
),
CliCommand::ClaimStorageReward {
node_account_pubkey,
storage_account_pubkey,
} => process_claim_storage_reward(
&rpc_client,
config,
node_account_pubkey,
&storage_account_pubkey,
),
CliCommand::ShowStorageAccount(storage_account_pubkey) => {
process_show_storage_account(&rpc_client, config, &storage_account_pubkey)
}
// Validator Info Commands
// Return all or single validator info
CliCommand::GetValidatorInfo(info_pubkey) => {
process_get_validator_info(&rpc_client, *info_pubkey)
}
// Publish validator info
CliCommand::SetValidatorInfo(validator_info, info_pubkey) => {
process_set_validator_info(&rpc_client, config, &validator_info, *info_pubkey)
}
2018-10-22 21:21:33 -07:00
// Vote Commands
// Create vote account
CliCommand::CreateVoteAccount(vote_account_pubkey, vote_init) => {
process_create_vote_account(&rpc_client, config, &vote_account_pubkey, &vote_init)
}
CliCommand::ShowVoteAccount {
pubkey: vote_account_pubkey,
use_lamports_unit,
} => process_show_vote_account(
&rpc_client,
config,
&vote_account_pubkey,
*use_lamports_unit,
),
CliCommand::VoteAuthorize(vote_account_pubkey, new_authorized_pubkey, vote_authorize) => {
process_vote_authorize(
&rpc_client,
config,
&vote_account_pubkey,
&new_authorized_pubkey,
*vote_authorize,
)
}
CliCommand::Uptime {
pubkey: vote_account_pubkey,
aggregate,
span,
} => process_uptime(&rpc_client, config, &vote_account_pubkey, *aggregate, *span),
// Wallet Commands
// Get address of this client
CliCommand::Address => unreachable!(),
// Request an airdrop from Solana Drone;
CliCommand::Airdrop {
drone_host,
drone_port,
lamports,
use_lamports_unit,
} => {
let drone_addr = SocketAddr::new(
drone_host.unwrap_or_else(|| {
let drone_host = url::Url::parse(&config.json_rpc_url)
.unwrap()
.host()
.unwrap()
.to_string();
solana_netutil::parse_host(&drone_host).unwrap_or_else(|err| {
panic!("Unable to resolve {}: {}", drone_host, err);
})
}),
*drone_port,
);
2018-10-24 09:01:19 -07:00
process_airdrop(
&rpc_client,
config,
&drone_addr,
*lamports,
*use_lamports_unit,
)
}
// Check client balance
CliCommand::Balance {
pubkey,
use_lamports_unit,
} => process_balance(&pubkey, &rpc_client, *use_lamports_unit),
// Cancel a contract by contract Pubkey
CliCommand::Cancel(pubkey) => process_cancel(&rpc_client, config, &pubkey),
// Confirm the last client transaction by signature
CliCommand::Confirm(signature) => process_confirm(&rpc_client, signature),
2019-03-05 17:22:46 -08:00
// If client has positive balance, pay lamports to another address
CliCommand::Pay {
2019-03-05 17:22:46 -08:00
lamports,
to,
timestamp,
timestamp_pubkey,
ref witnesses,
cancelable,
} => process_pay(
2019-03-05 17:22:46 -08:00
&rpc_client,
config,
*lamports,
&to,
*timestamp,
*timestamp_pubkey,
2019-03-05 17:22:46 -08:00
witnesses,
*cancelable,
2019-03-05 17:22:46 -08:00
),
CliCommand::ShowAccount {
pubkey,
output_file,
use_lamports_unit,
} => process_show_account(
&rpc_client,
config,
&pubkey,
&output_file,
*use_lamports_unit,
),
// Apply time elapsed to contract
CliCommand::TimeElapsed(to, pubkey, dt) => {
process_time_elapsed(&rpc_client, config, &to, &pubkey, *dt)
}
// Apply witness signature to contract
CliCommand::Witness(to, pubkey) => process_witness(&rpc_client, config, &to, &pubkey),
}
}
2019-03-15 06:39:42 -07:00
// Quick and dirty Keypair that assumes the client will do retries but not update the
// blockhash. If the client updates the blockhash, the signature will be invalid.
// TODO: Parse `msg` and use that data to make a new airdrop request.
struct DroneKeypair {
transaction: Transaction,
}
impl DroneKeypair {
fn new_keypair(
drone_addr: &SocketAddr,
to_pubkey: &Pubkey,
lamports: u64,
blockhash: Hash,
) -> Result<Self, Box<dyn error::Error>> {
let transaction = request_airdrop_transaction(drone_addr, to_pubkey, lamports, blockhash)?;
Ok(Self { transaction })
}
fn airdrop_transaction(&self) -> Transaction {
self.transaction.clone()
}
}
impl KeypairUtil for DroneKeypair {
fn new() -> Self {
unimplemented!();
}
/// Return the public key of the keypair used to sign votes
fn pubkey(&self) -> Pubkey {
2019-03-29 09:05:06 -07:00
self.transaction.message().account_keys[0]
2019-03-15 06:39:42 -07:00
}
fn sign_message(&self, _msg: &[u8]) -> Signature {
self.transaction.signatures[0]
}
}
pub fn request_and_confirm_airdrop(
rpc_client: &RpcClient,
drone_addr: &SocketAddr,
to_pubkey: &Pubkey,
2019-03-05 17:22:46 -08:00
lamports: u64,
) -> ProcessResult {
let (blockhash, _fee_calculator) = rpc_client.get_recent_blockhash()?;
let keypair = {
let mut retries = 5;
loop {
let result = DroneKeypair::new_keypair(drone_addr, to_pubkey, lamports, blockhash);
if result.is_ok() || retries == 0 {
break result;
}
retries -= 1;
sleep(Duration::from_secs(1));
}
}?;
2019-03-15 06:39:42 -07:00
let mut tx = keypair.airdrop_transaction();
let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&keypair]);
log_instruction_custom_error::<SystemError>(result)
2018-10-22 21:21:33 -07:00
}
pub fn log_instruction_custom_error<E>(result: Result<String, ClientError>) -> ProcessResult
2019-04-25 10:29:44 -07:00
where
E: 'static + std::error::Error + DecodeError<E> + FromPrimitive,
{
2019-10-02 18:33:01 -07:00
match result {
Err(err) => {
if let ClientError::TransactionError(TransactionError::InstructionError(
_,
InstructionError::CustomError(code),
)) = err
{
if let Some(specific_error) = E::decode_custom_error_to_enum(code) {
error!("{}::{:?}", E::type_of(), specific_error);
return Err(specific_error.into());
}
2019-04-25 10:29:44 -07:00
}
2019-10-02 18:33:01 -07:00
error!("{:?}", err);
Err(err.into())
2019-04-25 10:29:44 -07:00
}
2019-10-02 18:33:01 -07:00
Ok(sig) => Ok(sig),
2019-04-25 10:29:44 -07:00
}
}
pub(crate) fn build_balance_message(lamports: u64, use_lamports_unit: bool) -> String {
if use_lamports_unit {
let ess = if lamports == 1 { "" } else { "s" };
format!("{:?} lamport{}", lamports, ess)
} else {
let sol = lamports_to_sol(lamports);
let sol_str = format!("{:.8}", sol);
let pretty_sol = sol_str.trim_end_matches('0').trim_end_matches('.');
format!("{} SOL", pretty_sol)
}
}
pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, 'v> {
App::new(name)
.about(about)
.version(version)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("address").about("Get your public key"))
.cluster_query_subcommands()
.subcommand(
SubCommand::with_name("deploy")
.about("Deploy a program")
.arg(
Arg::with_name("program_location")
.index(1)
.value_name("PATH TO PROGRAM")
.takes_value(true)
.required(true)
.help("/path/to/program.o"),
), // TODO: Add "loader" argument; current default is bpf_loader
)
.stake_subcommands()
.storage_subcommands()
.subcommand(
SubCommand::with_name("airdrop")
.about("Request lamports")
.arg(
Arg::with_name("drone_host")
.long("drone-host")
.value_name("HOST")
.takes_value(true)
.help("Drone host to use [default: the --url host]"),
)
.arg(
Arg::with_name("drone_port")
.long("drone-port")
.value_name("PORT")
.takes_value(true)
.default_value(solana_drone::drone::DRONE_PORT_STR)
.help("Drone port to use"),
)
.arg(
Arg::with_name("amount")
.index(1)
.value_name("AMOUNT")
.takes_value(true)
.required(true)
.help("The airdrop amount to request (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(2)
.value_name("UNIT")
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request and balance display"),
),
)
.subcommand(
SubCommand::with_name("balance")
.about("Get your balance")
.arg(
Arg::with_name("pubkey")
.index(1)
.value_name("PUBKEY")
.takes_value(true)
.validator(is_pubkey_or_keypair)
.help("The public key of the balance to check"),
)
.arg(
Arg::with_name("lamports")
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
),
)
.subcommand(
SubCommand::with_name("cancel")
.about("Cancel a transfer")
.arg(
Arg::with_name("process_id")
.index(1)
.value_name("PROCESS ID")
.takes_value(true)
.required(true)
.validator(is_pubkey)
.help("The process id of the transfer to cancel"),
),
)
.subcommand(
SubCommand::with_name("confirm")
.about("Confirm transaction by signature")
.arg(
Arg::with_name("signature")
.index(1)
.value_name("SIGNATURE")
.takes_value(true)
.required(true)
.help("The transaction signature to confirm"),
),
)
.subcommand(
SubCommand::with_name("pay")
.about("Send a payment")
.arg(
Arg::with_name("to")
.index(1)
.value_name("PUBKEY")
.takes_value(true)
.required(true)
.validator(is_pubkey)
.help("The pubkey of recipient"),
)
.arg(
Arg::with_name("amount")
.index(2)
.value_name("AMOUNT")
.takes_value(true)
.required(true)
.help("The amount to send (default unit SOL)"),
)
.arg(
Arg::with_name("unit")
.index(3)
.value_name("UNIT")
.takes_value(true)
.possible_values(&["SOL", "lamports"])
.help("Specify unit to use for request"),
)
.arg(
Arg::with_name("timestamp")
.long("after")
.value_name("DATETIME")
.takes_value(true)
.help("A timestamp after which transaction will execute"),
)
.arg(
Arg::with_name("timestamp_pubkey")
.long("require-timestamp-from")
.value_name("PUBKEY")
.takes_value(true)
.requires("timestamp")
.validator(is_pubkey)
.help("Require timestamp from this third party"),
)
.arg(
Arg::with_name("witness")
.long("require-signature-from")
.value_name("PUBKEY")
.takes_value(true)
.multiple(true)
.use_delimiter(true)
.validator(is_pubkey)
.help("Any third party signatures required to unlock the lamports"),
)
.arg(
Arg::with_name("cancelable")
.long("cancelable")
.takes_value(false),
),
)
.subcommand(
SubCommand::with_name("send-signature")
.about("Send a signature to authorize a transfer")
.arg(
Arg::with_name("to")
.index(1)
.value_name("PUBKEY")
.takes_value(true)
.required(true)
.validator(is_pubkey)
.help("The pubkey of recipient"),
)
.arg(
Arg::with_name("process_id")
.index(2)
.value_name("PROCESS ID")
.takes_value(true)
.required(true)
.help("The process id of the transfer to authorize"),
),
)
.subcommand(
SubCommand::with_name("send-timestamp")
.about("Send a timestamp to unlock a transfer")
.arg(
Arg::with_name("to")
.index(1)
.value_name("PUBKEY")
.takes_value(true)
.required(true)
.validator(is_pubkey)
.help("The pubkey of recipient"),
)
.arg(
Arg::with_name("process_id")
.index(2)
.value_name("PROCESS ID")
.takes_value(true)
.required(true)
.help("The process id of the transfer to unlock"),
)
.arg(
Arg::with_name("datetime")
.long("date")
.value_name("DATETIME")
.takes_value(true)
.help("Optional arbitrary timestamp to apply"),
),
)
.subcommand(
SubCommand::with_name("show-account")
.about("Show the contents of an account")
.arg(
Arg::with_name("account_pubkey")
.index(1)
.value_name("ACCOUNT PUBKEY")
.takes_value(true)
.required(true)
.validator(is_pubkey_or_keypair)
.help("Account pubkey"),
)
.arg(
Arg::with_name("output_file")
.long("output")
.short("o")
.value_name("FILE")
.takes_value(true)
.help("Write the account data to this file"),
)
.arg(
Arg::with_name("lamports")
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
),
)
.subcommand(
SubCommand::with_name("validator-info")
.about("Publish/get Validator info on Solana")
.subcommand(
SubCommand::with_name("publish")
.about("Publish Validator info on Solana")
.arg(
Arg::with_name("info_pubkey")
.short("p")
.long("info-pubkey")
.value_name("PUBKEY")
.takes_value(true)
.validator(is_pubkey)
.help("The pubkey of the Validator info account to update"),
)
.arg(
Arg::with_name("name")
.index(1)
.value_name("NAME")
.takes_value(true)
.required(true)
.validator(is_short_field)
.help("Validator name"),
)
.arg(
Arg::with_name("website")
.short("w")
.long("website")
.value_name("URL")
.takes_value(true)
.validator(check_url)
.help("Validator website url"),
)
.arg(
Arg::with_name("keybase_username")
.short("n")
.long("keybase")
.value_name("USERNAME")
.takes_value(true)
.validator(is_short_field)
.help("Validator Keybase username"),
)
.arg(
Arg::with_name("details")
.short("d")
.long("details")
.value_name("DETAILS")
.takes_value(true)
.validator(check_details_length)
.help("Validator description")
)
.arg(
Arg::with_name("force")
.long("force")
.takes_value(false)
.hidden(true) // Don't document this argument to discourage its use
.help("Override keybase username validity check"),
),
)
.subcommand(
SubCommand::with_name("get")
.about("Get and parse Solana Validator info")
.arg(
Arg::with_name("info_pubkey")
.index(1)
.value_name("PUBKEY")
.takes_value(true)
.validator(is_pubkey)
.help("The pubkey of the Validator info account; without this argument, returns all"),
),
)
)
.vote_subcommands()
}
#[cfg(test)]
2018-09-14 01:59:09 -07:00
mod tests {
use super::*;
use serde_json::Value;
use solana_client::mock_rpc_client_request::SIGNATURE;
use solana_sdk::{
signature::{gen_keypair_file, read_keypair_file},
transaction::TransactionError,
};
use std::path::PathBuf;
2018-09-14 01:59:09 -07:00
fn make_tmp_path(name: &str) -> String {
let out_dir = std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
let keypair = Keypair::new();
let path = format!("{}/tmp/{}-{}", out_dir, name, keypair.pubkey());
// whack any possible collision
let _ignored = std::fs::remove_dir_all(&path);
// whack any possible collision
let _ignored = std::fs::remove_file(&path);
path
}
#[test]
#[should_panic]
fn test_bad_amount() {
let test_commands = app("test", "desc", "version");
let test_bad_airdrop = test_commands.get_matches_from(vec!["test", "airdrop", "notint"]);
let pubkey = Pubkey::new_rand();
let _ignored = parse_command(&pubkey, &test_bad_airdrop).unwrap();
}
2018-09-14 01:59:09 -07:00
#[test]
fn test_cli_parse_command() {
let test_commands = app("test", "desc", "version");
let pubkey = Pubkey::new_rand();
2018-09-19 12:44:16 -07:00
let pubkey_string = format!("{}", pubkey);
let witness0 = Pubkey::new_rand();
2018-09-19 12:44:16 -07:00
let witness0_string = format!("{}", witness0);
let witness1 = Pubkey::new_rand();
2018-09-19 12:44:16 -07:00
let witness1_string = format!("{}", witness1);
let dt = Utc.ymd(2018, 9, 19).and_hms(17, 30, 59);
2018-09-14 01:59:09 -07:00
2018-09-19 12:44:16 -07:00
// Test Airdrop Subcommand
2018-09-14 01:59:09 -07:00
let test_airdrop = test_commands
.clone()
.get_matches_from(vec!["test", "airdrop", "50", "lamports"]);
2018-09-14 01:59:09 -07:00
assert_eq!(
parse_command(&pubkey, &test_airdrop).unwrap(),
CliCommand::Airdrop {
drone_host: None,
drone_port: solana_drone::drone::DRONE_PORT,
lamports: 50,
use_lamports_unit: true,
}
2018-09-14 01:59:09 -07:00
);
// Test Balance Subcommand, incl pubkey and keypair-file inputs
let keypair_file = make_tmp_path("keypair_file");
gen_keypair_file(&keypair_file).unwrap();
let keypair = read_keypair_file(&keypair_file).unwrap();
let test_balance = test_commands.clone().get_matches_from(vec![
"test",
"balance",
&keypair.pubkey().to_string(),
]);
assert_eq!(
parse_command(&pubkey, &test_balance).unwrap(),
CliCommand::Balance {
pubkey: keypair.pubkey(),
use_lamports_unit: false
}
);
let test_balance = test_commands.clone().get_matches_from(vec![
"test",
"balance",
&keypair_file,
"--lamports",
]);
assert_eq!(
parse_command(&pubkey, &test_balance).unwrap(),
CliCommand::Balance {
pubkey: keypair.pubkey(),
use_lamports_unit: true
}
);
2018-09-19 12:44:16 -07:00
// Test Cancel Subcommand
let test_cancel =
test_commands
.clone()
.get_matches_from(vec!["test", "cancel", &pubkey_string]);
2018-09-14 01:59:09 -07:00
assert_eq!(
parse_command(&pubkey, &test_cancel).unwrap(),
CliCommand::Cancel(pubkey)
2018-09-14 01:59:09 -07:00
);
2018-09-19 12:44:16 -07:00
// Test Confirm Subcommand
2018-09-14 01:59:09 -07:00
let signature = Signature::new(&vec![1; 64]);
let signature_string = format!("{:?}", signature);
let test_confirm =
test_commands
.clone()
.get_matches_from(vec!["test", "confirm", &signature_string]);
assert_eq!(
parse_command(&pubkey, &test_confirm).unwrap(),
CliCommand::Confirm(signature)
2018-09-14 01:59:09 -07:00
);
2018-09-19 12:44:16 -07:00
let test_bad_signature = test_commands
.clone()
.get_matches_from(vec!["test", "confirm", "deadbeef"]);
assert!(parse_command(&pubkey, &test_bad_signature).is_err());
2018-09-19 12:44:16 -07:00
2018-11-12 12:48:59 -08:00
// Test Deploy Subcommand
let test_deploy =
test_commands
.clone()
.get_matches_from(vec!["test", "deploy", "/Users/test/program.o"]);
assert_eq!(
parse_command(&pubkey, &test_deploy).unwrap(),
CliCommand::Deploy("/Users/test/program.o".to_string())
2018-11-12 12:48:59 -08:00
);
2018-09-19 12:44:16 -07:00
// Test Simple Pay Subcommand
let test_pay = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
]);
2018-09-19 12:44:16 -07:00
assert_eq!(
parse_command(&pubkey, &test_pay).unwrap(),
CliCommand::Pay {
lamports: 50,
to: pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: None,
cancelable: None
}
2018-09-19 12:44:16 -07:00
);
// Test Pay Subcommand w/ Witness
let test_pay_multiple_witnesses = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
2018-09-19 12:44:16 -07:00
"--require-signature-from",
&witness0_string,
"--require-signature-from",
&witness1_string,
]);
assert_eq!(
parse_command(&pubkey, &test_pay_multiple_witnesses).unwrap(),
CliCommand::Pay {
lamports: 50,
to: pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: Some(vec![witness0, witness1]),
cancelable: None
}
2018-09-19 12:44:16 -07:00
);
let test_pay_single_witness = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
2018-09-19 12:44:16 -07:00
"--require-signature-from",
&witness0_string,
]);
assert_eq!(
parse_command(&pubkey, &test_pay_single_witness).unwrap(),
CliCommand::Pay {
lamports: 50,
to: pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: Some(vec![witness0]),
cancelable: None
}
2018-09-19 12:44:16 -07:00
);
// Test Pay Subcommand w/ Timestamp
let test_pay_timestamp = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
2018-09-19 12:44:16 -07:00
"--after",
"2018-09-19T17:30:59",
"--require-timestamp-from",
&witness0_string,
]);
assert_eq!(
parse_command(&pubkey, &test_pay_timestamp).unwrap(),
CliCommand::Pay {
lamports: 50,
to: pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(witness0),
witnesses: None,
cancelable: None
}
2018-09-19 12:44:16 -07:00
);
// Test Send-Signature Subcommand
2018-09-24 09:23:16 -07:00
let test_send_signature = test_commands.clone().get_matches_from(vec![
"test",
"send-signature",
&pubkey_string,
&pubkey_string,
]);
2018-09-19 12:44:16 -07:00
assert_eq!(
parse_command(&pubkey, &test_send_signature).unwrap(),
CliCommand::Witness(pubkey, pubkey)
2018-09-19 12:44:16 -07:00
);
let test_pay_multiple_witnesses = test_commands.clone().get_matches_from(vec![
"test",
"pay",
&pubkey_string,
"50",
"lamports",
2018-09-19 12:44:16 -07:00
"--after",
"2018-09-19T17:30:59",
"--require-signature-from",
&witness0_string,
"--require-timestamp-from",
&witness0_string,
"--require-signature-from",
&witness1_string,
]);
assert_eq!(
parse_command(&pubkey, &test_pay_multiple_witnesses).unwrap(),
CliCommand::Pay {
lamports: 50,
to: pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(witness0),
witnesses: Some(vec![witness0, witness1]),
cancelable: None
}
2018-09-19 12:44:16 -07:00
);
// Test Send-Timestamp Subcommand
let test_send_timestamp = test_commands.clone().get_matches_from(vec![
"test",
"send-timestamp",
&pubkey_string,
2018-09-24 09:23:16 -07:00
&pubkey_string,
2018-09-19 12:44:16 -07:00
"--date",
"2018-09-19T17:30:59",
]);
assert_eq!(
parse_command(&pubkey, &test_send_timestamp).unwrap(),
CliCommand::TimeElapsed(pubkey, pubkey, dt)
2018-09-19 12:44:16 -07:00
);
let test_bad_timestamp = test_commands.clone().get_matches_from(vec![
"test",
"send-timestamp",
&pubkey_string,
2018-09-24 09:23:16 -07:00
&pubkey_string,
2018-09-19 12:44:16 -07:00
"--date",
"20180919T17:30:59",
]);
assert!(parse_command(&pubkey, &test_bad_timestamp).is_err());
2018-09-14 01:59:09 -07:00
}
2018-09-14 01:59:09 -07:00
#[test]
fn test_cli_process_command() {
// Success cases
let mut config = CliConfig::default();
config.rpc_client = Some(RpcClient::new_mock("succeeds".to_string()));
2018-11-12 12:48:59 -08:00
let keypair = Keypair::new();
let pubkey = keypair.pubkey().to_string();
2019-04-02 06:08:11 -07:00
config.keypair = keypair;
config.command = CliCommand::Address;
assert_eq!(process_command(&config).unwrap(), pubkey);
2018-09-14 01:59:09 -07:00
config.command = CliCommand::Balance {
pubkey: config.keypair.pubkey(),
use_lamports_unit: true,
};
2019-03-20 09:44:16 -07:00
assert_eq!(process_command(&config).unwrap(), "50 lamports");
2018-09-14 01:59:09 -07:00
config.command = CliCommand::Balance {
pubkey: config.keypair.pubkey(),
use_lamports_unit: false,
};
assert_eq!(process_command(&config).unwrap(), "0 SOL");
let process_id = Pubkey::new_rand();
config.command = CliCommand::Cancel(process_id);
assert_eq!(process_command(&config).unwrap(), SIGNATURE);
let good_signature = Signature::new(&bs58::decode(SIGNATURE).into_vec().unwrap());
config.command = CliCommand::Confirm(good_signature);
2018-09-20 22:27:06 -07:00
assert_eq!(process_command(&config).unwrap(), "Confirmed");
2018-09-14 01:59:09 -07:00
let bob_pubkey = Pubkey::new_rand();
let node_pubkey = Pubkey::new_rand();
config.command = CliCommand::CreateVoteAccount(
bob_pubkey,
VoteInit {
node_pubkey,
authorized_voter: bob_pubkey,
authorized_withdrawer: bob_pubkey,
commission: 0,
},
);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let new_authorized_pubkey = Pubkey::new_rand();
config.command =
CliCommand::VoteAuthorize(bob_pubkey, new_authorized_pubkey, VoteAuthorize::Voter);
2019-03-06 20:29:08 -08:00
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let bob_pubkey = Pubkey::new_rand();
let custodian = Pubkey::new_rand();
config.command = CliCommand::CreateStakeAccount(
bob_pubkey,
Authorized {
staker: config.keypair.pubkey(),
withdrawer: config.keypair.pubkey(),
},
Lockup { slot: 0, custodian },
1234,
);
2019-03-06 20:29:08 -08:00
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let stake_pubkey = Pubkey::new_rand();
let to_pubkey = Pubkey::new_rand();
config.command = CliCommand::WithdrawStake(stake_pubkey, to_pubkey, 100);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let stake_pubkey = Pubkey::new_rand();
config.command = CliCommand::DeactivateStake(stake_pubkey);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
config.command = CliCommand::GetSlot;
assert_eq!(process_command(&config).unwrap(), "0");
config.command = CliCommand::GetTransactionCount;
assert_eq!(process_command(&config).unwrap(), "1234");
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: None,
cancelable: None,
};
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let date_string = "\"2018-09-19T17:30:59Z\"";
let dt: DateTime<Utc> = serde_json::from_str(&date_string).unwrap();
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(config.keypair.pubkey()),
witnesses: None,
cancelable: None,
};
let result = process_command(&config);
let json: Value = serde_json::from_str(&result.unwrap()).unwrap();
assert_eq!(
json.as_object()
.unwrap()
.get("signature")
.unwrap()
.as_str()
.unwrap(),
SIGNATURE.to_string()
);
let witness = Pubkey::new_rand();
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: Some(vec![witness]),
cancelable: Some(config.keypair.pubkey()),
};
let result = process_command(&config);
let json: Value = serde_json::from_str(&result.unwrap()).unwrap();
assert_eq!(
json.as_object()
.unwrap()
.get("signature")
.unwrap()
.as_str()
.unwrap(),
SIGNATURE.to_string()
2018-09-14 01:59:09 -07:00
);
let process_id = Pubkey::new_rand();
config.command = CliCommand::TimeElapsed(bob_pubkey, process_id, dt);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
2018-09-14 01:59:09 -07:00
let witness = Pubkey::new_rand();
config.command = CliCommand::Witness(bob_pubkey, witness);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
2018-09-20 22:27:06 -07:00
// Need airdrop cases
config.command = CliCommand::Airdrop {
drone_host: None,
drone_port: 1234,
lamports: 50,
use_lamports_unit: true,
};
assert!(process_command(&config).is_ok());
config.rpc_client = Some(RpcClient::new_mock("airdrop".to_string()));
config.command = CliCommand::TimeElapsed(bob_pubkey, process_id, dt);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
let witness = Pubkey::new_rand();
config.command = CliCommand::Witness(bob_pubkey, witness);
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
2019-04-05 19:56:17 -07:00
// sig_not_found case
config.rpc_client = Some(RpcClient::new_mock("sig_not_found".to_string()));
2019-03-17 00:40:45 -07:00
let missing_signature = Signature::new(&bs58::decode("5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW").into_vec().unwrap());
config.command = CliCommand::Confirm(missing_signature);
2019-03-17 00:40:45 -07:00
assert_eq!(process_command(&config).unwrap(), "Not found");
2019-04-05 19:56:17 -07:00
// Tx error case
config.rpc_client = Some(RpcClient::new_mock("account_in_use".to_string()));
let any_signature = Signature::new(&bs58::decode(SIGNATURE).into_vec().unwrap());
config.command = CliCommand::Confirm(any_signature);
2019-04-05 19:56:17 -07:00
assert_eq!(
process_command(&config).unwrap(),
format!(
"Transaction failed with error {:?}",
TransactionError::AccountInUse
)
);
2019-03-17 00:40:45 -07:00
// Failure cases
config.rpc_client = Some(RpcClient::new_mock("fails".to_string()));
config.command = CliCommand::Airdrop {
drone_host: None,
drone_port: 1234,
lamports: 50,
use_lamports_unit: true,
};
assert!(process_command(&config).is_err());
config.command = CliCommand::Balance {
pubkey: config.keypair.pubkey(),
use_lamports_unit: false,
};
assert!(process_command(&config).is_err());
config.command = CliCommand::CreateVoteAccount(
bob_pubkey,
VoteInit {
node_pubkey,
authorized_voter: bob_pubkey,
authorized_withdrawer: bob_pubkey,
commission: 0,
},
);
2019-03-06 20:29:08 -08:00
assert!(process_command(&config).is_err());
config.command = CliCommand::VoteAuthorize(bob_pubkey, bob_pubkey, VoteAuthorize::Voter);
2019-03-06 20:29:08 -08:00
assert!(process_command(&config).is_err());
config.command = CliCommand::GetSlot;
assert!(process_command(&config).is_err());
config.command = CliCommand::GetTransactionCount;
assert!(process_command(&config).is_err());
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: None,
cancelable: None,
};
assert!(process_command(&config).is_err());
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: Some(dt),
timestamp_pubkey: Some(config.keypair.pubkey()),
witnesses: None,
cancelable: None,
};
assert!(process_command(&config).is_err());
config.command = CliCommand::Pay {
lamports: 10,
to: bob_pubkey,
timestamp: None,
timestamp_pubkey: None,
witnesses: Some(vec![witness]),
cancelable: Some(config.keypair.pubkey()),
};
assert!(process_command(&config).is_err());
config.command = CliCommand::TimeElapsed(bob_pubkey, process_id, dt);
assert!(process_command(&config).is_err());
2018-09-14 01:59:09 -07:00
}
#[test]
fn test_cli_deploy() {
solana_logger::setup();
let mut pathbuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pathbuf.push("tests");
pathbuf.push("fixtures");
pathbuf.push("noop");
pathbuf.set_extension("so");
// Success case
let mut config = CliConfig::default();
config.rpc_client = Some(RpcClient::new_mock("deploy_succeeds".to_string()));
config.command = CliCommand::Deploy(pathbuf.to_str().unwrap().to_string());
let result = process_command(&config);
let json: Value = serde_json::from_str(&result.unwrap()).unwrap();
let program_id = json
.as_object()
.unwrap()
.get("programId")
.unwrap()
.as_str()
.unwrap();
assert!(program_id.parse::<Pubkey>().is_ok());
// Failure case
config.command = CliCommand::Deploy("bad/file/location.so".to_string());
assert!(process_command(&config).is_err());
}
2018-09-14 01:59:09 -07:00
}