Skip to content

Commit

Permalink
Prevent possible F#4.0 issue
Browse files Browse the repository at this point in the history
It seems that these attributes can cause this compilation error in F#4.0:
error FS0927: The kind of the type specified by its attributes does not match the kind implied by its definition

Newer versions of F# (like 4.5 or even newer, like the one being used
by .NETCore to build the binary that is later published in nuget) allow
compiling this code with no issues, but if you reference the generated
assembly later from an old F# compiler, it could generate exceptions at
runtime, e.g.: System.BadFormatImageException (or other types) whose
inner exception could be the following:

System.TypeLoadException : Could not load type of field 'GWallet.Backend.UtxoCoin.Lightning.SerializedChannel:MinSafeDepth@' (6) due to: Expected reference type but got type kind 17

FSharp.Core's Result type is also affected by this so in this commit we
create a replacement for it that is only used in the BouncyCastle build
(which we now rename as 'Portability' build).

Forward-ported from a4a59d0

Co-authored-by: Andres G. Aragoneses <[email protected]>
  • Loading branch information
Bobface and knocte committed Nov 13, 2020
1 parent 4edbd2e commit 4d462bd
Show file tree
Hide file tree
Showing 40 changed files with 216 additions and 103 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ jobs:
with:
dotnet-version: ${{ matrix.dotnet }}

- name: Run tests (BouncyCastle)
- name: Run tests (Portability)
# we want to run only once.
if: startsWith(matrix.os, 'ubuntu-18')
run: |
dotnet build tests/DotNetLightning.Core.Tests -p:BouncyCastle=True
dotnet build tests/DotNetLightning.Core.Tests -p:Portability=True
dotnet run --no-build --project tests/DotNetLightning.Core.Tests
- name: Clean to prepare for NSec build
Expand All @@ -40,6 +40,6 @@ jobs:
run: |
DEBIAN_FRONTEND=noninteractive sudo apt install -y msbuild fsharp
dotnet restore -p:BouncyCastle=True DotNetLightning.sln
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:BouncyCastle=True -p:TargetFramework=netstandard2.0
dotnet restore -p:Portability=True DotNetLightning.sln
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:Portability=True -p:TargetFramework=netstandard2.0
4 changes: 2 additions & 2 deletions .github/workflows/publish_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
with:
dotnet-version: '3.1.200'

