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

When opening, reserve inputs for 2016 blocks. #4510

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions contrib/pyln-client/pyln/client/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,22 +1246,24 @@ def txsend(self, txid):
}
return self.call("txsend", payload)

def reserveinputs(self, psbt, exclusive=True):
def reserveinputs(self, psbt, exclusive=True, reserve=None):
"""
Reserve any inputs in this psbt.
"""
payload = {
"psbt": psbt,
"exclusive": exclusive,
"reserve": reserve,
}
return self.call("reserveinputs", payload)

def unreserveinputs(self, psbt):
def unreserveinputs(self, psbt, reserve=None):
"""
Unreserve (or reduce reservation) on any UTXOs in this psbt were previously reserved.
"""
payload = {
"psbt": psbt,
"reserve": reserve,
}
return self.call("unreserveinputs", payload)

Expand Down Expand Up @@ -1308,12 +1310,13 @@ def signpsbt(self, psbt, signonly=None):
}
return self.call("signpsbt", payload)

def sendpsbt(self, psbt):
def sendpsbt(self, psbt, reserve=None):
"""
Finalize extract and broadcast a PSBT
"""
payload = {
"psbt": psbt,
"reserve": reserve,
}
return self.call("sendpsbt", payload)

Expand Down
12 changes: 7 additions & 5 deletions doc/lightning-fundpsbt.7

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

10 changes: 6 additions & 4 deletions doc/lightning-fundpsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ added any inputs.
*minconf* specifies the minimum number of confirmations that used
outputs should have. Default is 1.

*reserve* is a boolean: if true (the default), then *reserveinputs* is
called (successfully, with *exclusive* true) on the returned PSBT.
*reserve* is either boolean or a number: if *true* or a non-zero
number then *reserveinputs* is called (successfully, with
*exclusive* true) on the returned PSBT for this number of blocks (or
72 blocks if *reserve* is simply *true*).

*locktime* is an optional locktime: if not set, it is set to a recent
block height.
Expand Down Expand Up @@ -77,8 +79,8 @@ available. This could be zero, or dust. If *satoshi* was "all",
then *excess_msat* is the entire amount once fees are subtracted
for the weights of the inputs and startweight.

If *reserve* was true, then a *reservations* array is returned,
exactly like *reserveinputs*.
If *reserve* was *true* or a non-zero number, then a *reservations*
array is returned, exactly like *reserveinputs*.

If *excess_as_change* is true and the excess is enough to cover
an additional output above the `dust_limit`, then an output is
Expand Down
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
8 changes: 6 additions & 2 deletions doc/lightning-reserveinputs.7

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

4 changes: 3 additions & 1 deletion doc/lightning-reserveinputs.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-reserveinputs -- Construct a transaction and reserve the UTXOs it spen
SYNOPSIS
--------

**reserveinputs** *psbt* [*exclusive*]
**reserveinputs** *psbt* [*exclusive*] [*reserve*]

DESCRIPTION
-----------
Expand All @@ -18,6 +18,8 @@ Normally the command will fail (with no reservations made) if an input
is already reserved. If *exclusive* is set to *False*, then existing
reservations are simply extended, rather than causing failure.

By default, reservations are for the next 72 blocks (approximately 6
hours), but this can be changed by setting *reserve*.

RETURN VALUE
------------
Expand Down
6 changes: 4 additions & 2 deletions doc/lightning-sendpsbt.7

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

3 changes: 2 additions & 1 deletion doc/lightning-sendpsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ lightning-sendpsbt -- Command to finalize, extract and send a partially signed b
SYNOPSIS
--------

**sendpsbt** *psbt*
**sendpsbt** *psbt* [*reserve*]

DESCRIPTION
-----------

The **sendpsbt** is a low-level RPC command which sends a fully-signed PSBT.

- *psbt*: A string that represents psbt value.
- *reserve*: an optional number of blocks to increase reservation of any of our inputs by; default is 72.

EXAMPLE JSON REQUEST
--------------------
Expand Down
8 changes: 6 additions & 2 deletions doc/lightning-unreserveinputs.7

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

5 changes: 4 additions & 1 deletion doc/lightning-unreserveinputs.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-unreserveinputs -- Release reserved UTXOs
SYNOPSIS
--------

**unreserveinputs** *psbt*
**unreserveinputs** *psbt* [*reserve*]

DESCRIPTION
-----------
Expand All @@ -15,6 +15,9 @@ lightning-reserveinputs(7).

The inputs to unreserve are the inputs specified in the passed-in *psbt*.

