Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for PR 6657: fix fundchannel infinite loop with tiny UTXOs #6669

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/test_opening.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,3 +2418,17 @@ def test_anchor_min_emergency(bitcoind, node_factory):
l1.rpc.withdraw(addr2, 'all')
bitcoind.generate_block(1, wait_for_mempool=1)
wait_for(lambda: l1.rpc.listfunds()['outputs'] == [])


def test_fundchannel_utxo_too_small(bitcoind, node_factory):
l1, l2 = node_factory.get_nodes(2)

# Add 1 600 sat UTXO to a fresh node
bitcoind.rpc.sendtoaddress(l1.rpc.newaddr()['bech32'], 0.00000600)
bitcoind.generate_block(1, wait_for_mempool=1)
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)

# a higher fee rate, making the 600 sat UTXO uneconomical:
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
with pytest.raises(RpcError, match=r'Could not afford 100000sat using all 0 available UTXOs'):
l1.rpc.fundchannel(l2.info['id'], 100000, 10000)
10 changes: 9 additions & 1 deletion wallet/reservation.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
const jsmntok_t *params)
{
struct utxo **utxos;
const struct utxo **excluded;
u32 *feerate_per_kw;
u32 *minconf, *weight, *min_witness_weight;
struct amount_sat *amount, input, diff, change;
Expand Down Expand Up @@ -537,6 +538,9 @@ static struct command_result *json_fundpsbt(struct command *cmd,
/* We keep adding until we meet their output requirements. */
utxos = tal_arr(cmd, struct utxo *, 0);

/* Either uneconomical at this feerate, or already included. */
excluded = tal_arr(cmd, const struct utxo *, 0);

input = AMOUNT_SAT(0);
while (!inputs_sufficient(input, *amount, *feerate_per_kw, *weight,
&diff)) {
Expand All @@ -550,8 +554,10 @@ static struct command_result *json_fundpsbt(struct command *cmd,
*feerate_per_kw,
maxheight,
*nonwrapped,
cast_const2(const struct utxo **, utxos));
excluded);

if (utxo) {
tal_arr_expand(&excluded, utxo);
utxo_weight = utxo_spend_weight(utxo,
*min_witness_weight);
fee = amount_tx_fee(*feerate_per_kw, utxo_weight);
Expand Down Expand Up @@ -596,6 +602,8 @@ static struct command_result *json_fundpsbt(struct command *cmd,
&diff));
}

tal_free(excluded);

if (all) {
/* We need to afford one non-dust output, at least. */
if (!inputs_sufficient(input, AMOUNT_SAT(0),
Expand Down