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

Add pallet changed between two specVersion #2

Merged
merged 2 commits into from
Aug 1, 2023
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
26 changes: 26 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV10Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,31 @@ public void MetadataV10_SpecVersionCompare_V1032_And_V1039_ShouldSucceed()

Assert.That(res, Is.Not.Null);
}

[Test]
public void MetadataV10_SpecVersionCompare_V1039_And_V1040_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V10\\MetadataV10_Kusama_1039");
var metadataDestination = readMetadataFromFile("V10\\MetadataV10_Kusama_1040");

Assert.That(_metadataService.EnsureMetadataVersion(metadataSource, metadataDestination), Is.EqualTo(MetadataVersion.V10));

var res = _metadataService.MetadataCompareV10(
new MetadataV10(metadataSource),
new MetadataV10(metadataDestination));

Assert.That(res, Is.Not.Null);
Assert.That(res.AddedModules.First(x => x.ModuleName == "Society"), Is.Not.Null);
}

[Test]
public void MetadataV10_V1039_And_V1040_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V10\\MetadataV10_Kusama_1039");
var metadataDestination = readMetadataFromFile("V10\\MetadataV10_Kusama_1040");

Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Society", metadataSource, metadataDestination));
Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Balances", metadataSource, metadataDestination));
}
}
}
12 changes: 12 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV11Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,17 @@ public void MetadataV11_SpecVersionCompare_V23_And_V24_ShouldSucceed()
else
Assert.Fail();
}

[Test]
public void MetadataV11_V23_And_V24_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V11\\MetadataV11_23");
var metadataDestination = readMetadataFromFile("V11\\MetadataV11_24");

Assert.IsTrue(_metadataService.HasPalletChangedVersionBetween("Multisig", metadataSource, metadataDestination));

// Purschase has been removed, but does not count as changed
Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Purchase", metadataSource, metadataDestination));
}
}
}
11 changes: 11 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV12Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,16 @@ public void MetadataV12_SpecVersionCompare_V27_And_V28_ShouldSucceed()
Assert.That(electionPhragmenErrors[3].Item2.Name.Value, Is.EqualTo("InvalidCandidateCount"));
Assert.IsTrue(electionPhragmenModule.HasErrorRemoved("InvalidCandidateCount"));
}

[Test]
public void MetadataV12_V27_And_V28_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V12\\MetadataV12_27");
var metadataDestination = readMetadataFromFile("V12\\MetadataV12_28");

Assert.IsTrue(_metadataService.HasPalletChangedVersionBetween("ElectionsPhragmen", metadataSource, metadataDestination));

Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Balances", metadataSource, metadataDestination));
}
}
}
10 changes: 10 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV13Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@ public void MetadataV13_SpecVersionCompare_V9080_And_V9090_ShouldSucceed()
Assert.IsFalse(changedModules[2].HasErrorAdded("test"));
Assert.IsFalse(changedModules[2].HasErrorRemoved("test2"));
}

[Test]
public void MetadataV13_V9080_And_V9090_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V13\\MetadataV13_9080");
var metadataDestination = readMetadataFromFile("V13\\MetadataV13_9090");

Assert.IsTrue(_metadataService.HasPalletChangedVersionBetween("Balances", metadataSource, metadataDestination));
Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Babe", metadataSource, metadataDestination));
}
}
}
11 changes: 11 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV14Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,16 @@ public void MetadataV14_SpecVersionCompare_V9270_And_V9280_ShouldSucceed()
Assert.That(res.AddedModules.Count, Is.EqualTo(1));
Assert.That(res.AddedModules.First().ModuleName, Is.EqualTo("NominationPools"));
}

[Test]
public void MetadataV14_V9270_And_V9280_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V14\\MetadataV14_9270");
var metadataDestination = readMetadataFromFile("V14\\MetadataV14_9280");

Assert.IsTrue(_metadataService.HasPalletChangedVersionBetween("Auctions", metadataSource, metadataDestination));

Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("AuthorityDiscovery", metadataSource, metadataDestination));
}
}
}
18 changes: 18 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV15Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Substrate.NET.Metadata.Base;
using Substrate.NET.Metadata.Exceptions;
using Substrate.NET.Metadata.Service;
using Substrate.NET.Metadata.V14;

namespace Substrate.NET.Metadata.Tests
{
public class MetadataServiceV15Test : MetadataBaseTest
{
private MetadataService _metadataService;

[SetUp]
public void Setup()
{
_metadataService = new MetadataService();
}
}
}
10 changes: 10 additions & 0 deletions Substrate.NET.Metadata.Tests/MetadataServiceV9Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,15 @@ public void MetadataV9_SpecVersionCompare_V1020_And_V1022_ShouldSucceed()

Assert.That(res, Is.Not.Null);
}

