Skip to content

Commit

Permalink
Add SpendableBalanceFromParts function
Browse files Browse the repository at this point in the history
This is needed in geewallet for getting the spendable balance of channel
when we only have the components of a Channel object but not the Channel
object itself.

This method can be moved onto SavedChannelState when the necessary
fields remaining in Channel/Commitments are moved there.
  • Loading branch information
canndrew committed Jun 17, 2021
1 parent 5b3878f commit bdbd7f0
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,18 +1159,22 @@ and Channel = {
(not self.SavedChannelState.LocalChanges.ACKed.IsEmpty)
|| (not self.Commitments.ProposedRemoteChanges.IsEmpty)

member self.SpendableBalance (): LNMoney =
let remoteParams = self.SavedChannelState.StaticChannelConfig.RemoteParams
// FIXME: This is a temporary hack to make the geewallet updates easier.
static member SpendableBalanceFromParts (savedChannelState: SavedChannelState)
(remoteNextCommitInfo: Option<RemoteNextCommitInfo>)
(commitments: Commitments)
: LNMoney =
let remoteParams = savedChannelState.StaticChannelConfig.RemoteParams
let remoteCommit =
match self.RemoteNextCommitInfo with
match remoteNextCommitInfo with
| Some (RemoteNextCommitInfo.Waiting nextRemoteCommit) -> nextRemoteCommit
| Some (RemoteNextCommitInfo.Revoked _info) -> self.SavedChannelState.RemoteCommit
| Some (RemoteNextCommitInfo.Revoked _info) -> savedChannelState.RemoteCommit
// TODO: This could return a proper error, or report the full balance
| None -> failwith "funding is not locked"
let reducedRes =
remoteCommit.Spec.Reduce(
self.SavedChannelState.RemoteChanges.ACKed,
self.Commitments.ProposedLocalChanges
savedChannelState.RemoteChanges.ACKed,
commitments.ProposedLocalChanges
)
let reduced =
match reducedRes with
Expand All @@ -1181,7 +1185,7 @@ and Channel = {
err
| Ok reduced -> reduced
let fees =
if self.SavedChannelState.StaticChannelConfig.IsFunder then
if savedChannelState.StaticChannelConfig.IsFunder then
Transactions.commitTxFee remoteParams.DustLimitSatoshis reduced
|> LNMoney.FromMoney
else
Expand All @@ -1198,6 +1202,13 @@ and Channel = {
let spendableBalance = LNMoney.Max(untrimmedMax, untrimmedSpendableBalance)
spendableBalance


member self.SpendableBalance (): LNMoney =
Channel.SpendableBalanceFromParts
self.SavedChannelState
self.RemoteNextCommitInfo
self.Commitments

member private self.sendCommit (remoteNextCommitInfo: RemoteNextCommitInfo)
: Result<CommitmentSignedMsg * Channel, ChannelError> =
let channelPrivKeys = self.ChannelPrivKeys
Expand Down

0 comments on commit bdbd7f0

Please sign in to comment.