Skip to content

Commit

Permalink
Simplify Rpc request processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Aug 15, 2018
1 parent 387076e commit bdf7d6a
Showing 1 changed file with 27 additions and 118 deletions.
145 changes: 27 additions & 118 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bank::Bank;
use bs58;
use jsonrpc_core::*;
use jsonrpc_http_server::*;
use request::{Request as JsonRpcRequest, Response};
use service::Service;
use signature::{Pubkey, Signature};
use std::mem;
Expand Down Expand Up @@ -84,7 +83,7 @@ build_rpc_trait! {
#[rpc(meta, name = "getTransactionCount")]
fn get_transaction_count(&self, Self::Metadata) -> Result<u64>;

// #[rpc(meta, name = "solana_sendTransaction")]
// #[rpc(meta, name = "sendTransaction")]
// fn send_transaction(&self, Self::Metadata, String, i64) -> Result<String>;
}
}
Expand All @@ -103,21 +102,7 @@ impl RpcSol for RpcSolImpl {
return Err(Error::invalid_request());
}
let signature = Signature::new(&signature_vec);
let req = JsonRpcRequest::GetSignature { signature };
let resp = meta.request_processor.process_request(req);
match resp {
Some(Response::SignatureStatus { signature_status }) => Ok(signature_status),
Some(_) => Err(Error{
code: ErrorCode::ServerError(-32002),
message: "Server error: bad response".to_string(),
data: None,
}),
None => Err(Error {
code: ErrorCode::ServerError(-32001),
message: "Server error: no node found".to_string(),
data: None,
}),
}
meta.request_processor.get_signature_status(signature)
}
fn get_balance(&self, meta: Self::Metadata, id: String) -> Result<(String, i64)> {
let pubkey_vec = match bs58::decode(id).into_vec() {
Expand All @@ -129,72 +114,16 @@ impl RpcSol for RpcSolImpl {
return Err(Error::invalid_request());
}
let pubkey = Pubkey::new(&pubkey_vec);
let req = JsonRpcRequest::GetBalance { key: pubkey };
let resp = meta.request_processor.process_request(req);
match resp {
Some(Response::Balance { key, val }) => Ok((bs58::encode(key).into_string(), val)),
Some(_) => Err(Error{
code: ErrorCode::ServerError(-32002),
message: "Server error: bad response".to_string(),
data: None,
}),
None => Err(Error {
code: ErrorCode::ServerError(-32001),
message: "Server error: no node found".to_string(),
data: None,
}),
}
meta.request_processor.get_balance(pubkey)
}
fn get_finality(&self, meta: Self::Metadata) -> Result<usize> {
let req = JsonRpcRequest::GetFinality;
let resp = meta.request_processor.process_request(req);
match resp {
Some(Response::Finality { time }) => Ok(time),
Some(_) => Err(Error{
code: ErrorCode::ServerError(-32002),
message: "Server error: bad response".to_string(),
data: None,
}),
None => Err(Error {
code: ErrorCode::ServerError(-32001),
message: "Server error: no node found".to_string(),
data: None,
}),
}
meta.request_processor.get_finality()
}
fn get_last_id(&self, meta: Self::Metadata) -> Result<String> {
let req = JsonRpcRequest::GetLastId;
let resp = meta.request_processor.process_request(req);
match resp {
Some(Response::LastId { id }) => Ok(bs58::encode(id).into_string()),
Some(_) => Err(Error{
code: ErrorCode::ServerError(-32002),
message: "Server error: bad response".to_string(),
data: None,
}),
None => Err(Error {
code: ErrorCode::ServerError(-32001),
message: "Server error: no node found".to_string(),
data: None,
}),
}
meta.request_processor.get_last_id()
}
fn get_transaction_count(&self, meta: Self::Metadata) -> Result<u64> {
let req = JsonRpcRequest::GetTransactionCount;
let resp = meta.request_processor.process_request(req);
match resp {
Some(Response::TransactionCount { transaction_count }) => Ok(transaction_count),
Some(_) => Err(Error{
code: ErrorCode::ServerError(-32002),
message: "Server error: bad response".to_string(),
data: None,
}),
None => Err(Error {
code: ErrorCode::ServerError(-32001),
message: "Server error: no node found".to_string(),
data: None,
}),
}
meta.request_processor.get_transaction_count()
}
// fn send_transaction(&self, meta: Self::Metadata, to: String, tokens: i64) -> Result<String> {
// let client_keypair = read_keypair(&meta.keypair_location.unwrap()).unwrap();
Expand Down Expand Up @@ -225,43 +154,23 @@ impl JsonRpcRequestProcessor {
JsonRpcRequestProcessor { bank }
}

/// Process Request items sent via JSON-RPC.
fn process_request(
&self,
msg: JsonRpcRequest,
) -> Option<Response> {
match msg {
JsonRpcRequest::GetBalance { key } => {
let val = self.bank.get_balance(&key);
let rsp = Response::Balance { key, val };
info!("Response::Balance {:?}", rsp);
Some(rsp)
}
JsonRpcRequest::GetLastId => {
let id = self.bank.last_id();
let rsp = Response::LastId { id };
info!("Response::LastId {:?}", rsp);
Some(rsp)
}
JsonRpcRequest::GetTransactionCount => {
let transaction_count = self.bank.transaction_count() as u64;
let rsp = Response::TransactionCount { transaction_count };
info!("Response::TransactionCount {:?}", rsp);
Some(rsp)
}
JsonRpcRequest::GetSignature { signature } => {
let signature_status = self.bank.has_signature(&signature);
let rsp = Response::SignatureStatus { signature_status };
info!("Response::Signature {:?}", rsp);
Some(rsp)
}
JsonRpcRequest::GetFinality => {
let time = self.bank.finality();
let rsp = Response::Finality { time };
info!("Response::Finality {:?}", rsp);
Some(rsp)
}
}
/// Process JSON-RPC request items sent via JSON-RPC.
fn get_balance(&self, pubkey: Pubkey) -> Result<(String, i64)> {
let val = self.bank.get_balance(&pubkey);
Ok((bs58::encode(pubkey).into_string(), val))
}
fn get_finality(&self) -> Result<usize> {
Ok(self.bank.finality())
}
fn get_last_id(&self) -> Result<String> {
let id = self.bank.last_id();
Ok(bs58::encode(id).into_string())
}
fn get_signature_status(&self, signature: Signature) -> Result<bool> {
Ok(self.bank.has_signature(&signature))
}
fn get_transaction_count(&self) -> Result<u64> {
Ok(self.bank.transaction_count() as u64)
}
}

Expand Down Expand Up @@ -295,7 +204,7 @@ mod tests {
};

let req = format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"solana_getBalance","params":["{}"]}}"#,
r#"{{"jsonrpc":"2.0","id":1,"method":"getBalance","params":["{}"]}}"#,
bob_pubkey
);
let res = io.handle_request_sync(&req, meta.clone());
Expand All @@ -308,7 +217,7 @@ mod tests {
assert_eq!(expected, result);

let req = format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"solana_getTransactionCount"}}"#
r#"{{"jsonrpc":"2.0","id":1,"method":"getTransactionCount"}}"#
);
let res = io.handle_request_sync(&req, meta.clone());
let expected = format!(r#"{{"jsonrpc":"2.0","result":1,"id":1}}"#);
Expand All @@ -327,7 +236,7 @@ mod tests {
let mut io = MetaIoHandler::default();
let rpc = RpcSolImpl;
io.extend_with(rpc.to_delegate());
let req = r#"{"jsonrpc":"2.0","id":1,"method":"solana_getBalance","params":[1234567890]}"#;
let req = r#"{"jsonrpc":"2.0","id":1,"method":"confirmTransaction","params":[1234567890]}"#;
let meta = Meta {
request_processor: JsonRpcRequestProcessor::new(Arc::new(bank)),
};
Expand All @@ -350,7 +259,7 @@ mod tests {
let rpc = RpcSolImpl;
io.extend_with(rpc.to_delegate());
let req =
r#"{"jsonrpc":"2.0","id":1,"method":"solana_confirmTransaction","params":["a1b2c3d4e5"]}"#;
r#"{"jsonrpc":"2.0","id":1,"method":"confirmTransaction","params":["a1b2c3d4e5"]}"#;
let meta = Meta {
request_processor: JsonRpcRequestProcessor::new(Arc::new(bank)),
};
Expand Down

0 comments on commit bdf7d6a

Please sign in to comment.