Skip to content

Commit

Permalink
Merge branch 'master' into feature/websocket-events
Browse files Browse the repository at this point in the history
# Conflicts:
#	Substrate.NetApi/SubstrateClient.cs
  • Loading branch information
Apolixit committed Sep 14, 2024
2 parents 66150d4 + ecb2644 commit ca3e321
Show file tree
Hide file tree
Showing 60 changed files with 11,308 additions and 9,724 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

7 changes: 4 additions & 3 deletions Substrate.NetApi.Test/Extrinsic/InstanceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace Substrate.NetApi.Test.Extrinsic
public class InstanceTest
{

// Extrinsics created and signed with polkadot-js/apps.
public static string[] Extrinsic = new string[]
{
// Timestamp set(now) with now = 1673908758014
"0x280403000bfea5bcbc8501",
"0xc50184001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0188f4859d09355d8f964b8cbb8b84c5e0e2f86a423d30027d15764996dc198532044424e6cc2e281f4d23174b6bfb3d2af64917b7356e570f41a1e44bcaa47385650300000001000bfea5bcbc8501",
// Staking bondExtra(maxAdditional) with maxAditional = 60000000000
"0xbd01840021e64ca11afde772615b240dd09ada3fc96e46619b77f9c0ce3dcebc67878939006ac2017f7951358d09ca1c6da63501b8d00dd72703e593b4a0ca924d59360d4968abaf6361999a21943618ce0850f8b5f8dd51c54c772ffccb35ff63208a1d0025012000070107005847f80d",
"0x710284001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c01d0e951a27297d1418d17fcbadfc19441d2d276f358965a94ff87f1b1db287c4cae203b65dab926627c80deae720bca1335d771389843b1bf69b9e85139922b819503000000070000001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0a000000000000000000000000000000",
// Balances transferKeepAlive(dest, value)
"0x45028400f371affe2fd3ebd6f0a607e983bc8f905cea0ca2668667a229e112b30c5f9bbd0096f3ac43319d2e980db0aaf2b7e5e985055ea87f7b2a91a2ee09a934aef55bad4ec0dfd93e7010bc5dbf290403051ec40d475534df90ce3e1dd8a5b1ab3f7a0e4502cd0e000503001c3ef60163945e2b4602f7c6b27fc1f8e79d7c3430e3518cf63dc8a6ee84e9740700325f9d08"
"0x4d0284001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c01e6da8673127ec665dbfb76322ffe12721caa9b747abc5b6ec8ab73ff34f07831ed9edc49d185483a0c538a120149b7b6367bcde7088873408cc49ec280976c8ba5000000000403001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0f0080c6a47e8d03"
};

[Test]
Expand Down
5 changes: 3 additions & 2 deletions Substrate.NetApi.Test/Extrinsic/PayloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Types.Base;
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.Test.Extrinsic
{
Expand Down Expand Up @@ -52,14 +53,14 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

var assetTxPayment = new ChargeAssetTxPayment(0, 0);
var assetTxPayment = new ChargeAssetTxPayment(0, new BaseOpt<EnumNativeOrWithId>());

var signedExtensions = new SignedExtensions(259, 1, genesis, startEra, era, 0, assetTxPayment);

var payload = new Payload(method, signedExtensions);

var payloadBytes = Utils.StringValueArrayBytesArray(
"6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1, 58, 6, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");
"6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1, 58, 6, 0, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");

Assert.AreEqual(payloadBytes, payload.Encode());
}
Expand Down
9 changes: 5 additions & 4 deletions Substrate.NetApi.Test/Extrinsic/SignedExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Types.Base;
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.Test.Extrinsic
{
Expand Down Expand Up @@ -36,15 +37,15 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

var assetTxPayment = new ChargeAssetTxPayment(0, 0);
var assetTxPayment = new ChargeAssetTxPayment(0, new BaseOpt<EnumNativeOrWithId>());

var signedExtensions = new SignedExtensions(259, 1, genesis, startEra, era, 0, assetTxPayment);

var bytes = Utils.StringValueArrayBytesArray(
"58, 6, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");
"58, 6, 0, 0, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");