- name: Upload nuget packages (BouncyCastle)
- name: Upload nuget packages (Portability)
if: startsWith(matrix.os, 'ubuntu')
run: |
dotnet pack ./src/DotNetLightning.Core -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | head -c 7` -p:BouncyCastle=True
dotnet pack ./src/DotNetLightning.Core -p:Configuration=Release --version-suffix date`date +%Y%m%d-%H%M`-git-`echo $GITHUB_SHA | head -c 7` -p:Portability=True
dotnet nuget push ./src/DotNetLightning.Core/bin/Release/DotNetLightning.Kiss.1*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
10 changes: 5 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

<!-->Since NBitcoin.Secp256k1 does not support netstandard 2.0, we will fallback to BouncyCastle build<-->
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<BouncyCastle>True</BouncyCastle>
<Portability>True</Portability>
</PropertyGroup>


<Choose>
<When Condition="'$(BouncyCastle)'=='true'">
<When Condition="'$(Portability)'=='true'">
<PropertyGroup>
<OtherFlags>$(OtherFlags) -d:BouncyCastle</OtherFlags>
<DefineConstants>$(DefineConstants);BouncyCastle</DefineConstants>
<OtherFlags>$(OtherFlags) -d:NoDUsAsStructs -d:BouncyCastle</OtherFlags>
<DefineConstants>$(DefineConstants);NoDUsAsStructs;BouncyCastle</DefineConstants>
</PropertyGroup>
</When>
</Choose>

<ItemGroup>
<PackageReference Include="NBitcoin" Version="5.0.65" />
<PackageReference Condition="'$(BouncyCastle)'=='true'" Include="Portable.BouncyCastle" Version="1.8.6.7" />
<PackageReference Condition="'$(Portability)'=='true'" Include="Portable.BouncyCastle" Version="1.8.6.7" />
</ItemGroup>

<PropertyGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Channel/Channel.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Utils.NBitcoinExtensions
open DotNetLightning.Utils.Aether
Expand All @@ -12,6 +10,9 @@ open DotNetLightning.Serialization.Msgs
open NBitcoin
open System

open ResultUtils
open ResultUtils.Portability


type ProvideFundingTx = IDestination * Money * FeeRatePerKw -> Result<FinalizedTx * TxOutIndex, string>
type Channel = {
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Channel/ChannelError.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils
open DotNetLightning.Utils
open NBitcoinExtensions
open DotNetLightning.Utils.OnionError
Expand All @@ -11,6 +10,9 @@ open DotNetLightning.Transactions

open NBitcoin

open ResultUtils
open ResultUtils.Portability

type ChannelError =
| CryptoError of CryptoError
| TransactionRelatedErrors of TransactionError list
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Channel/ChannelOperations.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils
open DotNetLightning.Utils
open DotNetLightning.Utils.NBitcoinExtensions
open DotNetLightning.Utils.OnionError
Expand All @@ -13,6 +12,9 @@ open DotNetLightning.Serialization

open NBitcoin

open ResultUtils
open ResultUtils.Portability

type OperationMonoHopUnidirectionalPayment = {
Amount: LNMoney
}
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Channel/ChannelValidation.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Channel

open ResultUtils
open NBitcoin

open DotNetLightning.Chain
Expand All @@ -10,6 +9,9 @@ open DotNetLightning.Utils.NBitcoinExtensions
open DotNetLightning.Serialization.Msgs
open DotNetLightning.Transactions

open ResultUtils
open ResultUtils.Portability

exception ChannelException of ChannelError
module internal ChannelHelpers =

Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Channel/CommitmentsModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace DotNetLightning.Channel

open NBitcoin

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Transactions
open DotNetLightning.Crypto
open DotNetLightning.Chain
open DotNetLightning.Serialization.Msgs

open ResultUtils
open ResultUtils.Portability

[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module internal Commitments =
module private Helpers =
Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Crypto/Sphinx.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ namespace DotNetLightning.Crypto
open System
open NBitcoin

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Serialization
open DotNetLightning.Serialization.Msgs

open ResultUtils
open ResultUtils.Portability

module Sphinx =
open NBitcoin.Crypto

Expand Down
6 changes: 3 additions & 3 deletions src/DotNetLightning.Core/DotNetLightning.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<Choose>
<When Condition="'$(BouncyCastle)'=='true'">
<When Condition="'$(Portability)'=='true'">
<PropertyGroup>
<PackageId>DotNetLightning.Kiss</PackageId>
</PropertyGroup>
Expand All @@ -19,7 +19,7 @@
</Choose>

<ItemGroup>
<ProjectReference Condition="'$(BouncyCastle)'!='true'" Include="..\NSec\Experimental\NSec.Experimental.csproj" PrivateAssets="all" />
<ProjectReference Condition="'$(Portability)'!='true'" Include="..\NSec\Experimental\NSec.Experimental.csproj" PrivateAssets="all" />
<ProjectReference Include="..\ResultUtils\ResultUtils.fsproj" PrivateAssets="all" />
<ProjectReference Include="..\InternalBech32Encoder\InternalBech32Encoder.csproj" PrivateAssets="all" />
<ProjectReference Include="..\Macaroons\Macaroons.csproj" PrivateAssets="all" />
Expand Down Expand Up @@ -98,7 +98,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" />
<PackageReference Condition="'$(BouncyCastle)' != 'true'" Include="NBitcoin.Secp256k1" Version="1.0.8" />
<PackageReference Condition="'$(Portability)' != 'true'" Include="NBitcoin.Secp256k1" Version="1.0.8" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Payment/LSAT/Satisfier.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace DotNetLightning.Payment.LSAT

open ResultUtils
open DotNetLightning.Utils
open Macaroons
open System.Collections.Generic
open System.Runtime.CompilerServices
open NBitcoin

open ResultUtils
open ResultUtils.Portability

/// When we verify a macaroon for its caveats, usually it check each caveats independently.
/// In case of LSAT, this does not work since the validity of a caveat depends on a previous caveat
/// (more specifically, if there were two caveats with a same Condition, we usually check that whether the restriction
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Payment/LSAT/Service.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ open System
open System.Collections.Generic

open Macaroons
open ResultUtils

open System.Text
open DotNetLightning.Utils

open ResultUtils
open ResultUtils.Portability

/// See: https://github.com/lightninglabs/LSAT/blob/master/macaroons.md#target-services
type Service = {
Name: string
Expand Down
4 changes: 2 additions & 2 deletions src/DotNetLightning.Core/Payment/PaymentRequest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ open System.Text
open System.Collections
open System.Diagnostics

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Core.Utils.Extensions
open DotNetLightning.Serialization
Expand All @@ -16,6 +14,8 @@ open NBitcoin
open NBitcoin.Crypto
open NBitcoin.DataEncoders

open ResultUtils
open ResultUtils.Portability

module private Helpers =
let base58check = Base58CheckEncoder()
Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Peer/Peer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace DotNetLightning.Peer

open NBitcoin

open ResultUtils

open DotNetLightning.Serialization
open System.Collections
open DotNetLightning.Utils
open DotNetLightning.Serialization.Msgs
open DotNetLightning.Utils.Aether

open ResultUtils
open ResultUtils.Portability

type PeerHandleError = {
/// Used to indicate that we probably can't make any future connections to this peer, implying
/// we should go ahead and force-close any channels we have with it.
Expand Down
4 changes: 2 additions & 2 deletions src/DotNetLightning.Core/Peer/PeerChannelEncryptor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ open System
open NBitcoin
open NBitcoin.Crypto

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Utils.Aether
open DotNetLightning.Utils.Aether.Operators
open DotNetLightning.Crypto

open ResultUtils
open ResultUtils.Portability

[<AutoOpen>]
module PeerChannelEncryptor =
Expand Down
3 changes: 3 additions & 0 deletions src/DotNetLightning.Core/Routing/NetworkStats.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ open DotNetLightning.Utils
open DotNetLightning.Utils.Primitives
open NBitcoin

open ResultUtils
open ResultUtils.Portability

type Stats<'T> = {
Median: 'T
Percentile5: 'T
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Routing/Router.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNetLightning.Routing

open ResultUtils
open DotNetLightning.Utils.Primitives
open DotNetLightning.Utils

Expand All @@ -11,6 +10,9 @@ open DotNetLightning.Payment
open DotNetLightning.Routing.Graph
open NBitcoin

open ResultUtils
open ResultUtils.Portability

module Routing =

/// This method is used after a payment failed, and we want to exclude some nodes that we know are failing
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Routing/RouterPrimitives.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ namespace DotNetLightning.Routing
open DotNetLightning.Payment
open System
open NBitcoin
open ResultUtils
open DotNetLightning.Utils
open DotNetLightning.Serialization.Msgs
open Graph

open ResultUtils
open ResultUtils.Portability

[<AutoOpen>]
module RouterPrimitives =
let checkUpdate(x: ChannelUpdateMsg option, msg ) =
Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Routing/RouterState.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
namespace DotNetLightning.Routing

open ResultUtils
open System.Collections.Generic
open DotNetLightning.Payment
open DotNetLightning.Serialization.Msgs
open DotNetLightning.Utils
open Graph


open ResultUtils
open ResultUtils.Portability

type RouteParams = {
Randomize: bool
MaxFeeBase: LNMoney
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetLightning.Core/Routing/RouterTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ open System.Collections.Generic
open DotNetLightning.Utils
open DotNetLightning.Serialization.Msgs
open NBitcoin

open ResultUtils
open ResultUtils.Portability


type NetworkEvent =
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Serialization/Encoding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace DotNetLightning.Serialization

open DotNetLightning.Core.Utils.Extensions
open DotNetLightning.Utils.Primitives
open ResultUtils
open System
open System.IO
open System.IO.Compression

open ResultUtils
open ResultUtils.Portability


module Decoder =
let private tryDecode (encodingType: EncodingType) (bytes : byte[]) =
Expand Down
5 changes: 3 additions & 2 deletions src/DotNetLightning.Core/Serialization/Features.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ namespace DotNetLightning.Serialization

open System.Collections

open ResultUtils

open System
open System.Text
open DotNetLightning.Core.Utils.Extensions

open ResultUtils
open ResultUtils.Portability

type FeaturesSupport =
| Mandatory
| Optional
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetLightning.Core/Serialization/GenericTLV.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace DotNetLightning.Serialization

open System
open ResultUtils
open DotNetLightning.Core.Utils.Extensions

open ResultUtils
open ResultUtils.Portability

type GenericTLV = {
Type: uint64
Value: byte[]
Expand Down
Loading

0 comments on commit 4d462bd

Please sign in to comment.