Skip to content

Commit a52a543

Browse files
committed
channeldb: add BalanceAboveReserve helper
We add this helper which will be used in a following commit. For the noop htlcs that we are going to add we need to know whether the "potential" receiver's balance is above reserve, so we add this specific helper that helps us safely perform this check. To account for entries that have already been processed, and to avoid bursts of HTLC going way over the reserve, we also accept the delta parameter which keeps track of amounts that will be credited to the receiver.
1 parent 92a5d35 commit a52a543

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

channeldb/channel.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,32 @@ func (c *OpenChannel) clearChanStatus(status ChannelStatus) error {
21272127
return nil
21282128
}
21292129

2130+
// BalanceAboveReserve checks if the balance for the provided party is above the
2131+
// configured reserve. It also uses the balance delta for the party, to account
2132+
// for entry amounts that have been processed already.
2133+
func (c *OpenChannel) BalanceAboveReserve(
2134+
party lntypes.ChannelParty, delta int64) bool {
2135+
2136+
c.Lock()
2137+
defer c.Unlock()
2138+
2139+
// We need to add the delta to the balance we're checking against the
2140+
// reserve.
2141+
deltaAmt := lnwire.MilliSatoshi(delta).ToSatoshis()
2142+
2143+
switch {
2144+
case party.IsLocal():
2145+
return c.LocalCommitment.LocalBalance.ToSatoshis()+deltaAmt >
2146+
c.LocalChanCfg.ChanReserve
2147+
2148+
case party.IsRemote():
2149+
return c.RemoteCommitment.RemoteBalance.ToSatoshis()+deltaAmt >
2150+
c.RemoteChanCfg.ChanReserve
2151+
}
2152+
2153+
return false
2154+
}
2155+
21302156
// putOpenChannel serializes, and stores the current state of the channel in its
21312157
// entirety.
21322158
func putOpenChannel(chanBucket kvdb.RwBucket, channel *OpenChannel) error {

0 commit comments

Comments
 (0)