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).

Co-authored-by: Andres G. Aragoneses <[email protected]>
  • Loading branch information
Bobface and knocte committed Sep 2, 2020
1 parent ee56733 commit a4a59d0
Show file tree
Hide file tree
Showing 65 changed files with 254 additions and 103 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
# 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 @@ -41,5 +41,5 @@ jobs:
DEBIAN_FRONTEND=noninteractive sudo apt install -y msbuild fsharp nuget
nuget restore DotNetLightning.sln
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:BouncyCastle=True -p:TargetFramework=netstandard2.0
msbuild src/DotNetLightning.Core/DotNetLightning.Core.fsproj -p:Portability=True -p:TargetFramework=netstandard2.0
2 changes: 1 addition & 1 deletion .github/workflows/publish_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ jobs:
- name: Upload nuget packages (BouncyCastle)
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
2 changes: 1 addition & 1 deletion .github/workflows/publish_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
if: startsWith(matrix.os, 'ubuntu-18')
run: |
echo "releasing BouncyCastle version to nuget..."
dotnet pack -p:Configuration=Release src/DotNetLightning.Core -p:BouncyCastle=True
dotnet pack -p:Configuration=Release src/DotNetLightning.Core -p:Portability=True
if [ ${{ secrets.NUGET_API_KEY }} ]; then
dotnet nuget push ./src/DotNetLightning.Core/bin/Release/DotNetLightning.${{ steps.get_version.outputs.VERSION }}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
fi
Expand Down
4 changes: 2 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,8 @@ open DotNetLightning.Serialize.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/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.Serialize.Msgs
open DotNetLightning.Transactions

open ResultUtils
open ResultUtils.Portability

exception ChannelException of ChannelError
module internal ChannelHelpers =

Expand Down
3 changes: 3 additions & 0 deletions src/DotNetLightning.Core/Channel/Commitments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ open DotNetLightning.Crypto
open DotNetLightning.Transactions
open DotNetLightning.Serialize.Msgs

open ResultUtils
open ResultUtils.Portability

type LocalChanges = {
Proposed: IUpdateMsg list
Signed: IUpdateMsg list
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.Serialize.Msgs

open ResultUtils
open ResultUtils.Portability

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

#endif

open ResultUtils.Portability

type CryptoError =
| BadMac
| InvalidErrorPacketLength of expected: int * actual: int
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetLightning.Core/Crypto/RevocationSet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ open NBitcoin
open NBitcoin.Crypto
open DotNetLightning.Utils

open ResultUtils.Portability

type InsertRevocationKeyError =
| UnexpectedCommitmentNumber of got: CommitmentNumber * expected: CommitmentNumber
| KeyMismatch of previousCommitmentNumber: CommitmentNumber * newCommitmentNumber: CommitmentNumber
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 @@ -2,12 +2,13 @@ namespace DotNetLightning.Crypto
open System
open NBitcoin

open ResultUtils

open DotNetLightning.Utils
open DotNetLightning.Serialize
open DotNetLightning.Serialize.Msgs

open ResultUtils
open ResultUtils.Portability

module Sphinx =
open NBitcoin.Crypto

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

<!-->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>
<OtherFlags>$(OtherFlags) -d:NoDUsAsStructs -d:BouncyCastle</OtherFlags>
<PackageId>DotNetLightning.Kiss</PackageId>
</PropertyGroup>
</When>
Expand All @@ -24,7 +24,7 @@
</Otherwise>
</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 @@ -99,8 +99,8 @@
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" />
<PackageReference Include="NBitcoin" Version="5.0.53" />
<PackageReference Condition="'$(BouncyCastle)' != 'true'" Include="NBitcoin.Secp256k1" Version="1.0.3" />
<PackageReference Condition="'$(BouncyCastle)'=='true'" Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Condition="'$(Portability)' != 'true'" Include="NBitcoin.Secp256k1" Version="1.0.3" />
<PackageReference Condition="'$(Portability)' == 'true'" Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions src/DotNetLightning.Core/Payment/Amount.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ namespace DotNetLightning.Payment
open System
open DotNetLightning.Utils

open ResultUtils
open ResultUtils.Portability

[<RequireQualifiedAccess>]
module Amount =
let unit (amount: LNMoney): char =
Expand Down
1 change: 1 addition & 0 deletions src/DotNetLightning.Core/Payment/LSAT/CaveatsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace DotNetLightning.Payment.LSAT
open Macaroons
open System.Runtime.CompilerServices

open ResultUtils.Portability

[<Extension;AbstractClass;Sealed>]
type CaveatsExtensions() =
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetLightning.Core/Payment/LSAT/MacaroonIdentifier.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ open DotNetLightning.Core.Utils.Extensions
open DotNetLightning.Utils
open NBitcoin.Crypto

open ResultUtils.Portability

module private Helpers =
let hex = DataEncoders.HexEncoder()

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
5 changes: 3 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.Serialize
Expand All @@ -16,6 +14,9 @@ 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.Serialize
open System.Collections
open DotNetLightning.Utils
open DotNetLightning.Serialize.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/Graph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ open DotNetLightning.Utils
open DotNetLightning.Serialize.Msgs
open NBitcoin

open ResultUtils
open ResultUtils.Portability

// Graph algorithms are based on eclair

module Graph =
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.Serialize.Msgs
open Graph

open ResultUtils
open ResultUtils.Portability

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

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

open ResultUtils
open ResultUtils.Portability

type RouteParams = {
Randomize: bool
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetLightning.Core/Routing/RouterTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ open System.Collections.Generic
open DotNetLightning.Utils
open DotNetLightning.Serialize.Msgs
open NBitcoin
open ResultUtils

open ResultUtils.Portability

type NetworkEvent =
| NodeDiscovered of msg: NodeAnnouncementMsg
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetLightning.Core/Serialize/BitWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System.Collections
open System.Text
open System.Text

open ResultUtils.Portability

type BitReader(ba: BitArray, bitCount: int) =

member val Count = bitCount with get
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetLightning.Core/Serialize/EncodedTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace DotNetLightning.Serialize

open System.IO

open ResultUtils.Portability

type QueryFlags = private QueryFlags of uint8
with
static member Create (data) = QueryFlags(data)
Expand Down
3 changes: 2 additions & 1 deletion src/DotNetLightning.Core/Serialize/Encoding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace DotNetLightning.Serialize

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
Loading

0 comments on commit a4a59d0

Please sign in to comment.