Use default client keypair if --keypair argument is not provided

This commit is contained in:
Michael Vines 2020-11-16 08:43:07 -08:00
parent 6c5fb329b2
commit e9e5ee4362
3 changed files with 23 additions and 10 deletions

View File

@ -17,6 +17,7 @@ log = "0.4.8"
serde = "1.0.112" serde = "1.0.112"
serde_derive = "1.0.103" serde_derive = "1.0.103"
solana-clap-utils = { path = "../clap-utils", version = "1.5.0" } solana-clap-utils = { path = "../clap-utils", version = "1.5.0" }
solana-cli-config = { path = "../cli-config", version = "1.5.0" }
solana-logger = { path = "../logger", version = "1.5.0" } solana-logger = { path = "../logger", version = "1.5.0" }
solana-metrics = { path = "../metrics", version = "1.5.0" } solana-metrics = { path = "../metrics", version = "1.5.0" }
solana-sdk = { path = "../sdk", version = "1.5.0" } solana-sdk = { path = "../sdk", version = "1.5.0" }

View File

@ -13,6 +13,8 @@ use std::{
}; };
fn main() -> Result<(), Box<dyn error::Error>> { fn main() -> Result<(), Box<dyn error::Error>> {
let default_keypair = solana_cli_config::Config::default().keypair_path;
solana_logger::setup_with_default("solana=info"); solana_logger::setup_with_default("solana=info");
solana_metrics::set_panic_hook("faucet"); solana_metrics::set_panic_hook("faucet");
let matches = App::new(crate_name!()) let matches = App::new(crate_name!())
@ -25,7 +27,8 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.value_name("PATH") .value_name("PATH")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.help("File from which to read the mint's keypair"), .default_value(&default_keypair)
.help("File from which to read the faucet's keypair"),
) )
.arg( .arg(
Arg::with_name("slice") Arg::with_name("slice")
@ -51,7 +54,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
) )
.get_matches(); .get_matches();
let mint_keypair = read_keypair_file(matches.value_of("keypair").unwrap()) let faucet_keypair = read_keypair_file(matches.value_of("keypair").unwrap())
.expect("failed to read client keypair"); .expect("failed to read client keypair");
let time_slice = value_of(&matches, "slice"); let time_slice = value_of(&matches, "slice");
@ -61,7 +64,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let faucet_addr = socketaddr!(0, FAUCET_PORT); let faucet_addr = socketaddr!(0, FAUCET_PORT);
let faucet = Arc::new(Mutex::new(Faucet::new( let faucet = Arc::new(Mutex::new(Faucet::new(
mint_keypair, faucet_keypair,
time_slice, time_slice,
per_time_cap, per_time_cap,
per_request_cap, per_request_cap,

View File

@ -59,7 +59,7 @@ pub enum FaucetRequest {
} }
pub struct Faucet { pub struct Faucet {
mint_keypair: Keypair, faucet_keypair: Keypair,
ip_cache: Vec<IpAddr>, ip_cache: Vec<IpAddr>,
pub time_slice: Duration, pub time_slice: Duration,
per_time_cap: u64, per_time_cap: u64,
@ -69,7 +69,7 @@ pub struct Faucet {
impl Faucet { impl Faucet {
pub fn new( pub fn new(
mint_keypair: Keypair, faucet_keypair: Keypair,
time_input: Option<u64>, time_input: Option<u64>,
per_time_cap: Option<u64>, per_time_cap: Option<u64>,
per_request_cap: Option<u64>, per_request_cap: Option<u64>,
@ -77,7 +77,7 @@ impl Faucet {
let time_slice = Duration::new(time_input.unwrap_or(TIME_SLICE), 0); let time_slice = Duration::new(time_input.unwrap_or(TIME_SLICE), 0);
let per_time_cap = per_time_cap.unwrap_or(REQUEST_CAP); let per_time_cap = per_time_cap.unwrap_or(REQUEST_CAP);
Faucet { Faucet {
mint_keypair, faucet_keypair,
ip_cache: Vec::new(), ip_cache: Vec::new(),
time_slice, time_slice,
per_time_cap, per_time_cap,
@ -133,11 +133,15 @@ impl Faucet {
); );
info!("Requesting airdrop of {} to {:?}", lamports, to); info!("Requesting airdrop of {} to {:?}", lamports, to);
let mint_pubkey = self.mint_keypair.pubkey(); let mint_pubkey = self.faucet_keypair.pubkey();
let create_instruction = let create_instruction =
system_instruction::transfer(&mint_pubkey, &to, lamports); system_instruction::transfer(&mint_pubkey, &to, lamports);
let message = Message::new(&[create_instruction], Some(&mint_pubkey)); let message = Message::new(&[create_instruction], Some(&mint_pubkey));
Ok(Transaction::new(&[&self.mint_keypair], message, blockhash)) Ok(Transaction::new(
&[&self.faucet_keypair],
message,
blockhash,
))
} else { } else {
Err(Error::new( Err(Error::new(
ErrorKind::Other, ErrorKind::Other,
@ -254,14 +258,14 @@ pub fn request_airdrop_transaction(
// For integration tests. Listens on random open port and reports port to Sender. // For integration tests. Listens on random open port and reports port to Sender.
pub fn run_local_faucet( pub fn run_local_faucet(
mint_keypair: Keypair, faucet_keypair: Keypair,
sender: Sender<SocketAddr>, sender: Sender<SocketAddr>,
per_time_cap: Option<u64>, per_time_cap: Option<u64>,
) { ) {
thread::spawn(move || { thread::spawn(move || {
let faucet_addr = socketaddr!(0, 0); let faucet_addr = socketaddr!(0, 0);
let faucet = Arc::new(Mutex::new(Faucet::new( let faucet = Arc::new(Mutex::new(Faucet::new(
mint_keypair, faucet_keypair,
None, None,
per_time_cap, per_time_cap,
None, None,
@ -280,6 +284,11 @@ pub fn run_faucet(
send_addr.send(socket.local_addr().unwrap()).unwrap(); send_addr.send(socket.local_addr().unwrap()).unwrap();
} }
info!("Faucet started. Listening on: {}", faucet_addr); info!("Faucet started. Listening on: {}", faucet_addr);
info!(
"Faucet account address: {}",
faucet.lock().unwrap().faucet_keypair.pubkey()
);
let done = socket let done = socket
.incoming() .incoming()
.map_err(|e| debug!("failed to accept socket; error = {:?}", e)) .map_err(|e| debug!("failed to accept socket; error = {:?}", e))