Skip to content

Commit 2bff4e5

Browse files
committed
Merge #726: [bug-fix] Set the db sync height
08668ac Set the db sync height (rajarshimaitra) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description Fixes #719 Previously we weren't setting the db sync height in populate_test_db macro even when current height is provided.. This creates a bug that get_funded_wallet will return 0 balance. This PR fixes the populate_test_db macro and updates tests which were previously dependent on the unsynced wallet behavior. ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [x] I've added tests to reproduce the issue which are now passing * [x] I'm linking the issue being fixed by this PR ACKs for top commit: afilini: ACK 08668ac Tree-SHA512: 1dcc968e4b3551e916b450c5ff2fab6636083f104cc982eb3f7602c624382434e0170d9f0c0a356e6c9c5f834eebe5cb1365b37ef73d7b4ef15d652a364dc2ab
2 parents 12507c7 + 08668ac commit 2bff4e5

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/database/memory.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ macro_rules! populate_test_db {
486486
}};
487487
($db:expr, $tx_meta:expr, $current_height:expr, (@coinbase $is_coinbase:expr)$(,)?) => {{
488488
use std::str::FromStr;
489-
use $crate::database::BatchOperations;
489+
use $crate::database::SyncTime;
490+
use $crate::database::{BatchOperations, Database};
490491
let mut db = $db;
491492
let tx_meta = $tx_meta;
492493
let current_height: Option<u32> = $current_height;
@@ -512,14 +513,32 @@ macro_rules! populate_test_db {
512513
};
513514

514515
let txid = tx.txid();
516+
// Set Confirmation time only if current height is provided.
517+
// panics if `tx_meta.min_confirmation` is Some, and current_height is None.
515518
let confirmation_time = tx_meta
516519
.min_confirmations
517520
.and_then(|v| if v == 0 { None } else { Some(v) })
518521
.map(|conf| $crate::BlockTime {
519-
height: current_height.unwrap().checked_sub(conf as u32).unwrap() + 1,
522+
height: current_height.expect("Current height is needed for testing transaction with min-confirmation values").checked_sub(conf as u32).unwrap() + 1,
520523
timestamp: 0,
521524
});
522525

526+
// Set the database sync_time.
527+
// Check if the current_height is less than already known sync height, apply the max
528+
// If any of them is None, the other will be applied instead.
529+
// If both are None, this will not be set.
530+
if let Some(height) = db.get_sync_time().unwrap()
531+
.map(|sync_time| sync_time.block_time.height)
532+
.max(current_height) {
533+
let sync_time = SyncTime {
534+
block_time: BlockTime {
535+
height,
536+
timestamp: 0
537+
}
538+
};
539+
db.set_sync_time(sync_time).unwrap();
540+
}
541+
523542
let tx_details = $crate::TransactionDetails {
524543
transaction: Some(tx.clone()),
525544
txid,

src/wallet/mod.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,12 @@ pub(crate) mod test {
19261926
// OP_PUSH.
19271927
const P2WPKH_FAKE_WITNESS_SIZE: usize = 106;
19281928

1929+
#[test]
1930+
fn test_get_funded_wallet_balance() {
1931+
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
1932+
assert_eq!(wallet.get_balance().unwrap().confirmed, 50000);
1933+
}
1934+
19291935
#[test]
19301936
fn test_cache_addresses_fixed() {
19311937
let db = MemoryDatabase::new();
@@ -2173,7 +2179,22 @@ pub(crate) mod test {
21732179

21742180
#[test]
21752181
fn test_create_tx_default_locktime() {
2176-
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
2182+
let descriptors = testutils!(@descriptors (get_test_wpkh()));
2183+
let wallet = Wallet::new(
2184+
&descriptors.0,
2185+
None,
2186+
Network::Regtest,
2187+
AnyDatabase::Memory(MemoryDatabase::new()),
2188+
)
2189+
.unwrap();
2190+
2191+
let tx_meta = testutils! {
2192+
@tx ( (@external descriptors, 0) => 50_000 )
2193+
};
2194+
2195+
// Add the transaction to our db, but do not sync the db.
2196+
crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, None);
2197+
21772198
let addr = wallet.get_address(New).unwrap();
21782199
let mut builder = wallet.build_tx();
21792200
builder.add_recipient(addr.script_pubkey(), 25_000);
@@ -2364,7 +2385,23 @@ pub(crate) mod test {
23642385

23652386
#[test]
23662387
fn test_create_tx_default_sequence() {
2367-
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
2388+
let descriptors = testutils!(@descriptors (get_test_wpkh()));
2389+
let wallet = Wallet::new(
2390+
&descriptors.0,
2391+
None,
2392+
Network::Regtest,
2393+
AnyDatabase::Memory(MemoryDatabase::new()),
2394+
)
2395+
.unwrap();
2396+
2397+
let tx_meta = testutils! {
2398+
@tx ( (@external descriptors, 0) => 50_000 )
2399+
};
2400+
2401+
// Add the transaction to our db, but do not sync the db. Unsynced db
2402+
// should trigger the default sequence value for a new transaction as 0xFFFFFFFF
2403+
crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, None);
2404+
23682405
let addr = wallet.get_address(New).unwrap();
23692406
let mut builder = wallet.build_tx();
23702407
builder.add_recipient(addr.script_pubkey(), 25_000);
@@ -2862,7 +2899,22 @@ pub(crate) mod test {
28622899

28632900
#[test]
28642901
fn test_create_tx_policy_path_no_csv() {
2865-
let (wallet, _, _) = get_funded_wallet(get_test_a_or_b_plus_csv());
2902+
let descriptors = testutils!(@descriptors (get_test_wpkh()));
2903+
let wallet = Wallet::new(
2904+
&descriptors.0,
2905+
None,
2906+
Network::Regtest,
2907+
AnyDatabase::Memory(MemoryDatabase::new()),
2908+
)
2909+
.unwrap();
2910+
2911+
let tx_meta = testutils! {
2912+
@tx ( (@external descriptors, 0) => 50_000 )
2913+
};
2914+
2915+
// Add the transaction to our db, but do not sync the db. Unsynced db
2916+
// should trigger the default sequence value for a new transaction as 0xFFFFFFFF
2917+
crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, None);
28662918

28672919
let external_policy = wallet.policies(KeychainKind::External).unwrap().unwrap();
28682920
let root_id = external_policy.id;

0 commit comments

Comments
 (0)