Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub enum CreateTxError {
Version0,
/// Requested transaction version `1`, but at least `2` is needed to use OP_CSV
Version1Csv,
/// The synced chain tip height cannot be represented as a block-based locktime
InvalidChainTipHeight(u32),
/// Requested `LockTime` is less than is required to spend from this script
LockTime {
/// Requested `LockTime`
Expand Down Expand Up @@ -236,6 +238,9 @@ impl fmt::Display for CreateTxError {
"TxBuilder requested version `1`, but at least `2` is needed to use OP_CSV"
)
}
CreateTxError::InvalidChainTipHeight(height) => {
write!(f, "Invalid chain tip height: {height}")
}
CreateTxError::LockTime {
requested,
required,
Expand Down
19 changes: 11 additions & 8 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,14 +1369,17 @@ impl Wallet {
None => transaction::Version::TWO,
};

// We use a match here instead of a unwrap_or_else as it's way more readable :)
let current_height = match params.current_height {
// If they didn't tell us the current height, we assume it's the latest sync height.
// If they didn't tell us the current height, we assume it's the latest sync height.
// Keep the raw height for coinbase maturity, since not every valid chain height can be
// represented as a block-based nLockTime.
let (current_height, current_locktime) = match params.current_height {
Some(locktime) => (locktime.to_consensus_u32(), locktime),
None => {
let tip_height = self.chain.tip().height();
absolute::LockTime::from_height(tip_height).expect("invalid height")
let height = self.chain.tip().height();
let locktime = absolute::LockTime::from_height(height)
.map_err(|_| CreateTxError::InvalidChainTipHeight(height))?;
(height, locktime)
}
Some(h) => h,
};

let lock_time = match params.locktime {
Expand All @@ -1385,7 +1388,7 @@ impl Wallet {
// Fee sniping can be partially prevented by setting the timelock
// to current_height. If we don't know the current_height,
// we default to 0.
let fee_sniping_height = current_height;
let fee_sniping_height = current_locktime;

// We choose the biggest between the required nlocktime and the fee sniping
// height.
Expand Down Expand Up @@ -1498,7 +1501,7 @@ impl Wallet {
let (required_utxos, optional_utxos) = {
// NOTE: manual selection overrides unspendable
let mut required: Vec<WeightedUtxo> = params.utxos.clone();
let optional = self.filter_utxos(&params, current_height.to_consensus_u32(), version);
let optional = self.filter_utxos(&params, current_height, version);

// If `drain_wallet` is true, all UTxOs are required.
if params.drain_wallet {
Expand Down
21 changes: 21 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@ fn test_create_tx_default_locktime_is_last_sync_height() {
assert_eq!(psbt.unsigned_tx.lock_time.to_consensus_u32(), 2_000);
}

#[test]
fn test_create_tx_does_not_panic_for_invalid_chain_tip_height() {
let (mut wallet, _) = get_funded_wallet_wpkh();
insert_checkpoint(
&mut wallet,
BlockId {
height: 500_000_000,
hash: BlockHash::all_zeros(),
},
);

let addr = wallet.next_unused_address(KeychainKind::External);
let mut builder = wallet.build_tx();
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000));

assert_matches!(
builder.finish(),
Err(CreateTxError::InvalidChainTipHeight(500_000_000))
);
}

#[test]
fn test_create_tx_fee_sniping_locktime_last_sync() {
let (mut wallet, _) = get_funded_wallet_wpkh();
Expand Down
Loading