1#!/usr/bin/env python3
2"""Plugin that holds on to HTLCs for 10 seconds.
3
4Used to test restarts / crashes while HTLCs were accepted, but not yet
5settled/forwarded/
6
7"""
8from pyln.client import Plugin
9import json
10import os
11import tempfile
12import time
13
14plugin = Plugin()
15
16
17@plugin.hook("htlc_accepted")
18def on_htlc_accepted(htlc, onion, plugin, **kwargs):
19    # Stash the onion so the test can check it
20    fname = os.path.join(tempfile.mkdtemp(), "onion.json")
21    with open(fname, 'w') as f:
22        f.write(json.dumps(onion))
23
24    plugin.log("Holding onto an incoming htlc for {hold_time} seconds".format(
25        hold_time=plugin.hold_time
26    ))
27
28    time.sleep(plugin.hold_time)
29
30    print("Onion written to {}".format(fname))
31
32    # Give the tester something to look for
33    plugin.log("htlc_accepted hook called")
34    return {'result': plugin.hold_result}
35
36
37plugin.add_option(
38    'hold-time', 10,
39    'How long should we hold on to HTLCs?',
40    opt_type='int'
41)
42plugin.add_option(
43    'hold-result',
44    'continue', 'How should we continue after holding?',
45)
46
47
48@plugin.init()
49def init(options, configuration, plugin):
50    plugin.log("hold_htlcs.py initializing")
51    plugin.hold_time = options['hold-time']
52    plugin.hold_result = options['hold-result']
53
54
55plugin.run()
56