Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old metadata conversion #6

Merged
merged 16 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Substrate.NET.Metadata.Node.Tests/ConvertToV14Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Substrate.NET.Metadata.Conversion;
using Substrate.NET.Metadata.V10;
using Substrate.NET.Metadata.V11;
using Substrate.NET.Metadata.V12;
using Substrate.NET.Metadata.V13;
using Substrate.NET.Metadata.V9;
using Substrate.NetApi;
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Meta;

namespace Substrate.NET.Metadata.Node.Tests
{
public class ConvertToV14Test
{
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();
}

[Test]
[TestCase("0xc0096358534ec8d21d01d34b836eed476a1c343f8724fa2153dc0725ad797a90", 11)]
[TestCase("0xa15bf122d7a9b2cd07956c3af8f7eda61298aff0a3bd1193018df413d982a4ef", 11)]
[TestCase("0x49a695416fcf55487adf9b9f5808619cee6618bd285a0119a78db60c5a7f3e13", 11)]
[TestCase("0xdab56553594fd489adc085d2f83f3dcb65f5de5b1878325d5547fcf72b6dd6b3", 11)]
[TestCase("0xaff11447ba47e853bcadb73920c0780013e581a0286831eadce0e48bf1db9942", 11)]
[TestCase("0x4eba9b7a60fb063fbd8b9ceb8028c1c7b5a3cc2ea7b0ad703885ce580dee327c", 11)]
[TestCase("0x773e8d8c1fdcbc94d79bb2cdcaf2f7fca5ff869d34c50171fe50ac1640f3e05c", 11)]
[TestCase("0x209d24943d81e5910bfb7a57c8a5c3037c4a434af7c486a536d4f96a2160c9f5", 11)]
[TestCase("0x3eef3df8dd7eedfe6baaed544dcef9c36ee7a142035bd6ae3fd7fd2969ad5933", 11)]
[TestCase("0x4f61e8e6017cce5a10e2de7340061a037895411c19e7bc27f607953d8a56a943", 11)]
[TestCase("0x7cf0e5e3671b02ca341ab439453e1a9ca6616c6e351bdfa8cbb6f5124a8224b8", 11)]
[TestCase("0x349399f6b6481070047626f184d0152699bd02cb71040ef045adefc4f8daec5b", 11)]
[TestCase("0xab6f40697e72d0a4ab0d105145b1ddc1bd8cead0bb1d6a13f4ac1a05508b1882", 11)]
[TestCase("0x78433cba37c0cbc777faad71f27e21094defe24451033dd4d9a0443a1dc291b2", 11)]
[TestCase("0x9a18fb9522d9688eadefb59bf7389fc0deaf219b080a462d6db5ee1d3ec79562", 11)]
[TestCase("0xe187cabcfb9d30b66fae7257f3820b947c89b65e6e20f6ead2af4fd78926825f", 11)]
[TestCase("0xd85988f4f7bc3594200fd5f8e738153260f2ba342b56d9bf4edabf9209c65ae0", 11)]
[TestCase("0x8a0f28f399d3a85643413435e5a001c4ade7b8195eaf7e91f399549f192a56c7", 11)]
[TestCase("0x0b40f111395a0871b1a59da4bd8587bcddf662e3e7b4ba06d8862a44dacf6256", 12)]
[TestCase("0x780cd3091ef9cdc3a94dbcdfa7908d6120f5e037a1e534b4f18249c37005a533", 12)]
[TestCase("0x26fa4c7e27aad01f5a8367aefece17f8c0940b75423628d52a8c07a203bd2458", 12)]
[TestCase("0xd00026d2bdca30cd20f7c361544e9651041107a1fd35ea447d8dfabb5001960f", 12)]
[TestCase("0x766a02e9abe37328ad0dee3a64aad557dd1bade9e9f27849a9cf5838c0e7e6bb", 12)]
[TestCase("0x8a8a7aa9ac863359b2cc609b836e3ebd7086d775c31f8c177f183ed9ffa71fa5", 12)]
[TestCase("0x89f4608b568a05a89737bc826a32f8677f2646a5773312d7dee3a667458e5a98", 13)]
[TestCase("0xddca674d4e6e4935b1e5e81ed697f7ce027c2a9602523424bcc8dee3184e4863", 13)]
[TestCase("0x4b34bd42835a084af0f441c5986d216b18a85abb4d03762096692e6f06365203", 13)]
[TestCase("0x8192df1609d4478d1c1fcf5d39975596a8162d42d745c141c22148b0931708f2", 13)]
public async Task ConvertMetadataToV14_ShouldSucceedAsync(string blockHash, int version)
{
var metadataHex = await _substrateClient.State.GetMetaDataAsync(Utils.HexToByteArray(blockHash), CancellationToken.None);

IMetadataToV14 metadata = version switch
{
9 => new MetadataV9(metadataHex),
10 => new MetadataV10(metadataHex),
11 => new MetadataV11(metadataHex),
12 => new MetadataV12(metadataHex),
13 => new MetadataV13(metadataHex),
_ => throw new Exception()
};

var v14 = metadata.ToMetadataV14();

Assert.That(v14, Is.Not.Null);

// Now let's convert to NetApi classes (ok this should be done in another UT)
var v14NetApi = v14.ToNetApiMetadata();

Assert.That(v14NetApi, Is.Not.Null);
}
}
}
1 change: 1 addition & 0 deletions Substrate.NET.Metadata.Node.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Substrate.NET.Metadata\Substrate.NET.Metadata.csproj" />
</ItemGroup>

