Skip to content

Commit

Permalink
Merge pull request #58 from make-software/netstandard-2-0-target
Browse files Browse the repository at this point in the history
Prepare for multi-framework package publish
  • Loading branch information
mrkara authored May 2, 2024
2 parents bbb2883 + bc17466 commit f69e90e
Show file tree
Hide file tree
Showing 34 changed files with 492 additions and 71 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and Test

on:
push:
branches: [ master ]
branches: [ master, netstandard-2-0-target ]
pull_request:
branches: [ master ]

Expand All @@ -11,6 +11,13 @@ jobs:

runs-on: ubuntu-latest

strategy:
matrix:
include:
- name: net7.0
framework: net7.0
- name: netstandard2.0
framework: netstandard2.0
steps:
- uses: actions/checkout@v2
- name: Setup .NET
Expand All @@ -22,4 +29,4 @@ jobs:
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --filter "TestCategory!~NCTL"
run: TEST_FRAMEWORK=${{ matrix.framework }} dotnet test --no-build --verbosity normal --filter "TestCategory!~NCTL"
10 changes: 9 additions & 1 deletion .github/workflows/integration-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ jobs:
# The type of runner that the job will run on
runs-on: ubuntu-latest

strategy:
matrix:
include:
- name: net7.0
framework: net7.0
- name: netstandard2.0
framework: netstandard2.0

# Service containers to run with `runner-job`
services:
# Label used to access the service container
Expand Down Expand Up @@ -37,4 +45,4 @@ jobs:
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --settings Casper.Network.SDK.Test/test.runsettings --filter="TestCategory=NCTL"
run: TEST_FRAMEWORK=${{ matrix.framework }} dotnet test --no-build --verbosity normal --settings Casper.Network.SDK.Test/test.runsettings --filter="TestCategory=NCTL"
24 changes: 24 additions & 0 deletions .github/workflows/unity-compat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ame: Unity Compat - Build and Test

on:
push:
branches: [netstandard-2-0-target]
pull_request:
branches: [netstandard-2-0-target]

jobs:
buildntest:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --filter "TestCategory!~NCTL"
5 changes: 2 additions & 3 deletions Casper.Network.SDK.Test/CLValueJsonDeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Text.Json.Serialization;
using Casper.Network.SDK.Converters;
using Casper.Network.SDK.Types;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Org.BouncyCastle.Utilities.Encoders;

Expand Down Expand Up @@ -148,7 +147,7 @@ public void DeserializeI32CLValue()
var json = @"{""cl_type"":""I32"",""bytes"":""00000080"",""parsed"":-2147483648}";
var clValue = JsonSerializer.Deserialize<CLValue>(json);
Assert.IsNotNull(clValue);
Assert.AreEqual(int.MinValue, BitConverter.ToInt32(clValue.Bytes));
Assert.AreEqual(int.MinValue, BitConverterExtensions.ToInt32(clValue.Bytes));
Assert.AreEqual(int.MinValue, clValue.Parsed);
}

Expand All @@ -168,7 +167,7 @@ public void DeserializeU64CLValue()
var clValue = JsonSerializer.Deserialize<CLValue>(json);
Assert.IsNotNull(clValue);
Assert.AreEqual(CLType.U64, clValue.TypeInfo.Type);
Assert.AreEqual(ulong.MaxValue, BitConverter.ToUInt64(clValue.Bytes));
Assert.AreEqual(ulong.MaxValue, BitConverterExtensions.ToUInt64(clValue.Bytes));
Assert.AreEqual(ulong.MaxValue, clValue.Parsed);
}

Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/CLValueToTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void CLValueToBigInteger()
Assert.AreEqual(new BigInteger(5123456789012), clValue.ToBigInteger());
Assert.AreEqual(new BigInteger(5123456789012), (BigInteger) clValue);

