Skip to content

Commit 3dfe1e9

Browse files
authored
Merge pull request #3 from chainwayxyz/async-support
Async support
2 parents 84587f7 + bc22f84 commit 3dfe1e9

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

Diff for: src/client/mod.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@
33
//! Client crate mocks the `Client` struct in `bitcoincore-rpc`.
44
55
use crate::ledger::Ledger;
6+
use bitcoincore_rpc::{Auth, RpcApi};
67

78
mod rpc_api;
89

10+
/// This trait defines non-functional interfaces for RPC interfaces, like
11+
/// `new()`. This is needed if target application wants to choose actual rpc and
12+
/// this via trait definitions. This is helpful for choosing different rpc
13+
/// interface between test and release builds.
14+
pub trait RpcApiWrapper: RpcApi + std::marker::Sync + std::marker::Send + 'static {
15+
fn new(url: &str, auth: Auth) -> bitcoincore_rpc::Result<Self>;
16+
}
17+
18+
/// Compatibility implementation for `bitcoincore_rpc::Client`.
19+
impl RpcApiWrapper for bitcoincore_rpc::Client {
20+
fn new(url: &str, auth: Auth) -> bitcoincore_rpc::Result<Self> {
21+
bitcoincore_rpc::Client::new(url, auth)
22+
}
23+
}
24+
925
/// Mock Bitcoin RPC client.
1026
pub struct Client {
1127
/// Bitcoin ledger.
1228
ledger: Ledger,
1329
}
1430

15-
impl Client {
31+
impl RpcApiWrapper for Client {
1632
/// Creates a new mock Client interface.
1733
///
1834
/// # Parameters
1935
///
2036
/// Parameters are just here to match `bitcoincore_rpc::Client::new()`. They
2137
/// are not used and can be dummy values.
22-
///
23-
/// # Panics
24-
///
25-
/// This function can panic if `Ledger` can't be created.
26-
pub fn new(_url: &str, _auth: bitcoincore_rpc::Auth) -> bitcoincore_rpc::Result<Self> {
38+
fn new(_url: &str, _auth: bitcoincore_rpc::Auth) -> bitcoincore_rpc::Result<Self> {
2739
Ok(Self {
2840
ledger: Ledger::new(),
2941
})

Diff for: src/client/rpc_api.rs

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl RpcApi for Client {
206206
#[cfg(test)]
207207
mod tests {
208208
use super::*;
209+
use crate::RpcApiWrapper;
209210
use bitcoin::{Amount, Network};
210211

211212
#[test]

Diff for: src/ledger/address.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ mod tests {
119119
fn addresses() {
120120
let ledger = Ledger::new();
121121

122-
assert_eq!(ledger.credentials.take().len(), 0);
122+
assert_eq!(ledger.credentials.lock().unwrap().take().len(), 0);
123123

124124
let credential = Ledger::generate_credential();
125125
ledger.add_credential(credential.clone());
126126

127-
let credentials = ledger.credentials.take();
127+
let credentials = ledger.credentials.lock().unwrap().take();
128128
assert_eq!(credentials.len(), 1);
129129

130130
assert_eq!(credential, credentials.get(0).unwrap().to_owned());

Diff for: src/ledger/macros.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@
77
macro_rules! add_item_to_vec {
88
($member:expr, $item:expr) => {
99
// Update item list.
10-
let mut items = $member.take();
10+
let mut items = $member.lock().unwrap().take();
1111
items.push($item);
1212

1313
// Commit new change.
14-
$member.set(items);
14+
$member.lock().unwrap().set(items);
1515
};
1616
}
1717
/// Removes given item from a `Vec` member, which is guarded by a `Cell`.
1818
#[macro_export]
1919
macro_rules! remove_item_from_vec {
2020
($member:expr, $item:expr) => {
2121
// Get item list.
22-
let mut items = $member.take();
22+
let mut items = $member.lock().unwrap().take();
2323

2424
// Delete given item.
2525
items.retain(|&i| i != $item);
2626

2727
// Commit new change.
28-
$member.set(items);
28+
$member.lock().unwrap().set(items);
2929
};
3030
}
3131
/// Returns items of a `Vec` member, which is guarded by a `Cell`.
3232
#[macro_export]
3333
macro_rules! return_vec_item {
3434
($member:expr) => {
35-
let items = $member.take();
36-
$member.set(items.clone());
35+
let items = $member.lock().unwrap().take();
36+
$member.lock().unwrap().set(items.clone());
3737

3838
return items;
3939
};
@@ -43,28 +43,31 @@ macro_rules! return_vec_item {
4343
#[macro_export]
4444
macro_rules! update_item {
4545
($member:expr, $item:expr) => {
46-
$member.set($item);
46+
$member.lock().unwrap().set($item);
4747
};
4848
}
4949

5050
/// Assigns an item from member to given assignee, which is guarded by a `Cell`.
5151
#[macro_export]
5252
macro_rules! get_item {
5353
($member:expr, $assignee:ident) => {
54-
let $assignee = $member.take();
55-
$member.set($assignee.clone());
54+
let $assignee = $member.lock().unwrap().take();
55+
$member.lock().unwrap().set($assignee.clone());
5656
};
5757
}
5858

5959
#[cfg(test)]
6060
mod tests {
61-
use std::cell::Cell;
61+
use std::{
62+
cell::Cell,
63+
sync::{Arc, Mutex},
64+
};
6265

6366
/// Temporary struct for macro testing.
6467
#[derive(Default)]
6568
struct Test {
66-
pub vec_member_1: Cell<Vec<isize>>,
67-
pub int_member_1: Cell<isize>,
69+
pub vec_member_1: Arc<Mutex<Cell<Vec<isize>>>>,
70+
pub int_member_1: Arc<Mutex<Cell<isize>>>,
6871
}
6972

7073
#[test]

Diff for: src/ledger/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
88
use address::UserCredential;
99
use bitcoin::{OutPoint, Transaction};
10-
use std::cell::Cell;
10+
use std::{
11+
cell::Cell,
12+
sync::{Arc, Mutex},
13+
};
1114

1215
mod address;
1316
mod errors;
@@ -19,20 +22,20 @@ mod utxo;
1922
/// Mock Bitcoin ledger.
2023
pub struct Ledger {
2124
/// User's keys and address.
22-
credentials: Cell<Vec<UserCredential>>,
25+
credentials: Arc<Mutex<Cell<Vec<UserCredential>>>>,
2326
/// Happened transactions.
24-
transactions: Cell<Vec<Transaction>>,
27+
transactions: Arc<Mutex<Cell<Vec<Transaction>>>>,
2528
/// Unspent transaction outputs.
26-
utxos: Cell<Vec<OutPoint>>,
29+
utxos: Arc<Mutex<Cell<Vec<OutPoint>>>>,
2730
}
2831

2932
impl Ledger {
3033
/// Creates a new empty ledger.
3134
pub fn new() -> Self {
3235
Self {
33-
credentials: Cell::new(Vec::new()),
34-
utxos: Cell::new(Vec::new()),
35-
transactions: Cell::new(Vec::new()),
36+
credentials: Arc::new(Mutex::new(Cell::new(Vec::new()))),
37+
utxos: Arc::new(Mutex::new(Cell::new(Vec::new()))),
38+
transactions: Arc::new(Mutex::new(Cell::new(Vec::new()))),
3639
}
3740
}
3841
}

0 commit comments

Comments
 (0)