Skip to content

Commit b8a0b5e

Browse files
committed
fix(wallet)!: make TxBuilder::current_height type-safe via absolute::Height
1 parent fbf803a commit b8a0b5e

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/wallet/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,11 @@ impl Wallet {
13051305
// If they didn't tell us the current height, we assume it's the latest sync height.
13061306
None => {
13071307
let tip_height = self.chain.tip().height();
1308-
absolute::LockTime::from_height(tip_height).expect("invalid height")
1308+
// If the local chain tip height is not a valid block height for a locktime,
1309+
// that's an invariant violation in `LocalChain` (block heights are
1310+
// always below the locktime threshold) and should be addressed upstream.
1311+
absolute::LockTime::from_height(tip_height)
1312+
.expect("LocalChain tip height should be a valid block height")
13091313
}
13101314
Some(h) => h,
13111315
};

src/wallet/tx_builder.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,12 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
649649
/// them using [`TxBuilder::add_utxos`].
650650
///
651651
/// In both cases, if you don't provide a current height, we use the last sync height.
652-
pub fn current_height(&mut self, height: u32) -> &mut Self {
653-
self.params.current_height =
654-
Some(absolute::LockTime::from_height(height).expect("Invalid height"));
652+
///
653+
/// This method takes an [`absolute::Height`], which is guaranteed by the type system to
654+
/// be a valid block height for a locktime (below 500_000_000). Callers can construct one
655+
/// with [`absolute::Height::from_consensus`].
656+
pub fn current_height(&mut self, height: absolute::Height) -> &mut Self {
657+
self.params.current_height = Some(absolute::LockTime::from(height));
655658
self
656659
}
657660

tests/wallet.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn test_create_tx_custom_locktime() {
298298
let mut builder = wallet.build_tx();
299299
builder
300300
.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000))
301-
.current_height(630_001)
301+
.current_height(absolute::Height::from_consensus(630_001).unwrap())
302302
.nlocktime(absolute::LockTime::from_height(630_000).unwrap());
303303
let psbt = builder.finish().unwrap();
304304

@@ -2537,7 +2537,7 @@ fn test_spend_coinbase() {
25372537
let mut builder = wallet.build_tx();
25382538
builder
25392539
.add_recipient(addr.script_pubkey(), balance.immature / 2)
2540-
.current_height(confirmation_height);
2540+
.current_height(absolute::Height::from_consensus(confirmation_height).unwrap());
25412541
assert!(matches!(
25422542
builder.finish(),
25432543
Err(CreateTxError::CoinSelection(
@@ -2552,7 +2552,7 @@ fn test_spend_coinbase() {
25522552
let mut builder = wallet.build_tx();
25532553
builder
25542554
.add_recipient(addr.script_pubkey(), balance.immature / 2)
2555-
.current_height(not_yet_mature_time);
2555+
.current_height(absolute::Height::from_consensus(not_yet_mature_time).unwrap());
25562556
assert_matches!(
25572557
builder.finish(),
25582558
Err(CreateTxError::CoinSelection(
@@ -2583,7 +2583,7 @@ fn test_spend_coinbase() {
25832583
let mut builder = wallet.build_tx();
25842584
builder
25852585
.add_recipient(addr.script_pubkey(), balance.confirmed / 2)
2586-
.current_height(maturity_time);
2586+
.current_height(absolute::Height::from_consensus(maturity_time).unwrap());
25872587
builder.finish().unwrap();
25882588
}
25892589

0 commit comments

Comments
 (0)