Assert.AreEqual(bytes.AsMemory().Slice(0, 5).ToArray(), signedExtensions.GetExtra());
Assert.AreEqual(bytes.AsMemory().Slice(5, 72).ToArray(), signedExtensions.GetAdditionalSigned());
Assert.AreEqual(bytes.AsMemory().Slice(0, 6).ToArray(), signedExtensions.GetExtra());
Assert.AreEqual(bytes.AsMemory().Slice(6, 73).ToArray(), signedExtensions.GetAdditionalSigned());
Assert.AreEqual(bytes, signedExtensions.Encode());
}
}
Expand Down
94 changes: 94 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/EnumRustTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections.Generic;

namespace Substrate.NetApi.Tests
{
public enum PhaseState
{
None = 0,
Finalization = 1,
Initialization = 2
}

[TestFixture]
public class BaseEnumRustTests
{
[Test]
public void ExtEnumEncodingTest()
{
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

var extEnumType = new BaseEnumRust<PhaseState>(typeDecoderMap);

int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);

Assert.AreEqual(PhaseState.None, extEnumType.Value);
Assert.AreEqual("U8", extEnumType.Value2.GetType().Name);
Assert.AreEqual(1, (extEnumType.Value2 as U8).Value);
}

[Test]
public void ExtEnumDencodingTest()
{
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

var extEnumType = new BaseEnumRust<PhaseState>(typeDecoderMap);

int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);

Assert.AreEqual(PhaseState.None, extEnumType.Value);
Assert.AreEqual("U8", extEnumType.Value2.GetType().Name);
Assert.AreEqual(1, (extEnumType.Value2 as U8).Value);

Assert.AreEqual(new byte[] { 0x00, 0x01 }, extEnumType.Bytes);
}

[Test]
public void ExtEnumCreateTest()
{
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

var u8 = new U8(1);
var byValue = new BaseEnumRust<PhaseState>(typeDecoderMap);
byValue.Create(PhaseState.None, u8);

var byArray = new BaseEnumRust<PhaseState>();
byArray.AddTypeDecoder<U8>(PhaseState.None);
byArray.AddTypeDecoder<BaseVoid>(PhaseState.Finalization);
byArray.AddTypeDecoder<BaseVoid>(PhaseState.Initialization);
byArray.Create(new byte[] { 0, 1 });

var byHex = new BaseEnumRust<PhaseState>();
byHex.AddTypeDecoder<U8>(PhaseState.None);
byHex.AddTypeDecoder<BaseVoid>(PhaseState.Finalization);
byHex.AddTypeDecoder<BaseVoid>(PhaseState.Initialization);
byHex.Create("0x0001");

Assert.That(byValue.Bytes, Is.EqualTo(byArray.Bytes));
Assert.That(byValue.Value, Is.EqualTo(byArray.Value));

Assert.That(byValue.Bytes, Is.EqualTo(byHex.Bytes));
Assert.That(byValue.Value, Is.EqualTo(byHex.Value));
}
}
}
97 changes: 97 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/TupleRustTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections.Generic;

