Skip to content

Commit 0c83757

Browse files
aaraniknocte
authored andcommitted
Core/Channel: function to calc outpoints of commitTx
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
1 parent 272b4a3 commit 0c83757

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

src/DotNetLightning.Core/Channel/Commitments.fs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ open DotNetLightning.Serialization.Msgs
1010

1111
open ResultUtils
1212
open ResultUtils.Portability
13+
open DotNetLightning.Transactions
1314

1415
type LocalChanges = {
1516
Proposed: IUpdateMsg list
@@ -115,6 +116,12 @@ type RemoteNextCommitInfo =
115116
| Waiting _ -> remoteNextCommitInfo
116117
| Revoked _ -> Revoked commitmentPubKey)
117118

119+
type Amounts =
120+
{
121+
ToLocal: Money
122+
ToRemote: Money
123+
}
124+
118125
type Commitments = {
119126
LocalParams: LocalParams
120127
RemoteParams: RemoteParams
@@ -222,3 +229,46 @@ type Commitments = {
222229
let untrimmedMax = LNMoney.Min(untrimmedSpendableBalance, dustLimit)
223230
let spendableBalance = LNMoney.Max(untrimmedMax, untrimmedSpendableBalance)
224231
spendableBalance
232+
233+
static member RemoteCommitAmount (remoteParams: RemoteParams)
234+
(localParams: LocalParams)
235+
(remoteCommit: RemoteCommit)
236+
: Amounts =
237+
let commitFee = Transactions.commitTxFee
238+
remoteParams.DustLimitSatoshis
239+
remoteCommit.Spec
240+
241+
let (toLocalAmount, toRemoteAmount) =
242+
if (localParams.IsFunder) then
243+
(remoteCommit.Spec.ToLocal.Satoshi
244+
|> Money.Satoshis),
245+
(remoteCommit.Spec.ToRemote.Satoshi
246+
|> Money.Satoshis) - commitFee
247+
else
248+
(remoteCommit.Spec.ToLocal.Satoshi
249+
|> Money.Satoshis) - commitFee,
250+
(remoteCommit.Spec.ToRemote.Satoshi
251+
|> Money.Satoshis)
252+
253+
{Amounts.ToLocal = toLocalAmount; ToRemote = toRemoteAmount}
254+
255+
static member LocalCommitAmount (localParams: LocalParams)
256+
(localCommit: LocalCommit)
257+
: Amounts =
258+
let commitFee = Transactions.commitTxFee
259+
localParams.DustLimitSatoshis
260+
localCommit.Spec
261+
262+
let (toLocalAmount, toRemoteAmount) =
263+
if (localParams.IsFunder) then
264+
(localCommit.Spec.ToLocal.Satoshi
265+
|> Money.Satoshis) - commitFee,
266+
(localCommit.Spec.ToRemote.Satoshi
267+
|> Money.Satoshis)
268+
else
269+
(localCommit.Spec.ToLocal.Satoshi
270+
|> Money.Satoshis),
271+
(localCommit.Spec.ToRemote.Satoshi
272+
|> Money.Satoshis) - commitFee
273+
274+
{Amounts.ToLocal = toLocalAmount; ToRemote = toRemoteAmount}

src/DotNetLightning.Core/Channel/CommitmentsModule.fs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -505,34 +505,19 @@ module ForceCloseFundsRecovery =
505505

506506
let toLocalWitScriptPubKey = toLocalScriptPubKey.WitHash.ScriptPubKey
507507

508-
let commitFee =
509-
commitTxFee
510-
remoteParams.DustLimitSatoshis
511-
remoteCommit.Spec
512-
513-
let (toLocalAmount, toRemoteAmount) =
514-
if (localParams.IsFunder) then
515-
(remoteCommit.Spec.ToLocal.Satoshi
516-
|> Money.Satoshis),
517-
(remoteCommit.Spec.ToRemote.Satoshi
518-
|> Money.Satoshis) - commitFee
519-
else
520-
(remoteCommit.Spec.ToLocal.Satoshi
521-
|> Money.Satoshis) - commitFee,
522-
(remoteCommit.Spec.ToRemote.Satoshi
523-
|> Money.Satoshis)
508+
let amounts = Commitments.RemoteCommitAmount remoteParams localParams remoteCommit
524509

525510
let toLocalTxOut =
526-
TxOut(toLocalAmount, toLocalWitScriptPubKey)
511+
TxOut(amounts.ToLocal, toLocalWitScriptPubKey)
527512
let toRemoteTxOut =
528-
TxOut(toRemoteAmount, toRemoteScriptPubKey)
513+
TxOut(amounts.ToRemote, toRemoteScriptPubKey)
529514

530515
let outputs =
531516
seq {
532-
if toLocalAmount > remoteParams.DustLimitSatoshis then
517+
if amounts.ToLocal > remoteParams.DustLimitSatoshis then
533518
yield toLocalTxOut
534519

535-
if toRemoteAmount > remoteParams.DustLimitSatoshis then
520+
if amounts.ToRemote > remoteParams.DustLimitSatoshis then
536521
yield toRemoteTxOut
537522
}
538523
|> Seq.sortWith TxOut.LexicographicCompare

0 commit comments

Comments
 (0)