Skip to content
Draft
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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ jobs:
key: ccache-${{ runner.os }}-${{ github.sha }}
restore-keys: ccache-${{ runner.os }}-
- name: Checkout Bitcoin Core
run: git clone --depth 1 --branch master https://github.com/bitcoin/bitcoin.git
run: |
git clone --depth 1 https://github.com/bitcoin/bitcoin.git
# DO NOT MERGE: switch to bitcoin/bitcoin#36097 PR branch
cd bitcoin
git fetch --depth 1 origin pull/36097/head:pr-36097
git checkout pr-36097
- name: Build & Run Test Suite
run: |
ccache --zero-stats
Expand Down
9 changes: 7 additions & 2 deletions capnp/mining.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ interface Mining $Proxy.wrap("interfaces::Mining") {
waitTipChanged @3 (context :Proxy.Context, currentTip: Data, timeout: Float64 = .maxDouble) -> (result: Common.BlockRef);
createNewBlock @4 (context :Proxy.Context, options: BlockCreateOptions, cooldown: Bool = true) -> (result: BlockTemplate);
checkBlock @5 (context :Proxy.Context, block: Data, options: BlockCheckOptions) -> (reason: Text, debug: Text, result: Bool);
interrupt @6 () -> ();
submitBlock @7 (context :Proxy.Context, block: Data) -> (reason: Text, debug: Text, result: Bool);
getTransactionsByTxID @8 (context :Proxy.Context, txids: List(Data)) -> (result: List(Data));
getTransactionsByWitnessID @9 (context :Proxy.Context, wtxids: List(Data)) -> (result: List(Data));

# DEPRECATED: This is no longer needed. Any long-running server process can
# be stopped by dropping its promise.
interruptOld6 @6 () -> ();
}

interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
Expand All @@ -38,10 +41,12 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
getCoinbaseMerklePath @6 (context: Proxy.Context) -> (result: List(Data));
submitSolution @10 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (reason: Text, debug: Text, result: Bool);
waitNext @8 (context: Proxy.Context, options: BlockWaitOptions) -> (result: BlockTemplate);
interruptWait @9() -> ();

# DEPRECATED: older version of submitSolution which returns an error.
submitSolutionOld7 @7 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
# DEPRECATED: This is no longer needed. Any long-running server process can
# be stopped by dropping its promise.
interruptWaitOld9 @9() -> ();
}

struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
Expand Down
77 changes: 38 additions & 39 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ use bitcoin_capnp_types::{
};
use capnp_rpc::{RpcSystem, rpc_twoparty_capnp::Side};
use encoding::encode_to_vec;
use futures::FutureExt;
use std::time::Duration;
use tokio::task::LocalSet;

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_thread, 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 @@ -227,7 +228,7 @@ async fn mining_basic_queries() {
// Serialized because this assertion is sensitive to concurrent tip changes.
#[serial_test::serial]
async fn mining_wait_tip_changed() {
with_mining_client(|_client, mining| async move {
with_mining_client(|init, mining| async move {
// Get the current tip first.
let resp = mining.get_tip_request().send().promise.await.unwrap();
let results = resp.get().unwrap();
Expand All @@ -244,31 +245,30 @@ async fn mining_wait_tip_changed() {
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.
// No timeout. Block a dedicated server thread on the wait, then drop
// the promise to cancel it.
let thread = make_thread(&init, "wait_tip_changed").await;
let mut req = mining.wait_tip_changed_request();
req.get().get_context().unwrap().set_thread(thread.clone());
req.get().set_current_tip(&tip_hash);
let waiter = tokio::task::spawn_local(req.send().promise);
let mut wait = req.send().promise;

// Give the waiter time to block before interrupting.
// Give the server thread time to block before cancelling.
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(
!waiter.is_finished(),
"waitTipChanged returned before interrupt"
(&mut wait).now_or_never().is_none(),
"waitTipChanged returned before cancel"
);
mining
.interrupt_request()
.send()
.promise
.await
.expect("interrupt should not fail");
drop(wait);

let joined = tokio::time::timeout(Duration::from_secs(10), waiter)
// A server thread runs one call at a time, so this only completes if
// dropping the promise stopped the wait on the server.
let mut req = mining.get_tip_request();
req.get().get_context().unwrap().set_thread(thread);
tokio::time::timeout(Duration::from_secs(10), req.send().promise)
.await
.expect("interrupt should unblock waitTipChanged");
assert!(
joined.is_ok_and(|resp| resp.is_ok()),
"interrupted waitTipChanged should return normally"
);
.expect("dropped waitTipChanged should release its server thread")
.expect("getTip should not fail");
})
.await;
}
Expand Down Expand Up @@ -360,12 +360,12 @@ async fn mining_block_template_inspection() {
.await;
}

/// waitNext (short timeout), waitNext interrupted by interruptWait, submitSolution (garbage), destroy.
/// waitNext (short timeout), waitNext cancelled by dropping its promise, submitSolution (garbage), destroy.
#[tokio::test]
// Serialized because submitSolution behavior depends on current chain tip.
#[serial_test::serial]
async fn mining_block_template_lifecycle() {
with_mining_client(|_client, mining| async move {
with_mining_client(|init, mining| async move {
let template = make_block_template(&mining).await;

// waitNext — short timeout, no new transactions expected.
Expand All @@ -382,33 +382,32 @@ async fn mining_block_template_lifecycle() {
"waitNext should time out without a new template"
);

// waitNext — no timeout. Pin a local task on the wait, then interrupt it.
// waitNext — no timeout. Block a dedicated server thread on the wait,
// then drop the promise to cancel it.
let thread = make_thread(&init, "wait_next").await;
let mut req = template.wait_next_request();
req.get().get_context().unwrap().set_thread(thread.clone());
req.get()
.init_options()
.set_fee_threshold(mining_capnp::MAX_MONEY);
let waiter = tokio::task::spawn_local(req.send().promise);
let mut wait = req.send().promise;

// Give the waiter time to block before interrupting.
// Give the server thread time to block before cancelling.
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(
!waiter.is_finished(),
"waitNext returned before interruptWait"
(&mut wait).now_or_never().is_none(),
"waitNext returned before cancel"
);
template
.interrupt_wait_request()
.send()
.promise
.await
.expect("interruptWait should not fail");
drop(wait);

let joined = tokio::time::timeout(Duration::from_secs(10), waiter)
// A server thread runs one call at a time, so this only completes if
// dropping the promise stopped the wait on the server.
let mut req = template.get_block_header_request();
req.get().get_context().unwrap().set_thread(thread);
tokio::time::timeout(Duration::from_secs(10), req.send().promise)
.await
.expect("interrupWait should unblock waitNext");
assert!(
joined.is_ok_and(|resp| resp.is_ok()),
"interrupted waitNext should return normally"
);
.expect("dropped waitNext should release its server thread")
.expect("getBlockHeader should not fail");

// submitSolution — garbage coinbase should be rejected.
// This mutates the template, so we do it right before destroy.
Expand Down
13 changes: 12 additions & 1 deletion tests/util/bitcoin_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::util::bitcoin_core_wallet::{
use bitcoin_capnp_types::{
init_capnp::init,
mining_capnp::{block_template, mining},
proxy_capnp::thread_map,
proxy_capnp::{thread, thread_map},
rpc_capnp::rpc,
};
use capnp_rpc::{RpcSystem, rpc_twoparty_capnp::Side, twoparty::VatNetwork};
Expand Down Expand Up @@ -160,6 +160,17 @@ pub async fn make_mining(init: &init::Client) -> mining::Client {
resp.get().unwrap().get_result().unwrap()
}

/// Create a dedicated server thread. Requests with `context.thread` set to the
/// returned handle run on that thread instead of the worker pool.
pub async fn make_thread(init: &init::Client, name: &str) -> thread::Client {
let resp = init.construct_request().send().promise.await.unwrap();
let thread_map: thread_map::Client = resp.get().unwrap().get_thread_map().unwrap();
let mut req = thread_map.make_thread_request();
req.get().set_name(name);
let resp = req.send().promise.await.unwrap();
resp.get().unwrap().get_result().unwrap()
}

/// Obtain a Rpc client from an Init client.
pub async fn make_rpc(init: &init::Client) -> rpc::Client {
let resp = init.make_rpc_request().send().promise.await.unwrap();
Expand Down
Loading