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-17 07:55:56 -08:00
|
|
|
use crate::bank::{self, Bank, BankError};
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::cluster_info::ClusterInfo;
|
|
|
|
use crate::jsonrpc_core::*;
|
|
|
|
use crate::jsonrpc_http_server::*;
|
|
|
|
use crate::packet::PACKET_DATA_SIZE;
|
|
|
|
use crate::service::Service;
|
2018-12-17 07:55:56 -08:00
|
|
|
use crate::status_deque::Status;
|
2018-11-14 18:57:34 -08:00
|
|
|
use bincode::{deserialize, serialize};
|
2018-08-10 17:05:23 -06:00
|
|
|
use bs58;
|
2019-01-04 17:20:51 -08:00
|
|
|
use solana_drone::drone::request_airdrop_transaction;
|
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;
|
2018-11-29 16:18:47 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2018-08-10 17:05:23 -06:00
|
|
|
use std::mem;
|
2018-08-22 11:53:24 -06:00
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
2018-10-24 10:01:19 -06:00
|
|
|
use std::str::FromStr;
|
2018-09-10 22:41:44 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-10-12 14:25:56 -06:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-09-10 22:41:44 -07:00
|
|
|
use std::thread::{self, sleep, Builder, JoinHandle};
|
2018-08-22 12:25:21 -06:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
2018-08-10 17:05:23 -06:00
|
|
|
|
|
|
|
pub const RPC_PORT: u16 = 8899;
|
|
|
|
|
2018-08-14 18:03:48 -06:00
|
|
|
pub struct JsonRpcService {
|
2018-09-10 22:41:44 -07:00
|
|
|
thread_hdl: JoinHandle<()>,
|
2018-10-25 16:58:40 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2019-01-15 12:20:07 -08:00
|
|
|
request_processor: Arc<RwLock<JsonRpcRequestProcessor>>,
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonRpcService {
|
2018-08-22 11:53:24 -06:00
|
|
|
pub fn new(
|
2018-08-22 16:44:26 -06:00
|
|
|
bank: &Arc<Bank>,
|
2018-10-12 14:25:56 -06:00
|
|
|
cluster_info: &Arc<RwLock<ClusterInfo>>,
|
2018-08-22 11:53:24 -06:00
|
|
|
rpc_addr: SocketAddr,
|
2019-01-04 17:20:51 -08:00
|
|
|
drone_addr: SocketAddr,
|
2018-08-22 11:53:24 -06:00
|
|
|
) -> Self {
|
2019-01-15 12:20:07 -08:00
|
|
|
info!("rpc bound to {:?}", rpc_addr);
|
2018-10-25 16:58:40 -07:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2019-01-15 12:20:07 -08:00
|
|
|
let request_processor = Arc::new(RwLock::new(JsonRpcRequestProcessor::new(bank.clone())));
|
|
|
|
request_processor.write().unwrap().bank = bank.clone();
|
|
|
|
let request_processor_ = request_processor.clone();
|
|
|
|
|
2018-10-12 14:25:56 -06:00
|
|
|
let info = cluster_info.clone();
|
2018-10-25 16:58:40 -07:00
|
|
|
let exit_ = exit.clone();
|
2019-01-15 12:20:07 -08:00
|
|
|
|
2018-09-10 22:41:44 -07:00
|
|
|
let thread_hdl = Builder::new()
|
|
|
|
.name("solana-jsonrpc".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
let mut io = MetaIoHandler::default();
|
|
|
|
let rpc = RpcSolImpl;
|
|
|
|
io.extend_with(rpc.to_delegate());
|
2018-08-15 12:41:39 -06:00
|
|
|
|
2018-09-10 22:41:44 -07:00
|
|
|
let server =
|
2018-09-28 08:20:26 -06:00
|
|
|
ServerBuilder::with_meta_extractor(io, move |_req: &hyper::Request<hyper::Body>| Meta {
|
2019-01-15 12:20:07 -08:00
|
|
|
request_processor: request_processor_.clone(),
|
2018-10-12 14:25:56 -06:00
|
|
|
cluster_info: info.clone(),
|
2019-01-04 17:20:51 -08:00
|
|
|
drone_addr,
|
2018-10-10 14:51:43 -06:00
|
|
|
rpc_addr,
|
2018-09-10 22:41:44 -07:00
|
|
|
}).threads(4)
|
|
|
|
.cors(DomainsValidation::AllowOnly(vec![
|
|
|
|
AccessControlAllowOrigin::Any,
|
|
|
|
]))
|
|
|
|
.start_http(&rpc_addr);
|
2018-11-29 10:51:07 -08:00
|
|
|
if let Err(e) = server {
|
|
|
|
warn!("JSON RPC service unavailable error: {:?}. \nAlso, check that port {} is not already in use by another application", e, rpc_addr.port());
|
2018-09-10 22:41:44 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-10-25 16:58:40 -07:00
|
|
|
while !exit_.load(Ordering::Relaxed) {
|
2018-09-10 22:41:44 -07:00
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
}
|
2018-10-12 11:04:14 -07:00
|
|
|
server.unwrap().close();
|
2018-09-10 22:41:44 -07:00
|
|
|
})
|
|
|
|
.unwrap();
|
2019-01-15 12:20:07 -08:00
|
|
|
Self {
|
|
|
|
thread_hdl,
|
|
|
|
exit,
|
|
|
|
request_processor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_bank(&mut self, bank: &Arc<Bank>) {
|
|
|
|
self.request_processor.write().unwrap().bank = bank.clone();
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit(&self) {
|
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self) -> thread::Result<()> {
|
|
|
|
self.exit();
|
|
|
|
self.join()
|
2018-09-10 22:41:44 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2018-09-10 22:41:44 -07:00
|
|
|
impl Service for JsonRpcService {
|
2018-09-13 14:00:17 -07:00
|
|
|
type JoinReturnType = ();
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2018-09-10 22:41:44 -07:00
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-09-15 23:46:16 -07:00
|
|
|
self.thread_hdl.join()
|
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-10-10 14:51:43 -06:00
|
|
|
pub rpc_addr: SocketAddr,
|
2019-01-04 17:20:51 -08:00
|
|
|
pub drone_addr: SocketAddr,
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
|
|
|
impl Metadata for Meta {}
|
|
|
|
|
2018-10-10 14:51:43 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Serialize, Debug)]
|
2018-09-26 17:12:40 -07:00
|
|
|
pub enum RpcSignatureStatus {
|
2018-10-23 13:11:29 -07:00
|
|
|
AccountInUse,
|
2018-09-26 17:12:40 -07:00
|
|
|
Confirmed,
|
|
|
|
GenericFailure,
|
2018-10-23 13:11:29 -07:00
|
|
|
ProgramRuntimeError,
|
|
|
|
SignatureNotFound,
|
2018-09-26 17:12:40 -07:00
|
|
|
}
|
2018-10-24 10:01:19 -06:00
|
|
|
impl FromStr for RpcSignatureStatus {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<RpcSignatureStatus> {
|
|
|
|
match s {
|
|
|
|
"AccountInUse" => Ok(RpcSignatureStatus::AccountInUse),
|
|
|
|
"Confirmed" => Ok(RpcSignatureStatus::Confirmed),
|
|
|
|
"GenericFailure" => Ok(RpcSignatureStatus::GenericFailure),
|
|
|
|
"ProgramRuntimeError" => Ok(RpcSignatureStatus::ProgramRuntimeError),
|
|
|
|
"SignatureNotFound" => Ok(RpcSignatureStatus::SignatureNotFound),
|
|
|
|
_ => Err(Error::parse_error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:12:40 -07:00
|
|
|
|
2018-08-10 17:05:23 -06:00
|
|
|
build_rpc_trait! {
|
|
|
|
pub trait RpcSol {
|
|
|
|
type Metadata;
|
|
|
|
|
2018-08-15 11:34:17 -06:00
|
|
|
#[rpc(meta, name = "confirmTransaction")]
|
2018-08-10 17:05:23 -06:00
|
|
|
fn confirm_transaction(&self, Self::Metadata, String) -> Result<bool>;
|
|
|
|
|
2018-09-20 23:27:06 -06:00
|
|
|
#[rpc(meta, name = "getAccountInfo")]
|
|
|
|
fn get_account_info(&self, Self::Metadata, String) -> Result<Account>;
|
|
|
|
|
2018-08-15 11:34:17 -06:00
|
|
|
#[rpc(meta, name = "getBalance")]
|
2018-11-05 09:36:22 -07:00
|
|
|
fn get_balance(&self, Self::Metadata, String) -> Result<u64>;
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2018-12-21 08:25:07 -08:00
|
|
|
#[rpc(meta, name = "getConfirmationTime")]
|
|
|
|
fn get_confirmation_time(&self, Self::Metadata) -> Result<usize>;
|
2018-08-10 17:05:23 -06:00
|
|
|
|
2018-08-15 11:34:17 -06:00
|
|
|
#[rpc(meta, name = "getLastId")]
|
2018-08-13 12:52:37 -06:00
|
|
|
fn get_last_id(&self, Self::Metadata) -> Result<String>;
|
|
|
|
|
2018-09-26 17:12:40 -07:00
|
|
|
#[rpc(meta, name = "getSignatureStatus")]
|
|
|
|
fn get_signature_status(&self, Self::Metadata, String) -> Result<RpcSignatureStatus>;
|
|
|
|
|
2018-08-15 11:34:17 -06:00
|
|
|
#[rpc(meta, name = "getTransactionCount")]
|
2018-08-10 17:05:23 -06:00
|
|
|
fn get_transaction_count(&self, Self::Metadata) -> Result<u64>;
|
|
|
|
|
2018-08-22 11:54:37 -06:00
|
|
|
#[rpc(meta, name= "requestAirdrop")]
|
2018-08-26 22:32:51 -06:00
|
|
|
fn request_airdrop(&self, Self::Metadata, String, u64) -> Result<String>;
|
2018-08-22 11:54:37 -06:00
|
|
|
|
2018-08-22 11:53:24 -06:00
|
|
|
#[rpc(meta, name = "sendTransaction")]
|
|
|
|
fn send_transaction(&self, Self::Metadata, Vec<u8>) -> Result<String>;
|
2018-11-02 08:40:29 -07:00
|
|
|
|
|
|
|
#[rpc(meta, name = "getStorageMiningLastId")]
|
|
|
|
fn get_storage_mining_last_id(&self, Self::Metadata) -> Result<String>;
|
2018-12-10 11:38:29 -08:00
|
|
|
|
|
|
|
#[rpc(meta, name = "getStorageMiningEntryHeight")]
|
|
|
|
fn get_storage_mining_entry_height(&self, Self::Metadata) -> Result<u64>;
|
|
|
|
|
|
|
|
#[rpc(meta, name = "getStoragePubkeysForEntryHeight")]
|
|
|
|
fn get_storage_pubkeys_for_entry_height(&self, Self::Metadata, u64) -> Result<Vec<Pubkey>>;
|
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()
|
|
|
|
.get_account_info(pubkey)
|
2018-09-20 23:27:06 -06: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-01-15 12:20:07 -08:00
|
|
|
meta.request_processor.read().unwrap().get_balance(pubkey)
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
2018-12-21 08:25:07 -08:00
|
|
|
fn get_confirmation_time(&self, meta: Self::Metadata) -> Result<usize> {
|
|
|
|
info!("get_confirmation_time rpc request received");
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_confirmation_time()
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
2018-08-13 12:52:37 -06:00
|
|
|
fn get_last_id(&self, meta: Self::Metadata) -> Result<String> {
|
2018-10-31 13:27:48 -06:00
|
|
|
info!("get_last_id rpc request received");
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor.read().unwrap().get_last_id()
|
2018-08-13 12:52:37 -06: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
|
|
|
if res.is_none() {
|
|
|
|
return Ok(RpcSignatureStatus::SignatureNotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = match res.unwrap() {
|
|
|
|
Status::Reserved => {
|
|
|
|
// Report SignatureReserved as SignatureNotFound as SignatureReserved is
|
|
|
|
// transitory while the bank processes the associated transaction.
|
|
|
|
RpcSignatureStatus::SignatureNotFound
|
|
|
|
}
|
|
|
|
Status::Complete(res) => match res {
|
2018-09-26 17:12:40 -07:00
|
|
|
Ok(_) => RpcSignatureStatus::Confirmed,
|
2018-10-23 13:11:29 -07:00
|
|
|
Err(BankError::AccountInUse) => RpcSignatureStatus::AccountInUse,
|
2018-11-23 15:53:39 -07:00
|
|
|
Err(BankError::ProgramError(_, _)) => RpcSignatureStatus::ProgramRuntimeError,
|
2018-09-26 19:23:27 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("mapping {:?} to GenericFailure", err);
|
|
|
|
RpcSignatureStatus::GenericFailure
|
|
|
|
}
|
2018-09-26 17:12:40 -07: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
|
|
|
}
|
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
|
|
|
}
|
2018-08-26 22:32:51 -06:00
|
|
|
fn request_airdrop(&self, meta: Self::Metadata, id: String, tokens: u64) -> Result<String> {
|
2018-10-24 14:06:45 -07:00
|
|
|
trace!("request_airdrop id={} tokens={}", id, tokens);
|
2018-10-15 11:01:40 -06:00
|
|
|
let pubkey = verify_pubkey(id)?;
|
|
|
|
|
2019-01-15 12:20:07 -08:00
|
|
|
let last_id = meta.request_processor.read().unwrap().bank.last_id();
|
2019-01-04 17:20:51 -08:00
|
|
|
let transaction = request_airdrop_transaction(&meta.drone_addr, &pubkey, tokens, last_id)
|
2018-11-14 18:57:34 -08:00
|
|
|
.map_err(|err| {
|
2019-01-04 17:20:51 -08:00
|
|
|
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();
|
|
|
|
let transactions_addr = get_leader_addr(&meta.cluster_info)?;
|
|
|
|
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
|
|
|
|
2018-12-17 07:55:56 -08:00
|
|
|
if signature_status == Some(Status::Complete(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
|
|
|
}
|
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();
|
2018-10-15 11:01:40 -06:00
|
|
|
let transactions_addr = get_leader_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
|
|
|
}
|
2018-11-02 08:40:29 -07:00
|
|
|
fn get_storage_mining_last_id(&self, meta: Self::Metadata) -> Result<String> {
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_storage_mining_last_id()
|
2018-11-02 08:40:29 -07:00
|
|
|
}
|
2018-12-10 11:38:29 -08:00
|
|
|
fn get_storage_mining_entry_height(&self, meta: Self::Metadata) -> Result<u64> {
|
2019-01-15 12:20:07 -08:00
|
|
|
meta.request_processor
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get_storage_mining_entry_height()
|
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)
|
|
|
|
}
|
2018-08-14 18:03:48 -06:00
|
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct JsonRpcRequestProcessor {
|
|
|
|
bank: Arc<Bank>,
|
|
|
|
}
|
|
|
|
impl JsonRpcRequestProcessor {
|
|
|
|
/// Create a new request processor that wraps the given Bank.
|
|
|
|
pub fn new(bank: Arc<Bank>) -> Self {
|
|
|
|
JsonRpcRequestProcessor { bank }
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
|
|
|
|
2018-08-15 12:41:39 -06:00
|
|
|
/// Process JSON-RPC request items sent via JSON-RPC.
|
2018-10-10 14:51:43 -06:00
|
|
|
pub fn get_account_info(&self, pubkey: Pubkey) -> Result<Account> {
|
2018-09-20 23:27:06 -06:00
|
|
|
self.bank
|
|
|
|
.get_account(&pubkey)
|
2018-09-24 13:26:47 -06:00
|
|
|
.ok_or_else(Error::invalid_request)
|
2018-09-20 23:27:06 -06:00
|
|
|
}
|
2018-11-05 09:36:22 -07:00
|
|
|
fn get_balance(&self, pubkey: Pubkey) -> Result<u64> {
|
2018-08-15 12:41:39 -06:00
|
|
|
let val = self.bank.get_balance(&pubkey);
|
2018-08-20 14:20:20 -07:00
|
|
|
Ok(val)
|
2018-08-15 12:41:39 -06:00
|
|
|
}
|
2018-12-21 08:25:07 -08:00
|
|
|
fn get_confirmation_time(&self) -> Result<usize> {
|
|
|
|
Ok(self.bank.confirmation_time())
|
2018-08-15 12:41:39 -06:00
|
|
|
}
|
|
|
|
fn get_last_id(&self) -> Result<String> {
|
|
|
|
let id = self.bank.last_id();
|
|
|
|
Ok(bs58::encode(id).into_string())
|
|
|
|
}
|
2018-12-17 07:55:56 -08:00
|
|
|
pub fn get_signature_status(&self, signature: Signature) -> Option<Status<bank::Result<()>>> {
|
2018-09-26 17:12:40 -07:00
|
|
|
self.bank.get_signature_status(&signature)
|
2018-08-15 12:41:39 -06:00
|
|
|
}
|
|
|
|
fn get_transaction_count(&self) -> Result<u64> {
|
|
|
|
Ok(self.bank.transaction_count() as u64)
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
2018-11-02 08:40:29 -07:00
|
|
|
fn get_storage_mining_last_id(&self) -> Result<String> {
|
2018-12-23 21:40:52 +00:00
|
|
|
let id = self.bank.storage_state.get_last_id();
|
2018-11-02 08:40:29 -07:00
|
|
|
Ok(bs58::encode(id).into_string())
|
|
|
|
}
|
2018-12-10 11:38:29 -08:00
|
|
|
fn get_storage_mining_entry_height(&self) -> Result<u64> {
|
2018-12-23 21:40:52 +00:00
|
|
|
let entry_height = self.bank.storage_state.get_entry_height();
|
2018-12-10 11:38:29 -08:00
|
|
|
Ok(entry_height)
|
|
|
|
}
|
|
|
|
fn get_storage_pubkeys_for_entry_height(&self, entry_height: u64) -> Result<Vec<Pubkey>> {
|
|
|
|
Ok(self.bank.get_pubkeys_for_entry_height(entry_height))
|
|
|
|
}
|
2018-08-10 17:05:23 -06:00
|
|
|
}
|
2018-08-13 11:24:39 -06:00
|
|
|
|
2018-10-12 14:25:56 -06:00
|
|
|
fn get_leader_addr(cluster_info: &Arc<RwLock<ClusterInfo>>) -> Result<SocketAddr> {
|
|
|
|
if let Some(leader_data) = cluster_info.read().unwrap().leader_data() {
|
2018-11-15 13:23:26 -08:00
|
|
|
Ok(leader_data.tpu)
|
2018-10-12 14:25:56 -06:00
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
code: ErrorCode::InternalError,
|
|
|
|
message: "No leader detected".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
fn verify_pubkey(input: String) -> Result<Pubkey> {
|
2018-10-23 10:59:43 -07:00
|
|
|
let pubkey_vec = bs58::decode(input).into_vec().map_err(|err| {
|
|
|
|
info!("verify_pubkey: invalid input: {:?}", err);
|
|
|
|
Error::invalid_request()
|
|
|
|
})?;
|
2018-10-15 11:01:40 -06:00
|
|
|
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
|
2018-10-24 14:06:45 -07:00
|
|
|
info!(
|
|
|
|
"verify_pubkey: invalid pubkey_vec length: {}",
|
|
|
|
pubkey_vec.len()
|
|
|
|
);
|
2018-10-15 11:01:40 -06:00
|
|
|
Err(Error::invalid_request())
|
|
|
|
} else {
|
|
|
|
Ok(Pubkey::new(&pubkey_vec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:06:45 -07:00
|
|
|
fn verify_signature(input: &str) -> Result<Signature> {
|
2018-10-23 10:59:43 -07:00
|
|
|
let signature_vec = bs58::decode(input).into_vec().map_err(|err| {
|
2018-10-24 14:06:45 -07:00
|
|
|
info!("verify_signature: invalid input: {}: {:?}", input, err);
|
2018-10-23 10:59:43 -07:00
|
|
|
Error::invalid_request()
|
|
|
|
})?;
|
2018-10-15 11:01:40 -06:00
|
|
|
if signature_vec.len() != mem::size_of::<Signature>() {
|
2018-10-24 14:06:45 -07:00
|
|
|
info!(
|
|
|
|
"verify_signature: invalid signature_vec length: {}",
|
|
|
|
signature_vec.len()
|
|
|
|
);
|
2018-10-15 11:01:40 -06:00
|
|
|
Err(Error::invalid_request())
|
|
|
|
} else {
|
|
|
|
Ok(Signature::new(&signature_vec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 11:24:39 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::bank::Bank;
|
|
|
|
use crate::cluster_info::{Node, NodeInfo};
|
2019-01-09 14:33:44 -08:00
|
|
|
use crate::db_ledger::create_tmp_ledger_with_mint;
|
2018-12-07 20:16:27 -07:00
|
|
|
use crate::fullnode::Fullnode;
|
|
|
|
use crate::jsonrpc_core::Response;
|
|
|
|
use crate::leader_scheduler::LeaderScheduler;
|
|
|
|
use crate::mint::Mint;
|
|
|
|
use crate::rpc_request::get_rpc_request_str;
|
2019-01-11 12:58:31 -08:00
|
|
|
use crate::vote_signer_proxy::VoteSignerProxy;
|
2018-10-15 11:01:40 -06:00
|
|
|
use bincode::serialize;
|
|
|
|
use reqwest;
|
|
|
|
use reqwest::header::CONTENT_TYPE;
|
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;
|
2018-11-29 16:18:47 -08:00
|
|
|
use solana_sdk::transaction::Transaction;
|
2019-01-11 12:58:31 -08:00
|
|
|
use solana_vote_signer::rpc::LocalVoteSigner;
|
2018-10-15 11:01:40 -06:00
|
|
|
use std::fs::remove_dir_all;
|
2018-08-22 11:54:37 -06:00
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2018-08-13 11:24:39 -06:00
|
|
|
|
2018-10-15 11:01:40 -06:00
|
|
|
fn start_rpc_handler_with_tx(pubkey: Pubkey) -> (MetaIoHandler<Meta>, Meta, Hash, Keypair) {
|
2018-08-13 11:24:39 -06:00
|
|
|
let alice = Mint::new(10_000);
|
2018-08-14 18:03:48 -06:00
|
|
|
let bank = Bank::new(&alice);
|
|
|
|
|
|
|
|
let last_id = bank.last_id();
|
2018-10-15 11:01:40 -06:00
|
|
|
let tx = Transaction::system_move(&alice.keypair(), pubkey, 20, last_id, 0);
|
2018-08-14 18:03:48 -06:00
|
|
|
bank.process_transaction(&tx).expect("process transaction");
|
|
|
|
|
2019-01-15 12:20:07 -08:00
|
|
|
let request_processor = Arc::new(RwLock::new(JsonRpcRequestProcessor::new(Arc::new(bank))));
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new(NodeInfo::default())));
|
2018-10-15 11:01:40 -06:00
|
|
|
let leader = NodeInfo::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-10-15 11:01:40 -06:00
|
|
|
cluster_info.write().unwrap().set_leader(leader.id);
|
2018-10-10 14:51:43 -06:00
|
|
|
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
2019-01-04 17:20:51 -08:00
|
|
|
let drone_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
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,
|
2019-01-04 17:20:51 -08:00
|
|
|
drone_addr,
|
2018-10-10 14:51:43 -06:00
|
|
|
rpc_addr,
|
2018-08-22 11:54:37 -06:00
|
|
|
};
|
2018-10-15 11:01:40 -06:00
|
|
|
(io, meta, last_id, alice.keypair())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-11-13 17:12:25 -08:00
|
|
|
#[ignore]
|
2018-10-15 11:01:40 -06:00
|
|
|
fn test_rpc_new() {
|
|
|
|
let alice = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&alice);
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new(NodeInfo::default())));
|
2018-10-15 11:01:40 -06:00
|
|
|
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 24680);
|
2019-01-04 17:20:51 -08:00
|
|
|
let drone_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 24681);
|
|
|
|
let rpc_service = JsonRpcService::new(&Arc::new(bank), &cluster_info, rpc_addr, drone_addr);
|
2018-10-15 11:01:40 -06:00
|
|
|
let thread = rpc_service.thread_hdl.thread();
|
|
|
|
assert_eq!(thread.name().unwrap(), "solana-jsonrpc");
|
|
|
|
|
2018-12-03 15:24:54 -08:00
|
|
|
let rpc_string = get_rpc_request_str(rpc_addr);
|
2018-10-15 11:01:40 -06:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "getBalance",
|
2018-11-14 18:57:34 -08:00
|
|
|
"params": [alice.pubkey().to_string()],
|
2018-10-15 11:01:40 -06:00
|
|
|
});
|
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(10_000, json["result"].as_u64().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_request_processor_new() {
|
|
|
|
let alice = Mint::new(10_000);
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
let bank = Bank::new(&alice);
|
|
|
|
let arc_bank = Arc::new(bank);
|
|
|
|
let request_processor = JsonRpcRequestProcessor::new(arc_bank.clone());
|
|
|
|
thread::spawn(move || {
|
|
|
|
let last_id = arc_bank.last_id();
|
|
|
|
let tx = Transaction::system_move(&alice.keypair(), bob_pubkey, 20, last_id, 0);
|
|
|
|
arc_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();
|
|
|
|
let (io, meta, _last_id, _alice_keypair) = 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();
|
|
|
|
let (io, meta, _last_id, _alice_keypair) = 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();
|
|
|
|
let (io, meta, _last_id, _alice_keypair) = 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],
|
2018-09-20 13:20:37 -07:00
|
|
|
"tokens": 20,
|
2018-10-16 09:43:49 -07:00
|
|
|
"userdata": [],
|
|
|
|
"executable": false,
|
2018-11-12 09:11:24 -08:00
|
|
|
"loader": [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]
|
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();
|
|
|
|
let (io, meta, last_id, alice_keypair) = start_rpc_handler_with_tx(bob_pubkey);
|
|
|
|
let tx = Transaction::system_move(&alice_keypair, bob_pubkey, 20, last_id, 0);
|
|
|
|
|
|
|
|
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();
|
|
|
|
let (io, meta, last_id, alice_keypair) = start_rpc_handler_with_tx(bob_pubkey);
|
|
|
|
let tx = Transaction::system_move(&alice_keypair, bob_pubkey, 20, last_id, 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
|
|
|
|
let tx = Transaction::system_move(&alice_keypair, bob_pubkey, 10, last_id, 0);
|
|
|
|
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]
|
2018-12-20 15:47:48 -08:00
|
|
|
fn test_rpc_get_confirmation() {
|
2018-10-15 11:01:40 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
let (io, meta, _last_id, _alice_keypair) = start_rpc_handler_with_tx(bob_pubkey);
|
|
|
|
|
2018-12-21 08:25:07 -08:00
|
|
|
let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getConfirmationTime"}}"#);
|
2018-10-15 11:01:40 -06:00
|
|
|
let res = io.handle_request_sync(&req, meta);
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":18446744073709551615,"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]
|
|
|
|
fn test_rpc_get_last_id() {
|
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
let (io, meta, last_id, _alice_keypair) = start_rpc_handler_with_tx(bob_pubkey);
|
|
|
|
|
|
|
|
let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getLastId"}}"#);
|
|
|
|
let res = io.handle_request_sync(&req, meta);
|
|
|
|
let expected = format!(r#"{{"jsonrpc":"2.0","result":"{}","id":1}}"#, last_id);
|
|
|
|
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();
|
|
|
|
let (io, meta, _last_id, _alice_keypair) = start_rpc_handler_with_tx(bob_pubkey);
|
|
|
|
|
|
|
|
// Expect internal error because no leader is running
|
|
|
|
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}"#;
|
|
|
|
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_tx() {
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_keypair = Arc::new(Keypair::new());
|
2018-10-15 11:01:40 -06:00
|
|
|
let leader = Node::new_localhost_with_pubkey(leader_keypair.pubkey());
|
|
|
|
|
|
|
|
let alice = Mint::new(10_000_000);
|
2018-10-25 16:58:40 -07:00
|
|
|
let mut bank = Bank::new(&alice);
|
2018-10-15 11:01:40 -06:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
let leader_data = leader.info.clone();
|
2018-10-17 13:42:54 -07:00
|
|
|
let ledger_path = create_tmp_ledger_with_mint("rpc_send_tx", &alice);
|
2018-10-15 11:01:40 -06:00
|
|
|
|
|
|
|
let last_id = bank.last_id();
|
|
|
|
let tx = Transaction::system_move(&alice.keypair(), bob_pubkey, 20, last_id, 0);
|
|
|
|
let serial_tx = serialize(&tx).unwrap();
|
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::from_bootstrap_leader(
|
|
|
|
leader_data.id,
|
|
|
|
)));
|
|
|
|
bank.leader_scheduler = leader_scheduler;
|
|
|
|
|
|
|
|
let vote_account_keypair = Arc::new(Keypair::new());
|
2019-01-11 12:58:31 -08:00
|
|
|
let vote_signer =
|
|
|
|
VoteSignerProxy::new(&vote_account_keypair, Box::new(LocalVoteSigner::default()));
|
2018-10-30 10:05:18 -07:00
|
|
|
let entry_height = alice.create_entries().len() as u64;
|
2018-10-15 11:01:40 -06:00
|
|
|
let server = Fullnode::new_with_bank(
|
|
|
|
leader_keypair,
|
2019-01-11 12:58:31 -08:00
|
|
|
Arc::new(vote_signer),
|
2018-10-15 11:01:40 -06:00
|
|
|
bank,
|
2019-01-03 21:29:21 -08:00
|
|
|
None,
|
2018-10-15 11:01:40 -06:00
|
|
|
entry_height,
|
2018-10-30 10:05:18 -07:00
|
|
|
&last_id,
|
2018-10-15 11:01:40 -06:00
|
|
|
leader,
|
|
|
|
None,
|
|
|
|
&ledger_path,
|
|
|
|
false,
|
2018-11-05 10:50:58 -07:00
|
|
|
None,
|
2018-10-15 11:01:40 -06:00
|
|
|
);
|
|
|
|
sleep(Duration::from_millis(900));
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "sendTransaction",
|
2018-11-14 18:57:34 -08:00
|
|
|
"params": json!([serial_tx])
|
2018-10-15 11:01:40 -06:00
|
|
|
});
|
2018-11-15 13:23:26 -08:00
|
|
|
let rpc_addr = leader_data.rpc;
|
2018-12-03 15:24:54 -08:00
|
|
|
let rpc_string = get_rpc_request_str(rpc_addr);
|
2018-10-15 11:01:40 -06:00
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
|
|
|
let signature = &json["result"];
|
|
|
|
|
|
|
|
sleep(Duration::from_millis(500));
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "confirmTransaction",
|
2018-11-14 18:57:34 -08:00
|
|
|
"params": [signature],
|
2018-10-15 11:01:40 -06:00
|
|
|
});
|
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
2018-11-14 18:57:34 -08:00
|
|
|
let response_json_text = response.text().unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response_json_text).unwrap();
|
2018-10-15 11:01:40 -06:00
|
|
|
|
|
|
|
assert_eq!(true, json["result"]);
|
|
|
|
|
|
|
|
server.close().unwrap();
|
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_send_bad_tx() {
|
2018-08-15 10:37:02 -06:00
|
|
|
let alice = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&alice);
|
|
|
|
|
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-01-15 12:20:07 -08:00
|
|
|
request_processor: Arc::new(RwLock::new(JsonRpcRequestProcessor::new(Arc::new(bank)))),
|
2018-11-19 11:25:14 -08:00
|
|
|
cluster_info: Arc::new(RwLock::new(ClusterInfo::new(NodeInfo::default()))),
|
2019-01-04 17:20:51 -08:00
|
|
|
drone_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
|
2018-10-10 14:51:43 -06:00
|
|
|
rpc_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
|
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]
|
|
|
|
fn test_rpc_get_leader_addr() {
|
2018-11-19 11:25:14 -08:00
|
|
|
let cluster_info = Arc::new(RwLock::new(ClusterInfo::new(NodeInfo::default())));
|
2018-10-12 14:25:56 -06:00
|
|
|
assert_eq!(
|
|
|
|
get_leader_addr(&cluster_info),
|
|
|
|
Err(Error {
|
|
|
|
code: ErrorCode::InternalError,
|
|
|
|
message: "No leader detected".into(),
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
let leader = NodeInfo::new_with_socketaddr(&socketaddr!("127.0.0.1:1234"));
|
2018-11-15 13:23:26 -08:00
|
|
|
cluster_info.write().unwrap().insert_info(leader.clone());
|
2018-10-12 14:25:56 -06:00
|
|
|
cluster_info.write().unwrap().set_leader(leader.id);
|
|
|
|
assert_eq!(
|
|
|
|
get_leader_addr(&cluster_info),
|
|
|
|
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() {
|
|
|
|
let tx =
|
|
|
|
Transaction::system_move(&Keypair::new(), Keypair::new().pubkey(), 20, hash(&[0]), 0);
|
|
|
|
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())
|
|
|
|
);
|
|
|
|
}
|
2018-08-13 11:24:39 -06:00
|
|
|
}
|