Skip to content

Commit

Permalink
refactor(chain): replace unix epoch u64 with bitcoin::absolute::Time
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Mar 13, 2024
1 parent c01983d commit ab2297c
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 140 deletions.
45 changes: 34 additions & 11 deletions crates/bdk/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ mod test {
use core::str::FromStr;

use bdk_chain::ConfirmationTime;
use bitcoin::absolute::{Time, LOCK_TIME_THRESHOLD};
use bitcoin::{OutPoint, ScriptBuf, TxOut};

use super::*;
Expand Down Expand Up @@ -780,13 +781,27 @@ mod test {

fn get_test_utxos() -> Vec<WeightedUtxo> {
vec![
utxo(100_000, 0, ConfirmationTime::Unconfirmed { last_seen: 0 }),
utxo(
100_000,
0,
ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
),
utxo(
FEE_AMOUNT - 40,
1,
ConfirmationTime::Unconfirmed { last_seen: 0 },
ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
),
utxo(
200_000,
2,
ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
),
utxo(200_000, 2, ConfirmationTime::Unconfirmed { last_seen: 0 }),
]
}

Expand All @@ -797,23 +812,23 @@ mod test {
1,
ConfirmationTime::Confirmed {
height: 1,
time: 1231006505,
time: Time::from_consensus(1231006505).unwrap(),
},
);
let utxo2 = utxo(
80_000,
2,
ConfirmationTime::Confirmed {
height: 2,
time: 1231006505,
time: Time::from_consensus(1231006505).unwrap(),
},
);
let utxo3 = utxo(
300_000,
3,
ConfirmationTime::Confirmed {
height: 3,
time: 1231006505,
time: Time::from_consensus(1231006505).unwrap(),
},
);
vec![utxo1, utxo2, utxo3]
Expand All @@ -822,6 +837,8 @@ mod test {
fn generate_random_utxos(rng: &mut StdRng, utxos_number: usize) -> Vec<WeightedUtxo> {
let mut res = Vec::new();
for i in 0..utxos_number {
let random_time =
Time::from_consensus(rng.gen_range(LOCK_TIME_THRESHOLD..=u32::MAX)).unwrap();
res.push(WeightedUtxo {
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
utxo: Utxo::Local(LocalOutput {
Expand All @@ -840,10 +857,12 @@ mod test {
confirmation_time: if rng.gen_bool(0.5) {
ConfirmationTime::Confirmed {
height: rng.next_u32(),
time: rng.next_u64(),
time: random_time,
}
} else {
ConfirmationTime::Unconfirmed { last_seen: 0 }
ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
}
},
}),
});
Expand All @@ -868,7 +887,9 @@ mod test {
keychain: KeychainKind::External,
is_spent: false,
derivation_index: 42,
confirmation_time: ConfirmationTime::Unconfirmed { last_seen: 0 },
confirmation_time: ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
}),
})
.collect()
Expand Down Expand Up @@ -1159,7 +1180,9 @@ mod test {
optional.push(utxo(
500_000,
3,
ConfirmationTime::Unconfirmed { last_seen: 0 },
ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
));

// Defensive assertions, for sanity and in case someone changes the test utxos vector.
Expand Down Expand Up @@ -1520,7 +1543,7 @@ mod test {
derivation_index: 0,
confirmation_time: ConfirmationTime::Confirmed {
height: 12345,
time: 12345,
time: Time::from_consensus(LOCK_TIME_THRESHOLD + 12345).unwrap(),
},
}),
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bdk/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ mod test {
use core::str::FromStr;

use bdk_chain::{BlockId, ConfirmationTime};
use bitcoin::absolute::Time;
use bitcoin::hashes::Hash;
use bitcoin::{BlockHash, Network, Transaction};

Expand Down Expand Up @@ -244,7 +245,7 @@ mod test {
transaction,
ConfirmationTime::Confirmed {
height: 5000,
time: 0,
time: Time::MIN,
},
)
.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@ impl<D> Wallet<D> {
/// `last_seen` is prioritized.
pub fn apply_unconfirmed_txs<'t>(
&mut self,
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, absolute::Time)>,
) where
D: PersistBackend<ChangeSet>,
{
Expand Down Expand Up @@ -2572,6 +2572,7 @@ fn create_signers<E: IntoWalletDescriptor>(
macro_rules! doctest_wallet {
() => {{
use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
use $crate::bitcoin::absolute::{LOCK_TIME_THRESHOLD, Time};
use $crate::chain::{ConfirmationTime, BlockId};
use $crate::wallet::{AddressIndex, Wallet};
let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)";
Expand All @@ -2596,7 +2597,7 @@ macro_rules! doctest_wallet {
let _ = wallet.insert_checkpoint(BlockId { height: 1_000, hash: BlockHash::all_zeros() });
let _ = wallet.insert_tx(tx.clone(), ConfirmationTime::Confirmed {
height: 500,
time: 50_000
time: Time::from_consensus(LOCK_TIME_THRESHOLD+50_000).unwrap(),
});

wallet
Expand Down
7 changes: 5 additions & 2 deletions crates/bdk/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ mod test {
}

use bdk_chain::ConfirmationTime;
use bitcoin::absolute::{Time, LOCK_TIME_THRESHOLD};
use bitcoin::consensus::deserialize;
use bitcoin::hashes::hex::FromHex;

Expand Down Expand Up @@ -1023,7 +1024,9 @@ mod test {
txout: Default::default(),
keychain: KeychainKind::External,
is_spent: false,
confirmation_time: ConfirmationTime::Unconfirmed { last_seen: 0 },
confirmation_time: ConfirmationTime::Unconfirmed {
last_seen: Time::MIN,
},
derivation_index: 0,
},
LocalOutput {
Expand All @@ -1036,7 +1039,7 @@ mod test {
is_spent: false,
confirmation_time: ConfirmationTime::Confirmed {
height: 32,
time: 42,
time: Time::from_consensus(LOCK_TIME_THRESHOLD + 42).unwrap(),
},
derivation_index: 1,
},
Expand Down
5 changes: 3 additions & 2 deletions crates/bdk/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use bdk::{wallet::AddressIndex, KeychainKind, LocalOutput, Wallet};
use bdk_chain::indexed_tx_graph::Indexer;
use bdk_chain::{BlockId, ConfirmationTime};
use bitcoin::absolute::{Time, LOCK_TIME_THRESHOLD};
use bitcoin::hashes::Hash;
use bitcoin::{Address, BlockHash, Network, OutPoint, Transaction, TxIn, TxOut, Txid};
use std::str::FromStr;
Expand Down Expand Up @@ -82,7 +83,7 @@ pub fn get_funded_wallet_with_change(
tx0,
ConfirmationTime::Confirmed {
height: 1_000,
time: 100,
time: Time::from_consensus(LOCK_TIME_THRESHOLD + 100).unwrap(),
},
)
.unwrap();
Expand All @@ -91,7 +92,7 @@ pub fn get_funded_wallet_with_change(
tx1.clone(),
ConfirmationTime::Confirmed {
height: 2_000,
time: 200,
time: Time::from_consensus(LOCK_TIME_THRESHOLD + 200).unwrap(),
},
)
.unwrap();
Expand Down
Loading

0 comments on commit ab2297c

Please sign in to comment.