Skip to content
Open
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
47 changes: 45 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use bitcoin::secp256k1::{Message, Secp256k1, SecretKey};
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
use bitcoin::{
absolute::LockTime, key::TweakedPublicKey, transaction::Version, Address, Amount,
CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Witness,
CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Txid,
Witness,
};
use corepc_node::{Conf, Node, P2P};
use kernel_node::server_capnp::server;
Expand Down Expand Up @@ -214,6 +215,47 @@ impl TestNode {
}))
}

pub fn history(&self) -> String {
let socket = self.socket_path();
self.rt
.block_on(tokio::task::LocalSet::new().run_until(async move {
let client = connect(&socket).await;
let wallet = client
.make_wallet_request()
.send()
.promise
.await
.unwrap()
.get()
.unwrap()
.get_wallet()
.unwrap();
let response = wallet.get_history_request().send().promise.await.unwrap();
response
.get()
.unwrap()
.get_entries()
.unwrap()
.to_string()
.unwrap()
}))
}

pub fn wait_for_history(&self, expected: &str, timeout: Duration) {
let deadline = Instant::now() + timeout;
loop {
let history = self.history();
if history == expected {
return;
}
assert!(
Instant::now() < deadline,
"history did not reach {expected:?} within {timeout:?} (at {history:?})"
);
std::thread::sleep(TIP_POLL_INTERVAL);
}
}

pub fn wait_for_balance(&self, min: Amount, timeout: Duration) -> Amount {
let deadline = Instant::now() + timeout;
loop {
Expand Down Expand Up @@ -286,7 +328,7 @@ pub fn wait_for_core_mempool(core: &Node, txid: &str, timeout: Duration) {

// Core cannot send to a silent payment address, so build the BIP-352 payment
// here, broadcast it through Core, and mine it.
pub fn fund_silent_payment(core: &Node, sp_address: &str, amount: Amount) {
pub fn fund_silent_payment(core: &Node, sp_address: &str, amount: Amount) -> Txid {
let secp = Secp256k1::new();

let sender_sk = SecretKey::from_slice(&[0x21; 32]).unwrap();
Expand Down Expand Up @@ -369,4 +411,5 @@ pub fn fund_silent_payment(core: &Node, sp_address: &str, amount: Amount) {
core.client.send_raw_transaction(&tx).unwrap();
let miner = core.client.new_address().unwrap();
core.client.generate_to_address(1, &miner).unwrap();
tx.compute_txid()
}
61 changes: 61 additions & 0 deletions tests/wallet_reorg_remine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
mod common;

use std::time::Duration;

use bitcoin::{Amount, BlockHash, Txid};
use common::{fund_silent_payment, start_bitcoind, wait_for_core_mempool, TestNode};

const SYNC_TIMEOUT: Duration = Duration::from_secs(60);
const REORG_TIMEOUT: Duration = Duration::from_secs(60);
const MEMPOOL_TIMEOUT: Duration = Duration::from_secs(30);
const PAYMENT: Amount = Amount::from_sat(100_000_000);

fn received_at(txid: Txid, height: u64) -> String {
format!("recv {txid}:0 {} sats at {height}", PAYMENT.to_sat())
}

#[test]
fn reorg_moves_a_re_mined_payment_to_a_new_height() {
let core = start_bitcoind();
let p2p = core.params.p2p_socket.unwrap();
println!("step 1/6: bitcoind started on regtest");

let node = TestNode::start_connected(p2p, Some(common::random_signing_keys()));
let sp_address = node.receive_address();
println!("step 2/6: node started, receive address {sp_address}");

let funding_txid = fund_silent_payment(&core, &sp_address, PAYMENT);
let funding_height = core.client.get_block_count().unwrap().0;
let balance = node.wait_for_balance(PAYMENT, SYNC_TIMEOUT);
assert_eq!(balance, PAYMENT);
assert_eq!(node.history(), received_at(funding_txid, funding_height));
println!("step 3/6: payment mined at height {funding_height}, balance = {balance}");

let funding_block = core
.client
.get_block_hash(funding_height)
.unwrap()
.0
.parse::<BlockHash>()
.unwrap();
core.client.invalidate_block(funding_block).unwrap();
wait_for_core_mempool(&core, &funding_txid.to_string(), MEMPOOL_TIMEOUT);
println!("step 4/6: invalidated height {funding_height}, payment is back in the mempool");

let miner = core.client.new_address().unwrap().to_string();
core.client.generate_block(&miner, &[], true).unwrap();
core.client
.generate_block(&miner, &[funding_txid.to_string()], true)
.unwrap();
let tip_height = core.client.get_block_count().unwrap().0;
let tip_hash = core.client.best_block_hash().unwrap();
assert!(tip_height > funding_height);
println!("step 5/6: re-mined the payment at height {tip_height} on a longer branch");

node.wait_for_tip(tip_height, tip_hash, REORG_TIMEOUT);
node.wait_for_history(&received_at(funding_txid, tip_height), REORG_TIMEOUT);
assert_eq!(node.balance(), PAYMENT);
println!("step 6/6: node reorged, balance held and the coin moved to {tip_height}");

node.stop();
}
Loading