Skip to content

Commit

Permalink
Merge #13: Update BDK 1.0
Browse files Browse the repository at this point in the history
d4b9439 Update BDK 1.0 (bennyhodl)

Pull request description:

ACKs for top commit:
  ValuedMammal:
    tACK d4b9439

Tree-SHA512: 98772570dde0e89fd5588d7f861a05a3ee4b7ab87c889b304744c1b9897b5677ca1276af829d8cce37dd8b25ecd9614d4f3d9e0fdfe5859de904f76099789b09
  • Loading branch information
ValuedMammal committed Jan 11, 2025
2 parents e6ca323 + d4b9439 commit b9b2eb6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bdk_wallet = { version = "1.0.0-beta.5" }
bdk_wallet = { version = "1.0.0", features = ["test-utils"] }
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "sqlite", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
Expand All @@ -16,7 +16,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde_json
[dev-dependencies]
assert_matches = "1.5.0"
anyhow = "1.0.89"
bdk_electrum = { version = "0.19.0"}
bdk_electrum = { version = "0.20.1"}
rustls = "0.23.14"

[[example]]
Expand Down
103 changes: 59 additions & 44 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bdk_wallet::bitcoin::Network::{Regtest, Signet};
use bdk_wallet::bitcoin::{
transaction, Address, Amount, BlockHash, Network, OutPoint, Transaction, TxIn, TxOut, Txid,
};
use bdk_wallet::chain::{tx_graph, BlockId, ConfirmationBlockTime, ConfirmationTime};
use bdk_wallet::chain::{tx_graph, BlockId, ConfirmationBlockTime};
use bdk_wallet::miniscript::{Descriptor, DescriptorPublicKey};
use bdk_wallet::{
bitcoin, descriptor::ExtendedDescriptor, wallet_name_from_descriptor, AsyncWalletPersister,
Expand Down Expand Up @@ -219,42 +219,54 @@ pub fn insert_fake_tx(wallet: &mut Wallet, spent: Amount, change: Amount, fee: A
],
};

wallet
.insert_checkpoint(BlockId {
bdk_wallet::test_utils::insert_checkpoint(
wallet,
BlockId {
height: 42,
hash: BlockHash::all_zeros(),
})
.unwrap();
wallet
.insert_checkpoint(BlockId {
},
);

bdk_wallet::test_utils::insert_checkpoint(
wallet,
BlockId {
height: 1_000,
hash: BlockHash::all_zeros(),
})
.unwrap();
wallet
.insert_checkpoint(BlockId {
},
);
bdk_wallet::test_utils::insert_checkpoint(
wallet,
BlockId {
height: 2_000,
hash: BlockHash::all_zeros(),
})
.unwrap();
},
);

bdk_wallet::test_utils::insert_tx(wallet, tx0.clone());

wallet.insert_tx(tx0.clone());
bdk_wallet::test_utils::insert_tx(wallet, tx0.clone());
insert_anchor_from_conf(
wallet,
tx0.compute_txid(),
ConfirmationTime::Confirmed {
height: 1_000,
time: 100,
ConfirmationBlockTime {
block_id: BlockId {
height: 1_000,
hash: BlockHash::all_zeros(),
},
confirmation_time: 100,
},
);

wallet.insert_tx(tx1.clone());
bdk_wallet::test_utils::insert_tx(wallet, tx1.clone());
insert_anchor_from_conf(
wallet,
tx1.compute_txid(),
ConfirmationTime::Confirmed {
height: 2_000,
time: 200,
ConfirmationBlockTime {
block_id: BlockId {
height: 2_000,
hash: BlockHash::all_zeros(),
},
confirmation_time: 200,
},
);

Expand All @@ -264,29 +276,32 @@ pub fn insert_fake_tx(wallet: &mut Wallet, spent: Amount, change: Amount, fee: A
/// Simulates confirming a tx with `txid` at the specified `position` by inserting an anchor
/// at the lowest height in local chain that is greater or equal to `position`'s height,
/// assuming the confirmation time matches `ConfirmationTime::Confirmed`.
pub fn insert_anchor_from_conf(wallet: &mut Wallet, txid: Txid, position: ConfirmationTime) {
if let ConfirmationTime::Confirmed { height, time } = position {
// anchor tx to checkpoint with lowest height that is >= position's height
let anchor = wallet
.local_chain()
.range(height..)
.last()
.map(|anchor_cp| ConfirmationBlockTime {
block_id: anchor_cp.block_id(),
confirmation_time: time,
})
.expect("confirmation height cannot be greater than tip");

wallet
.apply_update(Update {
tx_update: tx_graph::TxUpdate {
anchors: [(anchor, txid)].into(),
..Default::default()
},
pub fn insert_anchor_from_conf(wallet: &mut Wallet, txid: Txid, position: ConfirmationBlockTime) {
let ConfirmationBlockTime {
block_id,
confirmation_time,
} = position;

// anchor tx to checkpoint with lowest height that is >= position's height
let anchor = wallet
.local_chain()
.range(block_id.height..)
.last()
.map(|anchor_cp| ConfirmationBlockTime {
block_id: anchor_cp.block_id(),
confirmation_time,
})
.expect("confirmation height cannot be greater than tip");

wallet
.apply_update(Update {
tx_update: tx_graph::TxUpdate {
anchors: [(anchor, txid)].into(),
..Default::default()
})
.unwrap();
}
},
..Default::default()
})
.unwrap();
}

#[tracing::instrument]
Expand Down Expand Up @@ -611,7 +626,7 @@ async fn two_wallets_load() -> anyhow::Result<()> {
height: 100,
hash: BlockHash::all_zeros(),
};
let _ = wallet_2.insert_checkpoint(block).unwrap();
let _ = bdk_wallet::test_utils::insert_checkpoint(&mut wallet_2, block);
assert!(wallet_2.persist_async(&mut store_2).await?);

// Recover the wallet_1
Expand Down

0 comments on commit b9b2eb6

Please sign in to comment.