Skip to content

Commit

Permalink
Add support for option_support_large_channel
Browse files Browse the repository at this point in the history
This commit adds support for option_support_large_channel
and there are some changes made to validation process;
more specifically when validating our OpenChannel msg,
instead of just sending out an Error if the funding_satoshis
is greater than the limit, we check if support_large_channel
is activated as mandatory (cause if we don't set that option as
mandatory the openChannel will be invalid if sent to a peet with
no support for support_large_channel) and also when we are validating
someone else's openChannelMsg, we check that we have
support_large_channel option activated (it doesn't matter if mandatory
or optional).
  • Loading branch information
aarani authored and knocte committed Jun 29, 2021
1 parent 0c83757 commit e2e13a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ module Channel =
ShutdownScriptPubKey = cs.Config.ChannelOptions.ShutdownScriptPubKey
}
result {
do! Validation.checkOurOpenChannelMsgAcceptable (cs.Config) openChannelMsgToSend
do! Validation.checkOurOpenChannelMsgAcceptable (cs.Config) openChannelMsgToSend inputInitFunder.LocalParams
return [
NewOutboundChannelStarted(
openChannelMsgToSend, {
Expand Down Expand Up @@ -290,8 +290,8 @@ module Channel =
makeChannelReestablish cs.ChannelPrivKeys state
| WaitForOpenChannel state, ApplyOpenChannel msg ->
result {
do! Validation.checkOpenChannelMsgAcceptable (cs.FeeEstimator) (cs.Config) msg
let localParams = state.InitFundee.LocalParams
do! Validation.checkOpenChannelMsgAcceptable (cs.FeeEstimator) (cs.Config) msg localParams
let channelKeys = state.InitFundee.ChannelPrivKeys
let localCommitmentPubKey = channelKeys.CommitmentSeed.DerivePerCommitmentPoint CommitmentNumber.FirstCommitment
let acceptChannelMsg: AcceptChannelMsg = {
Expand Down
19 changes: 16 additions & 3 deletions src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open NBitcoinExtensions
open DotNetLightning.Utils.OnionError
open DotNetLightning.Chain
open DotNetLightning.Crypto
open DotNetLightning.Serialization
open DotNetLightning.Serialization.Msgs
open DotNetLightning.Transactions

Expand Down Expand Up @@ -323,9 +324,21 @@ module internal OpenChannelMsgValidation =
else
Ok()

let checkFundingSatoshisLessThanMax (msg: OpenChannelMsg) =
if (msg.FundingSatoshis >= ChannelConstants.MAX_FUNDING_SATOSHIS) then
sprintf "funding_satoshis must be less than %A. It was %A" ChannelConstants.MAX_FUNDING_SATOSHIS msg.FundingSatoshis
let checkFundingSatoshisLessThanMax (msg: OpenChannelMsg) (localParams: LocalParams) (isOurOpenChannelMsg: bool) =
// If we are validating our own open message we make sure we are forcing support for option_support_large_channel on the other peer
let featureType =
match isOurOpenChannelMsg with
| true ->
Some FeaturesSupport.Mandatory
| false ->
None

if msg.FundingSatoshis >= ChannelConstants.MAX_FUNDING_SATOSHIS &&
not (localParams.Features.HasFeature (Feature.OptionSupportLargeChannel, ?featureType = featureType)) then
sprintf
"funding_satoshis must be less than %A. It was %A, consider activating option_support_large_channel feature."
ChannelConstants.MAX_FUNDING_SATOSHIS
msg.FundingSatoshis
|> Error
else
Ok()
Expand Down
8 changes: 4 additions & 4 deletions src/DotNetLightning.Core/Channel/ChannelValidation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,18 @@ module internal ChannelHelpers =
module internal Validation =

open DotNetLightning.Channel
let checkOurOpenChannelMsgAcceptable (_conf: ChannelConfig) (msg: OpenChannelMsg) =
Validation.ofResult(OpenChannelMsgValidation.checkFundingSatoshisLessThanMax msg)
let checkOurOpenChannelMsgAcceptable (_conf: ChannelConfig) (msg: OpenChannelMsg) (localParams: LocalParams) =
Validation.ofResult(OpenChannelMsgValidation.checkFundingSatoshisLessThanMax msg localParams true)
*^> OpenChannelMsgValidation.checkChannelReserveSatohisLessThanFundingSatoshis msg
*^> OpenChannelMsgValidation.checkPushMSatLesserThanFundingValue msg
*^> OpenChannelMsgValidation.checkFundingSatoshisLessThanDustLimitSatoshis msg
*^> OpenChannelMsgValidation.checkMaxAcceptedHTLCs msg
*^> OpenChannelMsgValidation.checkFunderCanAffordFee (msg.FeeRatePerKw) msg
|> Result.mapError((@)["open_channel msg is invalid"] >> InvalidOpenChannelError.Create msg >> InvalidOpenChannel)

let internal checkOpenChannelMsgAcceptable (feeEstimator: IFeeEstimator) (conf: ChannelConfig) (msg: OpenChannelMsg) =
let internal checkOpenChannelMsgAcceptable (feeEstimator: IFeeEstimator) (conf: ChannelConfig) (msg: OpenChannelMsg) (localParams: LocalParams) =
let feeRate = feeEstimator.GetEstSatPer1000Weight(ConfirmationTarget.Background)
Validation.ofResult(OpenChannelMsgValidation.checkFundingSatoshisLessThanMax msg)
Validation.ofResult(OpenChannelMsgValidation.checkFundingSatoshisLessThanMax msg localParams false)
*^> OpenChannelMsgValidation.checkChannelReserveSatohisLessThanFundingSatoshis msg
*^> OpenChannelMsgValidation.checkPushMSatLesserThanFundingValue msg
*^> OpenChannelMsgValidation.checkFundingSatoshisLessThanDustLimitSatoshis msg
Expand Down
1 change: 1 addition & 0 deletions src/DotNetLightning.Core/Serialization/Features.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module internal Feature =
yield Feature.PaymentSecret
// TODO: support this feature
// Feature.BasicMultiPartPayment
yield Feature.OptionSupportLargeChannel
}
|> Seq.map(fun f -> f.Mandatory)
|> Set
Expand Down
5 changes: 3 additions & 2 deletions tests/DotNetLightning.Core.Tests/Serialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ module SerializationTest =
1L <<< Feature.BasicMultiPartPayment.MandatoryBitPosition, false
1L <<< Feature.BasicMultiPartPayment.OptionalBitPosition, true

1L <<< Feature.OptionSupportLargeChannel.MandatoryBitPosition, false
1L <<< Feature.OptionSupportLargeChannel.MandatoryBitPosition, true
1L <<< Feature.OptionSupportLargeChannel.OptionalBitPosition, true
]
for (s, expected) in testCases do
Expand All @@ -860,8 +860,9 @@ module SerializationTest =
// unknown optional feature bits
|> Map.add " 10000000000000000000" true
|> Map.add " 001000000000000000000000" true
// support_large_channel_option(mandatory)
|> Map.add " 000001000000000000000000" true
// those are useful for nonreg testing of the areSupported method (which needs to be updated with every new supported mandatory bit)
|> Map.add " 000001000000000000000000" false
|> Map.add " 000100000000000000000000" false
|> Map.add " 010000000000000000000000" false
|> Map.add " 0001000000000000000000000000" false
Expand Down

0 comments on commit e2e13a5

Please sign in to comment.