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

Function _inline_plugin

contrib/pyln-testing/pyln/testing/utils.py:1982–2064  ·  view source on GitHub ↗

Set up an inline plugin serve thread for a not-yet-started node. Normally called via get_node(inline_plugin=setup_fn). The plugin's cwd (set by lightningd) is node.daemon.lightning_dir/TEST_NETWORK/, which is where the shim looks for inline-plugin.sock. Example:: def setu

(node, setup_fn)

Source from the content-addressed store, hash-verified

1980
1981
1982def _inline_plugin(node, setup_fn):
1983 """Set up an inline plugin serve thread for a not-yet-started node.
1984
1985 Normally called via get_node(inline_plugin=setup_fn). The plugin's cwd
1986 (set by lightningd) is node.daemon.lightning_dir/TEST_NETWORK/, which is
1987 where the shim looks for inline-plugin.sock.
1988
1989 Example::
1990
1991 def setup(plugin):
1992 @plugin.method('greet')
1993 def greet(name, plugin):
1994 return {'message': f'hello {name}'}
1995
1996 l1 = node_factory.get_node(inline_plugin=setup)
1997 assert l1.rpc.greet('world') == {'message': 'hello world'}
1998 """
1999 sock_path = os.path.join(node.daemon.lightning_dir, TEST_NETWORK, 'inline-plugin.sock')
2000 srv = socket.socket(socket.AF_UNIX)
2001 try:
2002 srv.bind(sock_path)
2003 except OSError as e:
2004 # AF_UNIX caps the bind path (108 bytes on Linux, 104 on macOS),
2005 # and the node dir embeds the (possibly long) test name. Bind
2006 # through a short symlink alias to the socket's directory -- the
2007 # bind-side analogue of UnixSocket.connect's Darwin workaround
2008 # (bind can't go through a dangling final-component symlink, so
2009 # alias the directory rather than the socket). The socket file
2010 # still lands at sock_path, where the shim's cwd-relative connect
2011 # expects it.
2012 if e.args[0] != "AF_UNIX path too long":
2013 raise
2014 alias_dir = tempfile.mkdtemp(prefix='pyln-sock-')
2015 alias = os.path.join(alias_dir, 'd')
2016 os.symlink(os.path.dirname(sock_path), alias)
2017 try:
2018 srv.bind(os.path.join(alias, os.path.basename(sock_path)))
2019 finally:
2020 os.unlink(alias)
2021 os.rmdir(alias_dir)
2022 srv.listen(1)
2023
2024 plugin = Plugin(autopatch=False)
2025 for handler in logging.getLogger().handlers:
2026 if isinstance(handler, PluginLogHandler) and handler.plugin is plugin:
2027 handler.addFilter(_NoPylnInternalsFilter())
2028 setup_fn(plugin)
2029
2030 def serve():
2031 while True:
2032 conn, _ = srv.accept()
2033
2034 class _SockWriter:
2035 def write(self, data):
2036 try:
2037 conn.sendall(data)
2038 except OSError:
2039 pass

Callers 1

get_nodeMethod · 0.85

Calls 4

PluginClass · 0.90
joinMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected