}
}
-#[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<u32>,
- ) -> 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<u32> = $current_height;
let tx = Transaction {
version: 1,
lock_time: 0,
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,
}
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)]
)
.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)
#[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),
);
#[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),
);
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),
);
// 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),
);
#[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),
);
#[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),
);
#[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),
);
#[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),
);
#[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),
);
#[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),
);