pytest: Add tests for htlc_accepted_hook

Two tests: one for failures and one for in-path resolution.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-01-11 22:39:18 +01:00 committed by Rusty Russell
parent 2b81e02a2e
commit 9fd8be6463
3 changed files with 81 additions and 0 deletions

15
tests/plugins/fail_htlcs.py Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
from lightning import Plugin
plugin = Plugin()
@plugin.hook("htlc_accepted")
def on_htlc_accepted(htlc, onion, plugin):
plugin.log("Failing htlc on purpose")
plugin.log("onion: %r" % (onion))
return {"result": "fail", "failure_code": 16399}
plugin.run()

13
tests/plugins/shortcircuit.py Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from lightning import Plugin
plugin = Plugin()
@plugin.hook("htlc_accepted")
def on_htlc_accepted(onion, htlc, plugin):
return {"result": "resolve", "payment_key": "00" * 32}
plugin.run()

View File

@ -331,3 +331,56 @@ def test_openchannel_hook(node_factory, bitcoind):
l1.connect(l2)
with pytest.raises(RpcError, match=r"I don't like odd amounts"):
l1.rpc.fundchannel(l2.info['id'], 100001)
def test_htlc_accepted_hook_fail(node_factory):
"""Send payments from l1 to l2, but l2 just declines everything.
l2 is configured with a plugin that'll hook into htlc_accepted and
always return failures. The same should also work for forwarded
htlcs in the second half.
"""
l1, l2, l3 = node_factory.line_graph(3, opts=[
{},
{'plugin': 'tests/plugins/fail_htlcs.py'},
{}
], wait_for_announce=True)
# This must fail
inv = l2.rpc.invoice(1000, "lbl", "desc")['bolt11']
with pytest.raises(RpcError) as excinfo:
l1.rpc.pay(inv)
assert excinfo.value.error['data']['failcode'] == 16399
assert excinfo.value.error['data']['erring_index'] == 1
# And the invoice must still be unpaid
inv = l2.rpc.listinvoices("lbl")['invoices']
assert len(inv) == 1 and inv[0]['status'] == 'unpaid'
# Now try with forwarded HTLCs: l2 should still fail them
# This must fail
inv = l3.rpc.invoice(1000, "lbl", "desc")['bolt11']
with pytest.raises(RpcError) as excinfo:
l1.rpc.pay(inv)
# And the invoice must still be unpaid
inv = l3.rpc.listinvoices("lbl")['invoices']
assert len(inv) == 1 and inv[0]['status'] == 'unpaid'
def test_htlc_accepted_hook_resolve(node_factory):
"""l3 creates an invoice, l2 knows the preimage and will shortcircuit.
"""
l1, l2, l3 = node_factory.line_graph(3, opts=[
{},
{'plugin': 'tests/plugins/shortcircuit.py'},
{}
], wait_for_announce=True)
inv = l3.rpc.invoice(msatoshi=1000, label="lbl", description="desc", preimage="00" * 32)['bolt11']
l1.rpc.pay(inv)
# And the invoice must still be unpaid
inv = l3.rpc.listinvoices("lbl")['invoices']
assert len(inv) == 1 and inv[0]['status'] == 'unpaid'