From 8c763bb7beb7f9bb2f07a523fff3b88c7e1f50cf Mon Sep 17 00:00:00 2001 From: ZmnSCPxj jxPCSnmZ Date: Thu, 6 Aug 2020 09:37:43 +0800 Subject: [PATCH] contrib/pyln-testing/pyln/testing/utils.py: Add a fundbalancedchannel method to LightningNode, for when you want to Thanos your channels. --- contrib/pyln-testing/pyln/testing/utils.py | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 37b5900a6..f07895a79 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -6,6 +6,7 @@ from collections import OrderedDict from decimal import Decimal from ephemeral_port_reserve import reserve from pyln.client import LightningRpc +from pyln.client import Millisatoshi import json import logging @@ -644,6 +645,30 @@ class LightningNode(object): self.daemon.wait_for_log('Owning output .* txid {} CONFIRMED'.format(txid)) return addr, txid + def fundbalancedchannel(self, remote_node, total_capacity, announce=True): + ''' + Creates a perfectly-balanced channel, as all things should be. + ''' + if isinstance(total_capacity, Millisatoshi): + total_capacity = int(total_capacity.to_satoshi()) + else: + total_capacity = int(total_capacity) + + self.fundwallet(total_capacity + 10000) + self.rpc.connect(remote_node.info['id'], 'localhost', remote_node.port) + + # Make sure the fundchannel is confirmed. + num_tx = len(self.bitcoin.rpc.getrawmempool()) + tx = self.rpc.fundchannel(remote_node.info['id'], total_capacity, feerate='slow', minconf=0, announce=announce, push_msat=Millisatoshi(total_capacity * 500))['tx'] + wait_for(lambda: len(self.bitcoin.rpc.getrawmempool()) == num_tx + 1) + self.bitcoin.generate_block(1) + + # Generate the scid. + # NOTE This assumes only the coinbase and the fundchannel is + # confirmed in the block. + return '{}x1x{}'.format(self.bitcoin.rpc.getblockcount(), + get_tx_p2wsh_outnum(self.bitcoin, tx, total_capacity)) + def getactivechannels(self): return [c for c in self.rpc.listchannels()['channels'] if c['active']]