[Test]
public void MetadataV9_V1020_And_V1022_PalletHasChanged_ShouldSucceed()
{
var metadataSource = readMetadataFromFile("V9\\MetadataV9_Kusama_1020");
var metadataDestination = readMetadataFromFile("V9\\MetadataV9_Kusama_1022");

Assert.IsTrue(_metadataService.HasPalletChangedVersionBetween("Democracy", metadataSource, metadataDestination));
Assert.IsFalse(_metadataService.HasPalletChangedVersionBetween("Balances", metadataSource, metadataDestination));
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Mocks\V10\MetadataV10_Kusama_1040.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Mocks\V10\MetadataV10_Kusama_1039.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
15 changes: 15 additions & 0 deletions Substrate.NET.Metadata/Compare/Base/IMetadataDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ namespace Substrate.NET.Metadata.Compare.Base
public interface IMetadataDiffBase<out T>
where T : IMetadataDifferentialModules
{
/// <summary>
/// List all pallets
/// </summary>
public IEnumerable<T> AllModulesDiff { get; }

/// <summary>
/// List all unchanged pallets
/// </summary>
public IEnumerable<T> UnchangedModules { get; }

/// <summary>
/// List all pallets which have modification. But do not track 'TypeId' modifications
/// </summary>
public IEnumerable<T> ChangedModules { get; }

/// <summary>
/// List added pallets
/// </summary>
public IEnumerable<T> AddedModules { get; }

/// <summary>
/// List removed pallets
/// </summary>
public IEnumerable<T> RemovedModules { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Substrate.NET.Metadata.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,5 +10,6 @@ namespace Substrate.NET.Metadata.Compare.Base
public interface IMetadataDifferentialModules
{
public string ModuleName { get; }
public CompareStatus CompareStatus { get; }
}
}
3 changes: 1 addition & 2 deletions Substrate.NET.Metadata/Compare/LookupDifferential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class LookupDifferential

public bool HasChanges()
{
return Id.Count > 0 ||
Path.Count > 0 ||
return Path.Count > 0 ||
Params.Count > 0 ||
LookupDifferentialType.HasChanges() ||
Docs.Count > 0;
Expand Down
9 changes: 9 additions & 0 deletions Substrate.NET.Metadata/Service/IMetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public interface IMetadataService
/// <returns></returns>
MetadataVersion EnsureMetadataVersion(string hexMetadata1, string hexMetadata2);

/// <summary>
/// Return true if pallet version has been found and has been modified
/// </summary>
/// <param name="palletName"></param>
/// <param name="hexMetadata1"></param>
/// <param name="hexMetadata2"></param>
/// <returns></returns>
bool HasPalletChangedVersionBetween(string palletName, string hexMetadata1, string hexMetadata2);

/// <summary>
/// Compare V9 metadata
/// </summary>
Expand Down
32 changes: 21 additions & 11 deletions Substrate.NET.Metadata/Service/MetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Substrate.NET.Metadata.Base;
using Substrate.NET.Metadata.Base.Portable;
using Substrate.NET.Metadata.Compare;
using Substrate.NET.Metadata.Compare.Base;
using Substrate.NET.Metadata.Compare.TypeDef;
using Substrate.NET.Metadata.Exceptions;
using Substrate.NET.Metadata.V10;
Expand Down Expand Up @@ -61,6 +62,26 @@ public MetadataVersion EnsureMetadataVersion(string hexMetadata1, string hexMeta
return v1;
}

public bool HasPalletChangedVersionBetween(string palletName, string hexMetadata1, string hexMetadata2)
{
Guard.Against.NullOrEmpty(palletName);

var version = EnsureMetadataVersion(hexMetadata1, hexMetadata2);

IMetadataDiffBase<IMetadataDifferentialModules> res = version switch
{
MetadataVersion.V9 => MetadataCompareV9(new MetadataV9(hexMetadata1), new MetadataV9(hexMetadata2)),
MetadataVersion.V10 => MetadataCompareV10(new MetadataV10(hexMetadata1), new MetadataV10(hexMetadata2)),
MetadataVersion.V11 => MetadataCompareV11(new MetadataV11(hexMetadata1), new MetadataV11(hexMetadata2)),
MetadataVersion.V12 => MetadataCompareV12(new MetadataV12(hexMetadata1), new MetadataV12(hexMetadata2)),
MetadataVersion.V13 => MetadataCompareV13(new MetadataV13(hexMetadata1), new MetadataV13(hexMetadata2)),
MetadataVersion.V14 => MetadataCompareV14(new MetadataV14(hexMetadata1), new MetadataV14(hexMetadata2)),
_ => throw new MetadataException($"Comparison for version {version} is not supported")
};

return res.ChangedModules.Any(x => x.ModuleName == palletName);
}

#region Metadata compare
/// <summary>
/// Compare element by name
Expand Down Expand Up @@ -171,17 +192,6 @@ private bool AreStringsEquals(IEnumerable<Str> source, IEnumerable<Str> target)
return !source.Select(x => x.Value).Except(target.Select(x => x.Value)).Any();
}

private IEnumerable<T> Sanitize<T>(IEnumerable<BaseVec<T>> elems)
where T : IType, new()
{
if (elems == null) return Enumerable.Empty<T>();

var res = elems
.SelectMany(x => x.Value);

return res;
}

private IEnumerable<T> Sanitize<T>(BaseOpt<BaseVec<T>> elems)
where T : IType, new()
{
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NET.Metadata/Substrate.NET.Metadata.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<Version>0.1.2</Version>
<Version>0.1.3</Version>
<Description>Substrate metadata version from V9 to V15 with .NET</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
Loading