Skip to content

Commit

Permalink
Merge pull request #14 from canndrew/nblockchain-update-fee
Browse files Browse the repository at this point in the history
Update fee fixes
  • Loading branch information
knocte authored Nov 26, 2020
2 parents 8be2dcd + 2e569cb commit 3de0b3e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
dotnet run --project tests/DotNetLightning.Core.Tests
- name: Run other tests
run: |
dotnet test
dotnet test --filter FullyQualifiedName\!~Macaroons
build_with_fsharp_from_mono:
runs-on: ubuntu-18.04
Expand Down
3 changes: 2 additions & 1 deletion src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,8 @@ module Channel =

| WeAcceptedOperationUpdateFee(_msg, newCommitments), ChannelState.Normal d ->
{ c with State = ChannelState.Normal({ d with Commitments = newCommitments }) }
| WeAcceptedUpdateFee(_msg), ChannelState.Normal _d -> c
| WeAcceptedUpdateFee(_msg, newCommitments), ChannelState.Normal normalData ->
{ c with State = ChannelState.Normal({ normalData with Commitments = newCommitments }) }

| WeAcceptedOperationSign(_msg, newCommitments), ChannelState.Normal d ->
{ c with State = ChannelState.Normal({ d with Commitments = newCommitments }) }
Expand Down
9 changes: 2 additions & 7 deletions src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,6 @@ module private ValidationHelper =
/// Helpers to create channel error
[<AutoOpen>]
module internal ChannelError =
let feeRateMismatch (FeeRatePerKw remote, FeeRatePerKw local) =
let remote = float remote
let local = float local
abs (2.0 * (remote - local) / (remote + local))

let inline feeDeltaTooHigh msg (actualDelta, maxAccepted) =
InvalidUpdateFeeError.Create
msg
Expand Down Expand Up @@ -366,7 +361,7 @@ module internal OpenChannelMsgValidation =
(maxFeeRateMismatchRatio: float) =
let localFeeRatePerKw =
feeEstimator.GetEstSatPer1000Weight(ConfirmationTarget.Background)
let diff = feeRateMismatch(remoteFeeRatePerKw, localFeeRatePerKw)
let diff = remoteFeeRatePerKw.MismatchRatio localFeeRatePerKw
if (diff > maxFeeRateMismatchRatio) then
sprintf
"Peer's feerate (%A) was unacceptably far from the estimated fee rate of %A"
Expand Down Expand Up @@ -600,7 +595,7 @@ module internal UpdateAddHTLCValidationWithContext =
module internal UpdateFeeValidation =
let checkFeeDiffTooHigh (msg: UpdateFeeMsg) (localFeeRatePerKw: FeeRatePerKw) (maxFeeRateMismatchRatio) =
let remoteFeeRatePerKw = msg.FeeRatePerKw
let diff = feeRateMismatch(remoteFeeRatePerKw, localFeeRatePerKw)
let diff = remoteFeeRatePerKw.MismatchRatio localFeeRatePerKw
if (diff > maxFeeRateMismatchRatio) then
(diff, maxFeeRateMismatchRatio)
|> feeDeltaTooHigh msg
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetLightning.Core/Channel/ChannelTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type ChannelEvent =
| WeAcceptedFailMalformedHTLC of origin: HTLCSource * msg: UpdateAddHTLCMsg * newCommitments: Commitments

| WeAcceptedOperationUpdateFee of msg: UpdateFeeMsg * nextCommitments: Commitments
| WeAcceptedUpdateFee of msg: UpdateFeeMsg
| WeAcceptedUpdateFee of msg: UpdateFeeMsg * newCommitments: Commitments

| WeAcceptedOperationSign of msg: CommitmentSignedMsg * nextCommitments: Commitments
| WeAcceptedCommitmentSigned of msg: RevokeAndACKMsg * nextCommitments: Commitments
Expand Down
15 changes: 9 additions & 6 deletions src/DotNetLightning.Core/Channel/CommitmentsModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,22 @@ module internal Commitments =
else
result {
do! Helpers.checkUpdateFee (config) (msg) (localFeerate)
let c1 = cm.AddRemoteProposal(msg)
let nextCommitments = cm.AddRemoteProposal(msg)
let! reduced =
c1.LocalCommit.Spec.Reduce(c1.LocalChanges.ACKed, c1.RemoteChanges.Proposed) |> expectTransactionError
nextCommitments.LocalCommit.Spec.Reduce(
nextCommitments.LocalChanges.ACKed,
nextCommitments.RemoteChanges.Proposed
) |> expectTransactionError

let fees = Transactions.commitTxFee(c1.RemoteParams.DustLimitSatoshis) reduced
let missing = reduced.ToRemote.ToMoney() - c1.RemoteParams.ChannelReserveSatoshis - fees
let fees = Transactions.commitTxFee(nextCommitments.RemoteParams.DustLimitSatoshis) reduced
let missing = reduced.ToRemote.ToMoney() - nextCommitments.RemoteParams.ChannelReserveSatoshis - fees
if (missing < Money.Zero) then
return!
(c1.LocalParams.ChannelReserveSatoshis, fees, (-1 * missing))
(nextCommitments.LocalParams.ChannelReserveSatoshis, fees, (-1 * missing))
|> cannotAffordFee
else
return
[ WeAcceptedUpdateFee msg ]
[ WeAcceptedUpdateFee(msg, nextCommitments) ]
}

let sendCommit (channelPrivKeys: ChannelPrivKeys) (n: Network) (cm: Commitments) =
Expand Down
6 changes: 6 additions & 0 deletions src/DotNetLightning.Core/Utils/Primitives.fs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,18 @@ module Primitives =
member this.AsNBitcoinFeeRate() =
this.Value |> uint64 |> (*)4UL |> Money.Satoshis |> FeeRate

member this.MismatchRatio (other: FeeRatePerKw) =
let local = double this.Value
let remote = double other.Value
abs (2.0 * (remote - local) / (remote + local))

static member Max(a: FeeRatePerKw, b: FeeRatePerKw) =
if (a.Value >= b.Value) then a else b
static member (+) (a: FeeRatePerKw, b: uint32) =
(a.Value + b) |> FeeRatePerKw
static member (*) (a: FeeRatePerKw, b: uint32) =
(a.Value * b) |> FeeRatePerKw

/// Block Hash
type BlockId = | BlockId of uint256 with
member x.Value = let (BlockId v) = x in v
Expand Down

0 comments on commit 3de0b3e

Please sign in to comment.