MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_channel_persistence

Function test_channel_persistence

tests/test_connection.py:2328–2411  ·  view source on GitHub ↗
(node_factory, bitcoind, executor)

Source from the content-addressed store, hash-verified

2326@unittest.skipIf(os.environ.get("TEST_CHECK_DBSTMTS", None) == "1",
2327 "We kill l2, dblog plugin replay will be unreliable")
2328def test_channel_persistence(node_factory, bitcoind, executor):
2329 # Start two nodes and open a channel (to remember). l2 will
2330 # mysteriously die while committing the first HTLC so we can
2331 # check that HTLCs reloaded from the DB work.
2332 # Feerates identical so we don't get gratuitous commit to update them
2333 disable_commit_after = 1
2334 if EXPERIMENTAL_DUAL_FUND:
2335 disable_commit_after = 2
2336
2337 l1 = node_factory.get_node(may_reconnect=True, feerates=(7500, 7500, 7500,
2338 7500))
2339 l2 = node_factory.get_node(options={'dev-disable-commit-after': disable_commit_after},
2340 may_reconnect=True, start=False)
2341 # Saving IO can cause JSON errors when we check it, due to partial writes if we
2342 # get lucky when we kill it.
2343 del l2.daemon.opts['dev-save-plugin-io']
2344 l2.start()
2345 l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
2346
2347 # Neither node should have a channel open, they are just connected
2348 for n in (l1, l2):
2349 assert(n.db_query('SELECT COUNT(id) as count FROM channels;')[0]['count'] == 0)
2350
2351 l1.fundchannel(l2, 100000)
2352
2353 channels = l1.rpc.listpeerchannels()['channels']
2354 assert(only_one(channels)['state'] == 'CHANNELD_NORMAL')
2355
2356 # Both nodes should now have exactly one channel in the database
2357 for n in (l1, l2):
2358 assert(n.db_query('SELECT COUNT(id) as count FROM channels;')[0]['count'] == 1)
2359
2360 # Fire off a sendpay request, it'll get interrupted by a restart
2361 executor.submit(l1.pay, l2, 10000)
2362 # Wait for it to be committed to, i.e., stored in the DB
2363 l1.daemon.wait_for_log('peer_in WIRE_CHANNEL_READY')
2364 l1.daemon.wait_for_log('peer_in WIRE_COMMITMENT_SIGNED')
2365
2366 # Stop l2, l1 will reattempt to connect
2367 print("Killing l2 in mid HTLC")
2368 l2.daemon.kill()
2369
2370 # Clear the disconnect and timer stop so we can proceed normally
2371 del l2.daemon.opts['dev-disable-commit-after']
2372
2373 # Wait for l1 to notice
2374 wait_for(lambda: 'connected' not in l1.rpc.listpeerchannels()['channels'])
2375
2376 # Now restart l2 and it should reload peers/channels from the DB
2377 l2.start()
2378 wait_for(lambda: len(l2.rpc.listpeers()['peers']) == 1)
2379
2380 # Wait for the restored HTLC to finish
2381 wait_for(lambda: only_one(l1.rpc.listpeerchannels()['channels'])['to_us_msat'] == 99990000)
2382
2383 wait_for(lambda: len([p for p in l1.rpc.listpeers()['peers'] if p['connected']]))
2384 wait_for(lambda: len([p for p in l2.rpc.listpeers()['peers'] if p['connected']]))
2385

Callers

nothing calls this directly

Calls 15

only_oneFunction · 0.90
wait_forFunction · 0.90
db_queryMethod · 0.80
listpeerchannelsMethod · 0.80
wait_for_logMethod · 0.80
killMethod · 0.80
restartMethod · 0.80
mock_rpcMethod · 0.80
dev_failMethod · 0.80
generate_blockMethod · 0.80
get_nodeMethod · 0.45

Tested by

no test coverage detected