11// SPDX-License-Identifier: CC0-1.0
22
3- //! JSON-RPC clients for testing against specific versions of Bitcoin Core .
3+ //! Async JSON-RPC clients for Bitcoin Core v25 to v30 .
44
55mod 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;
217
8+ use std:: fmt;
229use std:: fs:: File ;
2310use std:: io:: { BufRead , BufReader } ;
2411use std:: path:: PathBuf ;
2512
26- pub use crate :: client_sync :: error:: Error ;
13+ pub use crate :: bdk_client :: error:: Error ;
2714
2815/// Crate-specific Result type.
2916///
@@ -56,77 +43,63 @@ impl Auth {
5643 }
5744}
5845
59- /// Defines a `jsonrpc::Client` using `bitreq`.
60- #[ macro_export]
61- macro_rules! define_jsonrpc_bitreq_client {
62- ( $version: literal) => {
63- use std:: fmt;
46+ /// Client implements an async JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
47+ pub struct Client {
48+ pub ( crate ) inner : jsonrpc:: client_async:: Client ,
49+ }
6450
65- use $crate:: client_sync:: { log_response, Auth , Result } ;
66- use $crate:: client_sync:: error:: Error ;
51+ impl fmt:: Debug for Client {
52+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> core:: fmt:: Result {
53+ write ! ( f, "corepc_client::client_async::Client({:?})" , self . inner)
54+ }
55+ }
6756
68- /// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
69- pub struct Client {
70- inner: jsonrpc:: client:: Client ,
71- }
57+ impl Client {
58+ /// Creates a client to a bitcoind JSON-RPC server without authentication.
59+ pub fn new ( url : & str ) -> Self {
60+ let transport = jsonrpc:: bitreq_http_async:: Builder :: new ( )
61+ . url ( url)
62+ . expect ( "jsonrpc v0.19, this function does not error" )
63+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
64+ . build ( ) ;
65+ let inner = jsonrpc:: client_async:: Client :: with_transport ( transport) ;
66+
67+ Self { inner }
68+ }
7269
73- impl fmt:: Debug for Client {
74- fn fmt( & self , f: & mut fmt:: Formatter ) -> core:: fmt:: Result {
75- write!(
76- f,
77- "corepc_client::client_sync::{}::Client({:?})" , $version, self . inner
78- )
79- }
70+ /// Creates a client to a bitcoind JSON-RPC server with authentication.
71+ pub fn new_with_auth ( url : & str , auth : Auth ) -> Result < Self > {
72+ if matches ! ( auth, Auth :: None ) {
73+ return Err ( Error :: MissingUserPassword ) ;
8074 }
75+ let ( user, pass) = auth. get_user_pass ( ) ?;
76+ let user = user. ok_or ( Error :: MissingUserPassword ) ?;
77+ let transport = jsonrpc:: bitreq_http_async:: Builder :: new ( )
78+ . url ( url)
79+ . expect ( "jsonrpc v0.19, this function does not error" )
80+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
81+ . basic_auth ( user, pass)
82+ . build ( ) ;
83+ let inner = jsonrpc:: client_async:: Client :: with_transport ( transport) ;
84+
85+ Ok ( Self { inner } )
86+ }
8187
82- impl Client {
83- /// Creates a client to a bitcoind JSON-RPC server without authentication.
84- pub fn new( url: & str ) -> Self {
85- let transport = jsonrpc:: http:: bitreq_http:: Builder :: new( )
86- . url( url)
87- . expect( "jsonrpc v0.19, this function does not error" )
88- . timeout( std:: time:: Duration :: from_secs( 60 ) )
89- . build( ) ;
90- let inner = jsonrpc:: client:: Client :: with_transport( transport) ;
91-
92- Self { inner }
93- }
94-
95- /// Creates a client to a bitcoind JSON-RPC server with authentication.
96- pub fn new_with_auth( url: & str , auth: Auth ) -> Result <Self > {
97- if matches!( auth, Auth :: None ) {
98- return Err ( Error :: MissingUserPassword ) ;
99- }
100- let ( user, pass) = auth. get_user_pass( ) ?;
101-
102- let transport = jsonrpc:: http:: bitreq_http:: Builder :: new( )
103- . url( url)
104- . expect( "jsonrpc v0.19, this function does not error" )
105- . timeout( std:: time:: Duration :: from_secs( 60 ) )
106- . basic_auth( user. unwrap( ) , pass)
107- . build( ) ;
108- let inner = jsonrpc:: client:: Client :: with_transport( transport) ;
109-
110- Ok ( Self { inner } )
111- }
112-
113- /// Call an RPC `method` with given `args` list.
114- pub fn call<T : for <' a> serde:: de:: Deserialize <' a>>(
115- & self ,
116- method: & str ,
117- args: & [ serde_json:: Value ] ,
118- ) -> Result <T > {
119- let raw = serde_json:: value:: to_raw_value( args) ?;
120- let req = self . inner. build_request( & method, Some ( & * raw) ) ;
121- if log:: log_enabled!( log:: Level :: Debug ) {
122- log:: debug!( target: "corepc" , "request: {} {}" , method, serde_json:: Value :: from( args) ) ;
123- }
124-
125- let resp = self . inner. send_request( req) . map_err( Error :: from) ;
126- log_response( method, & resp) ;
127- Ok ( resp?. result( ) ?)
128- }
88+ /// Call an RPC `method` with given `args` list.
89+ pub async fn call < T : for < ' a > serde:: de:: Deserialize < ' a > > (
90+ & self ,
91+ method : & str ,
92+ args : & [ serde_json:: Value ] ,
93+ ) -> Result < T > {
94+ let raw = serde_json:: value:: to_raw_value ( args) ?;
95+ let req = self . inner . build_request ( method, Some ( & * raw) ) ;
96+ if log:: log_enabled!( log:: Level :: Debug ) {
97+ log:: debug!( target: "corepc" , "request: {} {}" , method, serde_json:: Value :: from( args) ) ;
12998 }
99+
100+ let resp = self . inner . send_request ( req) . await . map_err ( Error :: from) ;
101+ log_response ( method, & resp) ;
102+ Ok ( resp?. result ( ) ?)
130103 }
131104}
132105
@@ -139,14 +112,14 @@ macro_rules! define_jsonrpc_bitreq_client {
139112///
140113/// - `$expected_versions`: An vector of expected server versions e.g., `[230100, 230200]`.
141114#[ macro_export]
142- macro_rules! impl_client_check_expected_server_version {
115+ macro_rules! impl_async_client_check_expected_server_version {
143116 ( $expected_versions: expr) => {
144117 impl Client {
145118 /// Checks that the JSON-RPC endpoint is for a `bitcoind` instance with the expected version.
146- pub fn check_expected_server_version( & self ) -> Result <( ) > {
147- let server_version = self . server_version( ) ?;
119+ pub async fn check_expected_server_version( & self ) -> Result <( ) > {
120+ let server_version = self . server_version( ) . await ?;
148121 if !$expected_versions. contains( & server_version) {
149- return Err ( $crate:: client_sync :: error:: UnexpectedServerVersionError {
122+ return Err ( $crate:: bdk_client :: error:: UnexpectedServerVersionError {
150123 got: server_version,
151124 expected: $expected_versions. to_vec( ) ,
152125 } ) ?;
@@ -158,7 +131,7 @@ macro_rules! impl_client_check_expected_server_version {
158131}
159132
160133/// Shorthand for converting a variable into a `serde_json::Value`.
161- fn into_json < T > ( val : T ) -> Result < serde_json:: Value >
134+ pub ( crate ) fn into_json < T > ( val : T ) -> Result < serde_json:: Value >
162135where
163136 T : serde:: ser:: Serialize ,
164137{
@@ -181,10 +154,12 @@ fn log_response(method: &str, resp: &Result<jsonrpc::Response>) {
181154 log:: debug!( target: "corepc" , "response error for {}: {:?}" , method, e) ;
182155 }
183156 } else if log:: log_enabled!( Trace ) {
184- let def =
185- serde_json:: value:: to_raw_value ( & serde_json:: value:: Value :: Null ) . unwrap ( ) ;
186- let result = resp. result . as_ref ( ) . unwrap_or ( & def) ;
187- log:: trace!( target: "corepc" , "response for {}: {}" , method, result) ;
157+ if let Ok ( def) =
158+ serde_json:: value:: to_raw_value ( & serde_json:: value:: Value :: Null )
159+ {
160+ let result = resp. result . as_ref ( ) . unwrap_or ( & def) ;
161+ log:: trace!( target: "corepc" , "response for {}: {}" , method, result) ;
162+ }
188163 } ,
189164 }
190165 }
0 commit comments