contrib/pyln-testing/pyln/testing/utils.py: Add a fundbalancedchannel method to LightningNode, for when you want to Thanos your channels.

This commit is contained in:
ZmnSCPxj jxPCSnmZ 2020-08-06 09:37:43 +08:00 committed by Rusty Russell
parent 3edbacbb3c
commit 8c763bb7be

View File

@ -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']]