namespace Substrate.NetApi.Tests
{
[TestFixture]
public class BaseTupleTests
{
[Test]
public void BaseTupleTest()
{
var u16Param = new U16(42);

var t1 = new BaseTupleRust(typeof(U16));
t1.Create("0x2a00");

Assert.AreEqual(1, t1.Value.Length);
Assert.AreEqual(t1.Bytes, new BaseTupleRust(new U16(42)).Bytes);
Assert.AreEqual(((U16)t1.Value[0]).Value, ((U16)new BaseTupleRust(new U16(42)).Value[0]).Value);
Assert.AreEqual(t1.TypeSize, new BaseTupleRust(new U16(42)).TypeSize);

var t2 = new BaseTupleRust(typeof(U16), typeof(U16));
t2.Create("0x2a002a00");
Assert.AreEqual(2, t2.Value.Length);
Assert.AreEqual(t2.Bytes, new BaseTupleRust(new U16(42), new U16(42)).Bytes);
Assert.AreEqual(((U16)t2.Value[0]).Value, ((U16)new BaseTupleRust(new U16(42), new U16(42)).Value[0]).Value);
Assert.AreEqual(((U16)t2.Value[1]).Value, ((U16)new BaseTupleRust(new U16(42), new U16(42)).Value[1]).Value);
Assert.AreEqual(t2.TypeSize, new BaseTupleRust(new U16(42), new U16(42)).TypeSize);

var t3 = new BaseTupleRust(typeof(U16), typeof(U16), typeof(U16));
t3.Create("0x2a002a002a00");
Assert.AreEqual(3, t3.Value.Length);
Assert.AreEqual(t3.Bytes, new BaseTupleRust(new U16(42), new U16(42), new U16(42)).Bytes);
Assert.AreEqual(((U16)t3.Value[0]).Value, ((U16)new BaseTupleRust(new U16(42), new U16(42), new U16(42)).Value[0]).Value);
Assert.AreEqual(((U16)t3.Value[1]).Value, ((U16)new BaseTupleRust(new U16(42), new U16(42), new U16(42)).Value[1]).Value);
Assert.AreEqual(((U16)t3.Value[2]).Value, ((U16)new BaseTupleRust(new U16(42), new U16(42), new U16(42)).Value[2]).Value);
Assert.AreEqual(t3.TypeSize, new BaseTupleRust(new U16(42), new U16(42), new U16(42)).TypeSize);
}

[Test]
public void BaseTupleCreateTest()
{
var u16 = new U16();
u16.Create("0x2a00");

var u32 = new U32();
u32.Create("0xffffff00");

var tupleOfTwo_1 = new BaseTupleRust(u16, u32);
Assert.AreEqual("0x2A00FFFFFF00", Utils.Bytes2HexString(tupleOfTwo_1.Encode()));
Assert.AreEqual(tupleOfTwo_1.Value.Length, new BaseTupleRust(u16, u32).Value.Length);
Assert.AreEqual(tupleOfTwo_1.Bytes, new BaseTupleRust(u16, u32).Bytes);
Assert.AreEqual(((U16)tupleOfTwo_1.Value[0]).Value, ((U16)new BaseTupleRust(u16, u32).Value[0]).Value);
Assert.AreEqual(((U32)tupleOfTwo_1.Value[1]).Value, ((U32)new BaseTupleRust(u16, u32).Value[1]).Value);

var tupleOfTwo_2 = new BaseTupleRust(typeof(U16), typeof(U32));
tupleOfTwo_2.Create("0x2A00FFFFFF00");

Assert.AreEqual(u16.Value, ((U16)tupleOfTwo_2.Value[0]).Value);
Assert.AreEqual(u32.Value, ((U32)tupleOfTwo_2.Value[1]).Value);

Assert.AreEqual(tupleOfTwo_1.Value.Length, tupleOfTwo_2.Value.Length);
Assert.AreEqual(tupleOfTwo_1.Bytes, tupleOfTwo_2.Bytes);
Assert.AreEqual(((U16)tupleOfTwo_1.Value[0]).Value, ((U16)tupleOfTwo_2.Value[0]).Value);
Assert.AreEqual(((U32)tupleOfTwo_1.Value[1]).Value, ((U32)tupleOfTwo_2.Value[1]).Value);
}

[Test]
public void BaseTypeEqualityWithNullBytes()
{
var baseTuple_1 = new BaseTupleRust(typeof(U32), typeof(U32));
var baseTuple_2 = new BaseTupleRust(typeof(U32), typeof(U32));
Assert.That(baseTuple_1.Bytes, Is.Null);
Assert.That(baseTuple_1.Value, Is.Null);
Assert.AreEqual(baseTuple_1.Bytes, baseTuple_2.Bytes);
Assert.AreEqual(baseTuple_1.Value, baseTuple_2.Value);

var baseOpt_1 = new BaseOpt<U32>();
var baseOpt_2 = new BaseOpt<U32>();
Assert.That(baseOpt_1.Bytes, Is.Null);
Assert.That(baseOpt_1.Value, Is.Null);
Assert.AreEqual(baseOpt_1.Bytes, baseOpt_2.Bytes);
Assert.AreEqual(baseOpt_1.Value, baseOpt_2.Value);

var baseVec_1 = new BaseVec<U32>();
var baseVec_2 = new BaseVec<U32>();
Assert.That(baseVec_1.Bytes, Is.Null);
Assert.That(baseVec_1.Value, Is.Null);
Assert.AreEqual(baseVec_1.Bytes, baseVec_2.Bytes);
Assert.AreEqual(baseVec_1.Value, baseVec_2.Value);
}
}

}
4 changes: 2 additions & 2 deletions Substrate.NetApi.Test/TypeConverters/TypeEncodingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public enum PhaseState
[Test]
public void ExtEnumEncodingTest()
{
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid>();
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid>();

int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);
Expand All @@ -125,7 +125,7 @@ public void ExtEnumEncodingTest()
[Test]
public void ExtEnumDencodingTest()
{
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid>();
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid>();

int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);
Expand Down
12 changes: 6 additions & 6 deletions Substrate.NetApi.TestNode/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task GetSystemChainTestAsync()
{
var result = await _substrateClient.System.ChainAsync(CancellationToken.None);

Assert.AreEqual("Bajun Kusama", result);
Assert.AreEqual("Polkadot", result);
}

