|
| 1 | +#![allow(dead_code)] |
| 2 | +use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv}; |
| 3 | +use bdk_tx::{ |
| 4 | + filter_unspendable_now, group_by_spk, selection_algorithm_lowest_fee_bnb, Output, PsbtParams, |
| 5 | + ScriptSource, SelectorParams, |
| 6 | +}; |
| 7 | +use bitcoin::{absolute::LockTime, key::Secp256k1, Amount, FeeRate, Sequence}; |
| 8 | +use miniscript::Descriptor; |
| 9 | + |
| 10 | +mod common; |
| 11 | + |
| 12 | +use common::Wallet; |
| 13 | + |
| 14 | +fn main() -> anyhow::Result<()> { |
| 15 | + let secp = Secp256k1::new(); |
| 16 | + let (external, _) = Descriptor::parse_descriptor(&secp, bdk_testenv::utils::DESCRIPTORS[0])?; |
| 17 | + let (internal, _) = Descriptor::parse_descriptor(&secp, bdk_testenv::utils::DESCRIPTORS[1])?; |
| 18 | + |
| 19 | + let env = TestEnv::new()?; |
| 20 | + let genesis_hash = env.genesis_hash()?; |
| 21 | + env.mine_blocks(101, None)?; |
| 22 | + |
| 23 | + let mut wallet = Wallet::new(genesis_hash, external, internal.clone())?; |
| 24 | + wallet.sync(&env)?; |
| 25 | + |
| 26 | + let addr = wallet.next_address().expect("must derive address"); |
| 27 | + |
| 28 | + let txid1 = env.send(&addr, Amount::ONE_BTC)?; |
| 29 | + env.mine_blocks(1, None)?; |
| 30 | + wallet.sync(&env)?; |
| 31 | + println!("Received confirmed input: {}", txid1); |
| 32 | + |
| 33 | + let txid2 = env.send(&addr, Amount::ONE_BTC)?; |
| 34 | + env.mine_blocks(1, None)?; |
| 35 | + wallet.sync(&env)?; |
| 36 | + println!("Received confirmed input: {}", txid2); |
| 37 | + |
| 38 | + println!("Balance (confirmed): {}", wallet.balance()); |
| 39 | + |
| 40 | + let (tip_height, tip_time) = wallet.tip_info(env.rpc_client())?; |
| 41 | + println!("Current height: {}", tip_height); |
| 42 | + let longterm_feerate = FeeRate::from_sat_per_vb_unchecked(1); |
| 43 | + |
| 44 | + let recipient_addr = env |
| 45 | + .rpc_client() |
| 46 | + .get_new_address(None, None)? |
| 47 | + .assume_checked(); |
| 48 | + |
| 49 | + // When anti-fee-sniping is enabled, the transaction will either use nLockTime or nSequence. |
| 50 | + // |
| 51 | + // Locktime approach is used when: |
| 52 | + // - RBF is disabled, OR |
| 53 | + // - Any input requires locktime (non-taproot, unconfirmed, or >65535 confirmations), OR |
| 54 | + // - There are no taproot inputs, OR |
| 55 | + // - Random 50/50 coin flip chose locktime |
| 56 | + // |
| 57 | + // Sequence approach is used otherwise: |
| 58 | + // - Sets tx.lock_time to ZERO |
| 59 | + // - Modifies one randomly selected taproot input's sequence |
| 60 | + // |
| 61 | + // Once the approach is selected, to reduce transaction fingerprinting, |
| 62 | + // - For nLockTime: With 10% probability, subtract a random 0-99 block offset from current height |
| 63 | + // - For nSequence: With 10% probability, subtract a random 0-99 block offset (minimum value of 1) |
| 64 | + // |
| 65 | + // Note: When locktime is used, all sequence values remain unchanged. |
| 66 | + |
| 67 | + let mut locktime_count = 0; |
| 68 | + let mut sequence_count = 0; |
| 69 | + |
| 70 | + for _ in 0..10 { |
| 71 | + let selection = wallet |
| 72 | + .all_candidates() |
| 73 | + .regroup(group_by_spk()) |
| 74 | + .filter(filter_unspendable_now(tip_height, tip_time)) |
| 75 | + .into_selection( |
| 76 | + selection_algorithm_lowest_fee_bnb(longterm_feerate, 100_000), |
| 77 | + SelectorParams::new( |
| 78 | + FeeRate::from_sat_per_vb_unchecked(10), |
| 79 | + vec![Output::with_script( |
| 80 | + recipient_addr.script_pubkey(), |
| 81 | + Amount::from_sat(50_000_000), |
| 82 | + )], |
| 83 | + ScriptSource::Descriptor(Box::new(internal.at_derivation_index(0)?)), |
| 84 | + bdk_tx::ChangePolicyType::NoDustAndLeastWaste { longterm_feerate }, |
| 85 | + wallet.change_weight(), |
| 86 | + ), |
| 87 | + )?; |
| 88 | + |
| 89 | + let fallback_locktime: LockTime = LockTime::from_consensus(tip_height.to_consensus_u32()); |
| 90 | + |
| 91 | + let selection_inputs = selection.inputs.clone(); |
| 92 | + |
| 93 | + let psbt = selection.create_psbt(PsbtParams { |
| 94 | + enable_anti_fee_sniping: true, |
| 95 | + fallback_locktime, |
| 96 | + fallback_sequence: Sequence::ENABLE_RBF_NO_LOCKTIME, |
| 97 | + ..Default::default() |
| 98 | + })?; |
| 99 | + |
| 100 | + let tx = psbt.unsigned_tx; |
| 101 | + |
| 102 | + if tx.lock_time != LockTime::ZERO { |
| 103 | + locktime_count += 1; |
| 104 | + let locktime_value = tx.lock_time.to_consensus_u32(); |
| 105 | + let current_height = tip_height.to_consensus_u32(); |
| 106 | + |
| 107 | + let offset = current_height.saturating_sub(locktime_value); |
| 108 | + if offset > 0 { |
| 109 | + println!( |
| 110 | + "nLockTime = {} (tip height: {}, offset: -{})", |
| 111 | + locktime_value, current_height, offset |
| 112 | + ); |
| 113 | + } else { |
| 114 | + println!( |
| 115 | + "nLockTime = {} (tip height: {}, no offset)", |
| 116 | + locktime_value, current_height |
| 117 | + ); |
| 118 | + } |
| 119 | + } else { |
| 120 | + sequence_count += 1; |
| 121 | + |
| 122 | + for (i, inp) in tx.input.iter().enumerate() { |
| 123 | + let sequence_value = inp.sequence.to_consensus_u32(); |
| 124 | + |
| 125 | + if (1..0xFFFFFFFD).contains(&sequence_value) { |
| 126 | + let input_confirmations = selection_inputs[i].confirmations(tip_height); |
| 127 | + let offset = input_confirmations.saturating_sub(sequence_value); |
| 128 | + |
| 129 | + if offset > 0 { |
| 130 | + println!( |
| 131 | + "nSequence[{}] = {} (confirmations: {}, offset: -{})", |
| 132 | + i, sequence_value, input_confirmations, offset |
| 133 | + ); |
| 134 | + } else { |
| 135 | + println!( |
| 136 | + "nSequence[{}] = {} (confirmations: {}, no offset)", |
| 137 | + i, sequence_value, input_confirmations |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + println!("nLockTime approach used: {} times", locktime_count); |
| 148 | + println!("nSequence approach used: {} times", sequence_count); |
| 149 | + |
| 150 | + Ok(()) |
| 151 | +} |
0 commit comments