Skip to content

Commit 9aa4840

Browse files
committed
tests: Add tests for extra_tlvs in hook
Adds some testcases for custom tlvs, set by a htlc_accepted_hook. We check that the custom tlvs replace the update_add_htlc_tlvs and get forwarded to the peer. We also check that a malformed tlv will result in a **BROKEN** behaviour. Signed-off-by: Peter Neuroth <[email protected]>
1 parent fdd477d commit 9aa4840

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
"""A simply plugin that returns a custom tlv stream (byte encoded) to be
3+
attached to a forwarding HTLC.
4+
"""
5+
6+
from pyln.client import Plugin
7+
8+
9+
plugin = Plugin()
10+
custom_tlvs = None
11+
12+
13+
@plugin.hook("htlc_accepted")
14+
def on_htlc_accepted(htlc, onion, plugin, **kwargs):
15+
global custom_tlvs
16+
if 'extra_tlvs' in htlc:
17+
print(f"called htlc accepted hook with extra_tlvs: {htlc['extra_tlvs']}")
18+
print(f'returning continue with custom extra_tlvs: {custom_tlvs}')
19+
if custom_tlvs:
20+
return {"result": "continue", "extra_tlvs": custom_tlvs}
21+
return {"result": "continue"}
22+
23+
24+
@plugin.method("setcustomtlvs")
25+
def setcustomtlvs(plugin, tlvs):
26+
"""Sets the custom tlv to return when receiving an incoming HTLC.
27+
"""
28+
global custom_tlvs
29+
print(f'setting custom tlv to {tlvs}')
30+
custom_tlvs = tlvs
31+
32+
33+
@plugin.init()
34+
def on_init(**kwargs):
35+
global custom_tlvs
36+
custom_tlvs = None
37+
38+
39+
plugin.run()

tests/test_plugin.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,48 @@ def test_htlc_accepted_hook_failmsg(node_factory):
24192419
l1.rpc.pay(inv)
24202420

24212421

2422+
def test_htlc_accepted_hook_customtlvs(node_factory):
2423+
""" Passes an custom extra tlv field to the hooks return that should be set
2424+
as the `update_add_htlc_tlvs` in the `update_add_htlc` message on
2425+
forwards.
2426+
"""
2427+
plugin = os.path.join(os.path.dirname(__file__), 'plugins/htlc_accepted-customtlv.py')
2428+
l1, l2, l3 = node_factory.line_graph(3, opts=[{}, {'plugin': plugin}, {'plugin': plugin}], wait_for_announce=True)
2429+
2430+
# Single tlv - Check that we receive the extra tlv at l3 attached by l2.
2431+
single_tlv = "fe00010001012a" # represents type: 65537, lenght: 1, value: 42
2432+
l2.rpc.setcustomtlvs(tlvs=single_tlv)
2433+
inv = l3.rpc.invoice(1000, 'customtlvs-singletlv', '')['bolt11']
2434+
l1.rpc.pay(inv)
2435+
l3.daemon.wait_for_log(f"called htlc accepted hook with extra_tlvs: {single_tlv}")
2436+
2437+
# Mutliple tlvs - Check that we recieve multiple extra tlvs at l3 attached by l2.
2438+
multi_tlv = "fdffff012afe00010001020539" # represents type: 65535, length: 1, value: 42 and type: 65537, length: 2, value: 1337
2439+
l2.rpc.setcustomtlvs(tlvs=multi_tlv)
2440+
inv = l3.rpc.invoice(1000, 'customtlvs-multitlvs', '')['bolt11']
2441+
l1.rpc.pay(inv)
2442+
l3.daemon.wait_for_log(f"called htlc accepted hook with extra_tlvs: {multi_tlv}")
2443+
2444+
2445+
def test_htlc_accepted_hook_malformedtlvs(node_factory):
2446+
""" Passes an custom extra tlv field to the hooks return that is malformed
2447+
and should cause a broken log.
2448+
l1 -- l2 -- l3
2449+
"""
2450+
plugin = os.path.join(os.path.dirname(__file__), 'plugins/htlc_accepted-customtlv.py')
2451+
l1, l2, l3 = node_factory.line_graph(3, opts=[{}, {'plugin': plugin, 'broken_log': "lightningd: ", 'may_fail': True}, {}], wait_for_announce=True)
2452+
2453+
mal_tlv = "fe00010001020539fdffff012a" # is malformed, types are 65537 and 65535 not in asc order.
2454+
l2.rpc.setcustomtlvs(tlvs=mal_tlv)
2455+
inv = l3.rpc.invoice(1000, 'customtlvs-maltlvs', '')
2456+
phash = inv['payment_hash']
2457+
route = l1.rpc.getroute(l3.info['id'], 1000, 1)['route']
2458+
2459+
# Here shouldn't use `pay` command because l2 should fail with a broken log.
2460+
l1.rpc.sendpay(route, phash, payment_secret=inv['payment_secret'])
2461+
assert l2.daemon.wait_for_log("BROKEN.*htlc_accepted_hook returned bad extra_tlvs")
2462+
2463+
24222464
def test_hook_dep(node_factory):
24232465
dep_a = os.path.join(os.path.dirname(__file__), 'plugins/dep_a.py')
24242466
dep_b = os.path.join(os.path.dirname(__file__), 'plugins/dep_b.py')

0 commit comments

Comments
 (0)