Skip to content

Commit

Permalink
test(wallet): update tests to reflect new load and load_no_priv const…
Browse files Browse the repository at this point in the history
…ructors
  • Loading branch information
thunderbiscuit committed Jul 3, 2024
1 parent 033759f commit 0688a76
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const P2WPKH_FAKE_WITNESS_SIZE: usize = 106;
const DB_MAGIC: &[u8] = &[0x21, 0x24, 0x48];

#[test]
fn load_recovers_wallet() -> anyhow::Result<()> {
fn test_wallet_load_no_priv() -> anyhow::Result<()> {
fn run<Db, New, Recover, Read, Write>(
filename: &str,
create_new: New,
Expand Down Expand Up @@ -139,7 +139,7 @@ fn load_recovers_wallet() -> anyhow::Result<()> {
let db = &mut recover(&file_path).expect("must recover db");
let changeset = read(db).expect("must recover wallet").expect("changeset");

let wallet = Wallet::load_from_changeset(changeset).expect("must recover wallet");
let wallet = Wallet::load_no_priv(changeset).expect("must recover wallet");
assert_eq!(wallet.network(), Network::Testnet);
assert_eq!(
wallet.spk_index().keychains().collect::<Vec<_>>(),
Expand Down Expand Up @@ -180,7 +180,7 @@ fn load_recovers_wallet() -> anyhow::Result<()> {
}

#[test]
fn new_or_load() -> anyhow::Result<()> {
fn test_wallet_load() -> anyhow::Result<()> {
fn run<Db, NewOrRecover, Read, Write>(
filename: &str,
new_or_load: NewOrRecover,
Expand All @@ -196,10 +196,10 @@ fn new_or_load() -> anyhow::Result<()> {
let file_path = temp_dir.path().join(filename);
let (desc, change_desc) = get_test_wpkh_with_change_desc();

// init wallet when non-existent
// initialize wallet
let wallet_keychains: BTreeMap<_, _> = {
let wallet = &mut Wallet::new_or_load(desc, change_desc, None, Network::Testnet)
.expect("must init wallet");
let wallet =
&mut Wallet::new(desc, change_desc, Network::Testnet).expect("must init wallet");
let mut db = new_or_load(&file_path).expect("must create db");
if let Some(changeset) = wallet.take_staged() {
write(&mut db, &changeset)?;
Expand All @@ -210,13 +210,15 @@ fn new_or_load() -> anyhow::Result<()> {
// wrong network
{
let mut db = new_or_load(&file_path).expect("must create db");
let changeset = read(&mut db)?;
let err = Wallet::new_or_load(desc, change_desc, changeset, Network::Bitcoin)
let changeset = read(&mut db)
.expect("must recover wallet")
.expect("changeset");
let err = Wallet::load(desc, change_desc, changeset, Network::Bitcoin)
.expect_err("wrong network");
assert!(
matches!(
err,
bdk_wallet::wallet::NewOrLoadError::LoadedNetworkDoesNotMatch {
bdk_wallet::wallet::LoadError::LoadedNetworkDoesNotMatch {
got: Some(Network::Testnet),
expected: Network::Bitcoin
}
Expand All @@ -226,32 +228,6 @@ fn new_or_load() -> anyhow::Result<()> {
);
}

// wrong genesis hash
{
let exp_blockhash = BlockHash::all_zeros();
let got_blockhash = bitcoin::constants::genesis_block(Network::Testnet).block_hash();

let db = &mut new_or_load(&file_path).expect("must open db");
let changeset = read(db)?;
let err = Wallet::new_or_load_with_genesis_hash(
desc,
change_desc,
changeset,
Network::Testnet,
exp_blockhash,
)
.expect_err("wrong genesis hash");
assert!(
matches!(
err,
bdk_wallet::wallet::NewOrLoadError::LoadedGenesisDoesNotMatch { got, expected }
if got == Some(got_blockhash) && expected == exp_blockhash
),
"err: {}",
err,
);
}

// wrong external descriptor
{
let (exp_descriptor, exp_change_desc) = get_test_tr_single_sig_xprv_with_change_desc();
Expand All @@ -261,14 +237,13 @@ fn new_or_load() -> anyhow::Result<()> {
.0;

let db = &mut new_or_load(&file_path).expect("must open db");
let changeset = read(db)?;
let err =
Wallet::new_or_load(exp_descriptor, exp_change_desc, changeset, Network::Testnet)
.expect_err("wrong external descriptor");
let changeset = read(db).expect("must recover wallet").expect("changeset");
let err = Wallet::load(exp_descriptor, exp_change_desc, changeset, Network::Testnet)
.expect_err("wrong external descriptor");
assert!(
matches!(
err,
bdk_wallet::wallet::NewOrLoadError::LoadedDescriptorDoesNotMatch { ref got, keychain }
bdk_wallet::wallet::LoadError::LoadedDescriptorDoesNotMatch { ref got, keychain }
if got == &Some(got_descriptor) && keychain == KeychainKind::External
),
"err: {}",
Expand All @@ -285,13 +260,13 @@ fn new_or_load() -> anyhow::Result<()> {
.0;

let db = &mut new_or_load(&file_path).expect("must open db");
let changeset = read(db)?;
let err = Wallet::new_or_load(desc, exp_descriptor, changeset, Network::Testnet)
let changeset = read(db).expect("must recover wallet").expect("changeset");
let err = Wallet::load(desc, exp_descriptor, changeset, Network::Testnet)
.expect_err("wrong internal descriptor");
assert!(
matches!(
err,
bdk_wallet::wallet::NewOrLoadError::LoadedDescriptorDoesNotMatch { ref got, keychain }
bdk_wallet::wallet::LoadError::LoadedDescriptorDoesNotMatch { ref got, keychain }
if got == &Some(got_descriptor) && keychain == KeychainKind::Internal
),
"err: {}",
Expand All @@ -302,8 +277,8 @@ fn new_or_load() -> anyhow::Result<()> {
// all parameters match
{
let db = &mut new_or_load(&file_path).expect("must open db");
let changeset = read(db)?;
let wallet = Wallet::new_or_load(desc, change_desc, changeset, Network::Testnet)
let changeset = read(db).expect("must recover wallet").expect("changeset");
let wallet = Wallet::load(desc, change_desc, changeset, Network::Testnet)
.expect("must recover wallet");
assert_eq!(wallet.network(), Network::Testnet);
assert!(wallet
Expand All @@ -330,6 +305,20 @@ fn new_or_load() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn test_wallet_new() -> anyhow::Result<()> {
let (desc, change_desc) = get_test_wpkh_with_change_desc();

// initialize wallet
let _: BTreeMap<_, _> = {
let wallet =
&mut Wallet::new(desc, change_desc, Network::Testnet).expect("must init wallet");
wallet.keychains().map(|(k, v)| (*k, v.clone())).collect()
};

Ok(())
}

#[test]
fn test_error_external_and_internal_are_the_same() {
// identical descriptors should fail to create wallet
Expand Down

0 comments on commit 0688a76

Please sign in to comment.