requestAirdrop RPC API is now optional

This commit is contained in:
Michael Vines
2019-03-06 09:26:12 -08:00
parent 516aa44aad
commit 0a4f909566
7 changed files with 38 additions and 43 deletions

View File

@ -125,25 +125,11 @@ impl Fullnode {
keypair.clone(),
)));
// TODO: The RPC service assumes that there is a drone running on the cluster
// entrypoint, which is a bad assumption.
// See https://github.com/solana-labs/solana/issues/1830 for the removal of drone
// from the RPC API
let drone_addr = {
let mut entrypoint_drone_addr = match entrypoint_info_option {
Some(entrypoint_info_info) => entrypoint_info_info.rpc,
None => node.info.rpc,
};
entrypoint_drone_addr.set_port(solana_drone::drone::DRONE_PORT);
entrypoint_drone_addr
};
let storage_state = StorageState::new();
let rpc_service = JsonRpcService::new(
&cluster_info,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), node.info.rpc.port()),
drone_addr,
storage_state.clone(),
config.rpc_config.clone(),
&exit,

View File

@ -21,15 +21,17 @@ use std::sync::{Arc, RwLock};
use std::thread::sleep;
use std::time::{Duration, Instant};
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct JsonRpcConfig {
pub enable_fullnode_exit: bool, // Enable the 'fullnodeExit' command
pub drone_addr: Option<SocketAddr>,
}
impl Default for JsonRpcConfig {
fn default() -> Self {
Self {
enable_fullnode_exit: false,
drone_addr: None,
}
}
}
@ -170,7 +172,6 @@ fn verify_signature(input: &str) -> Result<Signature> {
pub struct Meta {
pub request_processor: Arc<RwLock<JsonRpcRequestProcessor>>,
pub cluster_info: Arc<RwLock<ClusterInfo>>,
pub drone_addr: SocketAddr,
}
impl Metadata for Meta {}
@ -291,6 +292,14 @@ impl RpcSol for RpcSolImpl {
fn request_airdrop(&self, meta: Self::Metadata, id: String, lamports: u64) -> Result<String> {
trace!("request_airdrop id={} lamports={}", id, lamports);
let drone_addr = meta
.request_processor
.read()
.unwrap()
.config
.drone_addr
.ok_or_else(Error::invalid_request)?;
let pubkey = verify_pubkey(id)?;
let blockhash = meta
@ -299,13 +308,11 @@ impl RpcSol for RpcSolImpl {
.unwrap()
.bank()?
.last_blockhash();
let transaction =
request_airdrop_transaction(&meta.drone_addr, &pubkey, lamports, blockhash).map_err(
|err| {
info!("request_airdrop_transaction failed: {:?}", err);
Error::internal_error()
},
)?;;
let transaction = request_airdrop_transaction(&drone_addr, &pubkey, lamports, blockhash)
.map_err(|err| {
info!("request_airdrop_transaction failed: {:?}", err);
Error::internal_error()
})?;;
let data = serialize(&transaction).map_err(|err| {
info!("request_airdrop: serialize error: {:?}", err);
@ -412,7 +419,7 @@ mod tests {
use solana_sdk::hash::{hash, Hash};
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction::SystemTransaction;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::net::SocketAddr;
use std::thread;
fn start_rpc_handler_with_tx(pubkey: Pubkey) -> (MetaIoHandler<Meta>, Meta, Hash, Keypair) {
@ -435,7 +442,6 @@ mod tests {
cluster_info.write().unwrap().insert_info(leader.clone());
cluster_info.write().unwrap().set_leader(leader.id);
let drone_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
let mut io = MetaIoHandler::default();
let rpc = RpcSolImpl;
@ -443,7 +449,6 @@ mod tests {
let meta = Meta {
request_processor,
cluster_info,
drone_addr,
};
(io, meta, blockhash, alice)
}
@ -599,14 +604,14 @@ mod tests {
let bob_pubkey = Keypair::new().pubkey();
let (io, meta, _blockhash, _alice) = start_rpc_handler_with_tx(bob_pubkey);
// Expect internal error because no leader is running
// Expect internal error because no drone is available
let req = format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"requestAirdrop","params":["{}", 50]}}"#,
bob_pubkey
);
let res = io.handle_request_sync(&req, meta);
let expected =
r#"{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error"},"id":1}"#;
r#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":1}"#;
let expected: Response =
serde_json::from_str(expected).expect("expected response deserialization");
let result: Response = serde_json::from_str(&res.expect("actual response"))
@ -634,7 +639,6 @@ mod tests {
Arc::new(RwLock::new(request_processor))
},
cluster_info: Arc::new(RwLock::new(ClusterInfo::new(NodeInfo::default()))),
drone_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
};
let req =

View File

@ -24,12 +24,12 @@ impl JsonRpcService {
pub fn new(
cluster_info: &Arc<RwLock<ClusterInfo>>,
rpc_addr: SocketAddr,
drone_addr: SocketAddr,
storage_state: StorageState,
config: JsonRpcConfig,
exit: &Arc<AtomicBool>,
) -> Self {
info!("rpc bound to {:?}", rpc_addr);
info!("rpc configuration: {:?}", config);
let request_processor = Arc::new(RwLock::new(JsonRpcRequestProcessor::new(
storage_state,
config,
@ -51,7 +51,6 @@ impl JsonRpcService {
ServerBuilder::with_meta_extractor(io, move |_req: &hyper::Request<hyper::Body>| Meta {
request_processor: request_processor_.clone(),
cluster_info: info.clone(),
drone_addr,
}).threads(4)
.cors(DomainsValidation::AllowOnly(vec![
AccessControlAllowOrigin::Any,
@ -105,14 +104,9 @@ mod tests {
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
solana_netutil::find_available_port_in_range((10000, 65535)).unwrap(),
);
let drone_addr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
solana_netutil::find_available_port_in_range((10000, 65535)).unwrap(),
);
let mut rpc_service = JsonRpcService::new(
&cluster_info,
rpc_addr,
drone_addr,
StorageState::default(),
JsonRpcConfig::default(),
&exit,