var bigInt = new BigInteger(Hex.Decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"), true);
var bigInt = BigIntegerCompat.Create(Hex.Decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"), true);
clValue = CLValue.U128(bigInt);
Assert.AreEqual(bigInt, clValue.ToBigInteger());
Assert.AreEqual(bigInt, (BigInteger) clValue);
Expand Down
18 changes: 13 additions & 5 deletions Casper.Network.SDK.Test/Casper.Network.SDK.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<LangVersion>9.0</LangVersion>
<IsPackable>false</IsPackable>

<RootNamespace>NetCasperTest</RootNamespace>
</PropertyGroup>

Expand All @@ -15,10 +14,19 @@
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" />
<!-- Define a property to read from an environment variable -->
<PropertyGroup>
<CustomTargetFramework Condition="'$(TEST_FRAMEWORK)' != ''">$(TEST_FRAMEWORK)</CustomTargetFramework>
<!-- Fallback to a default framework if the environment variable is not set -->
<CustomTargetFramework Condition="'$(CustomTargetFramework)' == ''">net7.0</CustomTargetFramework>
</PropertyGroup>

<ItemGroup Condition="'$(CustomTargetFramework)' == 'net7.0'">
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" Properties="TargetFramework=net7.0" />
</ItemGroup>
<ItemGroup Condition="'$(CustomTargetFramework)' == 'netstandard2.0'">
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" Properties="TargetFramework=netstandard2.0" />
</ItemGroup>

<ItemGroup>
<Content Include="TestData\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
18 changes: 9 additions & 9 deletions Casper.Network.SDK.Test/KeysTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ public void TestValidBlakeEd25519()
var publicKey = PublicKey.FromHexString(ED25519publicKey);
Assert.AreEqual(KeyAlgo.ED25519, publicKey.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, publicKey.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(publicKey.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(publicKey.RawBytes));

var hash = publicKey.GetAccountHash();
Assert.AreEqual(ED25519hash, hash.Substring("account-hash-".Length), "Unexpected ED25519 hash value");

var pk2 = PublicKey.FromBytes(Hex.Decode(ED25519publicKey));
Assert.AreEqual(KeyAlgo.ED25519, pk2.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk2.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk2.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk2.RawBytes));

var pk3 = PublicKey.FromRawBytes(Hex.Decode(ED25519publicKey)[1..], KeyAlgo.ED25519);
var pk3 = PublicKey.FromRawBytes(Hex.Decode(ED25519publicKey).Slice(1), KeyAlgo.ED25519);
Assert.AreEqual(KeyAlgo.ED25519, pk3.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk3.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk3.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk3.RawBytes));

var pemfile = TestContext.CurrentContext.TestDirectory +
"/TestData/test-ed25519-pk.pem";
var pk4 = PublicKey.FromPem(pemfile);
Assert.AreEqual(KeyAlgo.ED25519, pk4.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk4.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).SequenceEqual(pk4.GetBytes()));

var hash3 = pk4.GetAccountHash();
Expand All @@ -60,19 +60,19 @@ public void TestValidBlakeSecp256k1()
var pk2 = PublicKey.FromBytes(Hex.Decode(SECP256K1publicKey));
Assert.AreEqual(KeyAlgo.SECP256K1, pk2.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk2.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk2.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk2.RawBytes));

var pk3 = PublicKey.FromRawBytes(Hex.Decode(SECP256K1publicKey)[1..], KeyAlgo.SECP256K1);
var pk3 = PublicKey.FromRawBytes(Hex.Decode(SECP256K1publicKey).Slice(1), KeyAlgo.SECP256K1);
Assert.AreEqual(KeyAlgo.SECP256K1, pk3.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk3.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk3.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk3.RawBytes));

var pemfile = TestContext.CurrentContext.TestDirectory +
"/TestData/test-secp256k1-pk.pem";
var pk4 = PublicKey.FromPem(pemfile);
Assert.AreEqual(KeyAlgo.SECP256K1, pk4.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk4.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).SequenceEqual(pk4.GetBytes()));

