From da547bc4d9be1f133e1ebe489407d8cf6ea48f4b Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Fri, 19 Jan 2024 18:18:58 +0530 Subject: [PATCH] tests/test_misc.py: Add test_recover_plugin to check if the funds are being recovered when we lose some state and enter recover mode. --- tests/test_misc.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_plugin.py | 2 ++ 2 files changed, 54 insertions(+) diff --git a/tests/test_misc.py b/tests/test_misc.py index fa6e45ddce0e..683c355d4d24 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2808,6 +2808,58 @@ def test_emergencyrecover(node_factory, bitcoind): assert l2.rpc.listfunds()["channels"][0]["state"] == "ONCHAIN" +@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "sqlite3-specific DB rollback") +@pytest.mark.openchannel('v1') +@pytest.mark.openchannel('v2') +def test_recover_plugin(node_factory, bitcoind): + l1 = node_factory.get_node(may_reconnect=True, options={'log-level': 'info', 'experimental-peer-storage': None}, + allow_warning=True, + feerates=(7500, 7500, 7500, 7500)) + l2 = node_factory.get_node(may_reconnect=True, options={'log-level': 'info', 'experimental-peer-storage': None}, + feerates=(7500, 7500, 7500, 7500), allow_broken_log=True, allow_bad_gossip=True) + + l3 = node_factory.get_node(may_reconnect=True, options={'log-level': 'info', 'experimental-peer-storage': None}, + feerates=(7500, 7500, 7500, 7500), allow_broken_log=True) + + l1.rpc.connect(l2.info['id'], 'localhost', l2.port) + l2.fundchannel(l1, 10**6) + mine_funding_to_announce(bitcoind, [l1, l2, l3]) + + l2.stop() + + # Save copy of the db. + dbpath = os.path.join(l2.daemon.lightning_dir, TEST_NETWORK, "lightningd.sqlite3") + orig_db = open(dbpath, "rb").read() + l2.start() + + l2.rpc.connect(l3.info['id'], 'localhost', l3.port) + + l2.fundchannel(l3, 10**6) + mine_funding_to_announce(bitcoind, [l1, l2, l3]) + + l2.pay(l1, 200000000) + + # Now, move l2 back in time. + l2.stop() + + # Overwrite with OLD db. + open(dbpath, "wb").write(orig_db) + l2.start() + + bitcoind.generate_block(5, wait_for_mempool=1) + sync_blockheight(bitcoind, [l1, l2, l3]) + + l2.daemon.wait_for_log(f"{l3.info['id']}-chan#2: State changed from FUNDING_SPEND_SEEN to ONCHAIN") + wait_for(lambda: l2.rpc.listfunds()["channels"][1]["state"] == "ONCHAIN") + + l2.daemon.wait_for_log(f"{l1.info['id']}-chan#1: State changed from FUNDING_SPEND_SEEN to ONCHAIN") + wait_for(lambda: l2.rpc.listfunds()["channels"][0]["state"] == "ONCHAIN") + + # Both channels should go ONCHAIN! + assert l2.rpc.listfunds()["channels"][0]["state"] == "ONCHAIN" + assert l2.rpc.listfunds()["channels"][0]["state"] == "ONCHAIN" + + @unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "deletes database, which is assumed sqlite3") def test_restorefrompeer(node_factory, bitcoind): """ diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 98fbf621da53..7082baea6aeb 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -3750,6 +3750,8 @@ def test_sql(node_factory, bitcoind): 'type': 'u32'}, {'name': 'ignore_fee_limits', 'type': 'boolean'}, + {'name': 'lost_state', + 'type': 'boolean'}, {'name': 'feerate_perkw', 'type': 'u32'}, {'name': 'feerate_perkb',