[Test]
Expand Down Expand Up @@ -80,9 +80,9 @@ public async Task GetSystemPropertiesTestAsync()
{
var result = await _substrateClient.System.PropertiesAsync(CancellationToken.None);

Assert.AreEqual(1337, result.Ss58Format);
Assert.AreEqual(12, result.TokenDecimals);
Assert.AreEqual("BAJU", result.TokenSymbol);
Assert.AreEqual(0, result.Ss58Format);
Assert.AreEqual(10, result.TokenDecimals);
Assert.AreEqual("DOT", result.TokenSymbol);
}

[Test]
Expand All @@ -93,7 +93,7 @@ public async Task GetBlockNumberTestAsync()

var result = await _substrateClient.Chain.GetBlockHashAsync(blockNumber, CancellationToken.None);

Assert.AreEqual("0x35A06BFEC2EDF0FF4BE89A6428CCD9FF5BD0167D618C5A0D4341F9600A458D14", result.Value);
Assert.AreEqual("0x91B171BB158E2D3848FA23A9F1C25182FB8E20313B2C1EB49219DA7A70CE90C3", result.Value);
}

[Test]
Expand All @@ -110,7 +110,7 @@ public async Task GetAccountInfoTestAsync()
var result = await _substrateClient.GetStorageAsync<Arr32U8>(parameters, CancellationToken.None);

Assert.IsNotNull(result);
Assert.AreEqual("0x35A06BFEC2EDF0FF4BE89A6428CCD9FF5BD0167D618C5A0D4341F9600A458D14", Utils.Bytes2HexString(result.Value.Select(p => p.Value).ToArray()));
Assert.AreEqual("0x91B171BB158E2D3848FA23A9F1C25182FB8E20313B2C1EB49219DA7A70CE90C3", Utils.Bytes2HexString(result.Value.Select(p => p.Value).ToArray()));

blockNumber.Create(999999999);

Expand Down
Loading

0 comments on commit ca3e321

Please sign in to comment.