If *reserve* is specified, it is the number of blocks to decrease
reservation by; default is 72.

RETURN VALUE
------------

Expand Down
12 changes: 9 additions & 3 deletions doc/lightning-utxopsbt.7

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

9 changes: 7 additions & 2 deletions doc/lightning-utxopsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ the resulting transaction plus *startweight* at the given *feerate*,
with at least *satoshi* left over (unless *satoshi* is **all**, which
is equivalent to setting it to zero).

*reserve* is either boolean or a number: if *true* or a non-zero
number then *reserveinputs* is called (successfully, with
*exclusive* true) on the returned PSBT for this number of blocks (or
72 blocks if *reserve* is simply *true*).

Unless *reservedok* is set to true (default is false) it will also fail
if any of the *utxos* are already reserved.

Expand All @@ -47,8 +52,8 @@ which is available. This could be zero, or dust. If *satoshi* was "all",
then *excess_msat* is the entire amount once fees are subtracted
for the weights of the inputs and *startweight*.

If *reserve* was true, then a *reservations* array is returned,
exactly like *reserveinputs*.
If *reserve* was *true* or a non-zero number, then a *reservations*
array is returned, exactly like *reserveinputs*.

If *excess_as_change* is true and the excess is enough to cover
an additional output above the `dust_limit`, then an output is
Expand Down
14 changes: 12 additions & 2 deletions plugins/spender/multifundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ mfc_cleanup_psbt(struct command *cmd,
}

json_add_psbt(req->js, "psbt", take(pruned_psbt));
json_add_u32(req->js, "reserve", 2016);
send_outreq(cmd->plugin, req);
}

Expand Down Expand Up @@ -651,6 +652,9 @@ after_signpsbt(struct command *cmd,
&mfc_forward_error,
mfc);
json_add_psbt(req->js, "psbt", mfc->psbt);
/* We already reserved inputs by 2 weeks, we don't need
* another 72 blocks. */
json_add_u32(req->js, "reserve", 0);
return send_outreq(mfc->cmd->plugin, req);
}

Expand Down Expand Up @@ -1405,8 +1409,14 @@ perform_fundpsbt(struct multifundchannel_command *mfc)
json_add_u32(req->js, "minconf", mfc->minconf);
}

/* The entire point is to reserve the inputs. */
json_add_bool(req->js, "reserve", true);
/* The entire point is to reserve the inputs. */
/* BOLT #2:
* The sender:
*...
* - SHOULD ensure the funding transaction confirms in the next 2016
* blocks.
*/
json_add_u32(req->js, "reserve", 2016);
/* How much do we need to reserve? */
if (has_all(mfc))
json_add_string(req->js, "satoshi", "all");
Expand Down
34 changes: 34 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,40 @@ def test_second_channel(node_factory):
l1.fundchannel(l3, 10**6)


def test_channel_abandon(node_factory, bitcoind):
"""Our open tx isn't mined, we doublespend it away"""
l1, l2 = node_factory.get_nodes(2)

SATS = 10**6

# Add some for fees
l1.fundwallet(SATS + 10000)
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
l1.rpc.fundchannel(l2.info['id'], SATS, feerate='1875perkw')

opening_utxo = only_one([o for o in l1.rpc.listfunds()['outputs'] if o['reserved']])
psbt = l1.rpc.utxopsbt(0, "253perkw", 0, [opening_utxo['txid'] + ':' + str(opening_utxo['output'])], reserve=False, reservedok=True)['psbt']

# We expect a reservation for 2016 blocks; unreserve it.
reservations = only_one(l1.rpc.unreserveinputs(psbt, reserve=2015)['reservations'])
assert reservations['reserved']
assert reservations['reserved_to_block'] == bitcoind.rpc.getblockchaininfo()['blocks'] + 1

assert only_one(l1.rpc.unreserveinputs(psbt, reserve=1)['reservations'])['reserved'] is False

# Now it's unreserved, we can doublespend it (as long as we exceed
# previous fee to RBF!).
withdraw = l1.rpc.withdraw(l1.rpc.newaddr()['bech32'], "all")

assert bitcoind.rpc.decoderawtransaction(withdraw['tx'])['vout'][0]['value'] > SATS / 10**8
bitcoind.generate_block(1, wait_for_mempool=withdraw['txid'])

# FIXME: lightningd should notice channel will never now open!
print(l1.rpc.listpeers())
assert (only_one(only_one(l1.rpc.listpeers()['peers'])['channels'])['state']
== 'CHANNELD_AWAITING_LOCKIN')


@pytest.mark.developer
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
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
Loading