Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposing get_funded_wallet #411

Merged
Merged
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
2 changes: 1 addition & 1 deletion src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl PsbtUtils for Psbt {
mod test {
use crate::bitcoin::TxIn;
use crate::psbt::Psbt;
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
use crate::wallet::AddressIndex;
use crate::wallet::{get_funded_wallet, test::get_test_wpkh};
use crate::SignOptions;
use std::str::FromStr;

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/address_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ mod test {
use std::sync::Arc;

use super::*;
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
use crate::wallet::AddressIndex::New;
use crate::wallet::{get_funded_wallet, test::get_test_wpkh};

#[derive(Debug)]
struct TestValidator;
Expand Down
106 changes: 53 additions & 53 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use std::collections::{BTreeMap, HashSet};
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::sync::Arc;

use bitcoin::secp256k1::Secp256k1;
Expand Down Expand Up @@ -55,6 +56,7 @@ use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxParams};
use utils::{check_nlocktime, check_nsequence_rbf, After, Older, SecpCtx, DUST_LIMIT_SATOSHI};

use crate::blockchain::{Blockchain, Progress};
use crate::database::memory::MemoryDatabase;
use crate::database::{BatchDatabase, BatchOperations, DatabaseUtils};
use crate::descriptor::derived::AsDerived;
use crate::descriptor::policy::BuildSatisfaction;
Expand All @@ -66,6 +68,7 @@ use crate::descriptor::{
use crate::error::Error;
use crate::psbt::PsbtUtils;
use crate::signer::SignerError;
use crate::testutils;
use crate::types::*;

const CACHE_ADDR_BATCH_SIZE: u32 = 100;
Expand Down Expand Up @@ -167,6 +170,11 @@ where
secp,
})
}

/// Get the Bitcoin network the wallet is using.
pub fn network(&self) -> Network {
self.network
}
}

/// The address index selection strategy to use to derived an address from the wallet's external
Expand Down Expand Up @@ -1534,11 +1542,6 @@ where
&self.client
}

/// Get the Bitcoin network the wallet is using.
pub fn network(&self) -> Network {
self.network
}

/// Broadcast a transaction to the network
#[maybe_async]
pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
Expand All @@ -1548,19 +1551,60 @@ where
}
}

/// Return a fake wallet that appears to be funded for testing.
pub fn get_funded_wallet(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try with #[cfg(test)] to see if you could hide this to non-testing environments?

I didn't realize this yesterday, I thought this was still part of the test module, which is only present when testing is enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I add this, then I get the following error in bdk-reserves: "unresolved import bdk::wallet::get_funded_wallet"

And if I try the following in bdk-reserves/Cargo.toml: "bdk = { ... features = ["electrum", "test"] }" then I get the following error: "the package bdk-reserves depends on bdk, with features: test but bdk does not have these features."

It would be cool to make it work like this, but I don't know how.

descriptor: &str,
) -> (
Wallet<(), MemoryDatabase>,
(String, Option<String>),
bitcoin::Txid,
) {
let descriptors = testutils!(@descriptors (descriptor));
let wallet = Wallet::new_offline(
&descriptors.0,
None,
Network::Regtest,
MemoryDatabase::new(),
)
.unwrap();

let funding_address_kix = 0;

let tx_meta = testutils! {
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
};

wallet
.database
.borrow_mut()
.set_script_pubkey(
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
.unwrap()
.script_pubkey(),
KeychainKind::External,
funding_address_kix,
)
.unwrap();
wallet
.database
.borrow_mut()
.set_last_index(KeychainKind::External, funding_address_kix)
.unwrap();

let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));

(wallet, descriptors, txid)
}

#[cfg(test)]
pub(crate) mod test {
use std::str::FromStr;

use bitcoin::{util::psbt, Network};

use crate::database::memory::MemoryDatabase;
use crate::database::Database;
use crate::types::KeychainKind;

use super::*;
use crate::signer::{SignOptions, SignerError};
use crate::testutils;
use crate::wallet::AddressIndex::{LastUnused, New, Peek, Reset};

#[test]
Expand Down Expand Up @@ -1672,50 +1716,6 @@ pub(crate) mod test {
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))"
}

pub(crate) fn get_funded_wallet(
descriptor: &str,
) -> (
Wallet<(), MemoryDatabase>,
(String, Option<String>),
bitcoin::Txid,
) {
let descriptors = testutils!(@descriptors (descriptor));
let wallet = Wallet::new_offline(
&descriptors.0,
None,
Network::Regtest,
MemoryDatabase::new(),
)
.unwrap();

let funding_address_kix = 0;

let tx_meta = testutils! {
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
};

wallet
.database
.borrow_mut()
.set_script_pubkey(
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
.unwrap()
.script_pubkey(),
KeychainKind::External,
funding_address_kix,
)
.unwrap();
wallet
.database
.borrow_mut()
.set_last_index(KeychainKind::External, funding_address_kix)
.unwrap();

let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));

(wallet, descriptors, txid)
}

macro_rules! assert_fee_rate {
($tx:expr, $fees:expr, $fee_rate:expr $( ,@dust_change $( $dust_change:expr )* )* $( ,@add_signature $( $add_signature:expr )* )* ) => ({
let mut tx = $tx.clone();
Expand Down