A bit of a story about my struggles to get BTC out of LND (Part 1)

So I've been working for a while now to get all my BTC back from my LND node which was up for almost a year.

It went through lots of upgrades, and the DB is probably not in tip-top shape either.

So first of all, I closed all of my channels.  I had about 100 channels on the node.  I was able to cooperatively close many of them, but I had to force close about 20.  That means it takes a while to get the funds back.  And hopefully, the DB state is correct that I don't accidentally broadcast an old state closing transaction and lose all the funds!

So I closed it all up and I have about 0.6 BTC in my wallet.  not bad.  I want to send it to my new node.

I sent 0.3 BTC over (from the cooperative closes), and there remains about 0.3 BTC waiting to confirm.  Wait overnight...

The next day I use the new -sweepall method on sendcoins to move the rest of the coins, but I"m met with an error:  UTXO already spent.

That doesn't sound good.  I do some research and I find the dropwutxo tool https://github.com/btcsuite/btcwallet/blob/master/cmd/dropwtxmgr/main.go 

I backup my wallet and give it a try.  looks like it works.  I restart LND and it has to re-scan the blockchain for all the 4800 wallet addresses in the db.  This takes about 4 hours.

I open up and check my wallet balance.  0.05 BTC.  yikes!  Where'd it go?  OK - I can accept some of them may have been spent UTXO, but 0.25 BTC is a decent hit to the wallet.

I check my logs and see that there is a sweep transaction which tries to be broadcast, but it doesn't look like it actually gets broadcast.  Strange.

I do a pendingchannels and find:

"waiting_close_channels": [
        {
            "channel": {
                "remote_node_pub": "0332052748755d74b3395b91eaea78b53bd64b55eb031ccb6a7ae23ac445bb6af7",
                "channel_point": "f5c6598c5ad95bdd7335eeb2ec8f8cda4829d7bdb974447216edfc8f3c3df9ee:1",
                "capacity": "3000000",
                "local_balance": "2998914",
                "remote_balance": "0"
            },
            "limbo_balance": "2998914"
        }
    ]
Ok, so I have a little bit in limbo (0.03 BTC).  I do a force close on the channel point and it broadcasts the closing transaction.  It confirms and then goes over to pending:

So 360 more blocks until I can spend that.

        {
            "channel": {
                "remote_node_pub": "0332052748755d74b3395b91eaea78b53bd64b55eb031ccb6a7ae23ac445bb6af7",
                "channel_point": "f5c6598c5ad95bdd7335eeb2ec8f8cda4829d7bdb974447216edfc8f3c3df9ee:1",
                "capacity": "3000000",
                "local_balance": "2998914",
                "remote_balance": "0"
            },
            "closing_txid": "e0eb8dbca98a908d73f8f269bf53c0b67cb0eb09f76110de44f6b349352a1238",
            "limbo_balance": "2998914",
            "maturity_height": 561058,
            "blocks_til_maturity": 360,
            "recovered_balance": "0",
            "pending_htlcs": [
            ]
        }
So what else do I have in my pending channels? 

 "total_limbo_balance": "6753308",

So that's 12488381 Satoshi.  I'm still down to 0.12 BTC from the 0.3 I thought I had.  If I can extract that limbo balance!

Let's look at one of the  "pending_force_closing_channels"

{
            "channel": {
                "remote_node_pub": "020d41c5a32f06d7addc7378f2d4a6596358d8716a3eb5d5bbdd894a82a1fdd00c",
                "channel_point": "9cebba83a510e9b2d52bbaeb9986b9e57ba86ff7adec1310c6afda253334a408:0",
                "capacity": "58000",
                "local_balance": "602",
                "remote_balance": "0"
            },
            "closing_txid": "bcec52500748200ed0848ddfad1e9ed30f7ae1c552c528bbd9da69d52a291f59",
            "limbo_balance": "602",
            "maturity_height": 542671,
            "blocks_til_maturity": -18027,
            "recovered_balance": "0",
            "pending_htlcs": [
            ]
        },
Wait - what?  negative blocks_til_maturity?

ok, let's see if that closing_txid is in my chaintxns:

listchaintxns | grep bcec52500748200ed0848ddfad1e9ed30f7ae1c552c528bbd9da69d52a291f59
Nothing.  So it's not being recorded by the wallet.  I check the blockchain: https://blockstream.info/address/bc1qf5khvwsclgqq76ge4es323x8df6u9hzhkv8j4m6tswu2g4vtk4vqlr329x

Yep, there sits my 602 satoshi!

Wait!  I notice something else in listchaintxns:

        {
            "tx_hash": "e0eb8dbca98a908d73f8f269bf53c0b67cb0eb09f76110de44f6b349352a1238",
            "amount": "0",
            "num_confirmations": 1,
            "block_hash": "00000000000000000024a0303bb5e46bfcee93fb352b177136b04b7d618c8a4b",
            "block_height": 560698,
            "time_stamp": "1548809395",
            "total_fees": "0",
            "dest_addresses": [
                "bc1qnc39p44v0sxxuxhaf26xqf8lry3s6mg3gu9nrrdw5uclwp5rnarqtq73ze"
            ]
        }
Amount 0?  I am hoping this means that we're just waiting for the 360 confirmations.

Still haven't found the transaction for my 602 satoshi.  There are many others missing as well but I used that one as an example.

I found a long thread discussing negative maturity on the lnd github: https://github.com/lightningnetwork/lnd/issues/1610 

So, it looks like the key is restarting lnd?

Looks like LND can't sweep the funds.  It creates a bad sweep transaction.  Time to go to roasbeef for help!

I'll update with what happens.  I want my 0.067 BTC back!