Cli: move airdrop to rpc requests (#16557)

* Add recent_blockhash to requestAirdrop

* Move tx confirmation to separate method

* Add RpcClient airdrop methods

* Request cli airdrop via RpcClient

* Pass optional faucet_addr into TestValidator and fix tests

* Update client/src/rpc_client.rs

Co-authored-by: Michael Vines <mvines@gmail.com>

Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
Tyera Eulberg
2021-04-15 00:25:23 -06:00
committed by GitHub
parent 76ce28c723
commit 7dfb51c0b4
16 changed files with 261 additions and 305 deletions

View File

@ -1754,6 +1754,12 @@ fn verify_pubkey(input: String) -> Result<Pubkey> {
.map_err(|e| Error::invalid_params(format!("Invalid param: {:?}", e)))
}
fn verify_hash(input: String) -> Result<Hash> {
input
.parse()
.map_err(|e| Error::invalid_params(format!("Invalid param: {:?}", e)))
}
fn verify_signature(input: &str) -> Result<Signature> {
input
.parse()
@ -2355,7 +2361,7 @@ pub mod rpc_full {
meta: Self::Metadata,
pubkey_str: String,
lamports: u64,
commitment: Option<CommitmentConfig>,
config: Option<RpcRequestAirdropConfig>,
) -> Result<String>;
#[rpc(meta, name = "sendTransaction")]
@ -2786,28 +2792,28 @@ pub mod rpc_full {
meta: Self::Metadata,
pubkey_str: String,
lamports: u64,
commitment: Option<CommitmentConfig>,
config: Option<RpcRequestAirdropConfig>,
) -> Result<String> {
debug!("request_airdrop rpc request received");
trace!(
"request_airdrop id={} lamports={} commitment: {:?}",
"request_airdrop id={} lamports={} config: {:?}",
pubkey_str,
lamports,
&commitment
&config
);
let faucet_addr = meta.config.faucet_addr.ok_or_else(Error::invalid_request)?;
let pubkey = verify_pubkey(pubkey_str)?;
let (blockhash, last_valid_slot) = {
let bank = meta.bank(commitment);
let config = config.unwrap_or_default();
let bank = meta.bank(config.commitment);
let blockhash = bank.confirmed_last_blockhash().0;
(
blockhash,
bank.get_blockhash_last_valid_slot(&blockhash).unwrap_or(0),
)
let blockhash = if let Some(blockhash) = config.recent_blockhash {
verify_hash(blockhash)?
} else {
bank.confirmed_last_blockhash().0
};
let last_valid_slot = bank.get_blockhash_last_valid_slot(&blockhash).unwrap_or(0);
let transaction =
request_airdrop_transaction(&faucet_addr, &pubkey, lamports, blockhash).map_err(

View File

@ -95,6 +95,11 @@ impl TestValidatorGenesis {
self
}
pub fn faucet_addr(&mut self, faucet_addr: Option<SocketAddr>) -> &mut Self {
self.rpc_config.faucet_addr = faucet_addr;
self
}
pub fn warp_slot(&mut self, warp_slot: Slot) -> &mut Self {
self.warp_slot = Some(warp_slot);
self
@ -244,9 +249,10 @@ pub struct TestValidator {
impl TestValidator {
/// Create and start a `TestValidator` with no transaction fees and minimal rent.
/// Faucet optional.
///
/// This function panics on initialization failure.
pub fn with_no_fees(mint_address: Pubkey) -> Self {
pub fn with_no_fees(mint_address: Pubkey, faucet_addr: Option<SocketAddr>) -> Self {
TestValidatorGenesis::default()
.fee_rate_governor(FeeRateGovernor::new(0, 0))
.rent(Rent {
@ -254,14 +260,20 @@ impl TestValidator {
exemption_threshold: 1.0,
..Rent::default()
})
.faucet_addr(faucet_addr)
.start_with_mint_address(mint_address)
.expect("validator start failed")
}
/// Create and start a `TestValidator` with custom transaction fees and minimal rent.
/// Faucet optional.
///
/// This function panics on initialization failure.
pub fn with_custom_fees(mint_address: Pubkey, target_lamports_per_signature: u64) -> Self {
pub fn with_custom_fees(
mint_address: Pubkey,
target_lamports_per_signature: u64,
faucet_addr: Option<SocketAddr>,
) -> Self {
TestValidatorGenesis::default()
.fee_rate_governor(FeeRateGovernor::new(target_lamports_per_signature, 0))
.rent(Rent {
@ -269,6 +281,7 @@ impl TestValidator {
exemption_threshold: 1.0,
..Rent::default()
})
.faucet_addr(faucet_addr)
.start_with_mint_address(mint_address)
.expect("validator start failed")
}

View File

@ -34,7 +34,7 @@ fn test_rpc_client() {
solana_logger::setup();
let alice = Keypair::new();
let test_validator = TestValidator::with_no_fees(alice.pubkey());
let test_validator = TestValidator::with_no_fees(alice.pubkey(), None);
let bob_pubkey = solana_sdk::pubkey::new_rand();

View File

@ -55,7 +55,7 @@ fn test_rpc_send_tx() {
solana_logger::setup();
let alice = Keypair::new();
let test_validator = TestValidator::with_no_fees(alice.pubkey());
let test_validator = TestValidator::with_no_fees(alice.pubkey(), None);
let rpc_url = test_validator.rpc_url();
let bob_pubkey = solana_sdk::pubkey::new_rand();
@ -119,7 +119,7 @@ fn test_rpc_invalid_requests() {
solana_logger::setup();
let alice = Keypair::new();
let test_validator = TestValidator::with_no_fees(alice.pubkey());
let test_validator = TestValidator::with_no_fees(alice.pubkey(), None);
let rpc_url = test_validator.rpc_url();
let bob_pubkey = solana_sdk::pubkey::new_rand();
@ -150,7 +150,7 @@ fn test_rpc_invalid_requests() {
fn test_rpc_slot_updates() {
solana_logger::setup();
let test_validator = TestValidator::with_no_fees(Pubkey::new_unique());
let test_validator = TestValidator::with_no_fees(Pubkey::new_unique(), None);
// Create the pub sub runtime
let rt = Runtime::new().unwrap();
@ -215,7 +215,7 @@ fn test_rpc_subscriptions() {
solana_logger::setup();
let alice = Keypair::new();
let test_validator = TestValidator::with_no_fees(alice.pubkey());
let test_validator = TestValidator::with_no_fees(alice.pubkey(), None);
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
transactions_socket.connect(test_validator.tpu()).unwrap();