2018-08-02 17:10:01 +02:00
from fixtures import * # noqa: F401,F403
2019-09-18 21:55:41 +02:00
from fixtures import TEST_NETWORK
2020-08-24 13:42:06 +02:00
from pyln . client import RpcError , Millisatoshi
2022-01-30 04:37:23 +01:00
from utils import only_one , wait_for , wait_channel_quiescent , mine_funding_to_announce
2018-08-02 17:10:01 +02:00
import pytest
import time
import unittest
2019-05-09 21:28:21 +02:00
def test_invoice ( node_factory , chainparams ) :
2018-08-02 17:10:01 +02:00
l1 , l2 = node_factory . line_graph ( 2 , fundchannel = False )
2019-03-04 04:13:49 +01:00
addr1 = l2 . rpc . newaddr ( ' bech32 ' ) [ ' bech32 ' ]
addr2 = l2 . rpc . newaddr ( ' p2sh-segwit ' ) [ ' p2sh-segwit ' ]
2018-08-02 17:10:01 +02:00
before = int ( time . time ( ) )
2022-04-01 06:13:33 +02:00
inv = l1 . rpc . invoice ( 123000 , ' label ' , ' description ' , 3700 , [ addr1 , addr2 ] )
2018-08-02 17:10:01 +02:00
after = int ( time . time ( ) )
b11 = l1 . rpc . decodepay ( inv [ ' bolt11 ' ] )
2019-05-09 21:28:21 +02:00
assert b11 [ ' currency ' ] == chainparams [ ' bip173_prefix ' ]
2018-08-02 17:10:01 +02:00
assert b11 [ ' created_at ' ] > = before
assert b11 [ ' created_at ' ] < = after
assert b11 [ ' payment_hash ' ] == inv [ ' payment_hash ' ]
assert b11 [ ' description ' ] == ' description '
assert b11 [ ' expiry ' ] == 3700
assert b11 [ ' payee ' ] == l1 . info [ ' id ' ]
assert len ( b11 [ ' fallbacks ' ] ) == 2
assert b11 [ ' fallbacks ' ] [ 0 ] [ ' addr ' ] == addr1
assert b11 [ ' fallbacks ' ] [ 0 ] [ ' type ' ] == ' P2WPKH '
assert b11 [ ' fallbacks ' ] [ 1 ] [ ' addr ' ] == addr2
assert b11 [ ' fallbacks ' ] [ 1 ] [ ' type ' ] == ' P2SH '
2021-01-08 05:43:50 +01:00
assert b11 [ ' min_final_cltv_expiry ' ] == 5
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# There's no incoming channel, so no routeboost
assert ' routes ' not in b11
assert ' warning_capacity ' in inv
2018-08-02 17:10:01 +02:00
# Check pay_index is null
outputs = l1 . db_query ( ' SELECT pay_index IS NULL AS q FROM invoices WHERE label= " label " ; ' )
assert len ( outputs ) == 1 and outputs [ 0 ] [ ' q ' ] != 0
# Check any-amount invoice
inv = l1 . rpc . invoice ( " any " , ' label2 ' , ' description2 ' )
b11 = inv [ ' bolt11 ' ]
# Amount usually comes after currency (bcrt in our case),
# but an any-amount invoices will have no amount
2019-05-12 21:27:06 +02:00
assert b11 . startswith ( " ln " + chainparams [ ' bip173_prefix ' ] )
2018-08-02 17:10:01 +02:00
# By bech32 rules, the last '1' digit is the separator
# between the human-readable and data parts. We want
# to match the "lnbcrt1" above with the '1' digit as the
# separator, and not for example "lnbcrt1m1....".
assert b11 . count ( ' 1 ' ) == 1
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# There's no incoming channel, so no routeboost
assert ' routes ' not in b11
assert ' warning_capacity ' in inv
2018-08-02 17:10:01 +02:00
2021-01-08 05:43:50 +01:00
# Test cltv option.
2022-04-01 06:13:33 +02:00
inv = l1 . rpc . invoice ( 123000 , ' label3 ' , ' description ' , 3700 , cltv = 99 )
2021-01-08 05:43:50 +01:00
b11 = l1 . rpc . decodepay ( inv [ ' bolt11 ' ] )
assert b11 [ ' min_final_cltv_expiry ' ] == 99
2018-08-02 17:10:01 +02:00
2020-08-25 03:05:46 +02:00
def test_invoice_zeroval ( node_factory ) :
""" A zero value invoice is unpayable, did you mean ' any ' ? """
l1 = node_factory . get_node ( )
2020-08-25 23:20:50 +02:00
with pytest . raises ( RpcError , match = r " positive .*: invalid token ' 0 ' " ) :
2020-08-25 03:05:46 +02:00
l1 . rpc . invoice ( 0 , ' inv ' , ' ? ' )
2020-08-25 23:20:50 +02:00
with pytest . raises ( RpcError , match = r " positive .*: invalid token .*0msat " ) :
2020-08-25 03:05:46 +02:00
l1 . rpc . invoice ( ' 0msat ' , ' inv ' , ' ? ' )
2020-08-25 23:20:50 +02:00
with pytest . raises ( RpcError , match = r " positive .*: invalid token .*0sat " ) :
2020-08-25 03:05:46 +02:00
l1 . rpc . invoice ( ' 0sat ' , ' inv ' , ' ? ' )
2020-08-25 23:20:50 +02:00
with pytest . raises ( RpcError , match = r " positive .*: invalid token .*0.00000000btc " ) :
2020-08-25 03:05:46 +02:00
l1 . rpc . invoice ( ' 0.00000000btc ' , ' inv ' , ' ? ' )
2020-08-25 23:20:50 +02:00
with pytest . raises ( RpcError , match = r " positive .*: invalid token .*0.00000000000btc " ) :
2020-08-25 03:05:46 +02:00
l1 . rpc . invoice ( ' 0.00000000000btc ' , ' inv ' , ' ? ' )
2018-08-02 17:10:01 +02:00
def test_invoice_weirdstring ( node_factory ) :
l1 = node_factory . get_node ( )
weird_label = ' label \\ " \t \n '
weird_desc = ' description \\ " \t \n '
l1 . rpc . invoice ( 123000 , weird_label , weird_desc )
# FIXME: invoice RPC should return label!
# Can find by this label.
inv = only_one ( l1 . rpc . listinvoices ( weird_label ) [ ' invoices ' ] )
assert inv [ ' label ' ] == weird_label
# Can find this in list.
inv = only_one ( l1 . rpc . listinvoices ( ) [ ' invoices ' ] )
assert inv [ ' label ' ] == weird_label
b11 = l1 . rpc . decodepay ( inv [ ' bolt11 ' ] )
assert b11 [ ' description ' ] == weird_desc
# Can delete by weird label.
l1 . rpc . delinvoice ( weird_label , " unpaid " )
# We can also use numbers as labels.
weird_label = 25
weird_desc = ' " '
l1 . rpc . invoice ( 123000 , weird_label , weird_desc )
# FIXME: invoice RPC should return label!
# Can find by this label.
inv = only_one ( l1 . rpc . listinvoices ( weird_label ) [ ' invoices ' ] )
assert inv [ ' label ' ] == str ( weird_label )
# Can find this in list.
inv = only_one ( l1 . rpc . listinvoices ( ) [ ' invoices ' ] )
assert inv [ ' label ' ] == str ( weird_label )
b11 = l1 . rpc . decodepay ( inv [ ' bolt11 ' ] )
assert b11 [ ' description ' ] == weird_desc
# Can delete by weird label.
l1 . rpc . delinvoice ( weird_label , " unpaid " )
def test_invoice_preimage ( node_factory ) :
""" Test explicit invoice ' preimage ' .
"""
2018-12-08 00:27:14 +01:00
l1 , l2 = node_factory . line_graph ( 2 , wait_for_announce = True )
2018-08-02 17:10:01 +02:00
# I promise the below number is randomly generated
invoice_preimage = " 17b08f669513b7379728fc1abcea5eaf3448bc1eba55a68ca2cd1843409cdc04 "
# Make invoice and pay it
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv " , description = " ? " , preimage = invoice_preimage )
2018-08-02 17:10:01 +02:00
payment = l1 . rpc . pay ( inv [ ' bolt11 ' ] )
# Check preimage was given.
payment_preimage = payment [ ' payment_preimage ' ]
assert invoice_preimage == payment_preimage
# Creating a new invoice with same preimage should error.
with pytest . raises ( RpcError , match = r ' preimage already used ' ) :
l2 . rpc . invoice ( 123456 , ' inv2 ' , ' ? ' , preimage = invoice_preimage )
2021-04-26 21:58:58 +02:00
@pytest.mark.developer ( " gossip without DEVELOPER=1 is slow " )
2019-09-18 21:55:41 +02:00
@unittest.skipIf ( TEST_NETWORK != ' regtest ' , " Amounts too low, dominated by fees in elements " )
2018-10-15 06:53:55 +02:00
def test_invoice_routeboost ( node_factory , bitcoind ) :
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
""" Test routeboost ' r ' hint in bolt11 invoice.
"""
2020-02-11 11:14:38 +01:00
l0 , l1 , l2 = node_factory . line_graph ( 3 , fundamount = 2 * ( 10 * * 5 ) , wait_for_announce = True )
2018-10-15 06:53:55 +02:00
# Check routeboost.
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# Make invoice and pay it
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv1 " , description = " ? " )
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# Check routeboost.
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
2019-01-15 04:57:27 +01:00
assert r [ ' short_channel_id ' ] == l2 . rpc . listpeers ( l1 . info [ ' id ' ] ) [ ' peers ' ] [ 0 ] [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
# Pay it (and make sure it's fully resolved before we take l1 offline!)
l1 . rpc . pay ( inv [ ' bolt11 ' ] )
wait_channel_quiescent ( l1 , l2 )
2018-12-14 06:20:32 +01:00
# Due to reserve & fees, l1 doesn't have capacity to pay this.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 2 * ( 10 * * 8 ) - 123456 , label = " inv2 " , description = " ? " )
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
# Check warning
2021-06-14 23:07:38 +02:00
assert ' warning_capacity ' in inv
assert ' warning_private_unused ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
assert ' warning_offline ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
2019-01-15 04:57:27 +01:00
l1 . rpc . disconnect ( l2 . info [ ' id ' ] , True )
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
wait_for ( lambda : not only_one ( l2 . rpc . listpeers ( l1 . info [ ' id ' ] ) [ ' peers ' ] ) [ ' connected ' ] )
inv = l2 . rpc . invoice ( 123456 , label = " inv3 " , description = " ? " )
# Check warning.
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
assert ' warning_capacity ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
assert ' warning_offline ' in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
2019-01-15 04:57:27 +01:00
# Close l0, l2 will not use l1 at all.
l0 . rpc . close ( l1 . info [ ' id ' ] )
l0 . wait_for_channel_onchain ( l1 . info [ ' id ' ] )
bitcoind . generate_block ( 100 )
# l2 has to notice channel is gone.
wait_for ( lambda : len ( l2 . rpc . listchannels ( ) [ ' channels ' ] ) == 2 )
inv = l2 . rpc . invoice ( 123456 , label = " inv4 " , description = " ? " )
# Check warning.
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' in inv
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_capacity ' not in inv
2019-01-15 04:57:27 +01:00
assert ' warning_offline ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-01-15 04:57:27 +01:00
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
2021-04-26 21:58:58 +02:00
@pytest.mark.developer ( " gossip without DEVELOPER=1 is slow " )
2019-01-15 04:56:27 +01:00
def test_invoice_routeboost_private ( node_factory , bitcoind ) :
""" Test routeboost ' r ' hint in bolt11 invoice for private channels
"""
2020-08-07 05:14:55 +02:00
l1 , l2 , l3 = node_factory . get_nodes ( 3 )
node_factory . join_nodes ( [ l1 , l2 ] , fundamount = 16777215 , announce_channels = False )
2019-01-15 04:56:27 +01:00
2019-12-17 07:18:13 +01:00
scid = l1 . get_channel_scid ( l2 )
2019-01-15 04:57:27 +01:00
# Attach public channel to l1 so it doesn't look like a dead-end.
l0 = node_factory . get_node ( )
l0 . rpc . connect ( l1 . info [ ' id ' ] , ' localhost ' , l1 . port )
2020-10-15 20:10:31 +02:00
scid_dummy , _ = l0 . fundchannel ( l1 , 2 * ( 10 * * 5 ) )
2022-01-30 04:37:23 +01:00
mine_funding_to_announce ( bitcoind , [ l0 , l1 , l2 , l3 ] )
2019-01-15 04:57:27 +01:00
# Make sure channel is totally public.
2019-12-17 07:18:13 +01:00
wait_for ( lambda : [ c [ ' public ' ] for c in l2 . rpc . listchannels ( scid_dummy ) [ ' channels ' ] ] == [ True , True ] )
2019-01-15 04:57:27 +01:00
2019-01-15 04:56:27 +01:00
# Since there's only one route, it will reluctantly hint that even
# though it's private
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv0 " , description = " ? " )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-01-15 04:56:27 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-01-15 04:56:27 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == l1 . rpc . listchannels ( ) [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
2019-01-15 04:58:27 +01:00
# If we explicitly say not to, it won't expose.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv1 " , description = " ? " , exposeprivatechannels = False )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' in inv
assert ' warning_capacity ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-01-15 04:58:27 +01:00
assert ' routes ' not in l1 . rpc . decodepay ( inv [ ' bolt11 ' ] )
2019-12-17 07:18:13 +01:00
# If we ask for it, we get it.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv1a " , description = " ? " , exposeprivatechannels = scid )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:18:13 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-12-17 07:18:13 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == l1 . rpc . listchannels ( ) [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
# Similarly if we ask for an array.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv1b " , description = " ? " , exposeprivatechannels = [ scid ] )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:18:13 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-12-17 07:18:13 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == l1 . rpc . listchannels ( ) [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
2019-01-15 04:56:27 +01:00
# The existence of a public channel, even without capacity, will suppress
# the exposure of private channels.
l3 . rpc . connect ( l2 . info [ ' id ' ] , ' localhost ' , l2 . port )
2020-10-15 20:10:31 +02:00
scid2 , _ = l3 . fundchannel ( l2 , ( 10 * * 5 ) )
2022-01-30 04:37:23 +01:00
mine_funding_to_announce ( bitcoind , [ l0 , l1 , l2 , l3 ] )
2019-01-15 04:56:27 +01:00
# Make sure channel is totally public.
2019-12-17 07:18:13 +01:00
wait_for ( lambda : [ c [ ' public ' ] for c in l2 . rpc . listchannels ( scid2 ) [ ' channels ' ] ] == [ True , True ] )
2019-01-15 04:56:27 +01:00
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 10 * * 7 , label = " inv2 " , description = " ? " )
2021-06-14 23:07:38 +02:00
print ( inv )
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' in inv
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-01-15 04:56:27 +01:00
2019-01-15 04:58:27 +01:00
# Unless we tell it to include it.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 10 * * 7 , label = " inv3 " , description = " ? " , exposeprivatechannels = True )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-01-15 04:58:27 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
2019-12-17 07:11:08 +01:00
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-01-15 04:58:27 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == l1 . rpc . listchannels ( ) [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
2019-12-17 07:18:13 +01:00
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 10 * * 7 , label = " inv4 " , description = " ? " , exposeprivatechannels = scid )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:18:13 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-12-17 07:18:13 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == scid
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
# Ask it explicitly to use a channel it can't (insufficient capacity)
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = ( 10 * * 5 ) * 1000 + 1 , label = " inv5 " , description = " ? " , exposeprivatechannels = scid2 )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2020-04-08 09:40:58 +02:00
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_capacity ' in inv
2019-12-17 07:18:13 +01:00
assert ' warning_offline ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-12-17 07:18:13 +01:00
# Give it two options and it will pick one with suff capacity.
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = ( 10 * * 5 ) * 1000 + 1 , label = " inv6 " , description = " ? " , exposeprivatechannels = [ scid2 , scid ] )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2019-12-17 07:18:13 +01:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2019-12-17 07:18:13 +01:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == scid
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
2019-01-15 04:58:27 +01:00
2020-04-08 09:40:58 +02:00
# It will use an explicit exposeprivatechannels even if it thinks its a dead-end
l0 . rpc . close ( l1 . info [ ' id ' ] )
l0 . wait_for_channel_onchain ( l1 . info [ ' id ' ] )
bitcoind . generate_block ( 1 )
wait_for ( lambda : l2 . rpc . listchannels ( scid_dummy ) [ ' channels ' ] == [ ] )
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123456 , label = " inv7 " , description = " ? " , exposeprivatechannels = scid )
2021-06-14 23:07:38 +02:00
assert ' warning_private_unused ' not in inv
2020-04-08 09:40:58 +02:00
assert ' warning_capacity ' not in inv
assert ' warning_offline ' not in inv
assert ' warning_deadends ' not in inv
2021-06-14 23:07:38 +02:00
assert ' warning_mpp ' not in inv
2020-04-08 09:40:58 +02:00
# Route array has single route with single element.
r = only_one ( only_one ( l1 . rpc . decodepay ( inv [ ' bolt11 ' ] ) [ ' routes ' ] ) )
assert r [ ' pubkey ' ] == l1 . info [ ' id ' ]
assert r [ ' short_channel_id ' ] == l1 . rpc . listchannels ( ) [ ' channels ' ] [ 0 ] [ ' short_channel_id ' ]
assert r [ ' fee_base_msat ' ] == 1
assert r [ ' fee_proportional_millionths ' ] == 10
assert r [ ' cltv_expiry_delta ' ] == 6
2019-01-15 04:56:27 +01:00
2018-08-02 17:10:01 +02:00
def test_invoice_expiry ( node_factory , executor ) :
l1 , l2 = node_factory . line_graph ( 2 , fundchannel = True )
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123000 , label = ' test_pay ' , description = ' description ' , expiry = 1 ) [ ' bolt11 ' ]
2018-08-02 17:10:01 +02:00
time . sleep ( 2 )
with pytest . raises ( RpcError ) :
l1 . rpc . pay ( inv )
invoices = l2 . rpc . listinvoices ( ' test_pay ' ) [ ' invoices ' ]
assert len ( invoices ) == 1
assert invoices [ 0 ] [ ' status ' ] == ' expired ' and invoices [ 0 ] [ ' expires_at ' ] < time . time ( )
# Try deleting it.
with pytest . raises ( RpcError , match = r ' Invoice status is expired not unpaid ' ) :
l2 . rpc . delinvoice ( ' test_pay ' , ' unpaid ' )
with pytest . raises ( RpcError , match = r ' Invoice status is expired not paid ' ) :
l2 . rpc . delinvoice ( ' test_pay ' , ' paid ' )
l2 . rpc . delinvoice ( ' test_pay ' , ' expired ' )
with pytest . raises ( RpcError , match = r ' Unknown invoice ' ) :
l2 . rpc . delinvoice ( ' test_pay ' , ' expired ' )
# Test expiration waiting.
# The second invoice created expires first.
l2 . rpc . invoice ( ' any ' , ' inv1 ' , ' description ' , 10 )
l2 . rpc . invoice ( ' any ' , ' inv2 ' , ' description ' , 4 )
l2 . rpc . invoice ( ' any ' , ' inv3 ' , ' description ' , 16 )
creation = int ( time . time ( ) )
# Check waitinvoice correctly waits
w1 = executor . submit ( l2 . rpc . waitinvoice , ' inv1 ' )
w2 = executor . submit ( l2 . rpc . waitinvoice , ' inv2 ' )
w3 = executor . submit ( l2 . rpc . waitinvoice , ' inv3 ' )
time . sleep ( 2 ) # total 2
assert not w1 . done ( )
assert not w2 . done ( )
assert not w3 . done ( )
time . sleep ( 4 ) # total 6
assert not w1 . done ( )
with pytest . raises ( RpcError ) :
w2 . result ( )
assert not w3 . done ( )
time . sleep ( 6 ) # total 12
with pytest . raises ( RpcError ) :
w1 . result ( )
assert not w3 . done ( )
time . sleep ( 8 ) # total 20
with pytest . raises ( RpcError ) :
w3 . result ( )
# Test delexpiredinvoice
l2 . rpc . delexpiredinvoice ( maxexpirytime = creation + 8 )
# only inv2 should have been deleted
assert len ( l2 . rpc . listinvoices ( ) [ ' invoices ' ] ) == 2
assert len ( l2 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 0
# Test delexpiredinvoice all
l2 . rpc . delexpiredinvoice ( )
# all invoices are expired and should be deleted
assert len ( l2 . rpc . listinvoices ( ) [ ' invoices ' ] ) == 0
2019-04-10 06:58:24 +02:00
start = int ( time . time ( ) )
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
inv = l2 . rpc . invoice ( amount_msat = 123000 , label = ' inv_s ' , description = ' description ' , expiry = 1 ) [ ' bolt11 ' ]
2019-04-10 06:58:24 +02:00
end = int ( time . time ( ) )
expiry = only_one ( l2 . rpc . listinvoices ( ' inv_s ' ) [ ' invoices ' ] ) [ ' expires_at ' ]
assert expiry > = start + 1 and expiry < = end + 1
2018-08-02 17:10:01 +02:00
2021-04-26 21:58:58 +02:00
@pytest.mark.developer ( " Too slow without --dev-fast-gossip " )
2018-08-02 17:10:01 +02:00
def test_waitinvoice ( node_factory , executor ) :
""" Test waiting for one invoice will not return if another invoice is paid.
"""
# Setup
l1 , l2 = node_factory . line_graph ( 2 )
# Create invoices
inv1 = l2 . rpc . invoice ( 1000 , ' inv1 ' , ' inv1 ' )
inv2 = l2 . rpc . invoice ( 1000 , ' inv2 ' , ' inv2 ' )
l2 . rpc . invoice ( 1000 , ' inv3 ' , ' inv3 ' )
# Start waiting on invoice 3
f3 = executor . submit ( l2 . rpc . waitinvoice , ' inv3 ' )
# Start waiting on invoice 1, should block
f = executor . submit ( l2 . rpc . waitinvoice , ' inv1 ' )
time . sleep ( 1 )
assert not f . done ( )
# Pay invoice 2
l1 . rpc . pay ( inv2 [ ' bolt11 ' ] )
# Waiter should stil be blocked
time . sleep ( 1 )
assert not f . done ( )
# Waiting on invoice 2 should return immediately
r = executor . submit ( l2 . rpc . waitinvoice , ' inv2 ' ) . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv2 '
# Pay invoice 1
l1 . rpc . pay ( inv1 [ ' bolt11 ' ] )
# Waiter for invoice 1 should now finish
r = f . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv1 '
# Waiter for invoice 3 should still be waiting
time . sleep ( 1 )
assert not f3 . done ( )
2021-04-26 21:58:58 +02:00
@pytest.mark.developer ( " Too slow without --dev-fast-gossip " )
2018-08-02 17:10:01 +02:00
def test_waitanyinvoice ( node_factory , executor ) :
""" Test various variants of waiting for the next invoice to complete.
"""
l1 , l2 = node_factory . line_graph ( 2 )
inv1 = l2 . rpc . invoice ( 1000 , ' inv1 ' , ' inv1 ' )
inv2 = l2 . rpc . invoice ( 1000 , ' inv2 ' , ' inv2 ' )
inv3 = l2 . rpc . invoice ( 1000 , ' inv3 ' , ' inv3 ' )
2020-01-24 05:18:46 +01:00
inv4 = l2 . rpc . invoice ( 1000 , ' inv4 ' , ' inv4 ' )
2018-08-02 17:10:01 +02:00
# Attempt to wait for the first invoice
f = executor . submit ( l2 . rpc . waitanyinvoice )
time . sleep ( 1 )
# The call to waitanyinvoice should not have returned just yet
assert not f . done ( )
# Now pay the first two invoices and make sure we notice
l1 . rpc . pay ( inv1 [ ' bolt11 ' ] )
l1 . rpc . pay ( inv2 [ ' bolt11 ' ] )
r = f . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv1 '
pay_index = r [ ' pay_index ' ]
# This one should return immediately with inv2
r = executor . submit ( l2 . rpc . waitanyinvoice , pay_index ) . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv2 '
pay_index = r [ ' pay_index ' ]
# Now spawn the next waiter
f = executor . submit ( l2 . rpc . waitanyinvoice , pay_index )
time . sleep ( 1 )
assert not f . done ( )
l1 . rpc . pay ( inv3 [ ' bolt11 ' ] )
r = f . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv3 '
2020-01-24 05:18:46 +01:00
pay_index = r [ ' pay_index ' ]
# If timeout is 0 and a paid invoice is not yet
# available, it should fail immediately.
with pytest . raises ( RpcError ) :
l2 . rpc . waitanyinvoice ( pay_index , 0 )
# If timeout is 0 but a paid invoice is available
# anyway, it should return successfully immediately.
l1 . rpc . pay ( inv4 [ ' bolt11 ' ] )
r = executor . submit ( l2 . rpc . waitanyinvoice , pay_index , 0 ) . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv4 '
2018-08-02 17:10:01 +02:00
2022-04-01 06:13:34 +02:00
l2 . rpc . check_request_schemas = False
2018-08-02 17:10:01 +02:00
with pytest . raises ( RpcError ) :
l2 . rpc . waitanyinvoice ( ' non-number ' )
def test_waitanyinvoice_reversed ( node_factory , executor ) :
""" Test waiting for invoices, where they are paid in reverse order
to when they are created .
"""
# Setup
l1 , l2 = node_factory . line_graph ( 2 )
# Create invoices
inv1 = l2 . rpc . invoice ( 1000 , ' inv1 ' , ' inv1 ' )
inv2 = l2 . rpc . invoice ( 1000 , ' inv2 ' , ' inv2 ' )
# Pay inv2, wait, pay inv1, wait
# Pay inv2
l1 . rpc . pay ( inv2 [ ' bolt11 ' ] )
# Wait - should not block, should return inv2
r = executor . submit ( l2 . rpc . waitanyinvoice ) . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv2 '
pay_index = r [ ' pay_index ' ]
# Pay inv1
l1 . rpc . pay ( inv1 [ ' bolt11 ' ] )
# Wait inv2 - should not block, should return inv1
r = executor . submit ( l2 . rpc . waitanyinvoice , pay_index ) . result ( timeout = 5 )
assert r [ ' label ' ] == ' inv1 '
def test_autocleaninvoice ( node_factory ) :
l1 = node_factory . get_node ( )
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
l1 . rpc . invoice ( amount_msat = 12300 , label = ' inv1 ' , description = ' description1 ' , expiry = 4 )
l1 . rpc . invoice ( amount_msat = 12300 , label = ' inv2 ' , description = ' description2 ' , expiry = 12 )
2018-09-27 06:07:16 +02:00
l1 . rpc . autocleaninvoice ( cycle_seconds = 8 , expired_by = 2 )
start_time = time . time ( )
2018-08-02 17:10:01 +02:00
# time 0
# Both should still be there.
assert len ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) == 1
assert len ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 1
assert l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] [ 0 ] [ ' description ' ] == ' description1 '
time . sleep ( start_time - time . time ( ) + 6 ) # total 6
# Both should still be there - auto clean cycle not started.
# inv1 should be expired
assert len ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) == 1
assert only_one ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) [ ' status ' ] == ' expired '
assert len ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 1
assert only_one ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) [ ' status ' ] != ' expired '
time . sleep ( start_time - time . time ( ) + 10 ) # total 10
# inv1 should have deleted, inv2 still there and unexpired.
assert len ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) == 0
assert len ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 1
assert only_one ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) [ ' status ' ] != ' expired '
time . sleep ( start_time - time . time ( ) + 14 ) # total 14
# inv2 should still be there, but expired
assert len ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) == 0
assert len ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 1
assert only_one ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) [ ' status ' ] == ' expired '
time . sleep ( start_time - time . time ( ) + 18 ) # total 18
# Everything deleted
assert len ( l1 . rpc . listinvoices ( ' inv1 ' ) [ ' invoices ' ] ) == 0
assert len ( l1 . rpc . listinvoices ( ' inv2 ' ) [ ' invoices ' ] ) == 0
2019-05-10 03:17:49 +02:00
2022-07-13 13:56:08 +02:00
# stress test
l1 . rpc . autocleaninvoice ( cycle_seconds = 0 )
l1 . rpc . autocleaninvoice ( cycle_seconds = 1 )
l1 . rpc . autocleaninvoice ( cycle_seconds = 0 )
time . sleep ( 1 )
l1 . rpc . autocleaninvoice ( cycle_seconds = 1 , expired_by = 1 )
2019-05-10 03:17:49 +02:00
def test_decode_unknown ( node_factory ) :
l1 = node_factory . get_node ( )
b11 = l1 . rpc . decodepay ( ' lntb30m1pw2f2yspp5s59w4a0kjecw3zyexm7zur8l8n4scw674w8sftjhwec33km882gsdpa2pshjmt9de6zqun9w96k2um5ypmkjargypkh2mr5d9cxzun5ypeh2ursdae8gxqruyqvzddp68gup69uhnzwfj9cejuvf3xshrwde68qcrswf0d46kcarfwpshyaplw3skw0tdw4k8g6tsv9e8gu2etcvsym36pdjpz04wm9nn96f9ntc3t3h5r08pe9d62p3js5wt5rkurqnrl7zkj2fjpvl3rmn7wwazt80letwxlm22hngu8n88g7hsp542qpl ' )
assert b11 [ ' currency ' ] == ' tb '
assert b11 [ ' created_at ' ] == 1554294928
assert b11 [ ' payment_hash ' ] == ' 850aeaf5f69670e8889936fc2e0cff3ceb0c3b5eab8f04ae57767118db673a91 '
assert b11 [ ' description ' ] == ' Payment request with multipart support '
assert b11 [ ' expiry ' ] == 28800
assert b11 [ ' payee ' ] == ' 02330d13587b67a85c0a36ea001c4dba14bcd48dda8988f7303275b040bffb6abd '
2020-08-20 08:51:03 +02:00
assert b11 [ ' min_final_cltv_expiry ' ] == 18
2019-05-10 03:17:49 +02:00
extra = only_one ( b11 [ ' extra ' ] )
assert extra [ ' tag ' ] == ' v '
assert extra [ ' data ' ] == ' dp68gup69uhnzwfj9cejuvf3xshrwde68qcrswf0d46kcarfwpshyaplw3skw0tdw4k8g6tsv9e8g '
assert b11 [ ' signature ' ] == ' 3045022100e2b2bc3204dc7416c8227d5db2ce65d24b35e22b8de8379c392b74a0c650a397022041db8304c7ff0ad25264167e23dcfce7744b3bff95b8dfda9579a38799ce8f5e '
assert ' fallbacks ' not in b11
assert ' routes ' not in b11
2020-08-24 13:42:06 +02:00
def test_amountless_invoice ( node_factory ) :
""" The recipient should know how much was received by an amountless invoice.
"""
l1 , l2 = node_factory . line_graph ( 2 )
inv = l2 . rpc . invoice ( ' any ' , ' lbl ' , ' desc ' ) [ ' bolt11 ' ]
i = l2 . rpc . listinvoices ( ) [ ' invoices ' ]
assert ( len ( i ) == 1 )
assert ( ' amount_received_msat ' not in i [ 0 ] )
assert ( i [ 0 ] [ ' status ' ] == ' unpaid ' )
details = l1 . rpc . decodepay ( inv )
assert ( ' msatoshi ' not in details )
lightningd: change `msatoshi` args to `amount_msat`.
This is consistent with our output changes, and increases consistency.
It also keeps future sanity checks happy, that we only use JSON msat
helpers with '_msat' fields.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice`: `msatoshi` argument is now called `amount_msat` to match other fields.
Changelog-Deprecated: JSON-RPC: `invoice`, `sendonion`, `sendpay`, `pay`, `keysend`, `fetchinvoice`, `sendinvoice` `msatoshi` (use `amount_msat`)
2022-06-19 09:20:11 +02:00
l1 . rpc . pay ( inv , amount_msat = 1337 )
2020-08-24 13:42:06 +02:00
i = l2 . rpc . listinvoices ( ) [ ' invoices ' ]
assert ( len ( i ) == 1 )
assert ( i [ 0 ] [ ' amount_received_msat ' ] == Millisatoshi ( 1337 ) )
assert ( i [ 0 ] [ ' status ' ] == ' paid ' )
2021-01-05 18:58:41 +01:00
def test_listinvoices_filter ( node_factory ) :
""" Test the optional query arguments to listinvoices
"""
l1 = node_factory . get_node ( )
invoices = [ l1 . rpc . invoice ( 42 , ' label {} ' . format ( i ) , ' desc ' ) for i in range ( 10 ) ]
def match ( node , query , invoice ) :
r1 = l1 . rpc . listinvoices ( * * query ) [ ' invoices ' ]
assert len ( r1 ) == 1
assert r1 [ 0 ] [ ' payment_hash ' ] == inv [ ' payment_hash ' ]
assert r1 [ 0 ] [ ' bolt11 ' ] == inv [ ' bolt11 ' ]
for i , inv in enumerate ( invoices ) :
match ( l1 , { ' label ' : " label {} " . format ( i ) } , inv )
match ( l1 , { ' payment_hash ' : inv [ ' payment_hash ' ] } , inv )
match ( l1 , { ' invstring ' : inv [ ' bolt11 ' ] } , inv )
# Now test for failures
inv = invoices [ 0 ]
queries = [
{ " payment_hash " : inv [ ' payment_hash ' ] , " label " : " label0 " } ,
{ " invstring " : inv [ ' bolt11 ' ] , " label " : " label0 " } ,
{ " payment_hash " : inv [ ' payment_hash ' ] , " label " : " label0 " } ,
]
for q in queries :
with pytest . raises (
RpcError ,
2021-07-02 02:11:35 +02:00
match = r ' Can only specify one of {label} , {invstring} , {payment_hash} or {offer_id} '
2021-01-05 18:58:41 +01:00
) :
l1 . rpc . listinvoices ( * * q )
# Test querying for non-existent invoices
queries = [
{ ' label ' : ' doesnt exist ' } ,
{ ' payment_hash ' : ' AA ' * 32 } ,
{ ' invstring ' : ' lnbcrt420p1p0lfrl6pp5w4zsagnfqu08s93rd44z93s8tt920hd9jec2yph969wluwkzrwpqdq8v3jhxccxqyjw5qcqp9sp52kw0kp75f6v2jusd8nsg2nfmdr82pqj0gf3jc8tqp7a2j48rzweq9qy9qsqtlu8eslmd4yxqrtrz75v8vmqrwknnk64sm79cj4asxhgndnj22r3g2a6axdvfdkhw966zw63cy3uzzn5hxad9ja8amqpp3wputl3ffcpallm2g ' } ,
2021-07-02 02:11:35 +02:00
{ ' offer_id ' : ' AA ' * 32 } ,
2021-01-05 18:58:41 +01:00
]
for q in queries :
r = l1 . rpc . listinvoices ( * * q )
assert len ( r [ ' invoices ' ] ) == 0
2022-03-24 00:57:20 +01:00
def test_invoice_deschash ( node_factory , chainparams ) :
l1 , l2 = node_factory . line_graph ( 2 )
# BOLT #11:
# * `h`: tagged field: hash of description
# * `p5`: `data_length` (`p` = 1, `5` = 20; 1 * 32 + 20 == 52)
# * `8yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqs`: SHA256 of 'One piece of chocolate cake, one icecream cone, one pickle, one slice of swiss cheese, one slice of salami, one lollypop, one piece of cherry pie, one sausage, one cupcake, and one slice of watermelon'
inv = l2 . rpc . invoice ( 42 , ' label ' , ' One piece of chocolate cake, one icecream cone, one pickle, one slice of swiss cheese, one slice of salami, one lollypop, one piece of cherry pie, one sausage, one cupcake, and one slice of watermelon ' , deschashonly = True )
assert ' 8yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqs ' in inv [ ' bolt11 ' ]
b11 = l2 . rpc . decodepay ( inv [ ' bolt11 ' ] )
assert ' description ' not in b11
assert b11 [ ' description_hash ' ] == ' 3925b6f67e2c340036ed12093dd44e0368df1b6ea26c53dbe4811f58fd5db8c1 '
listinv = only_one ( l2 . rpc . listinvoices ( ) [ ' invoices ' ] )
assert listinv [ ' description ' ] == ' One piece of chocolate cake, one icecream cone, one pickle, one slice of swiss cheese, one slice of salami, one lollypop, one piece of cherry pie, one sausage, one cupcake, and one slice of watermelon '
2022-04-02 04:33:05 +02:00
# To pay it we need to provide the (correct!) description.
with pytest . raises ( RpcError , match = r ' you did not provide description parameter ' ) :
l1 . rpc . pay ( inv [ ' bolt11 ' ] )
with pytest . raises ( RpcError , match = r ' does not match description ' ) :
l1 . rpc . pay ( inv [ ' bolt11 ' ] , description = listinv [ ' description ' ] [ : - 1 ] )
l1 . rpc . pay ( inv [ ' bolt11 ' ] , description = listinv [ ' description ' ] )
2022-03-24 00:57:28 +01:00
2022-04-02 04:33:35 +02:00
# Description will be in some.
found = False
for p in l1 . rpc . listsendpays ( ) [ ' payments ' ] :
if ' description ' in p :
found = True
assert p [ ' description ' ] == listinv [ ' description ' ]
assert found
assert only_one ( l1 . rpc . listpays ( inv [ ' bolt11 ' ] ) [ ' pays ' ] ) [ ' description ' ] == listinv [ ' description ' ]
2022-03-24 00:57:28 +01:00
# Try removing description.
l2 . rpc . delinvoice ( ' label ' , " paid " , desconly = True )
assert ' description ' not in only_one ( l2 . rpc . listinvoices ( ) [ ' invoices ' ] )
with pytest . raises ( RpcError , match = r ' description already removed ' ) :
l2 . rpc . delinvoice ( ' label ' , " paid " , desconly = True )