Skip to content

Commit

Permalink
Merge pull request #5 from SubstrateGaming/feature/v15_metadata
Browse files Browse the repository at this point in the history
Update Metadata V15
  • Loading branch information
darkfriend77 authored Jul 31, 2024
2 parents 03415c4 + 5f866c4 commit 82bb926
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 20 deletions.
25 changes: 25 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV15Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Substrate.NET.Metadata.Exceptions;
using Substrate.NET.Metadata.Service;
using Substrate.NET.Metadata.V14;
using Substrate.NET.Metadata.V15;

namespace Substrate.NET.Metadata.Tests
{
Expand All @@ -14,5 +15,29 @@ public void Setup()
{
_metadataService = new MetadataService();
}

[Test]
public void MetadataV15_WhenInstanciate_ShouldSucceed()
{
var metadataHex = readMetadataFromFile("V15\\MetadataV15_FromPolkadotJs");

Assert.That(_metadataService.GetMetadataVersion(metadataHex), Is.EqualTo(MetadataVersion.V15));

var res = new MetadataV15(metadataHex);

Assert.That(res, Is.Not.Null);
Assert.That(res.Version, Is.EqualTo(MetadataVersion.V15));

Assert.That(res.RuntimeMetadataData.Lookup.Value.Length, Is.GreaterThan(0));
Assert.That(res.RuntimeMetadataData.Modules.Value.Length, Is.GreaterThan(0));

Assert.That((int)res.RuntimeMetadataData.OuterEnums.CallType.Value, Is.GreaterThanOrEqualTo(0));
Assert.That((int)res.RuntimeMetadataData.OuterEnums.ErrorType.Value, Is.GreaterThanOrEqualTo(0));
Assert.That((int)res.RuntimeMetadataData.OuterEnums.EventType.Value, Is.GreaterThanOrEqualTo(0));

Assert.That(res.RuntimeMetadataData.Apis.Value.Length, Is.GreaterThan(0));

Assert.That(res.RuntimeMetadataData.Custom.Map.Value, Is.Not.Null);
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
<None Update="Mocks\V14\MetadataV14_9430.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Mocks\V15\MetadataV15_Test.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Mocks\V15\MetadataV15_FromPolkadotJs.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Mocks\V9\MetadataV9_Kusama_1022.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
4 changes: 2 additions & 2 deletions Substrate.NET.Metadata/Base/StorageEntryTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public override void Decode(byte[] byteArray, ref int p)
Hasher = new BaseEnum<THasher>();
Hasher.Decode(byteArray, ref p);

var Key = new Str();
Key = new Str();
Key.Decode(byteArray, ref p);

var Value = new Str();
Value = new Str();
Value.Decode(byteArray, ref p);

Linked = new Bool();
Expand Down
61 changes: 61 additions & 0 deletions Substrate.NET.Metadata/V15/CustomMetadata15.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.VisualBasic;
using Newtonsoft.Json.Linq;
using Substrate.NET.Metadata.Base;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NET.Metadata.V15
{
public class CustomMetadata15 : BaseType
{
public BaseVec<BaseTuple<Str, CustomValueMetadata15>> Map { get; private set; } = default!;

public override void Decode(byte[] byteArray, ref int p)
{
var start = p;
Map = new BaseVec<BaseTuple<Str, CustomValueMetadata15>>();

Map.Decode(byteArray, ref p);
var bytesLength = p - start;
TypeSize = bytesLength;
Bytes = new byte[bytesLength];
Array.Copy(byteArray, start, Bytes, 0, bytesLength);
}

public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(Map.Encode());
return result.ToArray();
}
}

public class CustomValueMetadata15 : BaseType
{
public TType CustomType { get; private set; } = default!;
public BaseVec<U8> Value { get; private set; } = default!;

public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(CustomType.Encode());
result.AddRange(Value.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
CustomType = new TType();
CustomType.Decode(byteArray, ref p);

Value = new BaseVec<U8>();
Value.Decode(byteArray, ref p);
}
}
}
59 changes: 59 additions & 0 deletions Substrate.NET.Metadata/V15/ExtrinsicMetadataV15.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Substrate.NET.Metadata.Base;
using Substrate.NET.Metadata.V14;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NET.Metadata.V15
{
public class ExtrinsicMetadataV15 : BaseType
{
public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(Version.Encode());
result.AddRange(AddressType.Encode());
result.AddRange(CallType.Encode());
result.AddRange(SignatureType.Encode());
result.AddRange(ExtraType.Encode());
result.AddRange(SignedExtensions.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
var start = p;

Version = new U8();
Version.Decode(byteArray, ref p);

AddressType = new TType();
AddressType.Decode(byteArray, ref p);

CallType = new TType();
CallType.Decode(byteArray, ref p);

SignatureType = new TType();
SignatureType.Decode(byteArray, ref p);

ExtraType = new TType();
ExtraType.Decode(byteArray, ref p);

SignedExtensions = new BaseVec<SignedExtensionMetadataV14>();
SignedExtensions.Decode(byteArray, ref p);

TypeSize = p - start;
}

public U8 Version { get; private set; } = default!;
public TType AddressType { get; private set; } = default!;
public TType CallType { get; private set; } = default!;
public TType SignatureType { get; private set; } = default!;
public TType ExtraType { get; private set; } = default!;
public BaseVec<SignedExtensionMetadataV14> SignedExtensions { get; private set; } = default!;
}
}
44 changes: 44 additions & 0 deletions Substrate.NET.Metadata/V15/OuterEnums15.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Newtonsoft.Json.Linq;
using Substrate.NET.Metadata.Base;
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.V15
{
public class OuterEnums15 : BaseType
{
public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(CallType.Encode());
result.AddRange(EventType.Encode());
result.AddRange(ErrorType.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
CallType = new TType();
CallType.Decode(byteArray, ref p);

EventType = new TType();
EventType.Decode(byteArray, ref p);

ErrorType = new TType();
ErrorType.Decode(byteArray, ref p);
}

public override string ToString()
{
return $"CallType: {CallType}, EventType: {EventType}, ErrorType: {ErrorType}";
}

public TType CallType { get; private set; } = default!;
public TType EventType { get; private set; } = default!;
public TType ErrorType { get; private set; } = default!;
}
}
13 changes: 0 additions & 13 deletions Substrate.NET.Metadata/V15/PalletMetadataV15.cs

This file was deleted.

20 changes: 15 additions & 5 deletions Substrate.NET.Metadata/V15/RuntimeMetadataV15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ public override void Decode(byte[] byteArray, ref int p)
Apis = new BaseVec<RuntimeApiMetadataV15>();
Apis.Decode(byteArray, ref p);

OuterEnums = new OuterEnums15();
OuterEnums.Decode(byteArray, ref p);

Custom = new CustomMetadata15();
Custom.Decode(byteArray, ref p);

TypeSize = p - start;
}

public PortableRegistry Lookup { get; private set; }
public PortableRegistry Lookup { get; private set; } = default!;

public BaseVec<ModuleMetadataV15> Modules { get; private set; } = default!;

public ExtrinsicMetadataV15 Extrinsic { get; private set; } = default!;

public BaseVec<ModuleMetadataV15> Modules { get; private set; }
public BaseVec<RuntimeApiMetadataV15> Apis { get; private set; } = default!;

public ExtrinsicMetadataV15 Extrinsic { get; private set; }
public TType TypeId { get; private set; } = default!;

public BaseVec<RuntimeApiMetadataV15> Apis { get; private set; }
public OuterEnums15 OuterEnums { get; private set; } = default!;

public TType TypeId { get; private set; }
public CustomMetadata15 Custom { get; private set; } = default!;
}
}

0 comments on commit 82bb926

Please sign in to comment.