From 8ed511b3c71b2881ae0745f874878a4fdc369866 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 6 Dec 2017 14:19:50 +1030 Subject: [PATCH] test_lightningd: add test for funding failures. We should not disconnect from a peer just because it fails opening; we should return it to gossipd, and give a meaningful error. Closes: #401 Signed-off-by: Rusty Russell --- tests/test_lightningd.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index b8aca6e40..c1787bf18 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -2160,6 +2160,61 @@ class LightningDTests(BaseLightningDTests): assert outputs[0] > 8990000 assert outputs[2] == 10000000 + def test_funding_fail(self): + """Add some funds, fund a channel without enough funds""" + # Previous runs with same bitcoind can leave funds! + l1 = self.node_factory.get_node(random_hsm=True) + max_locktime = 3 * 6 * 24 + l2 = self.node_factory.get_node(options=['--locktime-blocks={}'.format(max_locktime + 1)]) + l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) + + addr = l1.rpc.newaddr()['address'] + txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.01) + bitcoind.generate_block(1) + + # Wait for it to arrive. + wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0) + + # Fail because l1 dislikes l2's huge locktime. + try: + l1.rpc.fundchannel(l2.info['id'], 100000) + except ValueError as verr: + str(verr).index('to_self_delay {} larger than {}' + .format(max_locktime+1, max_locktime)) + except Exception as err: + self.fail("Unexpected exception {}".format(err)) + else: + self.fail("huge locktime ignored?") + + # We don't have enough left to cover fees if we try to spend it all. + try: + l1.rpc.fundchannel(l2.info['id'], 1000000) + except ValueError as verr: + str(verr).index('Cannot afford funding transaction') + except Exception as err: + self.fail("Unexpected exception {}".format(err)) + else: + self.fail("We somehow covered fees?") + + # Should still be connected. + assert l1.rpc.getpeers()['peers'][0]['connected'] + assert l2.rpc.getpeers()['peers'][0]['connected'] + + # Restart l2 without ridiculous locktime. + l2.daemon.proc.terminate() + + l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1)) + + # Wait for l1 to notice + wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 0) + + # Now restart l2, reconnect. + l2.daemon.start() + l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) + + # This works. + l1.rpc.fundchannel(l2.info['id'], int(0.01 * 10**8 / 2)) + def test_addfunds_from_block(self): """Send funds to the daemon without telling it explicitly """