Skip to content

Commit

Permalink
refactoring extrinisc states (#63)
Browse files Browse the repository at this point in the history
* adding all states

* minor changes in test

* preparing next release

---------

Co-authored-by: darkfriend77 <[email protected]>
  • Loading branch information
darkfriend77 and darkfriend77 authored Oct 22, 2023
1 parent 01a63d2 commit b934e3e
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 142 deletions.
4 changes: 2 additions & 2 deletions Substrate.NetApi.Test/Substrate.NetApi.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Schnorrkel" Version="1.1.3" />
</ItemGroup>

Expand Down
32 changes: 0 additions & 32 deletions Substrate.NetApi.TestNode/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,38 +145,6 @@ public async Task GetBlocknumberAtBlockHashTestAsync()
Assert.AreEqual(currentBlocknumber.Value, result.Value);
}

/// <summary>
/// Simple extrinsic tester
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="extrinsicUpdate"></param>
private static void ActionExtrinsicUpdate(string subscriptionId, ExtrinsicStatus extrinsicUpdate)
{
switch (extrinsicUpdate.ExtrinsicState)
{
case ExtrinsicState.None:
Assert.IsTrue(true);
Assert.IsTrue(extrinsicUpdate.InBlock.Value.Length > 0 || extrinsicUpdate.Finalized.Value.Length > 0);
break;

case ExtrinsicState.Future:
Assert.IsTrue(false);
break;

case ExtrinsicState.Ready:
Assert.IsTrue(true);
break;

case ExtrinsicState.Dropped:
Assert.IsTrue(false);
break;

case ExtrinsicState.Invalid:
Assert.IsTrue(false);
break;
}
}

/// <summary>
/// >> 1 - Array
/// </summary>
Expand Down
148 changes: 148 additions & 0 deletions Substrate.NetApi.TestNode/ExtrinsicsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Rpc;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using NUnit.Framework;
using Schnorrkel.Keys;

namespace Substrate.NetApi.TestNode
{
public class ExtrinsicsTest
{
public MiniSecret MiniSecretAlice => new MiniSecret(Utils.HexToByteArray("0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a"), ExpandMode.Ed25519);
public Account Alice => Account.Build(KeyType.Sr25519, MiniSecretAlice.ExpandToSecret().ToBytes(), MiniSecretAlice.GetPair().Public.Key);
public MiniSecret MiniSecretBob => new MiniSecret(Utils.HexToByteArray("0x398f0c28f98885e046333d4a41c19cee4c37368a9832c6502f6cfd182e2aef89"), ExpandMode.Ed25519);
public Account Bob => Account.Build(KeyType.Sr25519, MiniSecretBob.ExpandToSecret().ToBytes(), MiniSecretBob.GetPair().Public.Key);

protected const string WebSocketUrl = "ws://127.0.0.1:9944";

protected SubstrateClient _substrateClient;

protected ChargeType _chargeType;

[SetUp]
public async Task ConnectAsync()
{
await _substrateClient.ConnectAsync();
}

[TearDown]
public async Task CloseAsync()
{
await _substrateClient.CloseAsync();
}

[OneTimeSetUp]
public void CreateClient()
{
_chargeType = ChargeAssetTxPayment.Default();
_substrateClient = new SubstrateClient(new Uri(WebSocketUrl), _chargeType);
}

[OneTimeTearDown]
public void DisposeClient()
{
_substrateClient.Dispose();
}

/// <summary>
/// Extrinsic Remark test
/// </summary>
/// <returns></returns>
[Test]
public async Task Extrinsic_RemarkAsync()
{
var method = new Method(0, "System", 0, "remark", new byte[] { 0x04, 0xFF });

var taskCompletionSource = new TaskCompletionSource<bool>();
await _substrateClient.Author.SubmitAndWatchExtrinsicAsync((string subscriptionId, ExtrinsicStatus extrinsicUpdate) => Callback(subscriptionId, extrinsicUpdate, taskCompletionSource), method, Alice, _chargeType, 64, CancellationToken.None);

var finished = await Task.WhenAny(taskCompletionSource.Task, Task.Delay(TimeSpan.FromMinutes(1))); // 5 minutes or any appropriate timeout
Assert.AreEqual(taskCompletionSource.Task, finished, "Test timed out waiting for final callback");
}

/// <summary>
/// Extrinsic Transfer Callback test
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="extrinsicUpdate"></param>
/// <param name="taskCompletionSource"></param>
private static void Callback(string subscriptionId, ExtrinsicStatus extrinsicUpdate, TaskCompletionSource<bool> taskCompletionSource)
{
ActionExtrinsicUpdate(subscriptionId, extrinsicUpdate);
if (extrinsicUpdate.ExtrinsicState == ExtrinsicState.Finalized ||
extrinsicUpdate.ExtrinsicState == ExtrinsicState.Dropped ||
extrinsicUpdate.ExtrinsicState == ExtrinsicState.Invalid)
{
taskCompletionSource.SetResult(true);
}
}

/// <summary>
/// Simple extrinsic tester
/// </summary>
/// <param name="subscriptionId"></param>
/// <param name="extrinsicUpdate"></param>
private static void ActionExtrinsicUpdate(string subscriptionId, ExtrinsicStatus extrinsicUpdate)
{
if (subscriptionId == null || subscriptionId.Length == 0)
{
Assert.IsTrue(false);
}

switch (extrinsicUpdate.ExtrinsicState)
{
case ExtrinsicState.Future:
Assert.IsTrue(false);
break;

case ExtrinsicState.Ready:
Assert.IsTrue(true);
break;

case ExtrinsicState.Dropped:
Assert.IsTrue(false);
break;

case ExtrinsicState.Invalid:
Assert.IsTrue(false);
break;

case ExtrinsicState.Broadcast:
Assert.IsTrue(extrinsicUpdate.Broadcast != null);
break;

case ExtrinsicState.InBlock:
Assert.IsTrue(extrinsicUpdate.Hash.Value.Length > 0);
break;

case ExtrinsicState.Retracted:
Assert.IsTrue(extrinsicUpdate.Hash.Value.Length > 0);
break;

case ExtrinsicState.FinalityTimeout:
Assert.IsTrue(extrinsicUpdate.Hash.Value.Length > 0);
break;

case ExtrinsicState.Finalized:
Assert.IsTrue(extrinsicUpdate.Hash.Value.Length > 0);
break;

case ExtrinsicState.Usurped:
Assert.IsTrue(extrinsicUpdate.Hash.Value.Length > 0);
break;

default:
Assert.IsTrue(false);
break;

}
}
}
}
21 changes: 9 additions & 12 deletions Substrate.NetApi.TestNode/QueryAllTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using Substrate.NetApi.Model.Extrinsics;
using NUnit.Framework;
using NUnit.Framework;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using Substrate.NetApi.Model.Types;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Substrate.NetApi.TestNode
{
public class QueryAllTest : NodeTest
{

[Test]
public async Task GetAllStorageTestAsync()
{
Expand Down Expand Up @@ -43,10 +40,10 @@ public async Task GetAllStorageTestAsync()

Assert.IsNotNull(allPages);
Assert.AreNotEqual(0, allPages.Count);
Assert.Greater(10000, stopwatch.ElapsedMilliseconds, "Get all storage did use more then 10 sec., verify!");
Assert.Greater(20000, stopwatch.ElapsedMilliseconds, "Get all storage did use more then 10 sec., verify!");
}

public async Task<List<(byte[], T1, T2)>> GetAllStoragePagedAsync<T1, T2>(string module, string item, byte[] startKey, uint page, string blockHash, CancellationToken token)
public async Task<List<(byte[], T1, T2)>> GetAllStoragePagedAsync<T1, T2>(string module, string item, byte[] startKey, uint page, string blockHash, CancellationToken token)
where T1 : IType, new()
where T2 : IType, new()
{
Expand Down Expand Up @@ -250,4 +247,4 @@ public void Create(U8[] array)
Bytes = Encode();
}
}
}
}
6 changes: 3 additions & 3 deletions Substrate.NetApi.TestNode/Substrate.NetApi.TestNode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NetApi/Model/Extrinsics/Extrinsic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal Extrinsic(Memory<byte> memory, ChargeType chargeType)
// nonce
Nonce = CompactInteger.Decode(memory.ToArray(), ref p);

