diff --git a/Cargo.toml b/Cargo.toml index 40a6703..a139d24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,5 +29,5 @@ futures = "0.3.0" serde = { version = "1", features = ["derive"] } serde_json = "1" serial_test = "3" -tokio = { version = "1", features = ["rt-multi-thread", "net", "macros", "io-util", "time"] } +tokio = { version = "1", features = ["rt-multi-thread", "net", "macros", "io-util", "time", "sync"] } tokio-util = { version = "0.7.16", features = ["compat"] } diff --git a/README.md b/README.md index a86d620..5189fc0 100644 --- a/README.md +++ b/README.md @@ -52,14 +52,15 @@ cmake --build build -j$(nproc) ### 3. Run tests -If `bitcoin` is not in your `PATH`, set `BITCOIN_BIN` to the full path of -the Bitcoin Core binary. +The tests talk to the node only over its IPC socket, including the JSON-RPC +calls they make through the `Rpc` interface. If the node was started with +`-datadir`, set `BITCOIN_DATADIR` to the same path so the tests can find the +socket. The test harness bootstraps regtest chain state and ensures the test wallet is available before running integration tests. ```sh -BITCOIN_BIN=./build/bin/bitcoin \ cargo test ``` diff --git a/contrib/run-tests.sh b/contrib/run-tests.sh index 88923ab..0ff7ae8 100755 --- a/contrib/run-tests.sh +++ b/contrib/run-tests.sh @@ -44,5 +44,5 @@ BITCOIN_STARTED=1 echo "==> Running cargo test" ( cd "$REPO_ROOT" - BITCOIN_BIN="$BITCOIN_BIN" BITCOIN_DATADIR="$TEMP_DATADIR" cargo test + BITCOIN_DATADIR="$TEMP_DATADIR" cargo test ) diff --git a/tests/test.rs b/tests/test.rs index 0f354a7..da6144b 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -12,8 +12,8 @@ mod util; use serde_json::{Value, json}; use util::bitcoin_core::{ - connect_unix_stream, destroy_template, make_block_template, mempool_tx_count, unix_socket_path, - with_init_client, with_mining_client, with_rpc_client, + connect_unix_stream, destroy_template, make_block_template, make_rpc, mempool_tx_count, + unix_socket_path, with_init_client, with_mining_client, with_rpc_client, }; use util::bitcoin_core_wallet::{ bitcoin_test_wallet, create_mempool_self_transfer, ensure_wallet_loaded_and_funded, @@ -590,11 +590,12 @@ async fn mining_submit_block_duplicate() { // Serialized because this test may mine blocks to recover wallet funding. #[serial_test::serial] async fn mining_get_transactions() { - with_mining_client(|_client, mining| async move { + with_mining_client(|client, mining| async move { + let rpc = make_rpc(&client).await; let wallet = bitcoin_test_wallet(); - ensure_wallet_loaded_and_funded(&wallet); + ensure_wallet_loaded_and_funded(&rpc, &wallet).await; - let real_tx = create_mempool_self_transfer(&wallet); + let real_tx = create_mempool_self_transfer(&rpc, &wallet).await; let real_txid = real_tx.compute_txid().to_byte_array(); let real_wtxid = real_tx.compute_wtxid().to_byte_array(); let real_raw_tx = encode_to_vec(&real_tx); @@ -773,13 +774,16 @@ async fn wallet_helpers_create_mempool_transaction() { let wallet = bitcoin_test_wallet(); assert!(!wallet.is_empty(), "test wallet name must not be empty"); - ensure_wallet_loaded_and_funded(&wallet); - let before = mempool_tx_count(); - let _tx = create_mempool_self_transfer(&wallet); - let after = mempool_tx_count(); - assert_eq!( - after, - before + 1, - "self-transfer should add one mempool transaction" - ); + with_rpc_client(|_init, rpc| async move { + ensure_wallet_loaded_and_funded(&rpc, &wallet).await; + let before = mempool_tx_count(&rpc).await; + let _tx = create_mempool_self_transfer(&rpc, &wallet).await; + let after = mempool_tx_count(&rpc).await; + assert_eq!( + after, + before + 1, + "self-transfer should add one mempool transaction" + ); + }) + .await; } diff --git a/tests/util/bitcoin_core.rs b/tests/util/bitcoin_core.rs index 1efebab..2042503 100644 --- a/tests/util/bitcoin_core.rs +++ b/tests/util/bitcoin_core.rs @@ -1,12 +1,11 @@ use std::{ future::Future, path::{Path, PathBuf}, - sync::Once, }; use crate::util::bitcoin_core_wallet::{ - bitcoin_datadir, bitcoin_rpc_json, bitcoin_test_wallet, ensure_wallet_loaded, - mine_blocks_to_new_address, + bitcoin_datadir, bitcoin_test_wallet, ensure_wallet_loaded, mine_blocks_to_new_address, + rpc_call, }; use bitcoin_capnp_types::{ init_capnp::init, @@ -16,12 +15,13 @@ use bitcoin_capnp_types::{ }; use capnp_rpc::{RpcSystem, rpc_twoparty_capnp::Side, twoparty::VatNetwork}; use futures::io::BufReader; -use serde::Deserialize; +use serde_json::json; use tokio::net::{UnixStream, unix::OwnedReadHalf}; +use tokio::sync::OnceCell; use tokio::task::LocalSet; use tokio_util::compat::{Compat, TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt}; -static CHAIN_SETUP: Once = Once::new(); +static CHAIN_SETUP: OnceCell<()> = OnceCell::const_new(); pub fn unix_socket_path() -> PathBuf { let bitcoin_dir = match bitcoin_datadir() { @@ -43,27 +43,34 @@ pub fn unix_socket_path() -> PathBuf { regtest_dir.join("node.sock") } -pub fn mempool_tx_count() -> usize { - let mempool_info: MempoolInfo = bitcoin_rpc_json(None, &["getmempoolinfo"]) - .unwrap_or_else(|e| panic!("failed to query mempool info: {e}")); - mempool_info.size +pub async fn mempool_tx_count(rpc: &rpc::Client) -> usize { + let mempool_info = rpc_call(rpc, "getmempoolinfo", json!([])).await; + mempool_info["size"] + .as_u64() + .expect("getmempoolinfo must report a numeric size") as usize } -fn ensure_bootstrap_chain_ready() { - // `call_once` serializes bootstrap initialization across all tests in this - // process. Other callers block until this setup completes. - CHAIN_SETUP.call_once(|| { - let wallet = bitcoin_test_wallet(); - ensure_chain_height_at_least(101, &wallet); - }); +async fn ensure_bootstrap_chain_ready(init: &init::Client) { + // `get_or_init` serializes bootstrap initialization across all tests in this + // process. Other callers wait until this setup completes. + CHAIN_SETUP + .get_or_init(|| async { + let rpc = make_rpc(init).await; + let wallet = bitcoin_test_wallet(); + ensure_chain_height_at_least(&rpc, 101, &wallet).await; + }) + .await; } -fn ensure_chain_height_at_least(min_height: u32, wallet: &str) { - ensure_wallet_loaded(wallet); - let height: u32 = bitcoin_rpc_json(None, &["getblockcount"]) - .unwrap_or_else(|e| panic!("failed to query block height: {e}")); +async fn ensure_chain_height_at_least(rpc: &rpc::Client, min_height: u32, wallet: &str) { + ensure_wallet_loaded(rpc, wallet).await; + let height = rpc_call(rpc, "getblockcount", json!([])) + .await + .as_u64() + .expect("getblockcount must return a number") as u32; if height < min_height { - mine_blocks_to_new_address(wallet, min_height - height) + mine_blocks_to_new_address(rpc, wallet, min_height - height) + .await .unwrap_or_else(|e| panic!("failed to reach height {min_height}: {e}")); } } @@ -139,8 +146,6 @@ pub async fn connect_unix_stream( pub async fn bootstrap( mut rpc_system: RpcSystem, ) -> init::Client { - ensure_bootstrap_chain_ready(); - let client: init::Client = rpc_system.bootstrap(Side::Server); tokio::task::spawn_local(rpc_system); let create_client_response = client @@ -157,6 +162,7 @@ pub async fn bootstrap( let mut pool_req = thread_map.make_pool_request(); pool_req.get().set_count(4); pool_req.send().promise.await.unwrap(); + ensure_bootstrap_chain_ready(&client).await; client } @@ -190,10 +196,3 @@ pub async fn make_block_template(mining: &mining::Client) -> block_template::Cli pub async fn destroy_template(template: &block_template::Client) { template.destroy_request().send().promise.await.unwrap(); } - -#[derive(Deserialize)] -// Intentionally partial: tests currently only need the `size` field from -// `getmempoolinfo`. -struct MempoolInfo { - size: usize, -} diff --git a/tests/util/bitcoin_core_wallet.rs b/tests/util/bitcoin_core_wallet.rs index e8fb4db..1775f56 100644 --- a/tests/util/bitcoin_core_wallet.rs +++ b/tests/util/bitcoin_core_wallet.rs @@ -1,129 +1,142 @@ -use std::process::Command; - +use bitcoin_capnp_types::rpc_capnp::rpc; use bitcoin_primitives::Transaction as BitcoinTransaction; use bitcoin_primitives::hex; use encoding::decode_from_slice; -use serde::de::DeserializeOwned; - -fn bitcoin_bin() -> String { - std::env::var("BITCOIN_BIN").unwrap_or_else(|_| "bitcoin".to_owned()) -} +use serde_json::{Value, json}; pub fn bitcoin_datadir() -> Option { std::env::var("BITCOIN_DATADIR").ok() } -fn bitcoin_rpc(wallet: Option<&str>, args: &[&str]) -> Result { - let owned_args: Vec = args.iter().map(|arg| (*arg).to_owned()).collect(); - bitcoin_rpc_owned(wallet, &owned_args) +/// Call a JSON-RPC method through the IPC `Rpc` interface and return its +/// `result`. With `wallet` set, the request is routed to that wallet the same +/// way `-rpcwallet` does, through the `/wallet/` URI. +pub async fn try_rpc_call( + rpc: &rpc::Client, + wallet: Option<&str>, + method: &str, + params: Value, +) -> Result { + let mut req = rpc.execute_rpc_request(); + let body = json!({ "jsonrpc": "2.0", "id": "test", "method": method, "params": params }); + req.get().set_request(body.to_string()); + if let Some(wallet) = wallet { + req.get().set_uri(format!("/wallet/{wallet}")); + } + + let resp = req + .send() + .promise + .await + .map_err(|e| format!("rpc {method} failed over IPC: {e}"))?; + let text = resp + .get() + .and_then(|r| r.get_result()) + .map_err(|e| format!("rpc {method} returned a malformed response: {e}"))? + .to_string() + .map_err(|e| format!("rpc {method} returned invalid UTF-8: {e}"))?; + let v: Value = serde_json::from_str(&text) + .map_err(|e| format!("failed to parse {method} response as JSON: {e}"))?; + if !v["error"].is_null() { + return Err(format!("rpc {method} failed: {}", v["error"])); + } + Ok(v["result"].clone()) } -pub fn bitcoin_rpc_json(wallet: Option<&str>, args: &[&str]) -> Result -where - T: DeserializeOwned, -{ - let output = bitcoin_rpc(wallet, args)?; - serde_json::from_str(&output).map_err(|e| format!("failed to parse rpc response as JSON: {e}")) +/// Call a JSON-RPC method through the IPC `Rpc` interface. +pub async fn rpc_call(rpc: &rpc::Client, method: &str, params: Value) -> Value { + try_rpc_call(rpc, None, method, params) + .await + .unwrap_or_else(|e| panic!("{e}")) } pub fn bitcoin_test_wallet() -> String { std::env::var("BITCOIN_TEST_WALLET").unwrap_or_else(|_| "ipc-test".to_owned()) } -fn bitcoin_rpc_owned(wallet: Option<&str>, args: &[String]) -> Result { - let mut command = Command::new(bitcoin_bin()); - command.arg("rpc").arg("-chain=regtest").arg("-rpcwait"); - if let Some(datadir) = bitcoin_datadir() { - command.arg(format!("-datadir={datadir}")); - } - if let Some(wallet) = wallet { - command.arg(format!("-rpcwallet={wallet}")); - } - command.args(args); - - let output = command - .output() - .map_err(|e| format!("failed to execute bitcoin rpc command: {e}"))?; - if output.status.success() { - Ok(String::from_utf8(output.stdout) - .unwrap_or_else(|_| String::new()) - .trim() - .to_owned()) - } else { - Err(format!( - "bitcoin rpc command failed: {}", - String::from_utf8_lossy(&output.stderr).trim() - )) - } -} - -pub fn ensure_wallet_loaded(wallet: &str) { - if bitcoin_rpc(Some(wallet), &["getwalletinfo"]).is_err() { +pub async fn ensure_wallet_loaded(rpc: &rpc::Client, wallet: &str) { + if try_rpc_call(rpc, Some(wallet), "getwalletinfo", json!([])).await.is_err() { // First try loading an existing wallet from disk (common when regtest data // directory is re-used), then fall back to creating it. - if bitcoin_rpc(None, &["loadwallet", wallet]).is_err() { - let _ = bitcoin_rpc(None, &["createwallet", wallet]); + if try_rpc_call(rpc, None, "loadwallet", json!([wallet])).await.is_err() { + let _ = try_rpc_call(rpc, None, "createwallet", json!([wallet])).await; } - bitcoin_rpc(Some(wallet), &["getwalletinfo"]).unwrap_or_else(|e| { - panic!("wallet {wallet} is not available after load/create attempts: {e}") - }); + try_rpc_call(rpc, Some(wallet), "getwalletinfo", json!([])) + .await + .unwrap_or_else(|e| { + panic!("wallet {wallet} is not available after load/create attempts: {e}") + }); } } -pub fn ensure_wallet_loaded_and_funded(wallet: &str) { - ensure_wallet_loaded(wallet); +pub async fn ensure_wallet_loaded_and_funded(rpc: &rpc::Client, wallet: &str) { + ensure_wallet_loaded(rpc, wallet).await; // getbalance "*" 1 only counts confirmed spendable funds. - let balance: f64 = bitcoin_rpc_json(Some(wallet), &["getbalance", "*", "1"]) - .unwrap_or_else(|e| panic!("failed to query wallet balance for {wallet}: {e}")); + let balance = try_rpc_call(rpc, Some(wallet), "getbalance", json!(["*", 1])) + .await + .unwrap_or_else(|e| panic!("failed to query wallet balance for {wallet}: {e}")) + .as_f64() + .expect("getbalance must return a number"); if balance < 1.0 { // Mining a single block can mature older coinbase outputs when balance is low. - mine_blocks_to_new_address(wallet, 1) + mine_blocks_to_new_address(rpc, wallet, 1) + .await .unwrap_or_else(|e| panic!("failed to mine blocks to wallet {wallet}: {e}")); } } -pub fn mine_blocks_to_new_address(wallet: &str, blocks: u32) -> Result<(), String> { - let blocks = blocks.to_string(); - let address = bitcoin_rpc(Some(wallet), &["getnewaddress"])?; - bitcoin_rpc( +pub async fn mine_blocks_to_new_address( + rpc: &rpc::Client, + wallet: &str, + blocks: u32, +) -> Result<(), String> { + let address = try_rpc_call(rpc, Some(wallet), "getnewaddress", json!([])).await?; + try_rpc_call( + rpc, Some(wallet), - &["generatetoaddress", blocks.as_str(), address.as_str()], - )?; + "generatetoaddress", + json!([blocks, address]), + ) + .await?; Ok(()) } -pub fn create_mempool_self_transfer(wallet: &str) -> BitcoinTransaction { - let send_self_transfer = || { - let address = bitcoin_rpc(Some(wallet), &["getnewaddress"])?; - let send_args = vec![ - "-named".to_owned(), - "sendtoaddress".to_owned(), - format!("address={address}"), - "amount=0.01".to_owned(), - "fee_rate=25".to_owned(), - ]; - bitcoin_rpc_owned(Some(wallet), &send_args) +pub async fn create_mempool_self_transfer(rpc: &rpc::Client, wallet: &str) -> BitcoinTransaction { + let send_self_transfer = || async { + let address = try_rpc_call(rpc, Some(wallet), "getnewaddress", json!([])).await?; + let txid = try_rpc_call( + rpc, + Some(wallet), + "sendtoaddress", + json!({ "address": address, "amount": 0.01, "fee_rate": 25 }), + ) + .await?; + txid.as_str() + .map(str::to_owned) + .ok_or_else(|| format!("sendtoaddress returned a non-string txid: {txid}")) }; - let txid_hex = match send_self_transfer() { + let txid_hex = match send_self_transfer().await { Ok(txid) => txid, Err(first_err) => { // If the wallet exists but is unfunded or in an unexpected state, // try to recover by ensuring funding and retry once. - ensure_wallet_loaded_and_funded(wallet); - send_self_transfer().unwrap_or_else(|second_err| { + ensure_wallet_loaded_and_funded(rpc, wallet).await; + send_self_transfer().await.unwrap_or_else(|second_err| { panic!( "failed to create self-transfer in {wallet}: initial send failed: {first_err}; retry after funding failed: {second_err}" ) }) } }; - let raw_tx_hex = bitcoin_rpc(None, &["getrawtransaction", txid_hex.as_str()]) - .unwrap_or_else(|e| panic!("failed to fetch raw transaction {txid_hex}: {e}")); - let raw_tx = hex::decode_to_vec(&raw_tx_hex) + let raw_tx_hex = rpc_call(rpc, "getrawtransaction", json!([txid_hex])).await; + let raw_tx_hex = raw_tx_hex + .as_str() + .unwrap_or_else(|| panic!("getrawtransaction {txid_hex} must return a hex string")); + let raw_tx = hex::decode_to_vec(raw_tx_hex) .unwrap_or_else(|e| panic!("failed to decode raw transaction {txid_hex} from hex: {e}")); let tx: BitcoinTransaction = decode_from_slice(&raw_tx) .unwrap_or_else(|e| panic!("failed to deserialize raw transaction {txid_hex}: {e}"));