Skip to content

Commit

Permalink
Core: remove the commit fee clamp, add max fee
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aarani committed Apr 18, 2023
1 parent 20b4f13 commit 8dfb1cb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
21 changes: 14 additions & 7 deletions src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,14 +1471,15 @@ and Channel =
let remoteChannelKeys =
this.SavedChannelState.StaticChannelConfig.RemoteChannelPubKeys

let lastCommitFeeSatoshi =
this.SavedChannelState.StaticChannelConfig.FundingScriptCoin.TxOut.Value
- (this.SavedChannelState.LocalCommit.PublishableTxs.CommitTx.Value.TotalOut)
let! idealFee =
this.FirstClosingFee
localShutdownScriptPubKey
remoteShutdownScriptPubKey
|> expectTransactionError

do!
checkRemoteProposedHigherFeeThanBaseFee
lastCommitFeeSatoshi
msg.FeeSatoshis
let maxFee =
this.SavedChannelState.StaticChannelConfig.LocalParams.MutualCloseMaxFeeMultiplier
* idealFee

do!
checkRemoteProposedFeeWithinNegotiatedRange
Expand Down Expand Up @@ -1550,6 +1551,12 @@ and Channel =
nextClosingFee
|> expectTransactionError

if this.SavedChannelState.StaticChannelConfig.IsFunder
&& nextClosingFee > maxFee then
return!
Error
<| ProposalExceedsMaxFee(nextClosingFee, maxFee)

let nextState =
{ this.NegotiatingState with
LocalClosingFeesProposed =
Expand Down
21 changes: 7 additions & 14 deletions src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ type ChannelError =
height: BlockHeight *
depth: BlockHeightOffset32
| CannotCloseChannel of msg: string
| RemoteProposedHigherFeeThanBaseFee of baseFee: Money * proposedFee: Money
| RemoteProposedFeeOutOfNegotiatedRange of
ourPreviousFee: Money *
theirPreviousFee: Money *
theirNextFee: Money
| ProposalExceedsMaxFee of proposalFee: Money * maxFee: Money
| NoUpdatesToSign
| CannotSignCommitmentBeforeRevocation
| InsufficientConfirmations of
Expand Down Expand Up @@ -108,8 +108,8 @@ type ChannelError =
| CannotSignCommitmentBeforeRevocation -> Ignore
| InsufficientConfirmations(_, _) -> Ignore
| InvalidOperationAddHTLC _ -> Ignore
| RemoteProposedHigherFeeThanBaseFee(_, _) -> Close
| RemoteProposedFeeOutOfNegotiatedRange(_, _, _) -> Close
| ProposalExceedsMaxFee(_, _) -> Ignore

member this.Message =
match this with
Expand Down Expand Up @@ -161,12 +161,6 @@ type ChannelError =
sprintf
"They sent shutdown msg (%A) while they have pending unsigned HTLCs, this is protocol violation"
msg
| RemoteProposedHigherFeeThanBaseFee(baseFee, proposedFee) ->
"remote proposed a closing fee higher than commitment fee of the final commitment transaction. "
+ sprintf
"commitment fee=%A; fee remote proposed=%A;"
baseFee
proposedFee
| RemoteProposedFeeOutOfNegotiatedRange
(
ourPreviousFee, theirPreviousFee, theirNextFee
Expand All @@ -178,6 +172,11 @@ type ChannelError =
ourPreviousFee
theirPreviousFee
theirNextFee
| ProposalExceedsMaxFee(proposalFee, maxFee) ->
sprintf
"latest fee proposal (%i) exceeds max fee (%i)"
proposalFee.Satoshi
maxFee.Satoshi
| CryptoError cryptoError ->
sprintf "Crypto error: %s" cryptoError.Message
| TransactionRelatedErrors transactionErrors ->
Expand Down Expand Up @@ -409,12 +408,6 @@ module internal ChannelError =
let receivedShutdownWhenRemoteHasUnsignedOutgoingHTLCs msg =
msg |> ReceivedShutdownWhenRemoteHasUnsignedOutgoingHTLCs |> Error

let checkRemoteProposedHigherFeeThanBaseFee baseFee proposedFee =
if (baseFee < proposedFee) then
RemoteProposedHigherFeeThanBaseFee(baseFee, proposedFee) |> Error
else
Ok()

let checkRemoteProposedFeeWithinNegotiatedRange
(ourPreviousFeeOpt: Option<Money>)
(theirPreviousFeeOpt: Option<Money>)
Expand Down
5 changes: 5 additions & 0 deletions src/DotNetLightning.Core/Channel/ChannelOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ type LocalParams =
ToSelfDelay: BlockHeightOffset16
MaxAcceptedHTLCs: uint16
Features: FeatureBits
// MutualCloseMaxFeeMultiplier is a multiplier we'll apply to the ideal fee
// of the funder, to decide when the negotiated fee is too high. By
// default, we want to bail out if we attempt to negotiate a fee that's
// 3x higher than our ideal fee.
MutualCloseMaxFeeMultiplier: int
}

type RemoteParams =
Expand Down
1 change: 1 addition & 0 deletions tests/DotNetLightning.Core.Tests/ChannelTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ let tests =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteParams: RemoteParams =
Expand Down
6 changes: 6 additions & 0 deletions tests/DotNetLightning.Core.Tests/TransactionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteLocalParam: LocalParams =
Expand All @@ -112,6 +113,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteParam: RemoteParams =
Expand Down Expand Up @@ -532,6 +534,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteLocalParam: LocalParams =
Expand All @@ -543,6 +546,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteParams: RemoteParams =
Expand Down Expand Up @@ -887,6 +891,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteLocalParam: LocalParams =
Expand All @@ -898,6 +903,7 @@ let testList =
ToSelfDelay = 144us |> BlockHeightOffset16
MaxAcceptedHTLCs = 1000us
Features = FeatureBits.Zero
MutualCloseMaxFeeMultiplier = 3
}

let remoteParam: RemoteParams =
Expand Down

0 comments on commit 8dfb1cb

Please sign in to comment.