Skip to content

Commit

Permalink
pytest: Add test for failcode conversion
Browse files Browse the repository at this point in the history
We added a conversion of failcodes that do not have sufficient information in
faac4b2. That means that a failcode that'd require additional information
in order to be a correct error to return in an onion is mapped to a generic
one since we can't backfill the information.

This tests that the mapping is performed correctly and replicates the
situation in #4070
  • Loading branch information
cdecker authored and rustyrussell committed Sep 24, 2020
1 parent 980a951 commit 5a87e88
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/plugins/htlc_accepted-failcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""A simply plugin that fails HTLCs with a configurable failcode.
"""
from pyln.client import Plugin


plugin = Plugin()


@plugin.hook('htlc_accepted')
def on_htlc_accepted(htlc, onion, plugin, **kwargs):
res = {"result": "fail"}

if plugin.failmsg is not None:
res['failure_message'] = plugin.failmsg

if plugin.failcode is not None:
res['failure_code'] = plugin.failcode

return res


@plugin.method('setfailcode')
def setfailcode(plugin, code=None, msg=None):
"""Sets the failcode to return when receiving an incoming HTLC.
"""
plugin.failcode = code
plugin.failmsg = msg


@plugin.init()
def on_init(**kwargs):
plugin.failcode = None
plugin.failmsg = None


plugin.run()
39 changes: 39 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1889,3 +1889,42 @@ def test_htlc_accepted_hook_crash(node_factory, executor):

with pytest.raises(RpcError, match=r'failed: WIRE_TEMPORARY_NODE_FAILURE'):
f.result(10)


def test_htlc_accepted_hook_failcodes(node_factory):
plugin = os.path.join(os.path.dirname(__file__), 'plugins/htlc_accepted-failcode.py')
l1, l2 = node_factory.line_graph(2, opts=[{}, {'plugin': plugin}])

# First let's test the newer failure_message, which should get passed
# through without being mapped.
tests = {
'2002': 'WIRE_TEMPORARY_NODE_FAILURE',
'400F' + 12 * '00': 'WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS',
'4009': 'WIRE_REQUIRED_CHANNEL_FEATURE_MISSING',
'4016' + 3 * '00': 'WIRE_INVALID_ONION_PAYLOAD',
}

for failmsg, expected in tests.items():
l2.rpc.setfailcode(msg=failmsg)
inv = l2.rpc.invoice(42, 'failmsg{}'.format(failmsg), '')['bolt11']
with pytest.raises(RpcError, match=r'failcodename.: .{}.'.format(expected)):
l1.rpc.pay(inv)

# And now test the older failcode return value. This is deprecated and can
# be removed once we have removed the failcode correction code in
# peer_htlcs.c. The following ones get remapped
tests.update({
'400F': 'WIRE_TEMPORARY_NODE_FAILURE',
'4009': 'WIRE_TEMPORARY_NODE_FAILURE',
'4016': 'WIRE_TEMPORARY_NODE_FAILURE',
})

for failcode, expected in tests.items():
# Do not attempt with full messages
if len(failcode) > 4:
continue

l2.rpc.setfailcode(code=failcode)
inv = l2.rpc.invoice(42, 'failcode{}'.format(failcode), '')['bolt11']
with pytest.raises(RpcError, match=r'failcodename.: .{}.'.format(expected)):
l1.rpc.pay(inv)

0 comments on commit 5a87e88

Please sign in to comment.