lightningnetwork / lnd

Lightning Network Daemon ⚡️
MIT License
7.69k stars 2.08k forks source link

btcwallet: don't initialize wallet namespace if scrypt derivation fails due to low memory #6210

Open callebtc opened 2 years ago

callebtc commented 2 years ago

Background

lnd is killed at wallet creation.

Your environment

Steps to reproduce

I start with a fresh lnd with the Raspibolt config except that I use the neutrino backend. I use the ARMv7 binary since I'm running this on a Rpi Zero 2 W.

I start lnd and it does it's thing, waiting for me to create a wallet. In another session, I run lncli create. I see this:

x@y:~/.lnd $ lncli create
Input wallet password:
Confirm password:

Do you have an existing cipher seed mnemonic or extended master root key you want to use?
Enter 'y' to use an existing cipher seed mnemonic, 'x' to use an extended master root key
or 'n' to create a new seed (Enter y/x/n): n

Your cipher seed can optionally be encrypted.
Input your passphrase if you wish to encrypt it (or press enter to proceed without a cipher seed passphrase):

Generating fresh cipher seed...

!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!!

---------------BEGIN LND CIPHER SEED---------------
 1. abandon    2. erupt   3. club       4. motion
 5. cabbage    6. leaf    7. build      8. fish
 9. enemy     10. basic  11. peanut    12. ski
13. slide     14. place  15. mechanic  16. fiction
17. crane     18. firm   19. chimney   20. rack
21. artefact  22. buyer  23. action    24. develop
---------------END LND CIPHER SEED-----------------

!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!!

Around 10 seconds later, lncli terminates with:

[lncli] rpc error: code = Unavailable desc = error reading from server: EOF

At the same time, lnd gets killed only saying:

Killed

At this point I have created a wallet. I can try to run lnd again but I get

error creating wallet config: error unlocking wallet with password from file: missing address manager namespace

To check, I uncomment these lines in lnd.conf so I can enter the password manually:

wallet-unlock-password-file=/asd/password.txt
wallet-unlock-allow-create=true

I start lnd again, this time it runs but wants me to unlock the wallet manually:

2022-01-27 23:18:06.862 [INF] LTND: Waiting for wallet encryption password. Use `lncli create` to create a wallet, `lncli unlock` to unlock an existing wallet, or `lncli changepassword` to change the password of an existing wallet and unlock it.

I do that with lncli and now I get:

x@y:~/.lnd $ lncli unlock
Input wallet password: <I enter the pw here>
[lncli] rpc error: code = Unknown desc = missing address manager namespace
x@y:~/.lnd $

lnd keeps running. Can't unlock the wallet.

I delete everything in the .lnd directory except for my lnd.conf file and start over. Same thing happens.

:(

Roasbeef commented 2 years ago

Do you have any swap alloacted? I'm guessing the initial scrypt operation to encrypt the keys on disk is running out of memory. Depending on the type of telemetry you have on the machine, you can watch the memory usage.

There're some other settings you can use on the lnd front to reduce memory usage as well.

Roasbeef commented 2 years ago

Closing due to inactivity. The issue here seems to be lack of swap AFAICT, will re-open if we get any other information.

joostjager commented 2 years ago

I think I may have a repro for this scenario. Ran into it as well in a docker environment with insufficient memory. It isn't great, because wallet db seems to get into a state that lnd can't recover from. Restarting doesn't help.

Repro steps:

joostjager commented 2 years ago

The reason seems to be that an empty wallet file is already created, but not yet initialized. This will skip the initialization on the next run.

joostjager commented 1 year ago

@saubyk can this be re-opened? I've added a repro scenario above.