var hash3 = pk4.GetAccountHash();
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NctlContractTest : NctlBase
[Test, Order(1)]
public async Task DeployContractTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlMyDictContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NctlMyDictContractTest : NctlBase
[Test, Order(1)]
public async Task DeployContractTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlSpeculativeExecutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Setup()
[Test, Order(1)]
public async Task SpeculativeExecutionTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
4 changes: 3 additions & 1 deletion Casper.Network.SDK.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\d\AppData\Local\JetBrains\Rider2021.3\resharper-host\temp\Rider\vAny\CoverageData\_Casper.Network.SDK.-947408436\Snapshot\snapshot.utdcvr</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=240e6887_002D6585_002D487e_002D89bf_002D0b306f497b30/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
14 changes: 9 additions & 5 deletions Casper.Network.SDK/Casper.Network.SDK.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0</FileVersion>
<PackageVersion>2.2.0</PackageVersion>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<NoWarn>CS1591</NoWarn>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0</FileVersion>
<PackageVersion>2.3.0</PackageVersion>
<Title>Casper.Network.SDK</Title>
<Authors>make-software</Authors>
<PackageProjectUrl>https://github.com/make-software/casper-net-sdk</PackageProjectUrl>
Expand All @@ -18,11 +20,13 @@
</Description>
<RootNamespace>Casper.Network.SDK</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions Casper.Network.SDK/Compat/BigIntegerCompat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Numerics;

public class BigIntegerCompat
{
public static BigInteger Create(byte[] value, bool isUnsigned = false, bool isBigEndian = false)
{
#if NET7_0_OR_GREATER
return new BigInteger(value, true, false);
#elif NETSTANDARD2_0
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (isBigEndian)
{
Array.Reverse(value);
}

if (isUnsigned)
{
// Ensure that the resulting BigInteger is treated as an unsigned value by appending a 0 byte if necessary
if (value[value.Length - 1] >= 0x80)
{
Array.Resize(ref value, value.Length + 1);
value[value.Length - 1] = 0;
}
}

return new BigInteger(value);
#endif
}
}

62 changes: 62 additions & 0 deletions Casper.Network.SDK/Compat/BigIntegerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#if NETSTANDARD2_0

using System;
using System.Numerics;

public static class BigIntegerExtensions
{
public static byte[] ToByteArray(this BigInteger value, bool isUnsigned = false)
{
byte[] bytes = value.ToByteArray();

if (isUnsigned)
{
if (value < 0)
{
throw new InvalidOperationException("Cannot retrieve an unsigned byte array representation of a negative BigInteger.");
}

// If the BigInteger is positive and the highest byte is 0x80 or higher,
// an additional byte is added to indicate a positive number when using two's complement notation.
// For the unsigned representation, this additional byte is not necessary.
if (bytes.Length > 1 && bytes[bytes.Length - 1] == 0)
{
Array.Resize(ref bytes, bytes.Length - 1);
}
}

return bytes;
}

public static int GetBitLength(this BigInteger value)
{
// If the value is zero, its bit length is 0
if (value == BigInteger.Zero)
return 0;

// Get the byte array without the sign
byte[] bytes = value.ToByteArray();

// Find the highest-order byte with a non-zero value
int highestByte = bytes.Length - 1;
while (highestByte > 0 && bytes[highestByte] == 0)
{
highestByte--;
}

// Count the bits in the last used byte
int bitsInLastByte = 0;
byte lastByte = bytes[highestByte];
while (lastByte != 0)
{
bitsInLastByte++;
lastByte >>= 1;
}

// Calculate the total bit length
// (Number of full bytes minus 1) * 8 + bits in last byte
return highestByte * 8 + bitsInLastByte;
}
}

#endif
26 changes: 26 additions & 0 deletions Casper.Network.SDK/Compat/BitConverterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

using System;

public static class BitConverterExtensions
{
public static Int64 ToInt64(byte[] value, int startIndex = 0)
{
return BitConverter.ToInt64(value, startIndex);
}

public static Int32 ToInt32(byte[] value, int startIndex = 0)
{
return BitConverter.ToInt32(value, startIndex);
}

public static UInt64 ToUInt64(byte[] value, int startIndex = 0)
{
return BitConverter.ToUInt64(value, startIndex);
}

public static UInt32 ToUInt32(byte[] value, int startIndex = 0)
{
return BitConverter.ToUInt32(value, startIndex);
}
}

Loading

0 comments on commit f69e90e

Please sign in to comment.