Skip to content

Commit 3cc032b

Browse files
committed
Test: Create client for bitcoind
Request a client from the BitcoinD-struct and use it in a test. I now, the client is `sync` and it blocks the run-time. However, we currently don't have an async client to work with. @steven: It would be great if we would get rust-bitcoin/rust-bitcoincore-rpc#294
1 parent 222fba4 commit 3cc032b

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ark-testing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
anyhow.workspace= true
88
bitcoin.workspace = true
9+
bitcoincore-rpc = "0.19.0"
910
env_logger.workspace = true
1011
log.workspace = true
1112
portpicker = "0.1.1"

ark-testing/src/daemon/bitcoind.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::daemon::{DaemonWrapper, DaemonHelper};
2-
use crate::constants::env::{BITCOIND_EXEC, TEST_LEAVE_INTACT};
2+
use crate::constants::env::BITCOIND_EXEC;
33

44
use anyhow::Context;
55
use std::path::PathBuf;
66
use std::env::VarError;
77
use which::which;
88
use tokio::sync::broadcast;
99

10+
use bitcoincore_rpc::{Client as BitcoinDClient, Auth};
11+
1012
pub struct BitcoinDHelper {
1113
name : String,
1214
bitcoind_exec: PathBuf,
@@ -55,6 +57,27 @@ impl BitcoinD {
5557
DaemonWrapper::wrap(inner)
5658
}
5759

60+
pub fn sync_client(&self) -> anyhow::Result<BitcoinDClient> {
61+
let bitcoind_url = self.inner.bitcoind_url();
62+
let auth = self.inner.auth();
63+
let client = BitcoinDClient::new(&bitcoind_url, auth)?;
64+
Ok(client)
65+
}
66+
}
67+
68+
impl BitcoinDHelper {
69+
70+
pub fn auth(&self) -> Auth {
71+
let cookie = self.config.datadir
72+
.join(&self.config.network)
73+
.join(".cookie");
74+
75+
Auth::CookieFile(cookie)
76+
}
77+
78+
pub fn bitcoind_url(&self) -> String {
79+
format!("http://127.0.0.1:{}", self.state.rpc_port.expect("A port has been picked. Is bitcoind running?"))
80+
}
5881
}
5982

6083
impl DaemonHelper for BitcoinDHelper {
@@ -92,7 +115,7 @@ impl DaemonHelper for BitcoinDHelper {
92115
Ok(())
93116
}
94117

95-
async fn wait_for_init(&self, stdout_rx: &mut broadcast::Receiver::<String>, _: &mut broadcast::Receiver::<String>) -> anyhow::Result<()> {
118+
async fn wait_for_init(&self, _: &mut broadcast::Receiver::<String>, _: &mut broadcast::Receiver::<String>) -> anyhow::Result<()> {
96119
// TODO: We can find a better implementation here
97120

98121
// Sleeping 2 seconds is always sufficient

ark-testing/tests/bitcoind.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#[macro_use]
2-
extern crate log;
1+
extern crate bitcoincore_rpc;
2+
3+
use bitcoincore_rpc::RpcApi;
34

45
use ark_testing::daemon::bitcoind::{get_exe_path, BitcoinD, BitcoinDConfig};
56
use ark_testing::daemon::Daemon;
@@ -19,9 +20,13 @@ async fn start_bitcoind() {
1920
bitcoind_exe,
2021
bitcoind_config
2122
);
23+
2224
bitcoind.start().await.expect("bitcoind can start");
2325

24-
// TODO: Call getblockchain info to verify bitcoind is working correctly
26+
let client = bitcoind.sync_client().expect("Client can be created");
27+
28+
let info = client.get_blockchain_info().expect("Get info about the blockchain");
29+
assert_eq!(info.chain.to_string(), "regtest");
2530
}
2631

2732

0 commit comments

Comments
 (0)