Skip to content

Commit 8ed511b

Browse files
committed
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 <[email protected]>
1 parent 97434d9 commit 8ed511b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/test_lightningd.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,61 @@ def test_funding_change(self):
21602160
assert outputs[0] > 8990000
21612161
assert outputs[2] == 10000000
21622162

2163+
def test_funding_fail(self):
2164+
"""Add some funds, fund a channel without enough funds"""
2165+
# Previous runs with same bitcoind can leave funds!
2166+
l1 = self.node_factory.get_node(random_hsm=True)
2167+
max_locktime = 3 * 6 * 24
2168+
l2 = self.node_factory.get_node(options=['--locktime-blocks={}'.format(max_locktime + 1)])
2169+
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
2170+
2171+
addr = l1.rpc.newaddr()['address']
2172+
txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.01)
2173+
bitcoind.generate_block(1)
2174+
2175+
# Wait for it to arrive.
2176+
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) > 0)
2177+
2178+
# Fail because l1 dislikes l2's huge locktime.
2179+
try:
2180+
l1.rpc.fundchannel(l2.info['id'], 100000)
2181+
except ValueError as verr:
2182+
str(verr).index('to_self_delay {} larger than {}'
2183+
.format(max_locktime+1, max_locktime))
2184+
except Exception as err:
2185+
self.fail("Unexpected exception {}".format(err))
2186+
else:
2187+
self.fail("huge locktime ignored?")
2188+
2189+
# We don't have enough left to cover fees if we try to spend it all.
2190+
try:
2191+
l1.rpc.fundchannel(l2.info['id'], 1000000)
2192+
except ValueError as verr:
2193+
str(verr).index('Cannot afford funding transaction')
2194+
except Exception as err:
2195+
self.fail("Unexpected exception {}".format(err))
2196+
else:
2197+
self.fail("We somehow covered fees?")
2198+
2199+
# Should still be connected.
2200+
assert l1.rpc.getpeers()['peers'][0]['connected']
2201+
assert l2.rpc.getpeers()['peers'][0]['connected']
2202+
2203+
# Restart l2 without ridiculous locktime.
2204+
l2.daemon.proc.terminate()
2205+
2206+
l2.daemon.cmd_line.remove('--locktime-blocks={}'.format(max_locktime + 1))
2207+
2208+
# Wait for l1 to notice
2209+
wait_for(lambda: len(l1.rpc.getpeers()['peers']) == 0)
2210+
2211+
# Now restart l2, reconnect.
2212+
l2.daemon.start()
2213+
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
2214+
2215+
# This works.
2216+
l1.rpc.fundchannel(l2.info['id'], int(0.01 * 10**8 / 2))
2217+
21632218
def test_addfunds_from_block(self):
21642219
"""Send funds to the daemon without telling it explicitly
21652220
"""

0 commit comments

Comments
 (0)