@@ -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
@@ -129,6 +131,8 @@ type ChannelError =
129131 " Received commitment signed when we have not pending changes"
130132 | InvalidAcceptChannel invalidAcceptChannelError ->
131133 sprintf " Invalid accept_channel msg: %s " invalidAcceptChannelError.Message
134+ | InvalidMonoHopUnidirectionalPayment invalidMonoHopUnidirectionalPaymentError ->
135+ sprintf " Invalid monohop unidirectional payment %s " invalidMonoHopUnidirectionalPaymentError.Message
132136 | InvalidUpdateAddHTLC invalidUpdateAddHTLCError ->
133137 sprintf " Invalid udpate_add_htlc msg: %s " invalidUpdateAddHTLCError.Message
134138 | InvalidRevokeAndACK invalidRevokeAndACKError ->
@@ -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
@@ -509,6 +525,22 @@ module internal AcceptChannelMsgValidation =
509525
510526 ( check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7
511527
528+ module UpdateMonoHopUnidirectionalPaymentWithContext =
529+ let internal checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
530+ let fees =
531+ if state.LocalParams.IsFunder then
532+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
533+ else
534+ Money.Zero
535+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
536+ if missing < Money.Zero then
537+ 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 "
538+ ( currentSpec.ToRemote.ToMoney())
539+ state.RemoteParams.ChannelReserveSatoshis
540+ fees
541+ |> Error
542+ else
543+ Ok()
512544
513545module UpdateAddHTLCValidation =
514546 let internal checkExpiryIsNotPast ( current : BlockHeight ) ( expiry ) =
@@ -523,7 +555,23 @@ module UpdateAddHTLCValidation =
523555 let internal checkAmountIsLargerThanMinimum ( htlcMinimum : LNMoney ) ( amount ) =
524556 check ( amount) (<) ( htlcMinimum) " htlc value (%A ) is too small. must be greater or equal to %A "
525557
526-
558+ module internal MonoHopUnidirectionalPaymentValidationWithContext =
559+ let checkWeHaveSufficientFunds ( state : Commitments ) ( currentSpec ) =
560+ let fees =
561+ if state.LocalParams.IsFunder then
562+ Transactions.commitTxFee state.RemoteParams.DustLimitSatoshis currentSpec
563+ else
564+ Money.Zero
565+ let missing = currentSpec.ToRemote.ToMoney() - state.RemoteParams.ChannelReserveSatoshis - fees
566+ if missing < Money.Zero then
567+ 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 "
568+ ( currentSpec.ToRemote.ToMoney())
569+ state.RemoteParams.ChannelReserveSatoshis
570+ fees
571+ |> Error
572+ else
573+ Ok()
574+
527575module internal UpdateAddHTLCValidationWithContext =
528576 let checkLessThanHTLCValueInFlightLimit ( currentSpec : CommitmentSpec ) ( limit ) ( add : UpdateAddHTLCMsg ) =
529577 let htlcValueInFlight = currentSpec.HTLCs |> Map.toSeq |> Seq.sumBy ( fun ( _ , v ) -> v.Add.Amount)
0 commit comments