Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion contrib/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
32 changes: 18 additions & 14 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
59 changes: 29 additions & 30 deletions tests/util/bitcoin_core.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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() {
Expand All @@ -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}"));
}
}
Expand Down Expand Up @@ -139,8 +146,6 @@ pub async fn connect_unix_stream(
pub async fn bootstrap(
mut rpc_system: RpcSystem<capnp_rpc::rpc_twoparty_capnp::Side>,
) -> 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
Expand All @@ -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
}

Expand Down Expand Up @@ -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,
}
Loading
Loading