1 //! Simple RPC client implementation which implements [`BlockSource`] against a Bitcoin Core RPC
4 use crate::{BlockHeaderData, BlockSource, AsyncBlockSourceResult};
5 use crate::http::{HttpClient, HttpEndpoint, HttpError, JsonResponse};
7 use bitcoin::blockdata::block::Block;
8 use bitcoin::hash_types::BlockHash;
9 use bitcoin::hashes::hex::ToHex;
13 use std::convert::TryFrom;
14 use std::convert::TryInto;
15 use std::sync::atomic::{AtomicUsize, Ordering};
17 /// A simple RPC client for calling methods using HTTP `POST`.
18 pub struct RpcClient {
20 endpoint: HttpEndpoint,
26 /// Creates a new RPC client connected to the given endpoint with the provided credentials. The
27 /// credentials should be a base64 encoding of a user name and password joined by a colon, as is
28 /// required for HTTP basic access authentication.
29 pub fn new(credentials: &str, endpoint: HttpEndpoint) -> std::io::Result<Self> {
30 let client = HttpClient::connect(&endpoint)?;
32 basic_auth: "Basic ".to_string() + credentials,
35 id: AtomicUsize::new(0),
39 /// Calls a method with the response encoded in JSON format and interpreted as type `T`.
40 pub async fn call_method<T>(&mut self, method: &str, params: &[serde_json::Value]) -> std::io::Result<T>
41 where JsonResponse: TryFrom<Vec<u8>, Error = std::io::Error> + TryInto<T, Error = std::io::Error> {
42 let host = format!("{}:{}", self.endpoint.host(), self.endpoint.port());
43 let uri = self.endpoint.path();
44 let content = serde_json::json!({
47 "id": &self.id.fetch_add(1, Ordering::AcqRel).to_string()
50 let mut response = match self.client.post::<JsonResponse>(&uri, &host, &self.basic_auth, content).await {
51 Ok(JsonResponse(response)) => response,
52 Err(e) if e.kind() == std::io::ErrorKind::Other => {
53 match e.get_ref().unwrap().downcast_ref::<HttpError>() {
54 Some(http_error) => match JsonResponse::try_from(http_error.contents.clone()) {
55 Ok(JsonResponse(response)) => response,
64 if !response.is_object() {
65 return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON object"));
68 let error = &response["error"];
70 // TODO: Examine error code for a more precise std::io::ErrorKind.
71 let message = error["message"].as_str().unwrap_or("unknown error");
72 return Err(std::io::Error::new(std::io::ErrorKind::Other, message));
75 let result = &mut response["result"];
77 return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON result"));
80 JsonResponse(result.take()).try_into()
84 impl BlockSource for RpcClient {
85 fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, _height: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
87 let header_hash = serde_json::json!(header_hash.to_hex());
88 Ok(self.call_method("getblockheader", &[header_hash]).await?)
92 fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
94 let header_hash = serde_json::json!(header_hash.to_hex());
95 let verbosity = serde_json::json!(0);
96 Ok(self.call_method("getblock", &[header_hash, verbosity]).await?)
100 fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
101 Box::pin(async move {
102 Ok(self.call_method("getblockchaininfo", &[]).await?)
110 use crate::http::client_tests::{HttpServer, MessageBody};
112 /// Credentials encoded in base64.
113 const CREDENTIALS: &'static str = "dXNlcjpwYXNzd29yZA==";
115 /// Converts a JSON value into `u64`.
116 impl TryInto<u64> for JsonResponse {
117 type Error = std::io::Error;
119 fn try_into(self) -> std::io::Result<u64> {
120 match self.0.as_u64() {
121 None => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "not a number")),
128 async fn call_method_returning_unknown_response() {
129 let server = HttpServer::responding_with_not_found();
130 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
132 match client.call_method::<u64>("getblockcount", &[]).await {
133 Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other),
134 Ok(_) => panic!("Expected error"),
139 async fn call_method_returning_malfomred_response() {
140 let response = serde_json::json!("foo");
141 let server = HttpServer::responding_with_ok(MessageBody::Content(response));
142 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
144 match client.call_method::<u64>("getblockcount", &[]).await {
146 assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
147 assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON object");
149 Ok(_) => panic!("Expected error"),
154 async fn call_method_returning_error() {
155 let response = serde_json::json!({
156 "error": { "code": -8, "message": "invalid parameter" },
158 let server = HttpServer::responding_with_server_error(response);
159 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
161 let invalid_block_hash = serde_json::json!("foo");
162 match client.call_method::<u64>("getblock", &[invalid_block_hash]).await {
164 assert_eq!(e.kind(), std::io::ErrorKind::Other);
165 assert_eq!(e.get_ref().unwrap().to_string(), "invalid parameter");
167 Ok(_) => panic!("Expected error"),
172 async fn call_method_returning_missing_result() {
173 let response = serde_json::json!({ "result": null });
174 let server = HttpServer::responding_with_ok(MessageBody::Content(response));
175 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
177 match client.call_method::<u64>("getblockcount", &[]).await {
179 assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
180 assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON result");
182 Ok(_) => panic!("Expected error"),
187 async fn call_method_returning_malformed_result() {
188 let response = serde_json::json!({ "result": "foo" });
189 let server = HttpServer::responding_with_ok(MessageBody::Content(response));
190 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
192 match client.call_method::<u64>("getblockcount", &[]).await {
194 assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
195 assert_eq!(e.get_ref().unwrap().to_string(), "not a number");
197 Ok(_) => panic!("Expected error"),
202 async fn call_method_returning_valid_result() {
203 let response = serde_json::json!({ "result": 654470 });
204 let server = HttpServer::responding_with_ok(MessageBody::Content(response));
205 let mut client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
207 match client.call_method::<u64>("getblockcount", &[]).await {
208 Err(e) => panic!("Unexpected error: {:?}", e),
209 Ok(count) => assert_eq!(count, 654470),