lnd icon indicating copy to clipboard operation
lnd copied to clipboard

[bug]: `reserved wallet balance` takes unconfirmed balance into account

Open luisschwab opened this issue 6 months ago • 9 comments

Background

For a channel to be opened, the node's wallet needs to have enough funds for fee bumping anchor channel closings. Currently, enough funds includes unconfirmed balance (which can easily be RBF'ed to another address, leaving no funds for bumps), allowing the channel open to happen even without enough confirmed funds.

Your environment

$ lnd --version
lnd version 0.18.5-beta commit=v0.18.5-beta

Steps to reproduce

  1. Let x be the total_balance, and let y be the reserved_balance_anchor_chan.
  2. Try to open a channel of size z, where z = x - y. It should fail because of lack of reserve funds for the new channel.
  3. Deposit enough bitcoin such that it would leave enough funds for the channel's fee bumping anchor channel closing reserve. Try to open the channel again while the additional balance is unconfirmed. It opens.

Expected behaviour

While the additional balance is unconfirmed, channel opening should fail.

Actual behaviour

Channel opening succeeds.

luisschwab avatar Jun 10 '25 16:06 luisschwab

cc @MPins

luisschwab avatar Jun 10 '25 23:06 luisschwab

cc @MPins

I’ll be reproducing the issue on my regtest environment and let you know soon the results.

MPins avatar Jun 11 '25 15:06 MPins

Scenario 1: 1M sats total balance, with 500k confirmed and 500k unconfirmed As expected, I could not open a channel with 500k sats — the unconfirmed balance is not considered:

