-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SubstrateGaming/fix/custom-nodes
Add support for extrinsics + account storage from genesis hash
- Loading branch information
Showing
32 changed files
with
653 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
Substrate.NET.Metadata.Node.Tests/Storage/AccountInfoTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Substrate.NetApi; | ||
using Substrate.NetApi.Model.Extrinsics; | ||
using Substrate.NetApi.Model.Types.Base; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static System.Runtime.InteropServices.JavaScript.JSType; | ||
|
||
namespace Substrate.NET.Metadata.Node.Tests.Storage | ||
{ | ||
internal class AccountInfoTests | ||
{ | ||
|
||
|
||
private SubstrateClient _substrateClient; | ||
|
||
[OneTimeSetUp] | ||
public async Task SetupAsync() | ||
{ | ||
//_substrateClient = new SubstrateClient(new Uri("wss://polkadot-rpc.dwellir.com"), ChargeTransactionPayment.Default()); | ||
_substrateClient = new SubstrateClient(new Uri("wss://polkadot.api.onfinality.io/public-ws"), ChargeTransactionPayment.Default()); | ||
|
||
await _substrateClient.ConnectAsync(); | ||
Assert.That(_substrateClient.IsConnected, Is.True); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void Teardown() | ||
{ | ||
_substrateClient.Dispose(); | ||
} | ||
|
||
/// <summary> | ||
/// Blockchain : Polkadot | ||
/// Fetch Account Info from block 1 | ||
/// </summary> | ||
/// <returns></returns> | ||
[Test] | ||
public async Task AccountInfo_MetadataV11_ShouldBeCorrectlyInstanciatedAsync() | ||
{ | ||
var accountId32 = new AccountId32(); | ||
accountId32.Create(Utils.GetPublicKeyFrom("12H7nsDUrJUSCQQJrTKAFfyCWSactiSdjoVUixqcd9CZHTGt")); | ||
|
||
var parameters = RequestGenerator.GetStorage("System", "Account", Substrate.NetApi.Model.Meta.Storage.Type.Map, [Substrate.NetApi.Model.Meta.Storage.Hasher.BlakeTwo128Concat], [accountId32]); | ||
|
||
var result = await _substrateClient.GetStorageAsync<AccountInfoOldVersion>(parameters, "0xC0096358534EC8D21D01D34B836EED476A1C343F8724FA2153DC0725AD797A90", CancellationToken.None); | ||
|
||
Assert.That(result.Data, Is.Not.Null); | ||
} | ||
} | ||
|
||
public sealed class AccountInfoOldVersion : BaseType | ||
{ | ||
// https://docs.rs/frame-system/2.0.0/frame_system/struct.AccountInfo.html | ||
public Substrate.NetApi.Model.Types.Primitive.U32 Nonce { get; set; } = default!; | ||
public Substrate.NetApi.Model.Types.Primitive.U8 RefCount { get; set; } = default!; | ||
public AccountDataOldVersion Data { get; set; } = default!; | ||
|
||
public override System.String TypeName() | ||
{ | ||
return "AccountInfo"; | ||
} | ||
|
||
public override System.Byte[] Encode() | ||
{ | ||
var result = new List<byte>(); | ||
result.AddRange(Nonce.Encode()); | ||
//result.AddRange(Consumers.Encode()); | ||
//result.AddRange(Providers.Encode()); | ||
//result.AddRange(Sufficients.Encode()); | ||
result.AddRange(RefCount.Encode()); | ||
result.AddRange(Data.Encode()); | ||
return result.ToArray(); | ||
} | ||
|
||
public override void Decode(byte[] byteArray, ref int p) | ||
{ | ||
var start = p; | ||
Nonce = new Substrate.NetApi.Model.Types.Primitive.U32(); | ||
Nonce.Decode(byteArray, ref p); | ||
RefCount = new Substrate.NetApi.Model.Types.Primitive.U8(); | ||
RefCount.Decode(byteArray, ref p); | ||
Data = new AccountDataOldVersion(); | ||
Data.Decode(byteArray, ref p); | ||
var bytesLength = p - start; | ||
TypeSize = bytesLength; | ||
Bytes = new byte[bytesLength]; | ||
System.Array.Copy(byteArray, start, Bytes, 0, bytesLength); | ||
} | ||
} | ||
|
||
public sealed class AccountDataOldVersion : BaseType | ||
{ | ||
// https://docs.rs/pallet-balances/2.0.1/pallet_balances/struct.Instance0.html | ||
|
||
public Substrate.NetApi.Model.Types.Primitive.U128 Free { get; set; } = default!; | ||
public Substrate.NetApi.Model.Types.Primitive.U128 Reserved { get; set; } = default!; | ||
public Substrate.NetApi.Model.Types.Primitive.U128 MiscFrozen { get; set; } = default!; | ||
public Substrate.NetApi.Model.Types.Primitive.U128 FeeFrozen { get; set; } = default!; | ||
|
||
public override System.String TypeName() | ||
{ | ||
return "AccountData"; | ||
} | ||
|
||
public override System.Byte[] Encode() | ||
{ | ||
var result = new List<byte>(); | ||
result.AddRange(Free.Encode()); | ||
result.AddRange(Reserved.Encode()); | ||
result.AddRange(MiscFrozen.Encode()); | ||
result.AddRange(FeeFrozen.Encode()); | ||
return result.ToArray(); | ||
} | ||
|
||
public override void Decode(byte[] byteArray, ref int p) | ||
{ | ||
var start = p; | ||
Free = new Substrate.NetApi.Model.Types.Primitive.U128(); | ||
Free.Decode(byteArray, ref p); | ||
Reserved = new Substrate.NetApi.Model.Types.Primitive.U128(); | ||
Reserved.Decode(byteArray, ref p); | ||
MiscFrozen = new Substrate.NetApi.Model.Types.Primitive.U128(); | ||
MiscFrozen.Decode(byteArray, ref p); | ||
FeeFrozen = new Substrate.NetApi.Model.Types.Primitive.U128(); | ||
FeeFrozen.Decode(byteArray, ref p); | ||
var bytesLength = p - start; | ||
TypeSize = bytesLength; | ||
Bytes = new byte[bytesLength]; | ||
System.Array.Copy(byteArray, start, Bytes, 0, bytesLength); | ||
} | ||
} | ||
|
||
public sealed class AccountId32 : BaseType | ||
{ | ||
public Substrate.NetApi.Model.Types.Primitive.U8[] Value { get; set; } = default!; | ||
|
||
public override System.String TypeName() | ||
{ | ||
return "AccountId32"; | ||
} | ||
|
||
public override int TypeSize | ||
{ | ||
get | ||
{ | ||
return 32; | ||
} | ||
} | ||
|
||
public override System.Byte[] Encode() | ||
{ | ||
var result = new List<byte>(); | ||
foreach (var v in Value) | ||
{ | ||
result.AddRange(v.Encode()); | ||
} | ||
|
||
return result.ToArray(); | ||
} | ||
|
||
public override void Decode(byte[] byteArray, ref int p) | ||
{ | ||
var start = p; | ||
var array = new Substrate.NetApi.Model.Types.Primitive.U8[TypeSize]; | ||
for (var i = 0; i < array.Length; i++) | ||
{ | ||
var t = new Substrate.NetApi.Model.Types.Primitive.U8(); | ||
t.Decode(byteArray, ref p); | ||
array[i] = t; | ||
} | ||
|
||
var bytesLength = p - start; | ||
Bytes = new byte[bytesLength]; | ||
System.Array.Copy(byteArray, start, Bytes, 0, bytesLength); | ||
Value = array; | ||
} | ||
|
||
public void Create(Substrate.NetApi.Model.Types.Primitive.U8[] array) | ||
{ | ||
Value = array; | ||
Bytes = Encode(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
Substrate.NET.Metadata.Tests/Conversion/CustomNodesBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Substrate.NET.Metadata.Base; | ||
using Substrate.NET.Metadata.Base.Portable; | ||
using Substrate.NET.Metadata.Conversion.Internal; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Substrate.NET.Metadata.Tests.Conversion | ||
{ | ||
internal class CustomNodesBuilderTests | ||
{ | ||
[Test] | ||
public void CreateCustomComposite_ShouldReturnCompositeBuilder() | ||
{ | ||
var conversion = new ConversionBuilder(new List<PortableType>()); | ||
var customNodeBuilder = new CustomNodeBuilder(); | ||
|
||
var accountInfoBuilder = customNodeBuilder | ||
.CreateCustomComposite() | ||
.AddField("nonce", "Index") | ||
.AddField("refcount", "u8") | ||
.AddField("data", "AccountData") | ||
.WithPath("frame_system", "accountInfo") | ||
.Build(conversion); | ||
|
||
Assert.That(accountInfoBuilder, Is.Not.Null); | ||
} | ||
|
||
[Test] | ||
public void CreateCustomComposite_WhenCallByParentNode_ShouldReturnTheOverrideOne() | ||
{ | ||
var conversion = new ConversionBuilder(new List<PortableType>()); | ||
var customNodeBuilder = new CustomNodeBuilder(); | ||
|
||
var accountDataBuilder = customNodeBuilder | ||
.CreateCustomComposite() | ||
.AddField("free", "Balance") | ||
.AddField("notFree", "Balance") | ||
.WithPath("pallet_balance", "types", "AccountData"); | ||
|
||
customNodeBuilder.Build(conversion); | ||
|
||
var res = conversion.BuildPortableTypes("AccountInfo"); | ||
|
||
// We should get the AccountInfo from v14 but with our override of AccountData | ||
var accountInfo = conversion.PortableTypes.SingleOrDefault(x => x.Id.Value == res.Value); | ||
|
||
Assert.That(accountInfo, Is.Not.Null); | ||
|
||
var accountInfoComposite = accountInfo.Ty.TypeDef.Value2 as TypeDefComposite; | ||
|
||
var accountData = conversion.PortableTypes.SingleOrDefault(x => x.Id.Value == accountInfoComposite!.Fields.Value[^1].FieldTy.Value); | ||
var accountDataComposite = accountData!.Ty.TypeDef.Value2 as TypeDefComposite; | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(accountDataComposite!.Fields.Value[0].Name.Value.Value, Is.EqualTo("free")); | ||
Assert.That(accountDataComposite!.Fields.Value[1].Name.Value.Value, Is.EqualTo("notFree")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void CustomComposite_WithSpecifiedVersionRange() | ||
{ | ||
var customNodeBuilder = new CustomNodeBuilder(); | ||
|
||
var accountDataBuilder = customNodeBuilder | ||
.CreateCustomComposite() | ||
.FromVersion(10) | ||
.ToVersion(20); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(accountDataBuilder.IsVersionValid(10), Is.True); | ||
Assert.That(accountDataBuilder.IsVersionValid(20), Is.True); | ||
Assert.That(accountDataBuilder.IsVersionValid(15), Is.True); | ||
Assert.That(accountDataBuilder.IsVersionValid(9), Is.False); | ||
Assert.That(accountDataBuilder.IsVersionValid(30), Is.False); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void CustomComposite_WithNoVersionSpecified() | ||
{ | ||
var customNodeBuilder = new CustomNodeBuilder(); | ||
|
||
var accountDataBuilder = customNodeBuilder | ||
.CreateCustomComposite(); | ||
|
||
Assert.That(accountDataBuilder.IsVersionValid(10), Is.True); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.