@@ -39,6 +39,7 @@ type ChannelError =
3939 | InvalidFundingLocked of InvalidFundingLockedError
4040 | InvalidOpenChannel of InvalidOpenChannelError
4141 | InvalidAcceptChannel of InvalidAcceptChannelError
42+ | InvalidMonoHopUnidirectionalPayment of InvalidMonoHopUnidirectionalPaymentError
4243 | InvalidUpdateAddHTLC of InvalidUpdateAddHTLCError
4344 | InvalidRevokeAndACK of InvalidRevokeAndACKError
4445 | InvalidUpdateFee of InvalidUpdateFeeError
@@ -76,6 +77,7 @@ type ChannelError =
7677 | InvalidFundingLocked _ -> DistrustPeer
7778 | InvalidOpenChannel _ -> DistrustPeer
7879 | InvalidAcceptChannel _ -> DistrustPeer
80+ | InvalidMonoHopUnidirectionalPayment _ -> Close
7981 | InvalidUpdateAddHTLC _ -> Close
8082 | InvalidRevokeAndACK _ -> Close
8183 | InvalidUpdateFee _ -> Close
@@ -143,6 +145,8 @@ type ChannelError =
143145 " Received commitment signed when we have not pending changes"
144146 | InvalidAcceptChannel invalidAcceptChannelError ->
145147 sprintf " Invalid accept_channel msg: %s " invalidAcceptChannelError.Message
148+ | InvalidMonoHopUnidirectionalPayment invalidMonohopUnidirectionalPaymentError ->
149+ sprintf " Invalid mono hop unidrectional payment: %s " invalidMonohopUnidirectionalPaymentError.Message
146150 | InvalidUpdateAddHTLC invalidUpdateAddHTLCError ->
147151 sprintf " Invalid udpate_add_htlc msg: %s " invalidUpdateAddHTLCError.Message
148152 | InvalidRevokeAndACK invalidRevokeAndACKError ->
@@ -207,7 +211,18 @@ and InvalidAcceptChannelError = {
207211 }
208212 member this.Message =
209213 String.concat " ; " this.Errors
210-
214+ and InvalidMonoHopUnidirectionalPaymentError = {
215+ NetworkMsg: MonoHopUnidirectionalPaymentMsg
216+ Errors: string list
217+ }
218+ with
219+ static member Create msg e = {
220+ NetworkMsg = msg
221+ Errors = e
222+ }
223+ member this.Message =
224+ String.concat " ; " this.Errors
225+
211226and InvalidUpdateAddHTLCError = {
212227 NetworkMsg: UpdateAddHTLCMsg
213228 Errors: string list
@@ -554,7 +569,23 @@ module internal AcceptChannelMsgValidation =
554569 let check7 = check ( msg.MinimumDepth.Value) (>) ( config.MaxMinimumDepth.Value |> uint32) " We consider the minimum depth (%A ) to be unreasonably large. Our max minimum depth is (%A )"
555570
556571 ( check1 |> Validation.ofResult) *^> check2 *^> check3 *^> check4 *^> check5 *^> check6 *^> check7
557-
572+
573+ module UpdateMonoHopUnidirectionalPaymentWithContext =
574+ let internal checkWeHaveSufficientFunds ( staticChannelConfig : StaticChannelConfig ) ( currentSpec ) =
575+ let fees =
576+ if staticChannelConfig.IsFunder then
577+ Transactions.commitTxFee staticChannelConfig.RemoteParams.DustLimitSatoshis currentSpec
578+ else
579+ Money.Zero
580+ let missing = currentSpec.ToRemote.ToMoney() - staticChannelConfig.RemoteParams.ChannelReserveSatoshis - fees
581+ if missing < Money.Zero then
582+ 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 "
583+ ( currentSpec.ToRemote.ToMoney())
584+ staticChannelConfig.RemoteParams.ChannelReserveSatoshis
585+ fees
586+ |> Error
587+ else
588+ Ok()
558589
559590module UpdateAddHTLCValidation =
560591 let internal checkExpiryIsNotPast ( current : BlockHeight ) ( expiry ) =
@@ -569,7 +600,23 @@ module UpdateAddHTLCValidation =
569600 let internal checkAmountIsLargerThanMinimum ( htlcMinimum : LNMoney ) ( amount ) =
570601 check ( amount) (<) ( htlcMinimum) " htlc value (%A ) is too small. must be greater or equal to %A "
571602
572-
603+ module internal MonoHopUnidirectionalPaymentValidationWithContext =
604+ let checkWeHaveSufficientFunds ( staticChannelConfig : StaticChannelConfig ) ( currentSpec ) =
605+ let fees =
606+ if staticChannelConfig.IsFunder then
607+ Transactions.commitTxFee staticChannelConfig.RemoteParams.DustLimitSatoshis currentSpec
608+ else
609+ Money.Zero
610+ let missing = currentSpec.ToRemote.ToMoney() - staticChannelConfig.RemoteParams.ChannelReserveSatoshis - fees
611+ if missing < Money.Zero then
612+ 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 "
613+ ( currentSpec.ToRemote.ToMoney())
614+ staticChannelConfig.RemoteParams.ChannelReserveSatoshis
615+ fees
616+ |> Error
617+ else
618+ Ok()
619+
573620module internal UpdateAddHTLCValidationWithContext =
574621 let checkLessThanHTLCValueInFlightLimit ( currentSpec : CommitmentSpec ) ( limit ) ( add : UpdateAddHTLCMsg ) =
575622 let outgoingValue =
0 commit comments