@@ -38,6 +38,7 @@ type ChannelError =
3838 // --- case they sent unacceptable msg ---
3939 | InvalidOpenChannel of InvalidOpenChannelError
4040 | InvalidAcceptChannel of InvalidAcceptChannelError
41+ | InvalidMonoHopUnidirectionalPayment of InvalidMonoHopUnidirectionalPaymentError
4142 | InvalidUpdateAddHTLC of InvalidUpdateAddHTLCError
4243 | InvalidRevokeAndACK of InvalidRevokeAndACKError
4344 | InvalidUpdateFee of InvalidUpdateFeeError
@@ -71,6 +72,7 @@ type ChannelError =
7172 | TheyCannotAffordFee (_, _, _) -> Close
7273 | InvalidOpenChannel _ -> DistrustPeer
7374 | InvalidAcceptChannel _ -> DistrustPeer
75+ | InvalidMonoHopUnidirectionalPayment _ -> Close
7476 | InvalidUpdateAddHTLC _ -> Close
7577 | InvalidRevokeAndACK _ -> Close
7678 | InvalidUpdateFee _ -> Close
@@ -141,6 +143,8 @@ type ChannelError =
141143 sprintf " Cannot close channel: %s " msg
142144 | InvalidOperationAddHTLC invalidOperationAddHTLCError ->
143145 sprintf " Invalid operation (add htlc): %s " invalidOperationAddHTLCError.Message
146+ | InvalidMonoHopUnidirectionalPayment invalidMonoHopUnidirectionalPaymentError ->
147+ sprintf " Invalid monohop-unidirectional payment: %s " invalidMonoHopUnidirectionalPaymentError.Message
144148
145149and ChannelConsumerAction =
146150 /// The error which should never happen.
@@ -181,6 +185,18 @@ and InvalidAcceptChannelError = {
181185 member this.Message =
182186 String.concat " ; " this.Errors
183187
188+ and InvalidMonoHopUnidirectionalPaymentError = {
189+ NetworkMsg: MonoHopUnidirectionalPaymentMsg
190+ Errors: string list
191+ }
192+ with
193+ static member Create msg e = {
194+ NetworkMsg = msg
195+ Errors = e
196+ }
197+ member this.Message =
198+ String.concat " ; " this.Errors
199+
184200and InvalidUpdateAddHTLCError = {
185201 NetworkMsg: UpdateAddHTLCMsg
186202 Errors: string list
@@ -504,6 +520,22 @@ module internal AcceptChannelMsgValidation =
504520
505521 ( check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7
506522
523+ module UpdateMonoHopUnidirectionalPaymentWithContext =
524+ let internal checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
525+ let fees =
526+ if state.LocalParams.IsFunder then
527+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
528+ else
529+ Money.Zero
530+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
531+ if missing < Money.Zero then
532+ sprintf " We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A . Remote Channel Reserve is: %A . and fee is %A "
533+ ( currentSpec.ToRemote.ToMoney())
534+ state.RemoteParams.ChannelReserveSatoshis
535+ fees
536+ |> Error
537+ else
538+ Ok()
507539
508540module UpdateAddHTLCValidation =
509541 let internal checkExpiryIsNotPast ( current : BlockHeight ) ( expiry ) =
@@ -518,7 +550,23 @@ module UpdateAddHTLCValidation =
518550 let internal checkAmountIsLargerThanMinimum ( htlcMinimum : LNMoney ) ( amount ) =
519551 check ( amount) (<) ( htlcMinimum) " htlc value (%A ) is too small. must be greater or equal to %A "
520552
521-
553+ module internal MonoHopUnidirectionalPaymentValidationWithContext =
554+ let checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
555+ let fees =
556+ if state.LocalParams.IsFunder then
557+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
558+ else
559+ Money.Zero
560+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
561+ if missing < Money.Zero then
562+ sprintf " We don't have sufficient funds to send mono-hop unidirectional payment. current to_remote amount is: %A . Remote Channel Reserve is: %A . and fee is %A "
563+ ( currentSpec.ToRemote.ToMoney())
564+ state.RemoteParams.ChannelReserveSatoshis
565+ fees
566+ |> Error
567+ else
568+ Ok()
569+
522570module internal UpdateAddHTLCValidationWithContext =
523571 let checkLessThanHTLCValueInFlightLimit ( currentSpec : CommitmentSpec ) ( limit ) ( add : UpdateAddHTLCMsg ) =
524572 let htlcValueInFlight = currentSpec.HTLCs |> Map.toSeq |> Seq.sumBy ( fun ( _ , v ) -> v.Add.Amount)
0 commit comments