Skip to content

Commit

Permalink
txprepare: don't crash if we are passed unconfirmed utxos
Browse files Browse the repository at this point in the history
Changelog-added: `txprepare` doesn't crash lightningd anymore if you pass unconfirmed utxos
  • Loading branch information
darosior authored and cdecker committed Feb 21, 2020
1 parent 9e9e23c commit cd15cec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 9 additions & 5 deletions common/wallet_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct command_result *wtx_from_utxos(struct wallet_tx *tx,
/* + segwit marker + flag */
weight = 4 * (4 + 1 + 1 + 4) + 4 * (8 + 1 + out_len) + 1 + 1;
for (size_t i = 0; i < tal_count(utxos); i++) {
if (!*utxos[i]->blockheight || *utxos[i]->blockheight > maxheight) {
if (!utxos[i]->blockheight || *utxos[i]->blockheight > maxheight) {
tal_arr_remove(&utxos, i);
continue;
}
Expand Down Expand Up @@ -236,17 +236,21 @@ struct command_result *wtx_from_utxos(struct wallet_tx *tx,
tx->amount = total_amount;
if (!amount_sat_sub(&tx->amount, tx->amount, fee_estimate))
return command_fail(tx->cmd, FUND_CANNOT_AFFORD,
"Cannot afford transaction with %s sats of fees",
type_to_string(tmpctx, struct amount_sat,
&fee_estimate));
"Cannot afford transaction with %s"
" sats of fees, make sure to use "
"confirmed utxos.",
type_to_string(tmpctx, struct amount_sat,
&fee_estimate));
} else {
if (!amount_sat_sub(&tx->change, tx->change, fee_estimate)) {
/* Try again without a change output */
weight -= (8 + 1 + out_len) * 4;
fee_estimate = amount_tx_fee(fee_rate_per_kw, weight);
if (!amount_sat_sub(&tx->change, tx->change, fee_estimate))
return command_fail(tx->cmd, FUND_CANNOT_AFFORD,
"Cannot afford transaction with %s sats of fees",
"Cannot afford transaction with %s"
" sats of fees, make sure to use "
"confirmed utxos.",
type_to_string(tmpctx, struct amount_sat,
&fee_estimate));
tx->change = AMOUNT_SAT(0);
Expand Down
8 changes: 5 additions & 3 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ def test_txprepare_multi(node_factory, bitcoind):
l1.rpc.txdiscard(prep['txid'])


@pytest.mark.xfail(strict=True)
def test_txprepare(node_factory, bitcoind, chainparams):
amount = 1000000
l1 = node_factory.get_node(random_hsm=True)
Expand Down Expand Up @@ -374,8 +373,11 @@ def test_txprepare(node_factory, bitcoind, chainparams):

# Try passing unconfirmed utxos
unconfirmed_utxo = l1.rpc.withdraw(l1.rpc.newaddr()["bech32"], 10**5)
utxos = [unconfirmed_utxo["txid"] + ":0"]
l1.rpc.txprepare([{addr: Millisatoshi(amount * 3.5 * 1000)}], utxos=utxos)
uutxos = [unconfirmed_utxo["txid"] + ":0"]
with pytest.raises(RpcError, match=r"Cannot afford transaction .* use "
"confirmed utxos."):
l1.rpc.txprepare([{addr: Millisatoshi(amount * 3.5 * 1000)}],
utxos=uutxos)

decode = bitcoind.rpc.decoderawtransaction(prep5['unsigned_tx'])
assert decode['txid'] == prep5['txid']
Expand Down

0 comments on commit cd15cec

Please sign in to comment.