-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(net): implement block hash calculation (#625)
- Loading branch information
1 parent
6fe755a
commit 9d3e18d
Showing
7 changed files
with
191 additions
and
10 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
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 |
---|---|---|
@@ -1,19 +1,23 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageVersion Include="Ensure.That" Version="10.1.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" /> | ||
<PackageVersion Include="PolySharp" Version="1.14.1" /> | ||
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.9" /> | ||
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.9" /> | ||
<PackageVersion Include="Substrate.NET.API" Version="0.9.24-rc6" /> | ||
<PackageVersion Include="System.Linq.Async" Version="6.0.1" /> | ||
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" /> | ||
<!-- For Tests --> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.1" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageVersion Include="xunit" Version="2.9.2" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
<!-- For Code Analysis --> | ||
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.9" /> | ||
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.9" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
65 changes: 65 additions & 0 deletions
65
net/src/Substrate.Gear.Client/Model/Rpc/HeaderExtensions.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,65 @@ | ||
using System; | ||
using System.Linq; | ||
using EnsureThat; | ||
using Substrate.Gear.Client.Model.Types.Base; | ||
using Substrate.NetApi; | ||
using Substrate.NetApi.Model.Rpc; | ||
using Substrate.NetApi.Model.Types.Base; | ||
|
||
namespace Substrate.Gear.Client.Model.Rpc; | ||
|
||
public static class HeaderExtensions | ||
{ | ||
public static Hash GetBlockHash(this Header header) | ||
{ | ||
EnsureArg.IsNotNull(header, nameof(header)); | ||
|
||
var parentHashBytes = header.ParentHash.AsBytesSpan(); | ||
var numberBytes = new CompactInteger(header.Number).Encode(); | ||
var stateRootBytes = header.StateRoot.AsBytesSpan(); | ||
var extrinsicsRootBytes = header.ExtrinsicsRoot.AsBytesSpan(); | ||
var logsCountBytes = new CompactInteger(header.Digest.Logs.Count).Encode(); | ||
var logsBytesLength = 0; | ||
var logsBytes = header.Digest.Logs | ||
.Select(log => | ||
{ | ||
var logBytes = Utils.HexToByteArray(log); | ||
logsBytesLength += logBytes.Length; | ||
return logBytes; | ||
}) | ||
.ToArray(); | ||
|
||
var bytesToHash = new byte[ | ||
parentHashBytes.Length | ||
+ numberBytes.Length | ||
+ stateRootBytes.Length | ||
+ extrinsicsRootBytes.Length | ||
+ logsCountBytes.Length | ||
+ logsBytesLength]; | ||
|
||
var copyAt = 0; | ||
|
||
parentHashBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += parentHashBytes.Length; | ||
|
||
numberBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += numberBytes.Length; | ||
|
||
stateRootBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += stateRootBytes.Length; | ||
|
||
extrinsicsRootBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += extrinsicsRootBytes.Length; | ||
|
||
logsCountBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += logsCountBytes.Length; | ||
|
||
foreach (var logBytes in logsBytes) | ||
{ | ||
logBytes.CopyTo(bytesToHash.AsSpan(copyAt)); | ||
copyAt += logBytes.Length; | ||
} | ||
|
||
return new Hash(HashExtension.Blake2(bytesToHash, 256)); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
net/tests/Substrate.Gear.Client.Tests/Model/Rpc/HeaderExtensionsTests.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,64 @@ | ||
using FluentAssertions; | ||
using Substrate.Gear.Client.Model.Rpc; | ||
using Substrate.Gear.Client.Model.Types.Base; | ||
using Substrate.NetApi.Model.Rpc; | ||
using Substrate.NetApi.Model.Types.Base; | ||
using Substrate.NetApi.Model.Types.Primitive; | ||
using Xunit; | ||
|
||
namespace Substrate.Gear.Client.Tests.Model.Rpc; | ||
|
||
public sealed class HeaderExtensionsTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(Block_Hash_Is_Calculated_Correctly), MemberType = typeof(TheoryData))] | ||
public void Block_Hash_Is_Calculated_Correctly((string ExpectedBlockHash, Header BlockHeader) data) | ||
{ | ||
var blockHash = data.BlockHeader.GetBlockHash(); | ||
var expectedBlockHash = new Hash(data.ExpectedBlockHash); | ||
|
||
blockHash.Should().Match<Hash>((hash) => hash.IsEqualTo(expectedBlockHash)); | ||
} | ||
|
||
private static class TheoryData | ||
{ | ||
// Taken from Vara Network | ||
public static readonly TheoryData<(string ExpectedBlockHash, Header BlockHeader)> Block_Hash_Is_Calculated_Correctly = | ||
new( | ||
( | ||
ExpectedBlockHash: "0x0A7CA5B5B7A7B4C186D9B9355B5864DB74DD71DEAB700DEDA11BC852EA710E4A", | ||
new Header | ||
{ | ||
ExtrinsicsRoot = new Hash("0xd5c7e252243071f25d8013aa60e87e1650b4f069983eeafcecec10c0a03619ae"), | ||
Number = (U64)16940619, | ||
ParentHash = new Hash("0x9060eda7f8699cfcfa69c26640473bf5a322274eb391d066ec758c8f990c1eab"), | ||
StateRoot = new Hash("0x7db88586b63b1ef556c13bec7a42040ebacacb2a1a8ad8517824674acf4f0106"), | ||
Digest = new Digest | ||
{ | ||
Logs = [ | ||
"0x0642414245340213000000ef9e612200000000", | ||
"0x054241424501015832548cc0447f21a474529d904a7681a0e2e67baf90ef6d9f208b8e5dd4a22940e5e5b99c4e8a62e6f3a0b543883b07503cdcb40fe2c0c40a24df45dcd44f8e" | ||
] | ||
} | ||
} | ||
), | ||
( | ||
ExpectedBlockHash: "0x79F23DDD3EF8B1F7A9BA7ABED94574B779B73CE4E9D955657ABF53C38551F8BF", | ||
new Header | ||
{ | ||
ExtrinsicsRoot = new Hash("0x246c1d21868a9f8476e74d06a7b2cc0077703862b1cbeca546e25de9f203a1c6"), | ||
Number = (U64)16940905, | ||
ParentHash = new Hash("0x302955194b49e227fa40b111718a3449b763323e778aa89ca96ae9eb8ded3f64"), | ||
StateRoot = new Hash("0xadac86b1731b2ebfb50a0c75f13ccfc884e21cab010cf511d2149836a61e9d6f"), | ||
Digest = new Digest | ||
{ | ||
Logs = [ | ||
"0x0642414245b501013100000011a0612200000000e4653ae16d22ee4766b802cae76c58273d793e59c70ddb173c2a63b452ec4820435f5d32fc12fcdbba1bd2b6a3f3349fd0356bbb003bbc3da1009cc5879c0d0d88baee2605129bd5cfb0e050df2c38aeaa927c548028a352dca52b954f375607", | ||
"0x05424142450101e8dd5abf4f1b1f600f1257110b51bdfdf6b7bf5462c618c0b22ad39e979eb32b25e70cbc7ca4d1d229f25b839c0e01c94270e7332d6b7b73bf9a86bc09262b8f" | ||
] | ||
} | ||
} | ||
) | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
net/tests/Substrate.Gear.Client.Tests/Substrate.Gear.Client.Tests.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="FluentAssertions" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Substrate.Gear.Client\Substrate.Gear.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |