diff --git a/Cargo.toml b/Cargo.toml index 299b11e74..5c41ba8fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,9 +75,9 @@ futures = "0.1.21" generic-array = { version = "0.11.1", default-features = false, features = ["serde"] } getopts = "0.2" influx_db_client = "0.3.4" -jsonrpc-core = "8.0" -jsonrpc-http-server = "8.0" -jsonrpc-macros = "8.0" +jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc", rev = "4486300" } +jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc", rev = "4486300" } +jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc", rev = "4486300" } itertools = "0.7.8" libc = "0.2.1" log = "0.4.2" diff --git a/src/rpc.rs b/src/rpc.rs index 1254e961b..655e0da2c 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -19,29 +19,32 @@ pub struct JsonRpcService { } impl JsonRpcService { - pub fn new(bank: Arc, rpc_addr: SocketAddr) -> Self { - let request_processor = JsonRpcRequestProcessor::new(bank); - 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()); + pub fn new( + bank: Arc, + rpc_addr: SocketAddr, + ) -> Self { + let request_processor = JsonRpcRequestProcessor::new(bank); + 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()); - let server = ServerBuilder::new(io) - .meta_extractor(move |_req: &hyper::Request| Meta { - request_processor: Some(request_processor.clone()), - }) - .threads(4) - .cors(DomainsValidation::AllowOnly(vec![ - AccessControlAllowOrigin::Any, - ])) - .start_http(&rpc_addr) - .unwrap(); - server.wait(); - () - }) - .unwrap(); + let server = ServerBuilder::with_meta_extractor(io, move |_req: &hyper::Request| Meta { + request_processor: request_processor.clone(), + }) + .threads(4) + .cors(DomainsValidation::AllowOnly(vec![ + AccessControlAllowOrigin::Any, + ])) + .start_http( + &rpc_addr, + ).unwrap(); + server.wait(); + () + }) + .unwrap(); JsonRpcService { thread_hdl } } } @@ -58,16 +61,9 @@ impl Service for JsonRpcService { #[derive(Clone)] pub struct Meta { - pub request_processor: Option, + pub request_processor: JsonRpcRequestProcessor, } impl Metadata for Meta {} -impl Default for Meta { - fn default() -> Self { - Meta { - request_processor: None, - } - } -} build_rpc_trait! { pub trait RpcSol { @@ -107,10 +103,10 @@ impl RpcSol for RpcSolImpl { } else { let signature = Signature::new(&signature_vec); let req = JsonRpcRequest::GetSignature { signature }; - let resp = meta.request_processor.unwrap().process_request(req); + let resp = meta.request_processor.process_request(req); match resp { Some(Response::SignatureStatus { signature_status }) => Ok(signature_status), - Some(_) => Err(Error { + Some(_) => Err(Error{ code: ErrorCode::ServerError(-32002), message: "Server error: bad response".to_string(), data: None, @@ -133,10 +129,10 @@ impl RpcSol for RpcSolImpl { } else { let pubkey = Pubkey::new(&pubkey_vec); let req = JsonRpcRequest::GetBalance { key: pubkey }; - let resp = meta.request_processor.unwrap().process_request(req); + 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 { + Some(_) => Err(Error{ code: ErrorCode::ServerError(-32002), message: "Server error: bad response".to_string(), data: None, @@ -151,10 +147,10 @@ impl RpcSol for RpcSolImpl { } fn get_finality(&self, meta: Self::Metadata) -> Result { let req = JsonRpcRequest::GetFinality; - let resp = meta.request_processor.unwrap().process_request(req); + let resp = meta.request_processor.process_request(req); match resp { Some(Response::Finality { time }) => Ok(time), - Some(_) => Err(Error { + Some(_) => Err(Error{ code: ErrorCode::ServerError(-32002), message: "Server error: bad response".to_string(), data: None, @@ -168,10 +164,10 @@ impl RpcSol for RpcSolImpl { } fn get_last_id(&self, meta: Self::Metadata) -> Result { let req = JsonRpcRequest::GetLastId; - let resp = meta.request_processor.unwrap().process_request(req); + let resp = meta.request_processor.process_request(req); match resp { Some(Response::LastId { id }) => Ok(bs58::encode(id).into_string()), - Some(_) => Err(Error { + Some(_) => Err(Error{ code: ErrorCode::ServerError(-32002), message: "Server error: bad response".to_string(), data: None, @@ -185,10 +181,10 @@ impl RpcSol for RpcSolImpl { } fn get_transaction_count(&self, meta: Self::Metadata) -> Result { let req = JsonRpcRequest::GetTransactionCount; - let resp = meta.request_processor.unwrap().process_request(req); + let resp = meta.request_processor.process_request(req); match resp { Some(Response::TransactionCount { transaction_count }) => Ok(transaction_count), - Some(_) => Err(Error { + Some(_) => Err(Error{ code: ErrorCode::ServerError(-32002), message: "Server error: bad response".to_string(), data: None, @@ -230,7 +226,10 @@ impl JsonRpcRequestProcessor { } /// Process Request items sent via JSON-RPC. - fn process_request(&self, msg: JsonRpcRequest) -> Option { + fn process_request( + &self, + msg: JsonRpcRequest, + ) -> Option { match msg { JsonRpcRequest::GetBalance { key } => { let val = self.bank.get_balance(&key); @@ -292,7 +291,7 @@ mod tests { let rpc = RpcSolImpl; io.extend_with(rpc.to_delegate()); let meta = Meta { - request_processor: Some(request_processor), + request_processor: request_processor, }; let req = format!( @@ -300,10 +299,7 @@ mod tests { bob_pubkey ); let res = io.handle_request_sync(&req, meta.clone()); - let expected = format!( - r#"{{"jsonrpc":"2.0","result":["{}", 20],"id":1}}"#, - bob_pubkey - ); + let expected = format!(r#"{{"jsonrpc":"2.0","result":["{}", 20],"id":1}}"#, bob_pubkey); let expected: Response = serde_json::from_str(&expected).expect("expected response deserialization"); @@ -311,7 +307,9 @@ mod tests { .expect("actual response deserialization"); assert_eq!(expected, result); - let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"solana_getTransactionCount"}}"#); + let req = format!( + r#"{{"jsonrpc":"2.0","id":1,"method":"solana_getTransactionCount"}}"# + ); let res = io.handle_request_sync(&req, meta.clone()); let expected = format!(r#"{{"jsonrpc":"2.0","result":1,"id":1}}"#); let expected: Response = @@ -323,12 +321,15 @@ mod tests { } #[test] fn test_rpc_request_bad_parameter_type() { + let alice = Mint::new(10_000); + let bank = Bank::new(&alice); + 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 meta = Meta { - request_processor: None, + request_processor: JsonRpcRequestProcessor::new(Arc::new(bank)), }; let res = io.handle_request_sync(req, meta); @@ -342,13 +343,16 @@ mod tests { } #[test] fn test_rpc_request_bad_signature() { + let alice = Mint::new(10_000); + let bank = Bank::new(&alice); + let mut io = MetaIoHandler::default(); let rpc = RpcSolImpl; io.extend_with(rpc.to_delegate()); let req = r#"{"jsonrpc":"2.0","id":1,"method":"solana_confirmTransaction","params":["a1b2c3d4e5"]}"#; let meta = Meta { - request_processor: None, + request_processor: JsonRpcRequestProcessor::new(Arc::new(bank)), }; let res = io.handle_request_sync(req, meta);