From bc22f84b44a20d823f17a1650929398e1721ea5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 27 Jun 2024 12:24:10 +0300 Subject: [PATCH] client: Add wrapper for async support. --- src/client/mod.rs | 24 ++++++++++++++++++------ src/client/rpc_api.rs | 1 + 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index b21f991..2bf4ebb 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -3,27 +3,39 @@ //! Client crate mocks the `Client` struct in `bitcoincore-rpc`. use crate::ledger::Ledger; +use bitcoincore_rpc::{Auth, RpcApi}; mod rpc_api; +/// This trait defines non-functional interfaces for RPC interfaces, like +/// `new()`. This is needed if target application wants to choose actual rpc and +/// this via trait definitions. This is helpful for choosing different rpc +/// interface between test and release builds. +pub trait RpcApiWrapper: RpcApi + std::marker::Sync + std::marker::Send + 'static { + fn new(url: &str, auth: Auth) -> bitcoincore_rpc::Result; +} + +/// Compatibility implementation for `bitcoincore_rpc::Client`. +impl RpcApiWrapper for bitcoincore_rpc::Client { + fn new(url: &str, auth: Auth) -> bitcoincore_rpc::Result { + bitcoincore_rpc::Client::new(url, auth) + } +} + /// Mock Bitcoin RPC client. pub struct Client { /// Bitcoin ledger. ledger: Ledger, } -impl Client { +impl RpcApiWrapper for Client { /// Creates a new mock Client interface. /// /// # Parameters /// /// Parameters are just here to match `bitcoincore_rpc::Client::new()`. They /// are not used and can be dummy values. - /// - /// # Panics - /// - /// This function can panic if `Ledger` can't be created. - pub fn new(_url: &str, _auth: bitcoincore_rpc::Auth) -> bitcoincore_rpc::Result { + fn new(_url: &str, _auth: bitcoincore_rpc::Auth) -> bitcoincore_rpc::Result { Ok(Self { ledger: Ledger::new(), }) diff --git a/src/client/rpc_api.rs b/src/client/rpc_api.rs index 7365eec..9d4813e 100644 --- a/src/client/rpc_api.rs +++ b/src/client/rpc_api.rs @@ -206,6 +206,7 @@ impl RpcApi for Client { #[cfg(test)] mod tests { use super::*; + use crate::RpcApiWrapper; use bitcoin::{Amount, Network}; #[test]