@@ -36,6 +36,7 @@ type ChannelError =
3636 // --- case they sent unacceptable msg ---
3737 | InvalidOpenChannel of InvalidOpenChannelError
3838 | InvalidAcceptChannel of InvalidAcceptChannelError
39+ | InvalidMonoHopUnidirectionalPayment of InvalidMonoHopUnidirectionalPaymentError
3940 | InvalidUpdateAddHTLC of InvalidUpdateAddHTLCError
4041 | InvalidRevokeAndACK of InvalidRevokeAndACKError
4142 | InvalidUpdateFee of InvalidUpdateFeeError
@@ -69,6 +70,7 @@ type ChannelError =
6970 | TheyCannotAffordFee (_, _, _) -> Close
7071 | InvalidOpenChannel _ -> DistrustPeer
7172 | InvalidAcceptChannel _ -> DistrustPeer
73+ | InvalidMonoHopUnidirectionalPayment _ -> Close
7274 | InvalidUpdateAddHTLC _ -> Close
7375 | InvalidRevokeAndACK _ -> Close
7476 | InvalidUpdateFee _ -> Close
@@ -179,6 +181,18 @@ and InvalidAcceptChannelError = {
179181 member this.Message =
180182 String.concat " ; " this.Errors
181183
184+ and InvalidMonoHopUnidirectionalPaymentError = {
185+ NetworkMsg: MonoHopUnidirectionalPaymentMsg
186+ Errors: string list
187+ }
188+ with
189+ static member Create msg e = {
190+ NetworkMsg = msg
191+ Errors = e
192+ }
193+ member this.Message =
194+ String.concat " ; " this.Errors
195+
182196and InvalidUpdateAddHTLCError = {
183197 NetworkMsg: UpdateAddHTLCMsg
184198 Errors: string list
@@ -507,6 +521,22 @@ module internal AcceptChannelMsgValidation =
507521
508522 ( check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7
509523
524+ module UpdateMonoHopUnidirectionalPaymentWithContext =
525+ let internal checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
526+ let fees =
527+ if state.LocalParams.IsFunder then
528+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
529+ else
530+ Money.Zero
531+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
532+ if missing < Money.Zero then
533+ 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 "
534+ ( currentSpec.ToRemote.ToMoney())
535+ state.RemoteParams.ChannelReserveSatoshis
536+ fees
537+ |> Error
538+ else
539+ Ok()
510540
511541module UpdateAddHTLCValidation =
512542 let internal checkExpiryIsNotPast ( current : BlockHeight ) ( expiry ) =
@@ -521,7 +551,23 @@ module UpdateAddHTLCValidation =
521551 let internal checkAmountIsLargerThanMinimum ( htlcMinimum : LNMoney ) ( amount ) =
522552 check ( amount) (<) ( htlcMinimum) " htlc value (%A ) is too small. must be greater or equal to %A "
523553
524-
554+ module internal MonoHopUnidirectionalPaymentValidationWithContext =
555+ let checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
556+ let fees =
557+ if state.LocalParams.IsFunder then
558+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
559+ else
560+ Money.Zero
561+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
562+ if missing < Money.Zero then
563+ 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 "
564+ ( currentSpec.ToRemote.ToMoney())
565+ state.RemoteParams.ChannelReserveSatoshis
566+ fees
567+ |> Error
568+ else
569+ Ok()
570+
525571module internal UpdateAddHTLCValidationWithContext =
526572 let checkLessThanHTLCValueInFlightLimit ( currentSpec : CommitmentSpec ) ( limit ) ( add : UpdateAddHTLCMsg ) =
527573 let htlcValueInFlight = currentSpec.HTLCs |> Map.toSeq |> Seq.sumBy ( fun ( _ , v ) -> v.Add.Amount)
0 commit comments