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

Function test_recover_command

tests/test_misc.py:4875–4934  ·  view source on GitHub ↗
(node_factory, bitcoind, old_hsmsecret)

Source from the content-addressed store, hash-verified

4873@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "deletes database, which is assumed sqlite3")
4874@pytest.mark.parametrize("old_hsmsecret", [False, True])
4875def test_recover_command(node_factory, bitcoind, old_hsmsecret):
4876 l1, l2 = node_factory.get_nodes(2, opts={'old_hsmsecret': old_hsmsecret})
4877
4878 l1oldid = l1.info['id']
4879
4880 def get_hsm_secret(n):
4881 """Returns recoversecret and hex"""
4882 hsmfile = os.path.join(n.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
4883 recover = subprocess.check_output(["tools/lightning-hsmtool", "getsecret", hsmfile, "leet"]).decode('utf-8').strip()
4884 with open(hsmfile, "rb") as f:
4885 hexhsm = f.read().hex()
4886 return recover, hexhsm
4887
4888 l1recover, l1hex = get_hsm_secret(l1)
4889 l2recover, l2hex = get_hsm_secret(l2)
4890
4891 # Get the PID for later
4892 with open(os.path.join(l1.daemon.lightning_dir,
4893 f"lightningd-{TEST_NETWORK}.pid"), "r") as f:
4894 pid = f.read().strip()
4895
4896 assert l1.rpc.check('recover', hsmsecret=l2recover) == {'command_to_check': 'recover'}
4897 l1.rpc.recover(hsmsecret=l2recover)
4898 l1.daemon.wait_for_log("Server started with public key")
4899 # l1.info is cached on start, so won't reflect current reality!
4900 assert l1.rpc.getinfo()['id'] == l2.info['id']
4901
4902 # Won't work if we issue an address...
4903 l2.rpc.newaddr()
4904
4905 with pytest.raises(RpcError, match='Node has already issued bitcoin addresses'):
4906 l2.rpc.recover(hsmsecret=l1recover)
4907
4908 with pytest.raises(RpcError, match='Node has already issued bitcoin addresses'):
4909 l2.rpc.check('recover', hsmsecret=l1recover)
4910
4911 # Now try recovering using hex secret (remove old prerecover!)
4912 shutil.rmtree(os.path.join(l1.daemon.lightning_dir, TEST_NETWORK,
4913 f"lightning.pre-recover.{pid}"))
4914
4915 # l1 already has --recover in cmdline: recovering again would add it
4916 # twice!
4917 with pytest.raises(RpcError, match='Already doing recover'):
4918 l1.rpc.check('recover', hsmsecret=l1recover)
4919
4920 with pytest.raises(RpcError, match='Already doing recover'):
4921 l1.rpc.recover(hsmsecret=l1recover)
4922
4923 l1.restart()
4924
4925 if old_hsmsecret:
4926 assert l1.rpc.check('recover', hsmsecret=l1hex) == {'command_to_check': 'recover'}
4927 l1.rpc.recover(hsmsecret=l1hex)
4928 else:
4929 # Modern style requires mnemonic arg.
4930 assert l1.rpc.check('recover', hsmsecret=l1recover) == {'command_to_check': 'recover'}
4931 l1.rpc.recover(hsmsecret=l1recover)
4932

Callers

nothing calls this directly

Calls 10

get_nodesMethod · 0.80
joinMethod · 0.80
recoverMethod · 0.80
wait_for_logMethod · 0.80
restartMethod · 0.80
get_hsm_secretFunction · 0.70
readMethod · 0.45
checkMethod · 0.45
getinfoMethod · 0.45
newaddrMethod · 0.45

Tested by

no test coverage detected