Skip to content

Commit

Permalink
wallet: add "reserved_to_block" field to listfunds.
Browse files Browse the repository at this point in the history
We already have this field in reserveinputs and unreserveinputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: JSON-RPC: `listfunds` has a new `reserved_to_block` field.
  • Loading branch information
rustyrussell committed May 26, 2021
1 parent 94419c7 commit f24dc91
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/lightning-listfunds.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions doc/lightning-listfunds.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Each entry in *outputs* will include:
- *redeemscript* (the redeemscript of the output, in hex, only if it's p2sh-wrapped)
- *status* (whether *unconfirmed*, *confirmed*, or *spent*)
- *reserved* (whether this is UTXO is currently reserved for an in-flight tx)
- *reserved_to_block* (when reservation expires, if *reserved* is true)

Each entry in *channels* will include:
- *peer\_id* - the peer with which the channel is opened.
Expand Down
1 change: 1 addition & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ def dont_spend_outputs(n, txid):
for out in l1.rpc.listfunds()['outputs']:
if out['reserved']:
inputs += [{'txid': out['txid'], 'vout': out['output']}]
assert out['reserved_to_block'] > bitcoind.rpc.getblockchaininfo()['blocks']
l1.rpc.unreserveinputs(bitcoind.rpc.createpsbt(inputs, []))

# Test withdrawal to self.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,27 +441,34 @@ def test_reserveinputs(node_factory, bitcoind, chainparams):
l1.rpc.reserveinputs(psbt)

assert all(o['reserved'] for o in l1.rpc.listfunds()['outputs'])
reserveheight = bitcoind.rpc.getblockchaininfo()['blocks'] + 72
assert all(o['reserved_to_block'] == reserveheight for o in l1.rpc.listfunds()['outputs'])

# Unreserve as a batch.
psbt = bitcoind.rpc.createpsbt([{'txid': out[0], 'vout': out[1]} for out in outputs], [])
l1.rpc.unreserveinputs(psbt)
assert not any(o['reserved'] for o in l1.rpc.listfunds()['outputs'])
assert not any('reserved_to_block' in o for o in l1.rpc.listfunds()['outputs'])

# Reserve twice fails unless exclusive.
l1.rpc.reserveinputs(psbt)
with pytest.raises(RpcError, match=r"already reserved"):
l1.rpc.reserveinputs(psbt)
l1.rpc.reserveinputs(psbt, False)
assert all(o['reserved_to_block'] == reserveheight + 72 for o in l1.rpc.listfunds()['outputs'])
l1.rpc.unreserveinputs(psbt)
assert all(o['reserved'] for o in l1.rpc.listfunds()['outputs'])
assert all(o['reserved_to_block'] == reserveheight for o in l1.rpc.listfunds()['outputs'])

# Stays reserved across restarts.
l1.restart()
assert all(o['reserved'] for o in l1.rpc.listfunds()['outputs'])
assert all(o['reserved_to_block'] == reserveheight for o in l1.rpc.listfunds()['outputs'])

# Final unreserve works.
l1.rpc.unreserveinputs(psbt)
assert not any(o['reserved'] for o in l1.rpc.listfunds()['outputs'])
assert not any('reserved_to_block' in o for o in l1.rpc.listfunds()['outputs'])


def test_fundpsbt(node_factory, bitcoind, chainparams):
Expand Down
10 changes: 7 additions & 3 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ static void json_add_utxo(struct json_stream *response,
const struct utxo *utxo)
{
const char *out;
bool reserved;

json_object_start(response, fieldname);
json_add_txid(response, "txid", &utxo->txid);
Expand Down Expand Up @@ -284,9 +285,12 @@ static void json_add_utxo(struct json_stream *response,
} else
json_add_string(response, "status", "unconfirmed");

json_add_bool(response, "reserved",
utxo_is_reserved(utxo,
get_block_height(wallet->ld->topology)));
reserved = utxo_is_reserved(utxo,
get_block_height(wallet->ld->topology));
json_add_bool(response, "reserved", reserved);
if (reserved)
json_add_num(response, "reserved_to_block",
utxo->reserved_til);
json_object_end(response);
}

Expand Down

0 comments on commit f24dc91

Please sign in to comment.