|
1 | 1 | // SPDX-License-Identifier: CC0-1.0 |
2 | 2 |
|
3 | | -//! JSON-RPC clients for testing against specific versions of Bitcoin Core. |
| 3 | +//! Async JSON-RPC client for Bitcoin Core v25 to v30. |
4 | 4 |
|
5 | 5 | mod error; |
6 | | -pub mod v17; |
7 | | -pub mod v18; |
8 | | -pub mod v19; |
9 | | -pub mod v20; |
10 | | -pub mod v21; |
11 | | -pub mod v22; |
12 | | -pub mod v23; |
13 | | -pub mod v24; |
14 | | -pub mod v25; |
15 | | -pub mod v26; |
16 | | -pub mod v27; |
17 | | -pub mod v28; |
18 | | -pub mod v29; |
19 | | -pub mod v30; |
20 | | -pub mod v31; |
| 6 | +mod rpcs; |
21 | 7 |
|
| 8 | +use std::fmt; |
22 | 9 | use std::fs::File; |
23 | 10 | use std::io::{BufRead, BufReader}; |
24 | 11 | use std::path::PathBuf; |
25 | 12 |
|
26 | | -pub use crate::client_sync::error::Error; |
| 13 | +pub use crate::client_async::error::Error; |
27 | 14 | pub(crate) use crate::{into_json, log_response}; |
28 | 15 |
|
29 | 16 | /// Crate-specific Result type. |
@@ -57,103 +44,62 @@ impl Auth { |
57 | 44 | } |
58 | 45 | } |
59 | 46 |
|
60 | | -/// Defines a `jsonrpc::Client` using `bitreq`. |
61 | | -#[macro_export] |
62 | | -macro_rules! define_jsonrpc_bitreq_client { |
63 | | - ($version:literal) => { |
64 | | - use std::fmt; |
65 | | - |
66 | | - use $crate::client_sync::{log_response, Auth, Result}; |
67 | | - use $crate::client_sync::error::Error; |
68 | | - |
69 | | - /// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs. |
70 | | - pub struct Client { |
71 | | - inner: jsonrpc::client::Client, |
72 | | - } |
73 | | - |
74 | | - impl fmt::Debug for Client { |
75 | | - fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result { |
76 | | - write!( |
77 | | - f, |
78 | | - "corepc_client::client_sync::{}::Client({:?})", $version, self.inner |
79 | | - ) |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - impl Client { |
84 | | - /// Creates a client to a bitcoind JSON-RPC server without authentication. |
85 | | - pub fn new(url: &str) -> Self { |
86 | | - let transport = jsonrpc::http::bitreq_http::Builder::new() |
87 | | - .url(url) |
88 | | - .expect("jsonrpc v0.19, this function does not error") |
89 | | - .timeout(std::time::Duration::from_secs(60)) |
90 | | - .build(); |
91 | | - let inner = jsonrpc::client::Client::with_transport(transport); |
92 | | - |
93 | | - Self { inner } |
94 | | - } |
95 | | - |
96 | | - /// Creates a client to a bitcoind JSON-RPC server with authentication. |
97 | | - pub fn new_with_auth(url: &str, auth: Auth) -> Result<Self> { |
98 | | - if matches!(auth, Auth::None) { |
99 | | - return Err(Error::MissingUserPassword); |
100 | | - } |
101 | | - let (user, pass) = auth.get_user_pass()?; |
102 | | - |
103 | | - let transport = jsonrpc::http::bitreq_http::Builder::new() |
104 | | - .url(url) |
105 | | - .expect("jsonrpc v0.19, this function does not error") |
106 | | - .timeout(std::time::Duration::from_secs(60)) |
107 | | - .basic_auth(user.unwrap(), pass) |
108 | | - .build(); |
109 | | - let inner = jsonrpc::client::Client::with_transport(transport); |
| 47 | +/// Client implements an async JSON-RPC client for the Bitcoin Core daemon or compatible APIs. |
| 48 | +pub struct Client { |
| 49 | + inner: jsonrpc::client_async::Client, |
| 50 | +} |
110 | 51 |
|
111 | | - Ok(Self { inner }) |
112 | | - } |
| 52 | +impl fmt::Debug for Client { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result { |
| 54 | + write!(f, "corepc_client::client_async::Client({:?})", self.inner) |
| 55 | + } |
| 56 | +} |
113 | 57 |
|
114 | | - /// Call an RPC `method` with given `args` list. |
115 | | - pub fn call<T: for<'a> serde::de::Deserialize<'a>>( |
116 | | - &self, |
117 | | - method: &str, |
118 | | - args: &[serde_json::Value], |
119 | | - ) -> Result<T> { |
120 | | - let raw = serde_json::value::to_raw_value(args)?; |
121 | | - let req = self.inner.build_request(&method, Some(&*raw)); |
122 | | - if log::log_enabled!(log::Level::Debug) { |
123 | | - log::debug!(target: "corepc", "request: {} {}", method, serde_json::Value::from(args)); |
124 | | - } |
| 58 | +impl Client { |
| 59 | + /// Creates a client to a bitcoind JSON-RPC server without authentication. |
| 60 | + pub fn new(url: &str) -> Self { |
| 61 | + let transport = jsonrpc::bitreq_http_async::Builder::new() |
| 62 | + .url(url) |
| 63 | + .expect("this function does not error") |
| 64 | + .timeout(std::time::Duration::from_secs(60)) |
| 65 | + .build(); |
| 66 | + let inner = jsonrpc::client_async::Client::with_transport(transport); |
| 67 | + |
| 68 | + Self { inner } |
| 69 | + } |
125 | 70 |
|
126 | | - let resp = self.inner.send_request(req).map_err(Error::from); |
127 | | - log_response(method, &resp); |
128 | | - Ok(resp?.result()?) |
129 | | - } |
| 71 | + /// Creates a client to a bitcoind JSON-RPC server with authentication. |
| 72 | + pub fn new_with_auth(url: &str, auth: Auth) -> Result<Self> { |
| 73 | + if matches!(auth, Auth::None) { |
| 74 | + return Err(Error::MissingUserPassword); |
130 | 75 | } |
| 76 | + let (user, pass) = auth.get_user_pass()?; |
| 77 | + let user = user.ok_or(Error::MissingUserPassword)?; |
| 78 | + let transport = jsonrpc::bitreq_http_async::Builder::new() |
| 79 | + .url(url) |
| 80 | + .expect("this function does not error") |
| 81 | + .timeout(std::time::Duration::from_secs(60)) |
| 82 | + .basic_auth(user, pass) |
| 83 | + .build(); |
| 84 | + let inner = jsonrpc::client_async::Client::with_transport(transport); |
| 85 | + |
| 86 | + Ok(Self { inner }) |
131 | 87 | } |
132 | | -} |
133 | 88 |
|
134 | | -/// Implements the `check_expected_server_version()` on `Client`. |
135 | | -/// |
136 | | -/// Requires `Client` to be in scope and implement `server_version()`. |
137 | | -/// See and/or use `impl_client_v17__getnetworkinfo`. |
138 | | -/// |
139 | | -/// # Parameters |
140 | | -/// |
141 | | -/// - `$expected_versions`: An vector of expected server versions e.g., `[230100, 230200]`. |
142 | | -#[macro_export] |
143 | | -macro_rules! impl_client_check_expected_server_version { |
144 | | - ($expected_versions:expr) => { |
145 | | - impl Client { |
146 | | - /// Checks that the JSON-RPC endpoint is for a `bitcoind` instance with the expected version. |
147 | | - pub fn check_expected_server_version(&self) -> Result<()> { |
148 | | - let server_version = self.server_version()?; |
149 | | - if !$expected_versions.contains(&server_version) { |
150 | | - return Err($crate::client_sync::error::UnexpectedServerVersionError { |
151 | | - got: server_version, |
152 | | - expected: $expected_versions.to_vec(), |
153 | | - })?; |
154 | | - } |
155 | | - Ok(()) |
156 | | - } |
| 89 | + /// Call an RPC `method` with given `args` list. |
| 90 | + pub async fn call<T: for<'a> serde::de::Deserialize<'a>>( |
| 91 | + &self, |
| 92 | + method: &str, |
| 93 | + args: &[serde_json::Value], |
| 94 | + ) -> Result<T> { |
| 95 | + let raw = serde_json::value::to_raw_value(args)?; |
| 96 | + let req = self.inner.build_request(method, Some(&*raw)); |
| 97 | + if log::log_enabled!(log::Level::Debug) { |
| 98 | + log::debug!(target: "corepc", "request: {} {}", method, serde_json::Value::from(args)); |
157 | 99 | } |
158 | | - }; |
| 100 | + |
| 101 | + let resp = self.inner.send_request(req).await.map_err(Error::from); |
| 102 | + log_response(method, &resp); |
| 103 | + Ok(resp?.result()?) |
| 104 | + } |
159 | 105 | } |
0 commit comments