bob:/# lncli walletbalance
{
    "total_balance": "1000000",
    "confirmed_balance": "500000",
    "unconfirmed_balance": "500000",
    "locked_balance": "0",
bob:/# 
bob:/# lncli walletbalance
{
    "total_balance": "1000000",
    "confirmed_balance": "500000",
    "unconfirmed_balance": "500000",
    "locked_balance": "0",
    "reserved_balance_anchor_chan": "0",
    "account_balance": {
        "default": {
            "confirmed_balance": "500000",
            "unconfirmed_balance": "500000"
        }
    }
}
bob:/# lncli openchannel 02fe4a579e6e19960e044f0af85ebe93964c0fc845d504e2ed552fa7bda570454f 500000
[lncli] rpc error: code = Unknown desc = not enough witness outputs to create funding transaction, need 0.00506087 BTC only have 0.00500000 BTC available

Scenario 2: 1M sats confirmed As expected, with the full amount confirmed, the channel opens successfully:

bob:/# lncli walletbalance
{
    "total_balance": "1000000",
    "confirmed_balance": "1000000",
    "unconfirmed_balance": "0",
    "locked_balance": "0",
    "reserved_balance_anchor_chan": "0",
    "account_balance": {
        "default": {
            "confirmed_balance": "1000000",
            "unconfirmed_balance": "0"
        }
    }
}
bob:/# lncli openchannel 02fe4a579e6e19960e044f0af85ebe93964c0fc845d504e2ed552fa7bda570454f 500000
{
    "funding_txid": "b52b2e8c4d6f7b8490b2ab890864464b67c46f143cf705e43fe606145b358429"
}
bob:/# lncli walletbalance
{
    "total_balance": "488350",
    "confirmed_balance": "0",
    "unconfirmed_balance": "488350",
    "locked_balance": "1000000",
    "reserved_balance_anchor_chan": "10000",
    "account_balance": {
        "default": {
            "confirmed_balance": "0",
            "unconfirmed_balance": "488350"
        }
    }
}

Scenario 3: 488,350 sats confirmed, trying to open channel with 478,350 sats As expected, the channel does not open, due to anchor reservation constraints:

bob:/# lncli walletbalance
{
    "total_balance": "488350",
    "confirmed_balance": "488350",
    "unconfirmed_balance": "0",
    "locked_balance": "0",
    "reserved_balance_anchor_chan": "10000",
    "account_balance": {
        "default": {
            "confirmed_balance": "488350",
            "unconfirmed_balance": "0"
        }
    }
}
bob:/# lncli openchannel 023f8e2e0b312b16a7707e1111c37d914cf5ed9c7bf4f5056f20efdb4811492066 478350
[lncli] rpc error: code = Unknown desc = reserved wallet balance invalidated: transaction would leave insufficient funds for fee bumping anchor channel closings (see debug log for details)

Scenario 4: 488,350 confirmed + 500,000 unconfirmed — Channel opens 🤔 As pointed out by @luisschwab, when adding unconfirmed funds (bringing total to ~988k sats), the channel does open, even though the confirmed balance alone is still insufficient:

bob:/# lncli walletbalance
{
    "total_balance": "988350",
    "confirmed_balance": "488350",
    "unconfirmed_balance": "500000",
    "locked_balance": "0",
    "reserved_balance_anchor_chan": "10000",
    "account_balance": {
        "default": {
            "confirmed_balance": "488350",
            "unconfirmed_balance": "500000"
        }
    }
}
bob:/# lncli openchannel 023f8e2e0b312b16a7707e1111c37d914cf5ed9c7bf4f5056f20efdb4811492066 478350
{
    "funding_txid": "444f328b14869cf4e4f76def914ca23b44df17d4466212b031ada19ad4b6b848"
}
bob:/# lncli walletbalance
{
    "total_balance": "502300",
    "confirmed_balance": "0",
    "unconfirmed_balance": "502300",
    "locked_balance": "0",
    "reserved_balance_anchor_chan": "20000",
    "account_balance": {
        "default": {
            "confirmed_balance": "0",
            "unconfirmed_balance": "502300"
        }
    }
}

MPins avatar Jun 12 '25 20:06 MPins

@saubyk from my understanding, this does reveal an issue. If you agree, feel free to assign it to me.

MPins avatar Jun 12 '25 20:06 MPins

To be clear, Lnd does allow you to open channel with unconfirmed balances, but user has to specify that option as an argument.

So, based on the above testing my understanding of the issue is that, Lnd allowed the channel open with unconfirmed balances without the specific argument being supplied here. As long as that specific problem is being addressed it's ok to proceed.

IMO the resolution here would be that above scenario of open channel should only succeed if the user specifically indicates to use the unconfirmed balances.

saubyk avatar Jun 12 '25 23:06 saubyk

To be clear, Lnd does allow you to open channel with unconfirmed balances, but user has to specify that option as an argument.

You mean zero-conf channels, right?

So, based on the above testing my understanding of the issue is that, Lnd allowed the channel open with unconfirmed balances without the specific argument being supplied here. As long as that specific problem is being addressed it's ok to proceed.

Yes, that’s the issue.

IMO the resolution here would be that above scenario of open channel should only succeed if the user specifically indicates to use the unconfirmed balances.

Here the documentation about how to set up zero conf channels.

https://docs.lightning.engineering/lightning-network-tools/pool/zero-confirmation-channels

MPins avatar Jun 12 '25 23:06 MPins

You mean zero-conf channels, right?

No I don't mean zero-conf channels. Zero-channel is a channel which is active instantly without the channel opening transaction being confirmed.

What I am referring to is a regular channel, which is opened with UTXOs which are still unconfirmed. It will become active after the required confirmation depth is reached for the channel opening transaction.

The parameter min_confs can be set to zero to enable unconfirmed UTXO to be utilized for opening channels: https://lightning.engineering/api-docs/api/lnd/lightning/open-channel/

saubyk avatar Jun 13 '25 03:06 saubyk

What I am referring to is a regular channel, which is opened with UTXOs which are still unconfirmed. It will become active after the required confirmation depth is reached for the channel opening transaction.

That's not the scenario here: when I first discovered this, I had enough confirmed funds for the channel but not for the bump reserve; that was unconfirmed. I could RBF those unconfirmed funds elsewhere and have no reserve for the bump, since unconfirmed funds cannot be locked by the internal wallet. For example:

  • 100k confirmed + 0 unconfirmed -> 100k channel won't open
  • 100k confirmed + 10k unconfirmed -> 100k channel will open, but reserve can be RBF'ed

luisschwab avatar Jun 13 '25 03:06 luisschwab

You mean zero-conf channels, right?

No I don't mean zero-conf channels. Zero-channel is a channel which is active instantly without the channel opening transaction being confirmed.

What I am referring to is a regular channel, which is opened with UTXOs which are still unconfirmed. It will become active after the required confirmation depth is reached for the channel opening transaction.

The parameter min_confs can be set to zero to enable unconfirmed UTXO to be utilized for opening channels: https://lightning.engineering/api-docs/api/lnd/lightning/open-channel/

Got it — you're referring to the --min_conf=0 flag on the openchannel command. Yes, absolutely! If we explicitly set --min_conf=0, then it's expected that the channel can be opened using unconfirmed funds. 🫡

MPins avatar Jun 13 '25 09:06 MPins