Skip to content

Commit

Permalink
Core/Channel: function to calc outpoints of commitTx
Browse files Browse the repository at this point in the history
Currently, we need to use a couple of internal functions to
calculate commitment outpoints amounts so we want to have this
to not only make a public API for this but also simplify it and
reduce the amount of duplicated code

We use this to calculate the exact amount of toRemote and toLocal
output to then be able to calculate the watch tower reward based
on that.

Upstream PR: joemphilips#162
  • Loading branch information
aarani authored and knocte committed May 28, 2021
1 parent 272b4a3 commit 0c83757
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
50 changes: 50 additions & 0 deletions src/DotNetLightning.Core/Channel/Commitments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open DotNetLightning.Serialization.Msgs

open ResultUtils
open ResultUtils.Portability
open DotNetLightning.Transactions

type LocalChanges = {
Proposed: IUpdateMsg list
Expand Down Expand Up @@ -115,6 +116,12 @@ type RemoteNextCommitInfo =
| Waiting _ -> remoteNextCommitInfo
| Revoked _ -> Revoked commitmentPubKey)

type Amounts =
{
ToLocal: Money
ToRemote: Money
}

type Commitments = {
LocalParams: LocalParams
RemoteParams: RemoteParams
Expand Down Expand Up @@ -222,3 +229,46 @@ type Commitments = {
let untrimmedMax = LNMoney.Min(untrimmedSpendableBalance, dustLimit)
let spendableBalance = LNMoney.Max(untrimmedMax, untrimmedSpendableBalance)
spendableBalance

static member RemoteCommitAmount (remoteParams: RemoteParams)
(localParams: LocalParams)
(remoteCommit: RemoteCommit)
: Amounts =
let commitFee = Transactions.commitTxFee
remoteParams.DustLimitSatoshis
remoteCommit.Spec

let (toLocalAmount, toRemoteAmount) =
if (localParams.IsFunder) then
(remoteCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis),
(remoteCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis) - commitFee
else
(remoteCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis) - commitFee,
(remoteCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis)

{Amounts.ToLocal = toLocalAmount; ToRemote = toRemoteAmount}

static member LocalCommitAmount (localParams: LocalParams)
(localCommit: LocalCommit)
: Amounts =
let commitFee = Transactions.commitTxFee
localParams.DustLimitSatoshis
localCommit.Spec

let (toLocalAmount, toRemoteAmount) =
if (localParams.IsFunder) then
(localCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis) - commitFee,
(localCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis)
else
(localCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis),
(localCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis) - commitFee

{Amounts.ToLocal = toLocalAmount; ToRemote = toRemoteAmount}
25 changes: 5 additions & 20 deletions src/DotNetLightning.Core/Channel/CommitmentsModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -505,34 +505,19 @@ module ForceCloseFundsRecovery =

let toLocalWitScriptPubKey = toLocalScriptPubKey.WitHash.ScriptPubKey

let commitFee =
commitTxFee
remoteParams.DustLimitSatoshis
remoteCommit.Spec

let (toLocalAmount, toRemoteAmount) =
if (localParams.IsFunder) then
(remoteCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis),
(remoteCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis) - commitFee
else
(remoteCommit.Spec.ToLocal.Satoshi
|> Money.Satoshis) - commitFee,
(remoteCommit.Spec.ToRemote.Satoshi
|> Money.Satoshis)
let amounts = Commitments.RemoteCommitAmount remoteParams localParams remoteCommit

let toLocalTxOut =
TxOut(toLocalAmount, toLocalWitScriptPubKey)
TxOut(amounts.ToLocal, toLocalWitScriptPubKey)
let toRemoteTxOut =
TxOut(toRemoteAmount, toRemoteScriptPubKey)
TxOut(amounts.ToRemote, toRemoteScriptPubKey)

let outputs =
seq {
if toLocalAmount > remoteParams.DustLimitSatoshis then
if amounts.ToLocal > remoteParams.DustLimitSatoshis then
yield toLocalTxOut

if toRemoteAmount > remoteParams.DustLimitSatoshis then
if amounts.ToRemote > remoteParams.DustLimitSatoshis then
yield toRemoteTxOut
}
|> Seq.sortWith TxOut.LexicographicCompare
Expand Down

0 comments on commit 0c83757

Please sign in to comment.