Skip to content

Commit 5117e8b

Browse files
committed
Core: remove the commit fee clamp, add max fee
This commit removes the check that compares mutual close fee with commit tx fee, this check is outdated and has been changed by this spec PR [1]. To prevent unreasonably high fee, this commit introduces MutualCloseMaxFeeMultiplier setting. [1] lightning/bolts#847
1 parent 5379324 commit 5117e8b

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/DotNetLightning.Core/Channel/Channel.fs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,9 +1113,17 @@ and Channel = {
11131113
| (None, None) ->
11141114
Error ReceivedClosingSignedBeforeSendingOrReceivingShutdown
11151115
let remoteChannelKeys = this.SavedChannelState.StaticChannelConfig.RemoteChannelPubKeys
1116-
let lastCommitFeeSatoshi =
1117-
this.SavedChannelState.StaticChannelConfig.FundingScriptCoin.TxOut.Value - (this.SavedChannelState.LocalCommit.PublishableTxs.CommitTx.Value.TotalOut)
1118-
do! checkRemoteProposedHigherFeeThanBaseFee lastCommitFeeSatoshi msg.FeeSatoshis
1116+
1117+
let! idealFee =
1118+
this.FirstClosingFee
1119+
localShutdownScriptPubKey
1120+
remoteShutdownScriptPubKey
1121+
|> expectTransactionError
1122+
1123+
let maxFee =
1124+
this.SavedChannelState.StaticChannelConfig.LocalParams.MutualCloseMaxFeeMultiplier
1125+
* idealFee
1126+
11191127
do!
11201128
checkRemoteProposedFeeWithinNegotiatedRange
11211129
(List.tryHead this.NegotiatingState.LocalClosingFeesProposed)
@@ -1146,18 +1154,20 @@ and Channel = {
11461154
if (areWeInDeal || hasTooManyNegotiationDone) then
11471155
return this, MutualClose (finalizedTx, None)
11481156
else
1149-
let lastLocalClosingFee = this.NegotiatingState.LocalClosingFeesProposed |> List.tryHead
1150-
let! localF =
1151-
match lastLocalClosingFee with
1152-
| Some v -> Ok v
1153-
| None ->
1154-
this.FirstClosingFee
1155-
localShutdownScriptPubKey
1156-
remoteShutdownScriptPubKey
1157-
|> expectTransactionError
1157+
let maybeLastLocalClosingFee = this.NegotiatingState.LocalClosingFeesProposed |> List.tryHead
1158+
let localF =
1159+
match maybeLastLocalClosingFee with
1160+
| Some lastLocalClosingFee -> lastLocalClosingFee
1161+
| None -> idealFee
1162+
11581163
let nextClosingFee =
11591164
Channel.NextClosingFee (localF, msg.FeeSatoshis)
1160-
if (Some nextClosingFee = lastLocalClosingFee) then
1165+
1166+
if this.SavedChannelState.StaticChannelConfig.IsFunder && nextClosingFee > maxFee then
1167+
return!
1168+
Error <| ProposalExceedsMaxFee(nextClosingFee, maxFee)
1169+
1170+
if (Some nextClosingFee = maybeLastLocalClosingFee) then
11611171
return this, MutualClose (finalizedTx, None)
11621172
else if (nextClosingFee = msg.FeeSatoshis) then
11631173
// we have reached on agreement!

src/DotNetLightning.Core/Channel/ChannelError.fs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ type ChannelError =
5353
| FundingTxNotGiven of msg: string
5454
| OnceConfirmedFundingTxHasBecomeUnconfirmed of height: BlockHeight * depth: BlockHeightOffset32
5555
| CannotCloseChannel of msg: string
56-
| RemoteProposedHigherFeeThanBaseFee of baseFee: Money * proposedFee: Money
5756
| RemoteProposedFeeOutOfNegotiatedRange of ourPreviousFee: Money * theirPreviousFee: Money * theirNextFee: Money
57+
| ProposalExceedsMaxFee of proposalFee: Money * maxFee: Money
5858
| NoUpdatesToSign
5959
| CannotSignCommitmentBeforeRevocation
6060
| InsufficientConfirmations of requiredDepth: BlockHeightOffset32 * currentDepth: BlockHeightOffset32
@@ -95,8 +95,8 @@ type ChannelError =
9595
| CannotSignCommitmentBeforeRevocation -> Ignore
9696
| InsufficientConfirmations(_, _) -> Ignore
9797
| InvalidOperationAddHTLC _ -> Ignore
98-
| RemoteProposedHigherFeeThanBaseFee(_, _) -> Close
9998
| RemoteProposedFeeOutOfNegotiatedRange(_, _, _) -> Close
99+
| ProposalExceedsMaxFee(_, _) -> Ignore
100100

101101
member this.Message =
102102
match this with
@@ -125,15 +125,17 @@ type ChannelError =
125125
sprintf "once confirmed funding tx has become less confirmed than threshold %A! This is probably caused by reorg. current depth is: %A " height depth
126126
| ReceivedShutdownWhenRemoteHasUnsignedOutgoingHTLCs msg ->
127127
sprintf "They sent shutdown msg (%A) while they have pending unsigned HTLCs, this is protocol violation" msg
128-
| RemoteProposedHigherFeeThanBaseFee(baseFee, proposedFee) ->
129-
"remote proposed a closing fee higher than commitment fee of the final commitment transaction. "
130-
+ sprintf "commitment fee=%A; fee remote proposed=%A;" baseFee proposedFee
131128
| RemoteProposedFeeOutOfNegotiatedRange(ourPreviousFee, theirPreviousFee, theirNextFee) ->
132129
"remote proposed a closing fee which was not strictly between the previous fee that \
133130
we proposed and the previous fee that they proposed. "
134131
+ sprintf
135132
"our previous fee = %A; their previous fee = %A; their next fee = %A"
136133
ourPreviousFee theirPreviousFee theirNextFee
134+
| ProposalExceedsMaxFee(proposalFee, maxFee) ->
135+
sprintf
136+
"latest fee proposal (%i) exceeds max fee (%i)"
137+
proposalFee.Satoshi
138+
maxFee.Satoshi
137139
| CryptoError cryptoError ->
138140
sprintf "Crypto error: %s" cryptoError.Message
139141
| TransactionRelatedErrors transactionErrors ->
@@ -352,12 +354,6 @@ module internal ChannelError =
352354
let receivedShutdownWhenRemoteHasUnsignedOutgoingHTLCs msg =
353355
msg |> ReceivedShutdownWhenRemoteHasUnsignedOutgoingHTLCs |> Error
354356

355-
let checkRemoteProposedHigherFeeThanBaseFee baseFee proposedFee =
356-
if (baseFee < proposedFee) then
357-
RemoteProposedHigherFeeThanBaseFee(baseFee, proposedFee) |> Error
358-
else
359-
Ok()
360-
361357
let checkRemoteProposedFeeWithinNegotiatedRange (ourPreviousFeeOpt: Option<Money>)
362358
(theirPreviousFeeOpt: Option<Money>)
363359
(theirNextFee: Money) =

src/DotNetLightning.Core/Channel/ChannelOperations.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ type LocalParams = {
6767
ToSelfDelay: BlockHeightOffset16
6868
MaxAcceptedHTLCs: uint16
6969
Features: FeatureBits
70+
// MutualCloseMaxFeeMultiplier is a multiplier we'll apply to the ideal fee
71+
// of the funder, to decide when the negotiated fee is too high. By
72+
// default, we want to bail out if we attempt to negotiate a fee that's
73+
// 3x higher than our ideal fee.
74+
MutualCloseMaxFeeMultiplier: int
7075
}
7176

7277
type RemoteParams = {

tests/DotNetLightning.Core.Tests/TransactionTests.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ let testList = testList "transaction tests" [
6363
ToSelfDelay = 144us |> BlockHeightOffset16
6464
MaxAcceptedHTLCs = 1000us
6565
Features = FeatureBits.Zero
66+
MutualCloseMaxFeeMultiplier = 3
6667
}
6768

6869
let remoteLocalParam : LocalParams = {
@@ -73,6 +74,7 @@ let testList = testList "transaction tests" [
7374
ToSelfDelay = 144us |> BlockHeightOffset16
7475
MaxAcceptedHTLCs = 1000us
7576
Features = FeatureBits.Zero
77+
MutualCloseMaxFeeMultiplier = 3
7678
}
7779

7880
let remoteParam : RemoteParams = {

0 commit comments

Comments
 (0)