diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50d7f77..700dbe6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,28 +12,22 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - uses: actions-rust-lang/setup-rust-toolchain@v1 - name: Install Bitcoin Dependencies run: | sudo apt-get update sudo apt-get install build-essential cmake pkgconf python3 libevent-dev libboost-dev capnproto libcapnp-dev ccache - name: ccache - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ~/.cache/ccache key: ccache-${{ runner.os }}-${{ github.sha }} restore-keys: ccache-${{ runner.os }}- - name: Checkout Bitcoin Core run: git clone --depth 1 --branch 32.x https://github.com/bitcoin/bitcoin.git - - name: Build Bitcoin Core + - name: Build & Run Test Suite run: | - cd bitcoin - cmake -B build -DENABLE_WALLET=ON -DBUILD_TESTS=OFF -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - cmake --build build -j $(nproc) - - name: Run Bitcoin Core Daemon - run: cd bitcoin && ./build/bin/bitcoin node -chain=regtest -ipcbind=unix -server -debug=ipc -daemon - - name: Run Test Suite - env: - BITCOIN_BIN: ${{ github.workspace }}/bitcoin/build/bin/bitcoin - run: cargo test + ccache --zero-stats + contrib/run-tests.sh bitcoin + ccache --show-stats diff --git a/README.md b/README.md index af642a4..a86d620 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ cargo build ## Running integration tests -The integration tests connect to a running bitcoin node via IPC. +The integration tests connect to a running bitcoin node via IPC. For a single test script, see `contrib/run-tests.sh`. ### 1. Build Bitcoin Core diff --git a/RELEASE_POLICY.md b/RELEASE_POLICY.md index a4fd929..3ad2dd2 100644 --- a/RELEASE_POLICY.md +++ b/RELEASE_POLICY.md @@ -13,10 +13,23 @@ This policy applies to: ## Branch Model -- `master` tracks the current active development line. -- Maintenance branches (for example `30.x`, `31.x`, `32.x`) track stable backport lines. +- `master` tracks the current active development line, i.e. Bitcoin Core `master`. +- Maintenance branches (for example `30.x`, `31.x`) track stable backport lines, i.e. the Bitcoin Core branch of the same name. - A release must be cut from the branch that owns that version line. +### Maintenance Branches + +- A maintenance branch is created from `master` after Bitcoin Core branches off, at a point where the schema files match the new Bitcoin Core branch. +- Its CI builds the Bitcoin Core branch of the same name, and runs for pushes and pull requests that target the maintenance branch. +- After creation it only receives backports from `master` and fixes specific to that branch. It must never be fast-forwarded to, or merged with, `master`. +- The schema files must keep matching the corresponding Bitcoin Core branch. Interface changes are only backported if Bitcoin Core backported them. Test and build changes can be backported more freely. +- Backported commits reference the original pull request and commit on `master`, using the Bitcoin Core convention: + + ``` + Github-Pull: # + Rebased-From: + ``` + ## Version Line Mapping - `30.x` branch -> `0.1.x` diff --git a/contrib/run-tests.sh b/contrib/run-tests.sh new file mode 100755 index 0000000..88923ab --- /dev/null +++ b/contrib/run-tests.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# Build Bitcoin Core, launch a regtest node with IPC enabled, run the +# integration test suite against it, and shut the node down. +# +# Usage: contrib/run-tests.sh + +set -euo pipefail + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +BITCOIN_SRC=$(cd "$1" && pwd) +REPO_ROOT=$(cd "$(dirname "$0")/.." && pwd) +BITCOIN_BIN="$BITCOIN_SRC/build/bin/bitcoin" +TEMP_DATADIR=$(mktemp -d) + +echo "==> Building Bitcoin Core in $BITCOIN_SRC" +( + cd "$BITCOIN_SRC" + CMAKE_ARGS=(-DENABLE_WALLET=ON -DBUILD_TESTS=OFF) + if command -v ccache >/dev/null 2>&1; then + CMAKE_ARGS+=(-DCMAKE_C_COMPILER_LAUNCHER=ccache + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache) + fi + cmake -B build "${CMAKE_ARGS[@]}" + cmake --build build --parallel "$(getconf _NPROCESSORS_ONLN)" +) + +stop_bitcoin() { + if [ -n "${BITCOIN_STARTED:-}" ]; then + echo "==> Stopping bitcoin" + "$BITCOIN_BIN" rpc -chain=regtest -datadir="$TEMP_DATADIR" stop || true + fi +} +trap stop_bitcoin EXIT + +echo "==> Starting bitcoin node (regtest, IPC) with -datadir=$TEMP_DATADIR" +"$BITCOIN_BIN" node -chain=regtest -ipcbind=unix -server -debug=ipc -datadir="$TEMP_DATADIR" -daemon +BITCOIN_STARTED=1 + +echo "==> Running cargo test" +( + cd "$REPO_ROOT" + BITCOIN_BIN="$BITCOIN_BIN" BITCOIN_DATADIR="$TEMP_DATADIR" cargo test +) diff --git a/tests/test.rs b/tests/test.rs index ef8eec2..0f354a7 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -5,6 +5,7 @@ use bitcoin_capnp_types::{ }; use capnp_rpc::{RpcSystem, rpc_twoparty_capnp::Side}; use encoding::encode_to_vec; +use std::time::Duration; use tokio::task::LocalSet; mod util; @@ -109,7 +110,7 @@ async fn rpc_query_uptime() { .map_err(|e| format!("failed to parse rpc response as JSON: {e}")) .unwrap(); let uptime = v["result"].as_i64().unwrap(); - assert!(uptime > 0, "Uptime must be greater than zero"); + assert!(uptime >= 0, "Uptime must not be negative"); }) .await; } @@ -242,6 +243,32 @@ async fn mining_wait_tip_changed() { let wait_result = resp.get().unwrap().get_result().unwrap(); assert_eq!(wait_result.get_hash().unwrap().len(), 32); assert_eq!(wait_result.get_height(), tip_height); + + // No timeout. Pin a local task on the wait, then interrupt it. + let mut req = mining.wait_tip_changed_request(); + req.get().set_current_tip(&tip_hash); + let waiter = tokio::task::spawn_local(req.send().promise); + + // Give the waiter time to block before interrupting. + tokio::time::sleep(Duration::from_millis(100)).await; + assert!( + !waiter.is_finished(), + "waitTipChanged returned before interrupt" + ); + mining + .interrupt_request() + .send() + .promise + .await + .expect("interrupt should not fail"); + + let joined = tokio::time::timeout(Duration::from_secs(10), waiter) + .await + .expect("interrupt should unblock waitTipChanged"); + assert!( + joined.is_ok_and(|resp| resp.is_ok()), + "interrupted waitTipChanged should return normally" + ); }) .await; } @@ -333,7 +360,7 @@ async fn mining_block_template_inspection() { .await; } -/// waitNext (short timeout), interruptWait, submitSolution (garbage), destroy. +/// waitNext (short timeout), waitNext interrupted by interruptWait, submitSolution (garbage), destroy. #[tokio::test] // Serialized because submitSolution behavior depends on current chain tip. #[serial_test::serial] @@ -355,7 +382,19 @@ async fn mining_block_template_lifecycle() { "waitNext should time out without a new template" ); - // interruptWait — should not crash. + // waitNext — no timeout. Pin a local task on the wait, then interrupt it. + let mut req = template.wait_next_request(); + req.get() + .init_options() + .set_fee_threshold(mining_capnp::MAX_MONEY); + let waiter = tokio::task::spawn_local(req.send().promise); + + // Give the waiter time to block before interrupting. + tokio::time::sleep(Duration::from_millis(100)).await; + assert!( + !waiter.is_finished(), + "waitNext returned before interruptWait" + ); template .interrupt_wait_request() .send() @@ -363,6 +402,14 @@ async fn mining_block_template_lifecycle() { .await .expect("interruptWait should not fail"); + let joined = tokio::time::timeout(Duration::from_secs(10), waiter) + .await + .expect("interrupWait should unblock waitNext"); + assert!( + joined.is_ok_and(|resp| resp.is_ok()), + "interrupted waitNext should return normally" + ); + // submitSolution — garbage coinbase should be rejected. // This mutates the template, so we do it right before destroy. let mut req = template.submit_solution_request(); @@ -627,11 +674,10 @@ async fn mining_get_transactions() { .await; } -/// checkBlock with a template block payload, and interrupt. +/// checkBlock with a template block payload. #[tokio::test] -// Serialized because interrupt() can affect other in-flight mining waits. -#[serial_test::serial] -async fn mining_check_block_and_interrupt() { +#[serial_test::parallel] +async fn mining_check_block() { with_mining_client(|_client, mining| async move { let template = make_block_template(&mining).await; @@ -663,14 +709,6 @@ async fn mining_check_block_and_interrupt() { } destroy_template(&template).await; - - // interrupt — should not crash. - mining - .interrupt_request() - .send() - .promise - .await - .expect("interrupt should not fail"); }) .await; } diff --git a/tests/util/bitcoin_core.rs b/tests/util/bitcoin_core.rs index b830e16..1efebab 100644 --- a/tests/util/bitcoin_core.rs +++ b/tests/util/bitcoin_core.rs @@ -5,7 +5,8 @@ use std::{ }; use crate::util::bitcoin_core_wallet::{ - bitcoin_rpc_json, bitcoin_test_wallet, ensure_wallet_loaded, mine_blocks_to_new_address, + bitcoin_datadir, bitcoin_rpc_json, bitcoin_test_wallet, ensure_wallet_loaded, + mine_blocks_to_new_address, }; use bitcoin_capnp_types::{ init_capnp::init, @@ -23,15 +24,20 @@ use tokio_util::compat::{Compat, TokioAsyncReadCompatExt, TokioAsyncWriteCompatE static CHAIN_SETUP: Once = Once::new(); pub fn unix_socket_path() -> PathBuf { - let home_dir_string = std::env::var("HOME").unwrap(); - let home_dir = home_dir_string.parse::().unwrap(); - let bitcoin_dir = if cfg!(target_os = "macos") { - home_dir - .join("Library") - .join("Application Support") - .join("Bitcoin") - } else { - home_dir.join(".bitcoin") + let bitcoin_dir = match bitcoin_datadir() { + Some(datadir) => datadir.parse::().unwrap(), + None => { + let home_dir_string = std::env::var("HOME").unwrap(); + let home_dir = home_dir_string.parse::().unwrap(); + if cfg!(target_os = "macos") { + home_dir + .join("Library") + .join("Application Support") + .join("Bitcoin") + } else { + home_dir.join(".bitcoin") + } + } }; let regtest_dir = bitcoin_dir.join("regtest"); regtest_dir.join("node.sock") diff --git a/tests/util/bitcoin_core_wallet.rs b/tests/util/bitcoin_core_wallet.rs index 9c37fdf..e8fb4db 100644 --- a/tests/util/bitcoin_core_wallet.rs +++ b/tests/util/bitcoin_core_wallet.rs @@ -9,6 +9,10 @@ fn bitcoin_bin() -> String { std::env::var("BITCOIN_BIN").unwrap_or_else(|_| "bitcoin".to_owned()) } +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) @@ -29,6 +33,9 @@ pub fn bitcoin_test_wallet() -> String { 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}")); }