From dd26a01c54f499c563497e578d9a5c2ea768a7ed Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 21 May 2019 14:16:00 +0200 Subject: [PATCH] pytest: Add a test for htlc_accepted hook replay on startup --- tests/plugins/hold_htlcs.py | 27 +++++++++++++++++++++++++++ tests/test_plugin.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 tests/plugins/hold_htlcs.py diff --git a/tests/plugins/hold_htlcs.py b/tests/plugins/hold_htlcs.py new file mode 100755 index 000000000..cca513213 --- /dev/null +++ b/tests/plugins/hold_htlcs.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +"""Plugin that holds on to HTLCs for 10 seconds. + +Used to test restarts / crashes while HTLCs were accepted, but not yet +settled/forwarded/ + +""" + + +from lightning import Plugin +import time + + +plugin = Plugin() + + +@plugin.hook("htlc_accepted") +def on_htlc_accepted(htlc, onion, plugin): + plugin.log("Holding onto an incoming htlc for 10 seconds") + time.sleep(10) + + # Give the tester something to look for + plugin.log("htlc_accepted hook called") + return {'result': 'continue'} + + +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 0b2bd1388..54ff7c866 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -384,3 +384,38 @@ def test_htlc_accepted_hook_resolve(node_factory): # 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_direct_restart(node_factory, executor): + """l2 restarts while it is pondering what to do with an HTLC. + """ + l1, l2 = node_factory.line_graph(2, opts=[ + {'may_reconnect': True}, + {'may_reconnect': True, 'plugin': 'tests/plugins/hold_htlcs.py'} + ]) + + i1 = l2.rpc.invoice(msatoshi=1000, label="direct", description="desc")['bolt11'] + f1 = executor.submit(l1.rpc.pay, i1) + + l2.daemon.wait_for_log(r'Holding onto an incoming htlc for 10 seconds') + l2.restart() + + f1.result() + + +def test_htlc_accepted_hook_forward_restart(node_factory, executor): + """l2 restarts while it is pondering what to do with an HTLC. + """ + l1, l2, l3 = node_factory.line_graph(3, opts=[ + {'may_reconnect': True}, + {'may_reconnect': True, 'plugin': 'tests/plugins/hold_htlcs.py'}, + {'may_reconnect': True}, + ], wait_for_announce=True) + + i1 = l3.rpc.invoice(msatoshi=1000, label="direct", description="desc")['bolt11'] + f1 = executor.submit(l1.rpc.pay, i1) + + l2.daemon.wait_for_log(r'Holding onto an incoming htlc for 10 seconds') + l2.restart() + + f1.result()