</Project>
170 changes: 170 additions & 0 deletions Substrate.NET.Metadata.Tests/Conversion/ConversionBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using Microsoft.VisualBasic;
using Substrate.NET.Metadata.Base;
using Substrate.NET.Metadata.Base.Portable;
using Substrate.NET.Metadata.Conversion.Internal;
using Substrate.NetApi.Model.Types.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NET.Metadata.Conversion.Tests
{
internal class ConversionBuilderTest
{
private ConversionBuilder _builder;
#region Setup
[SetUp]
public void Setup() {
_builder = new ConversionBuilder(InitPortableRegistry().ToList());
}

private static PortableType[] InitPortableRegistry()
{
var portableRegistry = new PortableRegistry();
portableRegistry.Create(new List<PortableType>().ToArray());
var pt = portableRegistry.Value;
Assert.That(pt.Length, Is.EqualTo(0));
return pt;
}
#endregion

[Test]
[TestCase("u32")]
public void GetNode_FromPrimitive_ShouldSucceed(string primitiveType)
{
var res = _builder.GetPortableType(primitiveType);

Assert.That(res.portableType.Ty.TypeDef.Value, Is.EqualTo(TypeDefEnum.Primitive));
Assert.That(((BaseEnum<TypeDefPrimitive>)res.portableType.Ty.TypeDef.Value2).Value, Is.EqualTo(TypeDefPrimitive.U32));
}

[Test]
[TestCase("Vec<EventRecord<T::Event, T::Hash>>", "System")]
public void GetSystemEvent_WhenOverrided_ShouldReturnOverride(string rustType, string palletContext = "")
{
_builder.CurrentPallet = palletContext;
_builder.CreateEventBlockchainRuntimeEvent();
//_builder.AddEventRuntimeLookup(palletContext, new Variant[0]);
var res = _builder.BuildPortableTypes(rustType);

Assert.That(res.Value, Is.EqualTo(18), "Index are not equals");
}

[Test]
[TestCase("TaskAddress<BlockNumber>", 29, 2)]
[TestCase("SignedSubmissionOf<T>", 504, 61)]
[TestCase("BalanceOf<T>", 6, 1)]
[TestCase("SubmissionIndicesOf<T>", 502, 5)]
[TestCase("[BalanceOf<T>; 4]", 1000, 2)]
[TestCase("Approvals", 14, 2)]
[TestCase("(Vec<ProxyDefinition<T::AccountId, T::ProxyType, T::BlockNumber>>,\n BalanceOf<T>)", 1000, 9)]
[TestCase("&[u8]", 10, 2, "Claims")]
[TestCase("Vec<Vec<(ParaId, CollatorId)>>", 1000, 5)]
[TestCase("Vec<BalanceOf<T>>", 1000, 2)]
[TestCase("LeasePeriod", 4, 1)]
[TestCase("LockIdentifier", 125, 2)]
[TestCase("(T::BlockNumber, Hash)", 1000, 5)]
[TestCase("(BalanceOf<T>, Vec<T::AccountId>)", 1000, 6)]
[TestCase("AuthorityId", 46, 4, "ImOnline")]
[TestCase("AuthorityId", 137, 4, "Babe")]
[TestCase("Vec<(AuthorityId, BabeAuthorityWeight)>", 367, 7, "Babe")]
[TestCase("Vec<IdentificationTuple>", 48, 10)]
[TestCase("Vec<(T::ValidatorId, T::Keys)>", 411, 14)]
[TestCase("Vec<T::ValidatorId>", 55, 4)]
[TestCase("ReportIdOf<T>", 9, 3)]
[TestCase("(T::AccountId, slashing::SpanIndex)", 400, 5)]
[TestCase("slashing::SpanRecord<BalanceOf<T>>", 406, 2)]
[TestCase("(EraIndex, T::AccountId)", 396, 5)]
[TestCase("Exposure<T::AccountId, BalanceOf<T>>", 50, 8)]
[TestCase("Multiplier", 384, 2)]
[TestCase("MaybeRandomness", 370, 3)]
[TestCase("AccountInfo<T::Index, T::AccountData>", 3, 4)]
[TestCase("schnorrkel::Randomness", 1, 2)]
[TestCase("[u8;32]", 1, 2)]
[TestCase("Option<Vec<u8>>", 30, 3)]
[TestCase("Vec<(T::BlockNumber, EventIndex)>", 105, 3)]
[TestCase("Vec<(EraIndex, SessionIndex)>", 105, 3)]
[TestCase("u32", 4, 1)]
[TestCase("ExtrinsicsWeight", 7, 2)] // Should be bound with PerDispatchClass
[TestCase("DispatchInfo", 22, 4)]
[TestCase("Weight", 8, 1)]
[TestCase("T::BlockNumber", 4, 1)]
[TestCase("T::AccountId", 0, 3)]
[TestCase("Vec<AccountId>", 55, 4)]
//[TestCase("Vec<DeferredOffenceOf<T>>", 22, 4)]
public void GetIndex_FromRustClass_ShouldSucceed(string rustType, int indexExpected, int lengthExpected, string palletContext = "")
{
_builder.CurrentPallet = palletContext;
var res = _builder.BuildPortableTypes(rustType);

Assert.That(res.Value, Is.EqualTo(indexExpected), "Index are not equals");
Assert.That(_builder.PortableTypes.Count, Is.EqualTo(lengthExpected), "Portable elements are not equals");
}

[Test]
[TestCase("Vec<AccountId>")] // 61
[TestCase("Vec<(ParaId, T::BlockNumber)>")]
[TestCase("Vec<(AccountId, Balance)>")]
[TestCase("Vec<DeferredOffenceOf<T>>")]
public void Build_FromSequence_ShouldSucceed(string sequenceClass)
{
var node = new NodeBuilderTypeUndefined(sequenceClass, string.Empty);
var res = ConversionBuilderTree.Build(node);

Assert.That(res, Is.Not.Null);
Assert.That(res.TypeDef == TypeDefEnum.Sequence);
}

[Test]
[TestCase("AccountId, Balance", new string[] { "AccountId", "Balance" })]
[TestCase("Toto, Titi<A, B>", new string[] { "Toto", "Titi<A, B>" })]
[TestCase("Titi<A, B>, Toto", new string[] { "Titi<A, B>", "Toto" })]
[TestCase("Titi<A<XX, YY>, B>, Titi<A<WW, TT>, L>", new string[] { "Titi<A<XX, YY>, B>", "Titi<A<WW, TT>, L>" })]
public void ExtractParameters_ShouldSucceed(string input, string[] expected)
{
var res = ConversionBuilderTree.ExtractParameters(input);

Assert.That(res, Is.Not.Null);
Assert.That(res.Count, Is.EqualTo(expected.Length));

for (int i = 0; i < res.Count; i++)
{
Assert.That(res[i], Is.EqualTo(expected[i]));
}
}

[Test]
[TestCase("[u8; 4]", "u8", 4)]
public void ExtractArray_ShouldSucceed(string input, string typeExpected, int lengthExpected)
{
var res = ConversionBuilderTree.ExtractArray(new NodeBuilderTypeUndefined(input, ""));

Assert.That(res, Is.Not.Null);
Assert.That(res.Children[0].Adapted, Is.EqualTo(typeExpected));
Assert.That(res.Length, Is.EqualTo(lengthExpected));
}

[Test]
public void ExtractPrimitive_ShouldSucceed()
{
var res = ConversionBuilderTree.ExtractPrimitive(new NodeBuilderTypeUndefined("u32", ""));

Assert.That(res, Is.Not.Null);
Assert.That(res.Primitive, Is.EqualTo(TypeDefPrimitive.U32));
}

[Test]
[TestCase("Vec<u32>", typeof(NodeBuilderTypeSequence))]
[TestCase("Option<u32>", typeof(NodeBuilderTypeOption))]
[TestCase("AccountData<u32>", typeof(NodeBuilderTypeComposite))]
public void ExtractGeneric_ShouldSucceed(string input, Type typeDefExpected)
{
var res = ConversionBuilderTree.ExtractGeneric(new NodeBuilderTypeUndefined(input, ""));

Assert.That(res, Is.Not.Null);
Assert.That(res.GetType(), Is.EqualTo(typeDefExpected));
}
}
}
67 changes: 67 additions & 0 deletions Substrate.NET.Metadata.Tests/Conversion/ConversionUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 ConversionUtilsTests
{
[Test]
public void EndsWithArray_ShouldReturnTrue()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 4, 5 };

var result = ConversionUtils.EndsWithArray(array1, array2);

Assert.That(result);
}

[Test]
public void EndsWithArray_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 4, 6 };

var result = ConversionUtils.EndsWithArray(array1, array2);

Assert.That(result, Is.False);
}

[Test]
public void AreArraysIdentical_WhenEquals_ShouldReturnTrue()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4, 5 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result);
}

[Test]
public void AreArraysIdentical_WhenNotSameSize_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result, Is.False);
}

[Test]
public void AreArraysIdentical_WhenDifferent_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4, 6 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result, Is.False);
}
}
}
Loading
Loading