def __init__(self, lightning_dir, bitcoind, port=9735, random_hsm=False, node_id=0): TailableProc.__init__(self, lightning_dir) self.lightning_dir = lightning_dir self.port = port self.cmd_prefix = [] self.disconnect_file = None self.rpcproxy = BitcoinRpcProxy(bitcoind) self.opts = LIGHTNINGD_CONFIG.copy() opts = { 'lightning-dir': lightning_dir, 'addr': '127.0.0.1:{}'.format(port), 'allow-deprecated-apis': 'false', 'network': 'regtest', 'ignore-fee-limits': 'false', 'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'], 'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'], } for k, v in opts.items(): self.opts[k] = v if not os.path.exists(lightning_dir): os.makedirs(lightning_dir) # Last 32-bytes of final part of dir -> seed. seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32] if not random_hsm: with open(os.path.join(lightning_dir, 'hsm_secret'), 'wb') as f: f.write(seed) if DEVELOPER: self.opts['dev-broadcast-interval'] = 1000 self.opts['dev-bitcoind-poll'] = 1 self.prefix = 'lightningd-%d' % (node_id)
def get_proxy(self): proxy = BitcoinRpcProxy(self) self.proxies.append(proxy) proxy.start() return proxy
class LightningD(TailableProc): def __init__(self, lightning_dir, bitcoind, port=9735, random_hsm=False, node_id=0): TailableProc.__init__(self, lightning_dir) self.lightning_dir = lightning_dir self.port = port self.cmd_prefix = [] self.disconnect_file = None self.rpcproxy = BitcoinRpcProxy(bitcoind) self.opts = LIGHTNINGD_CONFIG.copy() opts = { 'lightning-dir': lightning_dir, 'addr': '127.0.0.1:{}'.format(port), 'allow-deprecated-apis': 'false', 'network': 'regtest', 'ignore-fee-limits': 'false', 'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'], 'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'], } for k, v in opts.items(): self.opts[k] = v if not os.path.exists(lightning_dir): os.makedirs(lightning_dir) # Last 32-bytes of final part of dir -> seed. seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32] if not random_hsm: with open(os.path.join(lightning_dir, 'hsm_secret'), 'wb') as f: f.write(seed) if DEVELOPER: self.opts['dev-broadcast-interval'] = 1000 self.opts['dev-bitcoind-poll'] = 1 self.prefix = 'lightningd-%d' % (node_id) def cleanup(self): # To force blackhole to exit, disconnect file must be truncated! if self.disconnect_file: with open(self.disconnect_file, "w") as f: f.truncate() @property def cmd_line(self): opts = [] for k, v in sorted(self.opts.items()): if v is None: opts.append("--{}".format(k)) elif isinstance(v, list): for i in v: opts.append("--{}={}".format(k, i)) else: opts.append("--{}={}".format(k, v)) return self.cmd_prefix + ['lightningd/lightningd'] + opts def start(self): self.rpcproxy.start() self.opts['bitcoin-rpcport'] = self.rpcproxy.rpcport TailableProc.start(self) self.wait_for_log("Server started with public key") logging.info("LightningD started") def wait(self, timeout=10): """Wait for the daemon to stop for up to timeout seconds Returns the returncode of the process, None if the process did not return before the timeout triggers. """ self.proc.wait(timeout) self.rpcproxy.stop() return self.proc.returncode