MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_rpc_command_hook

Function test_rpc_command_hook

tests/test_plugin.py:1808–1849  ·  view source on GitHub ↗

Test the `rpc_command` hook chain

(node_factory)

Source from the content-addressed store, hash-verified

1806
1807
1808def test_rpc_command_hook(node_factory):
1809 """Test the `rpc_command` hook chain"""
1810 plugin = [
1811 os.path.join(os.getcwd(), "tests/plugins/rpc_command_1.py"),
1812 os.path.join(os.getcwd(), "tests/plugins/rpc_command_2.py")
1813 ]
1814 l1 = node_factory.get_node(options={"plugin": plugin})
1815
1816 # rpc_command_2 plugin restricts using "sendpay"
1817 with pytest.raises(RpcError, match=r"rpc_command_2 cannot do this"):
1818 l1.rpc.call("sendpay")
1819
1820 # Both plugins will replace calls made for the "invoice" command
1821 # The first will win, for the second a warning should be logged
1822 invoice = l1.rpc.invoice(10**6, "test_side", "test_input")
1823 decoded = l1.rpc.decode(invoice["bolt11"])
1824 assert decoded["description"] == "rpc_command_1 modified this description"
1825 l1.daemon.wait_for_log("rpc_command hook 'invoice' already modified, ignoring.")
1826
1827 # Disable schema checking here!
1828 schemas = l1.rpc.jsonschemas
1829 l1.rpc.jsonschemas = {}
1830 # rpc_command_1 plugin sends a custom response to "listfunds"
1831 funds = l1.rpc.listfunds()
1832 assert funds[0] == "Custom rpc_command_1 result"
1833
1834 # Test command redirection to a plugin
1835 l1.rpc.call('help', ['developer'])
1836
1837 # Check the 'already modified' warning is not logged on just 'continue'
1838 assert not l1.daemon.is_in_log("rpc_command hook 'listfunds' already modified, ignoring.")
1839
1840 # Tests removing a chained hook in random order.
1841 # Note: This will get flaky by design if theres a problem.
1842 if bool(random.getrandbits(1)):
1843 l1.rpc.plugin_stop('rpc_command_2.py')
1844 l1.rpc.plugin_stop('rpc_command_1.py')
1845 else:
1846 l1.rpc.plugin_stop('rpc_command_1.py')
1847 l1.rpc.plugin_stop('rpc_command_2.py')
1848
1849 l1.rpc.jsonschemas = schemas
1850
1851
1852def test_libplugin(node_factory):

Callers

nothing calls this directly

Calls 9

joinMethod · 0.80
wait_for_logMethod · 0.80
is_in_logMethod · 0.80
plugin_stopMethod · 0.80
get_nodeMethod · 0.45
callMethod · 0.45
invoiceMethod · 0.45
decodeMethod · 0.45
listfundsMethod · 0.45

Tested by

no test coverage detected