Skip to content

Commit

Permalink
client: Add wrapper for async support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jun 27, 2024
1 parent c2e00a2 commit bc22f84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;
}

/// Compatibility implementation for `bitcoincore_rpc::Client`.
impl RpcApiWrapper for bitcoincore_rpc::Client {
fn new(url: &str, auth: Auth) -> bitcoincore_rpc::Result<Self> {
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<Self> {
fn new(_url: &str, _auth: bitcoincore_rpc::Auth) -> bitcoincore_rpc::Result<Self> {
Ok(Self {
ledger: Ledger::new(),
})
Expand Down
1 change: 1 addition & 0 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl RpcApi for Client {
#[cfg(test)]
mod tests {
use super::*;
use crate::RpcApiWrapper;
use bitcoin::{Amount, Network};

#[test]
Expand Down

0 comments on commit bc22f84

Please sign in to comment.