diff --git a/src/database/memory.rs b/src/database/memory.rs index cda3775e..84ecf30a 100644 --- a/src/database/memory.rs +++ b/src/database/memory.rs @@ -458,16 +458,17 @@ impl ConfigurableDatabase for MemoryDatabase { } } -#[cfg(test)] -impl MemoryDatabase { - // Artificially insert a tx in the database, as if we had found it with a `sync` - pub fn received_tx( - &mut self, - tx_meta: testutils::TestIncomingTx, - current_height: Option, - ) -> bitcoin::Txid { - use std::str::FromStr; - +#[macro_export] +#[doc(hidden)] +/// Artificially insert a tx in the database, as if we had found it with a `sync`. This is a hidden +/// macro and not a `[cfg(test)]` function so it can be called within the context of doctests which +/// don't have `test` set. +macro_rules! populate_test_db { + ($db:expr, $tx_meta:expr, $current_height:expr$(,)?) => {{ + use $crate::database::BatchOperations; + let mut db = $db; + let tx_meta = $tx_meta; + let current_height: Option = $current_height; let tx = Transaction { version: 1, lock_time: 0, @@ -499,9 +500,9 @@ impl MemoryDatabase { fees: 0, }; - self.set_tx(&tx_details).unwrap(); + db.set_tx(&tx_details).unwrap(); for (vout, out) in tx.output.iter().enumerate() { - self.set_utxo(&UTXO { + db.set_utxo(&UTXO { txout: out.clone(), outpoint: OutPoint { txid, @@ -513,7 +514,37 @@ impl MemoryDatabase { } txid - } + }}; +} + +#[macro_export] +#[doc(hidden)] +/// Macro for getting a wallet for use in a doctest +macro_rules! doctest_wallet { + () => {{ + use $crate::bitcoin::Network; + use $crate::database::MemoryDatabase; + use testutils::testutils; + let descriptor = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"; + let descriptors = testutils!(@descriptors (descriptor) (descriptor)); + + let mut db = MemoryDatabase::new(); + let txid = populate_test_db!( + &mut db, + testutils! { + @tx ( (@external descriptors, 0) => 500_000 ) (@confirmations 1) + }, + Some(100), + ); + + $crate::Wallet::new_offline( + &descriptors.0, + descriptors.1.as_ref(), + Network::Regtest, + db + ) + .unwrap() + }} } #[cfg(test)] diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 1a82f16f..cd6366a5 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -1569,11 +1569,12 @@ mod test { ) .unwrap(); - let txid = wallet.database.borrow_mut().received_tx( + let txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! { @tx ( (@external descriptors, 0) => 50_000 ) (@confirmations 1) }, - Some(100), + Some(100) ); (wallet, descriptors, txid) @@ -2262,7 +2263,8 @@ mod test { #[test] fn test_create_tx_add_utxo() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - let small_output_txid = wallet.database.borrow_mut().received_tx( + let small_output_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -2291,7 +2293,8 @@ mod test { #[should_panic(expected = "InsufficientFunds")] fn test_create_tx_manually_selected_insufficient() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - let small_output_txid = wallet.database.borrow_mut().received_tx( + let small_output_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -2765,7 +2768,8 @@ mod test { fn test_bump_fee_drain_wallet() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); // receive an extra tx so that our wallet has two utxos. - let incoming_txid = wallet.database.borrow_mut().received_tx( + let incoming_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -2823,7 +2827,8 @@ mod test { // them, and make sure that `bump_fee` doesn't try to add more. eventually, it should fail // because the fee rate is too high and the single utxo isn't enough to create a non-dust // output - let incoming_txid = wallet.database.borrow_mut().received_tx( + let incoming_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -2873,7 +2878,8 @@ mod test { #[test] fn test_bump_fee_add_input() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - wallet.database.borrow_mut().received_tx( + crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -2938,7 +2944,8 @@ mod test { #[test] fn test_bump_fee_absolute_add_input() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - wallet.database.borrow_mut().received_tx( + crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -3000,7 +3007,8 @@ mod test { #[test] fn test_bump_fee_no_change_add_input_and_change() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - let incoming_txid = wallet.database.borrow_mut().received_tx( + let incoming_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -3079,7 +3087,8 @@ mod test { #[test] fn test_bump_fee_add_input_change_dust() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - wallet.database.borrow_mut().received_tx( + crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -3141,7 +3150,8 @@ mod test { #[test] fn test_bump_fee_force_add_input() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - let incoming_txid = wallet.database.borrow_mut().received_tx( + let incoming_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), ); @@ -3213,7 +3223,8 @@ mod test { #[test] fn test_bump_fee_absolute_force_add_input() { let (wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - let incoming_txid = wallet.database.borrow_mut().received_tx( + let incoming_txid = crate::populate_test_db!( + wallet.database.borrow_mut(), testutils! (@tx ( (@external descriptors, 0) => 25_000 ) (@confirmations 1)), Some(100), );