// chargeAssetTxPayment
// charge type
Charge = chargeType;
Charge.Decode(memory.ToArray(), ref p);
}
Expand Down
12 changes: 6 additions & 6 deletions Substrate.NetApi/Model/Rpc/ExtrinsicStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Substrate.NetApi.Model.Rpc
{
public enum ExtrinsicState
{
None, Future, Ready, Dropped, Invalid
Future, Ready, Broadcast, InBlock, Retracted, FinalityTimeout, Finalized, Usurped, Dropped, Invalid
}

public class ExtrinsicStatus
{
public ExtrinsicState ExtrinsicState { get; set; }

public string[] Broadcast { get; set; }
public Hash InBlock { get; set; }
public Hash Retracted { get; set; }
public Hash FinalityTimeout { get; set; }
public Hash Finalized { get; set; }
public Hash Usurped { get; set; }

public Hash Hash { get; set; }

//public ulong? TxIndex { get; set; }

public override string ToString()
{
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NetApi/Substrate.NetApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>Substrate.NET.API</PackageId>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Version>0.9.9</Version>
<Version>0.9.10</Version>
<Company>Substrate Gaming</Company>
<Authors>Substrate Gaming</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
12 changes: 4 additions & 8 deletions Substrate.NetApi/TypeConverters/ExtrinsicJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using Newtonsoft.Json;
using Substrate.NetApi.Model.Extrinsics;
using Newtonsoft.Json;
using Serilog;
using System;

namespace Substrate.NetApi.TypeConverters
{
/// <summary>
/// <br />
/// Extrinsic Json Converter
/// </summary>
internal class ExtrinsicJsonConverter : JsonConverter<Extrinsic>
{
/// <summary> The logger. </summary>
private static readonly ILogger Logger = new LoggerConfiguration().CreateLogger();

private ChargeType _chargeType;
private readonly ChargeType _chargeType;

public ExtrinsicJsonConverter(ChargeType chargeType)
{
Expand Down
Loading

0 comments on commit b934e3e

Please sign in to comment.