2018-08-16 08:46:07 -06:00
|
|
|
//! The `rpc` module implements the Solana RPC interface.
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::cluster_info::ClusterInfo;
|
|
|
|
use crate::packet::PACKET_DATA_SIZE;
|
2019-01-28 14:53:50 -08:00
|
|
|
use crate::storage_stage::StorageState;
|
2018-11-14 18:57:34 -08:00
|
|
|
use bincode::{deserialize, serialize};
|
2018-08-10 17:05:23 -06:00
|
|
|
use bs58;
|
2019-02-17 11:01:21 -07:00
|
|
|
use jsonrpc_core::{Error, ErrorCode, Metadata, Result};
|
2019-02-04 17:41:03 -07:00
|
|
|
use jsonrpc_derive::rpc;
|
2019-03-16 17:43:39 -07:00
|
|
|
use solana_client::rpc_signature_status::RpcSignatureStatus;
|
2019-01-04 17:20:51 -08:00
|
|
|
use solana_drone::drone::request_airdrop_transaction;
|
2019-03-13 14:37:24 -06:00
|
|
|
use solana_runtime::bank::{self, Bank};
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::Account;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::Signature;
|
2019-03-13 14:37:24 -06:00
|
|
|
use solana_sdk::transaction::{Transaction, TransactionError};
|
2018-08-10 17:05:23 -06:00
|
|
|
use std::mem;
|
2018-08-22 11:53:24 -06:00
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
2019-03-03 22:01:09 -08:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-10-12 14:25:56 -06:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-02-17 11:01:21 -07:00
|
|
|
use std::thread::sleep;
|
2019-01-21 10:48:40 -08:00
|
|
|
use std::time::{Duration, Instant};
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2019-03-06 09:26:12 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-05 21:12:30 -08:00
|
|
|
pub struct JsonRpcConfig {
|
|
|
|
pub enable_fullnode_exit: bool, // Enable the 'fullnodeExit' command
|
2019-03-06 09:26:12 -08:00
|
|
|
pub drone_addr: Option<SocketAddr>,
|
2019-03-03 22:01:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for JsonRpcConfig {
|
|
|
|
fn default() -> Self {
|
2019-03-05 21:12:30 -08:00
|
|
|
Self {
|
|
|
|
enable_fullnode_exit: false,
|
2019-03-06 09:26:12 -08:00
|
|
|
drone_addr: None,
|
2019-03-05 21:12:30 -08:00
|
|
|
}
|
2019-03-03 22:01:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct JsonRpcRequestProcessor {
|
2019-02-22 17:15:15 -08:00
|
|
|
bank: Option<Arc<Bank>>,
|
2019-02-17 10:29:08 -07:00
|
|
|
storage_state: StorageState,
|
2019-03-03 22:01:09 -08:00
|
|
|
config: JsonRpcConfig,
|
|
|
|
fullnode_exit: Arc<AtomicBool>,
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
impl JsonRpcRequestProcessor {
|
2019-02-22 17:15:15 -08:00
|
|
|
fn bank(&self) -> Result<&Arc<Bank>> {
|
|
|
|
self.bank.as_ref().ok_or(Error {
|
|
|
|
code: ErrorCode::InternalError,
|
|
|
|
message: "No bank available".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-27 10:23:27 -08:00
|
|
|
pub fn set_bank(&mut self, bank: &Arc<Bank>) {
|
|
|
|
self.bank = Some(bank.clone());
|
2019-02-20 21:23:44 -08:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:01:09 -08:00
|
|
|
pub fn new(
|
|
|
|
storage_state: StorageState,
|
|
|
|
config: JsonRpcConfig,
|
2019-03-04 16:21:33 -08:00
|
|
|
fullnode_exit: &Arc<AtomicBool>,
|
2019-03-03 22:01:09 -08:00
|
|
|
) -> Self {
|
2019-02-17 10:29:08 -07:00
|
|
|
JsonRpcRequestProcessor {
|
2019-02-22 17:15:15 -08:00
|
|
|
bank: None,
|
2019-01-28 14:53:50 -08:00
|
|
|
storage_state,
|
2019-03-03 22:01:09 -08:00
|
|
|
config,
|
2019-03-04 16:21:33 -08:00
|
|
|
fullnode_exit: fullnode_exit.clone(),
|
2019-01-15 12:20:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
pub fn get_account_info(&self, pubkey: &Pubkey) -> Result<Account> {
|
2019-02-22 17:15:15 -08:00
|
|
|
self.bank()?
|
2019-02-17 10:29:08 -07:00
|
|
|
.get_account(&pubkey)
|
|
|
|
.ok_or_else(Error::invalid_request)
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
pub fn get_balance(&self, pubkey: &Pubkey) -> Result<u64> {
|
2019-02-22 17:15:15 -08:00
|
|
|
let val = self.bank()?.get_balance(&pubkey);
|
2019-02-17 10:29:08 -07:00
|
|
|
Ok(val)
|
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
fn get_recent_blockhash(&self) -> Result<String> {
|
|
|
|
let id = self.bank()?.last_blockhash();
|
2019-02-17 10:29:08 -07:00
|
|
|
Ok(bs58::encode(id).into_string())
|
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
pub fn get_signature_status(&self, signature: Signature) -> Option<bank::Result<()>> {
|
2019-02-22 17:15:15 -08:00
|
|
|
self.bank()
|
|
|
|
.ok()
|
|
|
|
.and_then(|bank| bank.get_signature_status(&signature))
|
2019-02-17 10:29:08 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
fn get_transaction_count(&self) -> Result<u64> {
|
2019-02-22 17:15:15 -08:00
|
|
|
Ok(self.bank()?.transaction_count() as u64)
|
2019-02-17 10:29:08 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
fn get_storage_blockhash(&self) -> Result<String> {
|
|
|
|
let hash = self.storage_state.get_storage_blockhash();
|
2019-03-02 09:50:41 -08:00
|
|
|
Ok(bs58::encode(hash).into_string())
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 09:56:06 -08:00
|
|
|
fn get_storage_entry_height(&self) -> Result<u64> {
|
2019-02-17 10:29:08 -07:00
|
|
|
let entry_height = self.storage_state.get_entry_height();
|
|
|
|
Ok(entry_height)
|
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
fn get_storage_pubkeys_for_entry_height(&self, entry_height: u64) -> Result<Vec<Pubkey>> {
|
|
|
|
Ok(self
|
|
|
|
.storage_state
|
|
|
|
.get_pubkeys_for_entry_height(entry_height))
|
|
|
|
}
|
2019-03-03 22:01:09 -08:00
|
|
|
|
|
|
|
pub fn fullnode_exit(&self) -> Result<bool> {
|
2019-03-05 21:12:30 -08:00
|
|
|
if self.config.enable_fullnode_exit {
|
|
|
|
warn!("fullnode_exit request...");
|
|
|
|
self.fullnode_exit.store(true, Ordering::Relaxed);
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
debug!("fullnode_exit ignored");
|
|
|
|
Ok(false)
|
2019-03-03 22:01:09 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 10:29:08 -07:00
|
|
|
}
|
2018-10-25 16:58:40 -07:00
|
|
|
|
2019-03-08 09:48:21 -08:00
|
|
|
fn get_tpu_addr(cluster_info: &Arc<RwLock<ClusterInfo>>) -> Result<SocketAddr> {
|
2019-03-08 17:23:07 -08:00
|
|
|
let contact_info = cluster_info.read().unwrap().my_data();
|
|
|
|
Ok(contact_info.tpu)
|
2018-09-10 22:41:44 -07:00
|
|
|
}
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
fn verify_pubkey(input: String) -> Result<Pubkey> {
|
|
|
|
let pubkey_vec = bs58::decode(input).into_vec().map_err(|err| {
|
|
|
|
info!("verify_pubkey: invalid input: {:?}", err);
|
|
|
|
Error::invalid_request()
|
|
|
|
})?;
|
|
|
|
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
|
|
|
|
info!(
|
|
|
|
"verify_pubkey: invalid pubkey_vec length: {}",
|
|
|
|
pubkey_vec.len()
|
|
|
|
);
|
|
|
|
Err(Error::invalid_request())
|
|
|
|
} else {
|
|
|
|
Ok(Pubkey::new(&pubkey_vec))
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2019-02-17 10:29:08 -07:00
|
|
|
fn verify_signature(input: &str) -> Result<Signature> {
|
|
|
|
let signature_vec = bs58::decode(input).into_vec().map_err(|err| {
|
|
|
|
info!("verify_signature: invalid input: {}: {:?}", input, err);
|
|
|
|
Error::invalid_request()
|
|
|
|
})?;
|
|
|
|
if signature_vec.len() != mem::size_of::<Signature>() {
|
|
|
|
info!(
|
|
|
|
"verify_signature: invalid signature_vec length: {}",
|
|
|
|
signature_vec.len()
|
|
|
|
);
|
|
|
|
Err(Error::invalid_request())
|
|
|
|
} else {
|
|
|
|
Ok(Signature::new(&signature_vec))
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:05:23 -06:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Meta {
|
2019-01-15 12:20:07 -08:00
|
|
|
pub request_processor: Arc<RwLock<JsonRpcRequestProcessor>>,
|
2018-10-12 14:25:56 -06:00
|
|
|
pub cluster_info: Arc<RwLock<ClusterInfo>>,
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
|
|
|
impl Metadata for Meta {}
|
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc]
|
|
|
|
pub trait RpcSol {
|
|
|
|
type Metadata;
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "confirmTransaction")]
|
|
|
|
fn confirm_transaction(&self, _: Self::Metadata, _: String) -> Result<bool>;
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "getAccountInfo")]
|
|
|
|
fn get_account_info(&self, _: Self::Metadata, _: String) -> Result<Account>;
|
2018-09-20 23:27:06 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "getBalance")]
|
|
|
|
fn get_balance(&self, _: Self::Metadata, _: String) -> Result<u64>;
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
#[rpc(meta, name = "getRecentBlockhash")]
|
|
|
|
fn get_recent_blockhash(&self, _: Self::Metadata) -> Result<String>;
|
2018-08-13 12:52:37 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "getSignatureStatus")]
|
|
|
|
fn get_signature_status(&self, _: Self::Metadata, _: String) -> Result<RpcSignatureStatus>;
|
2018-09-26 17:12:40 -07:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "getTransactionCount")]
|
|
|
|
fn get_transaction_count(&self, _: Self::Metadata) -> Result<u64>;
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "requestAirdrop")]
|
|
|
|
fn request_airdrop(&self, _: Self::Metadata, _: String, _: u64) -> Result<String>;
|
2018-08-22 11:54:37 -06:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "sendTransaction")]
|
|
|
|
fn send_transaction(&self, _: Self::Metadata, _: Vec<u8>) -> Result<String>;
|
2018-11-02 08:40:29 -07:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
#[rpc(meta, name = "getStorageBlockhash")]
|
|
|
|
fn get_storage_blockhash(&self, _: Self::Metadata) -> Result<String>;
|
2018-12-10 11:38:29 -08:00
|
|
|
|
2019-03-02 09:56:06 -08:00
|
|
|
#[rpc(meta, name = "getStorageEntryHeight")]
|
|
|
|
fn get_storage_entry_height(&self, _: Self::Metadata) -> Result<u64>;
|
2018-12-10 11:38:29 -08:00
|
|
|
|
2019-02-04 17:41:03 -07:00
|
|
|
#[rpc(meta, name = "getStoragePubkeysForEntryHeight")]
|
|
|
|
fn get_storage_pubkeys_for_entry_height(
|
|
|
|
&self,
|
|
|
|
_: Self::Metadata,
|
|
|
|
_: u64,
|
|
|
|
) -> Result<Vec<Pubkey>>;
|
2019-03-03 22:01:09 -08:00
|
|
|
|
|
|
|
#[rpc(meta, name = "fullnodeExit")]
|
|
|
|
fn fullnode_exit(&self, _: Self::Metadata) -> Result<bool>;
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RpcSolImpl;
|
|
|
|
impl RpcSol for RpcSolImpl {
|
|
|
|
type Metadata = Meta;
|
|
|
|
|
|
|
|
fn confirm_transaction(&self, meta: Self::Metadata, id: String) -> Result<bool> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("confirm_transaction rpc request received: {:?}", id);
|
2018-09-26 17:12:40 -07:00
|
|
|
self.get_signature_status(meta, id)
|
|
|
|
.map(|status| status == RpcSignatureStatus::Confirmed)
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
2018-09-26 17:12:40 -07:00
|
|
|
|
2018-09-20 23:27:06 -06:00
|
|
|
fn get_account_info(&self, meta: Self::Metadata, id: String) -> Result<Account> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("get_account_info rpc request received: {:?}", id);
|
2018-10-15 11:01:40 -06:00
|
|
|
let pubkey = verify_pubkey(id)?;
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-03-09 19:28:43 -08:00
|
|
|
.get_account_info(&pubkey)
|
2018-09-20 23:27:06 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-11-05 09:36:22 -07:00
|
|
|
fn get_balance(&self, meta: Self::Metadata, id: String) -> Result<u64> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("get_balance rpc request received: {:?}", id);
|
2018-10-15 11:01:40 -06:00
|
|
|
let pubkey = verify_pubkey(id)?;
|
2019-03-09 19:28:43 -08:00
|
|
|
meta.request_processor.read().unwrap().get_balance(&pubkey)
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
fn get_recent_blockhash(&self, meta: Self::Metadata) -> Result<String> {
|
|
|
|
info!("get_recent_blockhash rpc request received");
|
2019-03-02 10:20:10 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-03-02 10:25:16 -08:00
|
|
|
.get_recent_blockhash()
|
2018-08-13 12:52:37 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-09-26 17:12:40 -07:00
|
|
|
fn get_signature_status(&self, meta: Self::Metadata, id: String) -> Result<RpcSignatureStatus> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("get_signature_status rpc request received: {:?}", id);
|
2018-10-24 14:06:45 -07:00
|
|
|
let signature = verify_signature(&id)?;
|
2019-01-15 12:20:07 -08:00
|
|
|
let res = meta
|
|
|
|
.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_signature_status(signature);
|
2018-12-17 07:55:56 -08:00
|
|
|
|
2019-01-16 11:46:52 -08:00
|
|
|
let status = {
|
|
|
|
if res.is_none() {
|
2018-12-17 07:55:56 -08:00
|
|
|
RpcSignatureStatus::SignatureNotFound
|
2019-01-16 11:46:52 -08:00
|
|
|
} else {
|
|
|
|
match res.unwrap() {
|
2019-01-31 06:53:52 -08:00
|
|
|
Ok(_) => RpcSignatureStatus::Confirmed,
|
2019-03-13 13:58:44 -06:00
|
|
|
Err(TransactionError::AccountInUse) => RpcSignatureStatus::AccountInUse,
|
|
|
|
Err(TransactionError::AccountLoadedTwice) => {
|
|
|
|
RpcSignatureStatus::AccountLoadedTwice
|
|
|
|
}
|
|
|
|
Err(TransactionError::InstructionError(_, _)) => {
|
2019-03-13 11:46:49 -06:00
|
|
|
RpcSignatureStatus::ProgramRuntimeError
|
|
|
|
}
|
2019-01-31 06:53:52 -08:00
|
|
|
Err(err) => {
|
|
|
|
trace!("mapping {:?} to GenericFailure", err);
|
|
|
|
RpcSignatureStatus::GenericFailure
|
2019-01-16 11:46:52 -08:00
|
|
|
}
|
2018-09-26 19:23:27 -07:00
|
|
|
}
|
2019-01-16 11:46:52 -08:00
|
|
|
}
|
2018-12-17 07:55:56 -08:00
|
|
|
};
|
2019-01-15 12:20:07 -08:00
|
|
|
info!("get_signature_status rpc request status: {:?}", status);
|
2018-12-17 07:55:56 -08:00
|
|
|
Ok(status)
|
2018-09-26 17:12:40 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-08-10 17:05:23 -06:00
|
|
|
fn get_transaction_count(&self, meta: Self::Metadata) -> Result<u64> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("get_transaction_count rpc request received");
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_transaction_count()
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-05 16:28:14 -08:00
|
|
|
fn request_airdrop(&self, meta: Self::Metadata, id: String, lamports: u64) -> Result<String> {
|
|
|
|
trace!("request_airdrop id={} lamports={}", id, lamports);
|
2019-03-06 09:26:12 -08:00
|
|
|
|
|
|
|
let drone_addr = meta
|
|
|
|
.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.config
|
|
|
|
.drone_addr
|
|
|
|
.ok_or_else(Error::invalid_request)?;
|
2018-10-15 11:01:40 -06:00
|
|
|
let pubkey = verify_pubkey(id)?;
|
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = meta
|
2019-03-02 10:20:10 -08:00
|
|
|
.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.bank()?
|
2019-03-02 10:25:16 -08:00
|
|
|
.last_blockhash();
|
2019-03-06 09:26:12 -08:00
|
|
|
let transaction = request_airdrop_transaction(&drone_addr, &pubkey, lamports, blockhash)
|
|
|
|
.map_err(|err| {
|
|
|
|
info!("request_airdrop_transaction failed: {:?}", err);
|
|
|
|
Error::internal_error()
|
|
|
|
})?;;
|
2018-11-14 18:57:34 -08:00
|
|
|
|
|
|
|
let data = serialize(&transaction).map_err(|err| {
|
|
|
|
info!("request_airdrop: serialize error: {:?}", err);
|
2018-10-23 10:59:43 -07:00
|
|
|
Error::internal_error()
|
2018-11-14 18:57:34 -08:00
|
|
|
})?;
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2018-11-14 18:57:34 -08:00
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2019-03-08 09:48:21 -08:00
|
|
|
let transactions_addr = get_tpu_addr(&meta.cluster_info)?;
|
2018-11-14 18:57:34 -08:00
|
|
|
transactions_socket
|
|
|
|
.send_to(&data, transactions_addr)
|
|
|
|
.map_err(|err| {
|
|
|
|
info!("request_airdrop: send_to error: {:?}", err);
|
|
|
|
Error::internal_error()
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let signature = transaction.signatures[0];
|
2018-08-22 12:25:21 -06:00
|
|
|
let now = Instant::now();
|
2018-08-26 22:32:51 -06:00
|
|
|
let mut signature_status;
|
2018-08-22 12:25:21 -06:00
|
|
|
loop {
|
2019-01-15 12:20:07 -08:00
|
|
|
signature_status = meta
|
|
|
|
.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_signature_status(signature);
|
2018-08-26 22:32:51 -06:00
|
|
|
|
2019-01-31 06:53:52 -08:00
|
|
|
if signature_status == Some(Ok(())) {
|
2018-11-14 18:57:34 -08:00
|
|
|
info!("airdrop signature ok");
|
2018-08-26 22:32:51 -06:00
|
|
|
return Ok(bs58::encode(signature).into_string());
|
2018-08-22 12:25:21 -06:00
|
|
|
} else if now.elapsed().as_secs() > 5 {
|
2018-10-23 10:59:43 -07:00
|
|
|
info!("airdrop signature timeout");
|
2018-08-22 12:25:21 -06:00
|
|
|
return Err(Error::internal_error());
|
|
|
|
}
|
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
}
|
2018-08-22 11:54:37 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-08-22 11:53:24 -06:00
|
|
|
fn send_transaction(&self, meta: Self::Metadata, data: Vec<u8>) -> Result<String> {
|
2018-09-18 11:31:21 -07:00
|
|
|
let tx: Transaction = deserialize(&data).map_err(|err| {
|
2018-10-23 10:59:43 -07:00
|
|
|
info!("send_transaction: deserialize error: {:?}", err);
|
2018-09-18 11:31:21 -07:00
|
|
|
Error::invalid_request()
|
|
|
|
})?;
|
2018-10-24 14:06:45 -07:00
|
|
|
if data.len() >= PACKET_DATA_SIZE {
|
|
|
|
info!(
|
|
|
|
"send_transaction: transaction too large: {} bytes (max: {} bytes)",
|
|
|
|
data.len(),
|
|
|
|
PACKET_DATA_SIZE
|
|
|
|
);
|
|
|
|
return Err(Error::invalid_request());
|
|
|
|
}
|
2018-08-22 11:53:24 -06:00
|
|
|
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2019-03-08 09:48:21 -08:00
|
|
|
let transactions_addr = get_tpu_addr(&meta.cluster_info)?;
|
2019-01-15 12:20:07 -08:00
|
|
|
trace!("send_transaction: leader is {:?}", &transactions_addr);
|
2018-08-22 11:53:24 -06:00
|
|
|
transactions_socket
|
2018-10-12 14:25:56 -06:00
|
|
|
.send_to(&data, transactions_addr)
|
2018-09-18 11:31:21 -07:00
|
|
|
.map_err(|err| {
|
2018-10-23 10:59:43 -07:00
|
|
|
info!("send_transaction: send_to error: {:?}", err);
|
2018-09-18 11:31:21 -07:00
|
|
|
Error::internal_error()
|
|
|
|
})?;
|
2018-10-26 14:43:34 -07:00
|
|
|
let signature = bs58::encode(tx.signatures[0]).into_string();
|
2018-10-24 14:06:45 -07:00
|
|
|
trace!(
|
|
|
|
"send_transaction: sent {} bytes, signature={}",
|
|
|
|
data.len(),
|
|
|
|
signature
|
|
|
|
);
|
|
|
|
Ok(signature)
|
2018-08-22 11:53:24 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
fn get_storage_blockhash(&self, meta: Self::Metadata) -> Result<String> {
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-03-02 10:25:16 -08:00
|
|
|
.get_storage_blockhash()
|
2018-11-02 08:40:29 -07:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2019-03-02 09:56:06 -08:00
|
|
|
fn get_storage_entry_height(&self, meta: Self::Metadata) -> Result<u64> {
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2019-03-02 09:56:06 -08:00
|
|
|
.get_storage_entry_height()
|
2018-12-10 11:38:29 -08:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-12-10 11:38:29 -08:00
|
|
|
fn get_storage_pubkeys_for_entry_height(
|
|
|
|
&self,
|
|
|
|
meta: Self::Metadata,
|
|
|
|
entry_height: u64,
|
|
|
|
) -> Result<Vec<Pubkey>> {
|
|
|
|
meta.request_processor
|
2019-01-15 12:20:07 -08:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
2018-12-10 11:38:29 -08:00
|
|
|
.get_storage_pubkeys_for_entry_height(entry_height)
|
|
|
|
}
|
2019-03-03 22:01:09 -08:00
|
|
|
|
|
|
|
fn fullnode_exit(&self, meta: Self::Metadata) -> Result<bool> {
|
|
|
|
meta.request_processor.read().unwrap().fullnode_exit()
|
|
|
|
}
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
2019-02-26 12:25:46 -08:00
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-08 17:23:07 -08:00
|
|
|
use crate::contact_info::ContactInfo;
|
2019-02-17 11:17:58 -07:00
|
|
|
use jsonrpc_core::{MetaIoHandler, Response};
|
2019-02-18 23:26:22 -07:00
|
|
|
use solana_sdk::genesis_block::GenesisBlock;
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::{hash, Hash};
|
2018-12-03 10:26:28 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2018-12-04 20:19:48 -08:00
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
2019-02-17 11:17:58 -07:00
|
|
|
use std::thread;
|
2018-08-13 11:24:39 -06:00
|
|
|
|
2019-03-09 19:28:43 -08:00
|
|
|
fn start_rpc_handler_with_tx(pubkey: &Pubkey) -> (MetaIoHandler<Meta>, Meta, Hash, Keypair) {
|
2019-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, alice) = GenesisBlock::new(10_000);
|
2019-02-22 17:15:15 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2019-03-03 22:01:09 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = bank.last_blockhash();
|
|
|
|
let tx = SystemTransaction::new_move(&alice, pubkey, 20, blockhash, 0);
|
2018-08-14 18:03:48 -06:00
|
|
|
bank.process_transaction(&tx).expect("process transaction");
|
|
|
|
|
2019-01-28 14:53:50 -08:00
|
|
|
let request_processor = Arc::new(RwLock::new(JsonRpcRequestProcessor::new(
|
|
|
|
StorageState::default(),
|
2019-03-03 22:01:09 -08:00
|
|
|
JsonRpcConfig::default(),
|
2019-03-04 16:21:33 -08:00
|
|
|
&exit,
|
2019-01-28 14:53:50 -08:00
|
|
|
)));
|
2019-02-27 10:23:27 -08:00
|
|
|
request_processor.write().unwrap().set_bank(&bank);
|
2019-03-06 19:09:37 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new_with_invalid_keypair(
|
2019-03-08 17:23:07 -08:00
|
|
|
ContactInfo::default(),
|
2019-03-06 19:09:37 -08:00
|
|
|
)));
|
2019-03-08 17:23:07 -08:00
|
|
|
let leader = ContactInfo::new_with_socketaddr(&socketaddr!("127.0.0.1:1234"));
|
2018-11-02 08:40:29 -07:00
|
|
|
|
2018-11-15 13:23:26 -08:00
|
|
|
cluster_info.write().unwrap().insert_info(leader.clone());
|
2018-08-13 11:24:39 -06:00
|
|
|
|
|
|
|
let mut io = MetaIoHandler::default();
|
|
|
|
let rpc = RpcSolImpl;
|
|
|
|
io.extend_with(rpc.to_delegate());
|
2018-08-22 11:54:37 -06:00
|
|
|
let meta = Meta {
|
|
|
|
request_processor,
|
2018-10-12 14:25:56 -06:00
|
|
|
cluster_info,
|
2018-08-22 11:54:37 -06:00
|
|
|
};
|
2019-03-02 10:25:16 -08:00
|
|
|
(io, meta, blockhash, alice)
|
2018-10-15 11:01:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_request_processor_new() {
|
2019-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, alice) = GenesisBlock::new(10_000);
|
2018-10-15 11:01:40 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-02-22 17:15:15 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2019-03-03 22:01:09 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
let mut request_processor =
|
2019-03-04 16:21:33 -08:00
|
|
|
JsonRpcRequestProcessor::new(StorageState::default(), JsonRpcConfig::default(), &exit);
|
2019-02-27 10:23:27 -08:00
|
|
|
request_processor.set_bank(&bank);
|
2018-10-15 11:01:40 -06:00
|
|
|
thread::spawn(move || {
|
2019-03-02 10:25:16 -08:00
|
|
|
let blockhash = bank.last_blockhash();
|
2019-03-09 19:28:43 -08:00
|
|
|
let tx = SystemTransaction::new_move(&alice, &bob_pubkey, 20, blockhash, 0);
|
2019-02-20 21:23:44 -08:00
|
|
|
bank.process_transaction(&tx).expect("process transaction");
|
2018-12-07 20:01:28 -07:00
|
|
|
})
|
|
|
|
.join()
|
2018-10-15 11:01:40 -06:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(request_processor.get_transaction_count().unwrap(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_get_balance() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, _blockhash, _alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
let req = format!(
|
2018-08-15 12:41:39 -06:00
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"getBalance","params":["{}"]}}"#,
|
2018-08-13 11:24:39 -06:00
|
|
|
bob_pubkey
|
|
|
|
);
|
2018-10-15 11:01:40 -06:00
|
|
|
let res = io.handle_request_sync(&req, meta);
|
2018-08-20 14:20:20 -07:00
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":20,"id":1}}"#);
|
2018-08-13 11:24:39 -06:00
|
|
|
let expected: Response =
|
2018-08-14 18:03:48 -06:00
|
|
|
serde_json::from_str(&expected).expect("expected response deserialization");
|
2018-08-13 11:24:39 -06:00
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
2018-10-15 11:01:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_get_tx_count() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, _blockhash, _alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
2018-08-13 11:24:39 -06:00
|
|
|
|
2018-08-15 12:41:39 -06:00
|
|
|
let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getTransactionCount"}}"#);
|
2018-10-15 11:01:40 -06:00
|
|
|
let res = io.handle_request_sync(&req, meta);
|
2018-08-14 18:03:48 -06:00
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":1,"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"))
|
2018-08-13 11:24:39 -06:00
|
|
|
.expect("actual response deserialization");
|
2018-08-14 18:03:48 -06:00
|
|
|
assert_eq!(expected, result);
|
2018-10-15 11:01:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_get_account_info() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, _blockhash, _alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
2018-09-20 13:20:37 -07:00
|
|
|
|
|
|
|
let req = format!(
|
2018-09-20 14:51:17 -07:00
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"getAccountInfo","params":["{}"]}}"#,
|
2018-09-20 13:20:37 -07:00
|
|
|
bob_pubkey
|
|
|
|
);
|
2018-10-15 11:01:40 -06:00
|
|
|
let res = io.handle_request_sync(&req, meta);
|
2018-09-20 13:20:37 -07:00
|
|
|
let expected = r#"{
|
|
|
|
"jsonrpc":"2.0",
|
|
|
|
"result":{
|
2018-11-12 09:29:17 -08:00
|
|
|
"owner": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
2019-03-05 16:28:14 -08:00
|
|
|
"lamports": 20,
|
2019-03-14 10:48:27 -06:00
|
|
|
"data": [],
|
2019-02-14 10:57:54 -07:00
|
|
|
"executable": false
|
2018-09-20 13:20:37 -07:00
|
|
|
},
|
|
|
|
"id":1}
|
|
|
|
"#;
|
|
|
|
let expected: Response =
|
|
|
|
serde_json::from_str(&expected).expect("expected response deserialization");
|
2018-10-15 11:01:40 -06:00
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
2018-09-20 13:20:37 -07:00
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
#[test]
|
|
|
|
fn test_rpc_confirm_tx() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, blockhash, alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
|
|
|
let tx = SystemTransaction::new_move(&alice, &bob_pubkey, 20, blockhash, 0);
|
2018-10-15 11:01:40 -06:00
|
|
|
|
|
|
|
let req = format!(
|
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"confirmTransaction","params":["{}"]}}"#,
|
2018-10-26 14:43:34 -07:00
|
|
|
tx.signatures[0]
|
2018-10-15 11:01:40 -06:00
|
|
|
);
|
|
|
|
let res = io.handle_request_sync(&req, meta);
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":true,"id":1}}"#);
|
|
|
|
let expected: Response =
|
|
|
|
serde_json::from_str(&expected).expect("expected response deserialization");
|
2018-09-20 13:20:37 -07:00
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
2018-08-13 11:24:39 -06:00
|
|
|
}
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
#[test]
|
2018-10-15 11:01:40 -06:00
|
|
|
fn test_rpc_get_signature_status() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, blockhash, alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
|
|
|
let tx = SystemTransaction::new_move(&alice, &bob_pubkey, 20, blockhash, 0);
|
2018-08-15 10:37:02 -06:00
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
let req = format!(
|
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatus","params":["{}"]}}"#,
|
2018-10-26 14:43:34 -07:00
|
|
|
tx.signatures[0]
|
2018-10-15 11:01:40 -06:00
|
|
|
);
|
|
|
|
let res = io.handle_request_sync(&req, meta.clone());
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":"Confirmed","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"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
2018-08-13 11:24:39 -06:00
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
// Test getSignatureStatus request on unprocessed tx
|
2019-03-09 19:28:43 -08:00
|
|
|
let tx = SystemTransaction::new_move(&alice, &bob_pubkey, 10, blockhash, 0);
|
2018-10-15 11:01:40 -06:00
|
|
|
let req = format!(
|
|
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatus","params":["{}"]}}"#,
|
2018-10-26 14:43:34 -07:00
|
|
|
tx.signatures[0]
|
2018-10-15 11:01:40 -06:00
|
|
|
);
|
|
|
|
let res = io.handle_request_sync(&req, meta);
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":"SignatureNotFound","id":1}}"#);
|
2018-08-13 11:24:39 -06:00
|
|
|
let expected: Response =
|
2018-10-15 11:01:40 -06:00
|
|
|
serde_json::from_str(&expected).expect("expected response deserialization");
|
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-02 10:25:16 -08:00
|
|
|
fn test_rpc_get_recent_blockhash() {
|
2018-10-15 11:01:40 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, blockhash, _alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2019-03-02 10:25:16 -08:00
|
|
|
let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getRecentBlockhash"}}"#);
|
2018-10-15 11:01:40 -06:00
|
|
|
let res = io.handle_request_sync(&req, meta);
|
2019-03-02 10:25:16 -08:00
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":"{}","id":1}}"#, blockhash);
|
2018-10-15 11:01:40 -06:00
|
|
|
let expected: Response =
|
|
|
|
serde_json::from_str(&expected).expect("expected response deserialization");
|
2018-08-13 11:24:39 -06:00
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
#[test]
|
2018-11-01 11:30:56 -06:00
|
|
|
fn test_rpc_fail_request_airdrop() {
|
2018-10-15 11:01:40 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
2019-03-09 19:28:43 -08:00
|
|
|
let (io, meta, _blockhash, _alice) = start_rpc_handler_with_tx(&bob_pubkey);
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2019-03-06 09:26:12 -08:00
|
|
|
// Expect internal error because no drone is available
|
2018-10-15 11:01:40 -06:00
|
|
|
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 =
|
2019-03-06 09:26:12 -08:00
|
|
|
r#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":1}"#;
|
2018-10-15 11:01:40 -06:00
|
|
|
let expected: Response =
|
|
|
|
serde_json::from_str(expected).expect("expected response deserialization");
|
|
|
|
let result: Response = serde_json::from_str(&res.expect("actual response"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_send_bad_tx() {
|
2019-01-24 12:04:04 -08:00
|
|
|
let (genesis_block, _) = GenesisBlock::new(10_000);
|
2019-02-22 17:15:15 -08:00
|
|
|
let bank = Arc::new(Bank::new(&genesis_block));
|
2019-03-03 22:01:09 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-08-15 10:37:02 -06:00
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
let mut io = MetaIoHandler::default();
|
|
|
|
let rpc = RpcSolImpl;
|
|
|
|
io.extend_with(rpc.to_delegate());
|
|
|
|
let meta = Meta {
|
2019-02-22 17:15:15 -08:00
|
|
|
request_processor: {
|
2019-03-03 22:01:09 -08:00
|
|
|
let mut request_processor = JsonRpcRequestProcessor::new(
|
|
|
|
StorageState::default(),
|
|
|
|
JsonRpcConfig::default(),
|
2019-03-04 16:21:33 -08:00
|
|
|
&exit,
|
2019-03-03 22:01:09 -08:00
|
|
|
);
|
2019-02-27 10:23:27 -08:00
|
|
|
request_processor.set_bank(&bank);
|
2019-02-22 17:15:15 -08:00
|
|
|
Arc::new(RwLock::new(request_processor))
|
|
|
|
},
|
2019-03-06 19:09:37 -08:00
|
|
|
cluster_info: Arc::new(RwLock::new(ClusterInfo::new_with_invalid_keypair(
|
2019-03-08 17:23:07 -08:00
|
|
|
ContactInfo::default(),
|
2019-03-06 19:09:37 -08:00
|
|
|
))),
|
2018-08-13 11:24:39 -06:00
|
|
|
};
|
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
let req =
|
|
|
|
r#"{"jsonrpc":"2.0","id":1,"method":"sendTransaction","params":[[0,0,0,0,0,0,0,0]]}"#;
|
|
|
|
let res = io.handle_request_sync(req, meta.clone());
|
2018-08-13 11:24:39 -06:00
|
|
|
let expected =
|
|
|
|
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"))
|
|
|
|
.expect("actual response deserialization");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
2018-10-15 11:01:40 -06:00
|
|
|
|
2018-10-12 14:25:56 -06:00
|
|
|
#[test]
|
2019-03-08 09:48:21 -08:00
|
|
|
fn test_rpc_get_tpu_addr() {
|
2019-03-06 19:09:37 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new_with_invalid_keypair(
|
2019-03-08 17:23:07 -08:00
|
|
|
ContactInfo::new_with_socketaddr(&socketaddr!("127.0.0.1:1234")),
|
2019-03-06 19:09:37 -08:00
|
|
|
)));
|
2018-10-12 14:25:56 -06:00
|
|
|
assert_eq!(
|
2019-03-08 09:48:21 -08:00
|
|
|
get_tpu_addr(&cluster_info),
|
2018-10-12 14:25:56 -06:00
|
|
|
Ok(socketaddr!("127.0.0.1:1234"))
|
|
|
|
);
|
|
|
|
}
|
2018-10-15 11:01:40 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_verify_pubkey() {
|
|
|
|
let pubkey = Keypair::new().pubkey();
|
|
|
|
assert_eq!(verify_pubkey(pubkey.to_string()).unwrap(), pubkey);
|
|
|
|
let bad_pubkey = "a1b2c3d4";
|
|
|
|
assert_eq!(
|
|
|
|
verify_pubkey(bad_pubkey.to_string()),
|
|
|
|
Err(Error::invalid_request())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_verify_signature() {
|
2019-02-01 08:36:35 -07:00
|
|
|
let tx = SystemTransaction::new_move(
|
|
|
|
&Keypair::new(),
|
2019-03-09 19:28:43 -08:00
|
|
|
&Keypair::new().pubkey(),
|
2019-02-01 08:36:35 -07:00
|
|
|
20,
|
|
|
|
hash(&[0]),
|
|
|
|
0,
|
|
|
|
);
|
2018-10-15 11:01:40 -06:00
|
|
|
assert_eq!(
|
2018-10-26 14:43:34 -07:00
|
|
|
verify_signature(&tx.signatures[0].to_string()).unwrap(),
|
|
|
|
tx.signatures[0]
|
2018-10-15 11:01:40 -06:00
|
|
|
);
|
|
|
|
let bad_signature = "a1b2c3d4";
|
|
|
|
assert_eq!(
|
2018-10-24 14:06:45 -07:00
|
|
|
verify_signature(&bad_signature.to_string()),
|
2018-10-15 11:01:40 -06:00
|
|
|
Err(Error::invalid_request())
|
|
|
|
);
|
|
|
|
}
|
2019-03-03 22:01:09 -08:00
|
|
|
|
|
|
|
#[test]
|
2019-03-04 09:59:36 -08:00
|
|
|
fn test_rpc_request_processor_config_default_trait_fullnode_exit_fails() {
|
2019-03-03 22:01:09 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-03-04 16:21:33 -08:00
|
|
|
let request_processor =
|
|
|
|
JsonRpcRequestProcessor::new(StorageState::default(), JsonRpcConfig::default(), &exit);
|
2019-03-03 22:03:36 -08:00
|
|
|
assert_eq!(request_processor.fullnode_exit(), Ok(false));
|
2019-03-03 22:01:09 -08:00
|
|
|
assert_eq!(exit.load(Ordering::Relaxed), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-04 09:59:36 -08:00
|
|
|
fn test_rpc_request_processor_allow_fullnode_exit_config() {
|
2019-03-03 22:01:09 -08:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-03-05 21:12:30 -08:00
|
|
|
let mut config = JsonRpcConfig::default();
|
|
|
|
config.enable_fullnode_exit = true;
|
|
|
|
let request_processor =
|
|
|
|
JsonRpcRequestProcessor::new(StorageState::default(), config, &exit);
|
2019-03-03 22:03:36 -08:00
|
|
|
assert_eq!(request_processor.fullnode_exit(), Ok(true));
|
2019-03-03 22:01:09 -08:00
|
|
|
assert_eq!(exit.load(Ordering::Relaxed), true);
|
|
|
|
}
|
2018-08-13 11:24:39 -06:00
|
|
|
}
|