|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv}; |
| 4 | +use bdk_tx::{ |
| 5 | + filter_unspendable_now, group_by_spk, selection_algorithm_lowest_fee_bnb, ChangePolicyType, |
| 6 | + Output, PsbtParams, ScriptSource, SelectorParams, Signer, |
| 7 | +}; |
| 8 | +use bitcoin::{absolute::LockTime, key::Secp256k1, Amount, FeeRate, Sequence, Transaction}; |
| 9 | +use miniscript::Descriptor; |
| 10 | + |
| 11 | +mod common; |
| 12 | +use common::Wallet; |
| 13 | + |
| 14 | +fn main() -> anyhow::Result<()> { |
| 15 | + let secp = Secp256k1::new(); |
| 16 | + let (external, external_keymap) = |
| 17 | + Descriptor::parse_descriptor(&secp, bdk_testenv::utils::DESCRIPTORS[3])?; |
| 18 | + let (internal, internal_keymap) = |
| 19 | + Descriptor::parse_descriptor(&secp, bdk_testenv::utils::DESCRIPTORS[4])?; |
| 20 | + |
| 21 | + let signer = Signer(external_keymap.into_iter().chain(internal_keymap).collect()); |
| 22 | + |
| 23 | + let env = TestEnv::new()?; |
| 24 | + let genesis_hash = env.genesis_hash()?; |
| 25 | + env.mine_blocks(101, None)?; |
| 26 | + |
| 27 | + let mut wallet = Wallet::new(genesis_hash, external, internal.clone())?; |
| 28 | + wallet.sync(&env)?; |
| 29 | + |
| 30 | + let addr = wallet.next_address().expect("must derive address"); |
| 31 | + println!("Wallet address: {addr}"); |
| 32 | + |
| 33 | + // Fund the wallet with two transactions |
| 34 | + env.send(&addr, Amount::from_sat(100_000_000))?; |
| 35 | + env.send(&addr, Amount::from_sat(100_000_000))?; |
| 36 | + env.mine_blocks(1, None)?; |
| 37 | + wallet.sync(&env)?; |
| 38 | + println!("Balance: {}", wallet.balance()); |
| 39 | + |
| 40 | + // Create two low-fee parent transactions |
| 41 | + let (tip_height, tip_time) = wallet.tip_info(env.rpc_client())?; |
| 42 | + let mut parent_txids = vec![]; |
| 43 | + for i in 0..4 { |
| 44 | + let low_fee_selection = wallet |
| 45 | + .all_candidates() |
| 46 | + .regroup(group_by_spk()) |
| 47 | + .filter(filter_unspendable_now(tip_height, tip_time)) |
| 48 | + .into_selection( |
| 49 | + selection_algorithm_lowest_fee_bnb(FeeRate::from_sat_per_vb_unchecked(1), 100_000), |
| 50 | + SelectorParams::new( |
| 51 | + FeeRate::from_sat_per_vb_unchecked(1), |
| 52 | + vec![Output::with_script( |
| 53 | + addr.script_pubkey(), |
| 54 | + Amount::from_sat(49_000_000), |
| 55 | + )], |
| 56 | + ScriptSource::Descriptor(Box::new(internal.at_derivation_index(i)?)), |
| 57 | + ChangePolicyType::NoDustAndLeastWaste { |
| 58 | + longterm_feerate: FeeRate::from_sat_per_vb_unchecked(1), |
| 59 | + }, |
| 60 | + wallet.change_weight(), |
| 61 | + ), |
| 62 | + )?; |
| 63 | + let mut parent_psbt = low_fee_selection.create_psbt(PsbtParams { |
| 64 | + fallback_sequence: Sequence::MAX, |
| 65 | + ..Default::default() |
| 66 | + })?; |
| 67 | + let parent_finalizer = low_fee_selection.into_finalizer(); |
| 68 | + parent_psbt.sign(&signer, &secp).expect("failed to sign"); |
| 69 | + assert!(parent_finalizer.finalize(&mut parent_psbt).is_finalized()); |
| 70 | + let parent_tx = parent_psbt.extract_tx()?; |
| 71 | + let parent_txid = env.rpc_client().send_raw_transaction(&parent_tx)?; |
| 72 | + println!("Parent tx {} broadcasted: {}", i + 1, parent_txid); |
| 73 | + parent_txids.push(parent_txid); |
| 74 | + wallet.sync(&env)?; |
| 75 | + } |
| 76 | + println!("Balance after parent txs: {}", wallet.balance()); |
| 77 | + |
| 78 | + // Verify parent transactions are in mempool |
| 79 | + let mempool = env.rpc_client().get_raw_mempool()?; |
| 80 | + for (i, txid) in parent_txids.iter().enumerate() { |
| 81 | + if mempool.contains(txid) { |
| 82 | + println!("Parent TX {} {} is in mempool", i + 1, txid); |
| 83 | + } else { |
| 84 | + println!("Parent TX {} {} is NOT in mempool", i + 1, txid); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Create CPFP transaction to boost both parents |
| 89 | + let cpfp_selection = wallet.create_cpfp_tx( |
| 90 | + parent_txids.clone(), |
| 91 | + FeeRate::from_sat_per_vb_unchecked(10), // user specified |
| 92 | + )?; |
| 93 | + |
| 94 | + let mut cpfp_psbt = cpfp_selection.create_psbt(PsbtParams { |
| 95 | + fallback_sequence: Sequence::MAX, |
| 96 | + fallback_locktime: LockTime::ZERO, |
| 97 | + ..Default::default() |
| 98 | + })?; |
| 99 | + let cpfp_finalizer = cpfp_selection.into_finalizer(); |
| 100 | + cpfp_psbt.sign(&signer, &secp).expect("failed to sign"); |
| 101 | + assert!(cpfp_finalizer.finalize(&mut cpfp_psbt).is_finalized()); |
| 102 | + let cpfp_tx = cpfp_psbt.extract_tx()?; |
| 103 | + let cpfp_txid = env.rpc_client().send_raw_transaction(&cpfp_tx)?; |
| 104 | + |
| 105 | + wallet.sync(&env)?; |
| 106 | + println!("Balance after CPFP: {}", wallet.balance()); |
| 107 | + |
| 108 | + // Verify all transactions are in mempool |
| 109 | + let mempool = env.rpc_client().get_raw_mempool()?; |
| 110 | + println!("\nChecking transactions in mempool:"); |
| 111 | + for (i, txid) in parent_txids.iter().enumerate() { |
| 112 | + if mempool.contains(txid) { |
| 113 | + println!("Parent TX {} {} is in mempool", i + 1, txid); |
| 114 | + } else { |
| 115 | + println!("Parent TX {} {} is NOT in mempool", i + 1, txid); |
| 116 | + } |
| 117 | + } |
| 118 | + if mempool.contains(&cpfp_txid) { |
| 119 | + println!("CPFP TX {cpfp_txid} is in mempool"); |
| 120 | + } else { |
| 121 | + println!("CPFP TX {cpfp_txid} is NOT in mempool"); |
| 122 | + } |
| 123 | + |
| 124 | + // Verify child spends parents |
| 125 | + for (i, parent_txid) in parent_txids.iter().enumerate() { |
| 126 | + let parent_tx = env.rpc_client().get_raw_transaction(parent_txid, None)?; |
| 127 | + if child_spends_parent(&parent_tx, &cpfp_tx) { |
| 128 | + println!("CPFP transaction spends an output of parent {}.", i + 1); |
| 129 | + } else { |
| 130 | + println!( |
| 131 | + "CPFP transaction does NOT spend outputs of parent {}.", |
| 132 | + i + 1 |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + println!("\n=== MINING BLOCK TO CONFIRM TRANSACTIONS ==="); |
| 138 | + let block_hashes = env.mine_blocks(1, None)?; // Revert to None, rely on mempool |
| 139 | + println!("Mined block: {}", block_hashes[0]); |
| 140 | + wallet.sync(&env)?; |
| 141 | + |
| 142 | + println!("Final wallet balance: {}", wallet.balance()); |
| 143 | + |
| 144 | + println!("\nChecking transactions in mempool again:"); |
| 145 | + let mempool = env.rpc_client().get_raw_mempool()?; |
| 146 | + for (i, txid) in parent_txids.iter().enumerate() { |
| 147 | + if mempool.contains(txid) { |
| 148 | + println!("Parent TX {} {} is in mempool", i + 1, txid); |
| 149 | + } else { |
| 150 | + println!("Parent TX {} {} is NOT in mempool", i + 1, txid); |
| 151 | + } |
| 152 | + } |
| 153 | + if mempool.contains(&cpfp_txid) { |
| 154 | + println!("CPFP TX {cpfp_txid} is in mempool"); |
| 155 | + } else { |
| 156 | + println!("CPFP TX {cpfp_txid} is NOT in mempool"); |
| 157 | + } |
| 158 | + Ok(()) |
| 159 | +} |
| 160 | + |
| 161 | +fn child_spends_parent(parent_tx: &Transaction, child_tx: &Transaction) -> bool { |
| 162 | + let parent_txid = parent_tx.compute_txid(); |
| 163 | + child_tx |
| 164 | + .input |
| 165 | + .iter() |
| 166 | + .any(|input| input.previous_output.txid == parent_txid) |
| 167 | +} |
0 commit comments