Skip to content

Commit e2ee8b1

Browse files
committed
refactor unit tests
1 parent 95285f9 commit e2ee8b1

File tree

9 files changed

+85
-87
lines changed

9 files changed

+85
-87
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
namespace Nethermind.Core.Test.Builders;
5+
6+
public partial class Build
7+
{
8+
public XdcBlockHeaderBuilder XdcBlockHeader => new();
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
using System;
4+
using Nethermind.Xdc;
5+
using Nethermind.Core.Crypto;
6+
using Nethermind.Int256;
7+
8+
namespace Nethermind.Core.Test.Builders;
9+
10+
public class XdcBlockHeaderBuilder : BuilderBase<XdcBlockHeader>
11+
{
12+
public XdcBlockHeaderBuilder()
13+
{
14+
TestObjectInternal = new XdcBlockHeader(
15+
Keccak.Compute("parent"),
16+
Keccak.OfAnEmptySequenceRlp,
17+
Address.Zero,
18+
UInt256.One,
19+
1,
20+
30_000_000,
21+
1_700_000_000,
22+
new byte[] { 1, 2, 3 })
23+
{
24+
StateRoot = Keccak.EmptyTreeHash,
25+
TxRoot = Keccak.EmptyTreeHash,
26+
ReceiptsRoot = Keccak.EmptyTreeHash,
27+
Bloom = Bloom.Empty,
28+
GasUsed = 21_000,
29+
MixHash = Keccak.Compute("mix_hash"),
30+
Nonce = 1000,
31+
Validators = new byte[20 * 2],
32+
Validator = new byte[20],
33+
Penalties = Array.Empty<byte>(),
34+
};
35+
}
36+
37+
public XdcBlockHeaderBuilder WithBaseFee(UInt256 baseFee)
38+
{
39+
TestObjectInternal.BaseFeePerGas = baseFee;
40+
return this;
41+
}
42+
}

src/Nethermind/Nethermind.Core.Test/Nethermind.Core.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
33
<Import Project="../tests.props" />
44

55
<PropertyGroup>
@@ -17,6 +17,7 @@
1717
<ProjectReference Include="..\Nethermind.Facade\Nethermind.Facade.csproj" />
1818
<ProjectReference Include="..\Nethermind.Merge.Plugin\Nethermind.Merge.Plugin.csproj" />
1919
<ProjectReference Include="..\Nethermind.Specs.Test\Nethermind.Specs.Test.csproj" />
20+
<ProjectReference Include="..\Nethermind.Xdc\Nethermind.Xdc.csproj" />
2021
</ItemGroup>
2122
<ItemGroup>
2223
<Content Include="TestRopstenBlocks\*.*">

src/Nethermind/Nethermind.Serialization.Rlp/BlockDecoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace Nethermind.Serialization.Rlp
99
{
1010
public class BlockDecoder : IRlpValueDecoder<Block>, IRlpStreamDecoder<Block>
1111
{
12-
private readonly IHeaderRlpCodec _headerDecoder;
12+
private readonly IHeaderDecoder _headerDecoder;
1313
private readonly BlockBodyDecoder _blockBodyDecoder = BlockBodyDecoder.Instance;
1414

1515
public BlockDecoder() : this(new HeaderDecoder()) { }
1616

17-
public BlockDecoder(IHeaderRlpCodec headerCodec)
17+
public BlockDecoder(IHeaderDecoder headerCodec)
1818
{
1919
_headerDecoder = headerCodec ?? throw new ArgumentNullException(nameof(headerCodec));
2020
}

src/Nethermind/Nethermind.Serialization.Rlp/HeaderDecoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace Nethermind.Serialization.Rlp
1010
{
11-
public interface IHeaderRlpCodec : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<BlockHeader> { }
11+
public interface IHeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<BlockHeader> { }
1212

13-
public class HeaderDecoder : IHeaderRlpCodec
13+
public class HeaderDecoder : IHeaderDecoder
1414
{
1515
public const int NonceLength = 8;
1616

src/Nethermind/Nethermind.Xdc.Test/Nethermind.Xdc.Test.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="NSubstitute" />
12+
<PackageReference Include="FluentAssertions" />
1213
</ItemGroup>
1314

14-
<ItemGroup>
15-
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
16-
<ProjectReference Include="..\Nethermind.Serialization.Rlp\Nethermind.Serialization.Rlp.csproj" />
17-
<ProjectReference Include="..\Nethermind.Xdc\Nethermind.Xdc.csproj" />
18-
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\Nethermind.Core.Test\Nethermind.Core.Test.csproj" />
17+
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
18+
<ProjectReference Include="..\Nethermind.Serialization.Rlp\Nethermind.Serialization.Rlp.csproj" />
19+
<ProjectReference Include="..\Nethermind.Xdc\Nethermind.Xdc.csproj" />
20+
</ItemGroup>
1921

2022
</Project>

src/Nethermind/Nethermind.Xdc.Test/XdcHeaderDecoderTests.cs

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,51 @@
1-
using System;
1+
using FluentAssertions;
22
using NUnit.Framework;
33
using Nethermind.Core;
4-
using Nethermind.Core.Crypto;
54
using Nethermind.Int256;
65
using Nethermind.Serialization.Rlp;
76

7+
using Nethermind.Core.Test.Builders;
8+
89
namespace Nethermind.Xdc.Test
910
{
1011
[TestFixture]
1112
public class XdcHeaderRlpCodecTests
1213
{
13-
private static XdcBlockHeader MakeHeader(bool includeBaseFee = true)
14+
private static (XdcBlockHeader Header, byte[] Bytes) BuildHeaderAndDefaultEncode(XdcHeaderDecoder codec, bool includeBaseFee = true)
1415
{
15-
var parent = new Hash256(new byte[32]);
16-
var uncles = new Hash256(new byte[32]);
17-
var coinbase = new Address(new byte[20]);
18-
UInt256 diff = UInt256.One;
19-
long number = 1;
20-
long gasLim = 30_000_000;
21-
ulong ts = 1_700_000_000;
22-
var extra = Array.Empty<byte>();
23-
24-
var header = new XdcBlockHeader(parent, uncles, coinbase, in diff, number, gasLim, ts, extra)
25-
{
26-
StateRoot = new Hash256(new byte[32]),
27-
TxRoot = new Hash256(new byte[32]),
28-
ReceiptsRoot = new Hash256(new byte[32]),
29-
Bloom = new Bloom(new byte[256]),
30-
GasUsed = 21_000,
31-
MixHash = new Hash256(new byte[32]),
32-
Nonce = 0UL,
33-
Validators = new byte[20 * 2],
34-
Validator = new byte[20],
35-
Penalties = Array.Empty<byte>(),
36-
};
37-
38-
if (includeBaseFee)
39-
{
40-
header.BaseFeePerGas = (UInt256)1_000_000_000; // 1 gwei
41-
}
16+
XdcBlockHeaderBuilder builder = Build.A.XdcBlockHeader;
17+
XdcBlockHeader header = (includeBaseFee ? builder.WithBaseFee((UInt256)1_000_000_000) : builder).TestObject;
4218

43-
return header;
19+
Rlp encoded = codec.Encode(header);
20+
return (header, encoded.Bytes);
4421
}
4522

4623
[Test]
4724
public void EncodeDecode_RoundTrip_Matches_AllFields()
4825
{
4926
var codec = new XdcHeaderDecoder();
50-
XdcBlockHeader original = MakeHeader();
51-
52-
// Encode
53-
Rlp r = codec.Encode(original);
54-
byte[] bytes = r.Bytes;
27+
var (original, encodedBytes) = BuildHeaderAndDefaultEncode(codec);
5528

5629
// Decode
57-
var stream = new RlpStream(bytes);
30+
var stream = new RlpStream(encodedBytes);
5831
BlockHeader? decodedBase = codec.Decode(stream);
5932
Assert.That(decodedBase, Is.Not.Null, "The decoded header should not be null.");
6033
Assert.That(decodedBase, Is.InstanceOf<XdcBlockHeader>(), "The decoded header should be an instance of XdcBlockHeader.");
6134

6235
var decoded = (XdcBlockHeader)decodedBase!;
6336

64-
// Spot-check key fields
65-
Assert.That(original.ParentHash, Is.EqualTo(decoded.ParentHash), "The parent hash should be the same.");
66-
Assert.That(original.UnclesHash, Is.EqualTo(decoded.UnclesHash), "The uncles hash should be the same.");
67-
Assert.That(original.Beneficiary, Is.EqualTo(decoded.Beneficiary), "The beneficiary should be the same.");
68-
Assert.That(original.StateRoot, Is.EqualTo(decoded.StateRoot), "The state root should be the same.");
69-
Assert.That(original.TxRoot, Is.EqualTo(decoded.TxRoot), "The tx root should be the same.");
70-
Assert.That(original.ReceiptsRoot, Is.EqualTo(decoded.ReceiptsRoot), "The receipts root should be the same.");
71-
Assert.That(original.Bloom, Is.EqualTo(decoded.Bloom), "The bloom should be the same.");
72-
Assert.That(original.Difficulty, Is.EqualTo(decoded.Difficulty), "The difficulty should be the same.");
73-
Assert.That(original.Number, Is.EqualTo(decoded.Number), "The number should be the same.");
74-
Assert.That(original.GasLimit, Is.EqualTo(decoded.GasLimit), "The gas limit should be the same.");
75-
Assert.That(original.GasUsed, Is.EqualTo(decoded.GasUsed), "The gas used should be the same.");
76-
Assert.That(original.Timestamp, Is.EqualTo(decoded.Timestamp), "The timestamp should be the same.");
77-
Assert.That(original.ExtraData, Is.EqualTo(decoded.ExtraData), "The extra data should be the same.");
78-
Assert.That(original.MixHash, Is.EqualTo(decoded.MixHash), "The mix hash should be the same.");
79-
Assert.That(original.Nonce, Is.EqualTo(decoded.Nonce), "The nonce should be the same.");
80-
Assert.That(decoded.Validators, Is.EqualTo(original.Validators), "Validators should match.");
81-
Assert.That(original.Validator, Is.EqualTo(decoded.Validator), "Validator should match.");
82-
Assert.That(original.Penalties, Is.EqualTo(decoded.Penalties), "Penalties should match.");
83-
Assert.That(original.BaseFeePerGas, Is.EqualTo(decoded.BaseFeePerGas), "BaseFeePerGas should be the same.");
37+
// Hash is excluded since decoder sets it from RLP, but original is often not set
38+
decoded.Should().BeEquivalentTo(original, options => options.Excluding(h => h.Hash));
8439
}
8540

8641
[Test]
8742
public void No_BaseFee()
8843
{
8944
var codec = new XdcHeaderDecoder();
90-
XdcBlockHeader header = MakeHeader(includeBaseFee: false);
91-
92-
// Encode without base fee
93-
Rlp r = codec.Encode(header);
94-
byte[] bytes = r.Bytes;
45+
var (original, encodedBytes) = BuildHeaderAndDefaultEncode(codec, false);
9546

9647
// Decode back
97-
var stream = new RlpStream(bytes);
48+
var stream = new RlpStream(encodedBytes);
9849
var decoded = (XdcBlockHeader)codec.Decode(stream)!;
9950

10051
Assert.That(decoded.BaseFeePerGas.IsZero, "BaseFeePerGas should be zero when omitted.");
@@ -104,26 +55,19 @@ public void No_BaseFee()
10455
public void TotalLength_Equals_GetLength()
10556
{
10657
var codec = new XdcHeaderDecoder();
107-
XdcBlockHeader header = MakeHeader();
108-
109-
// encode
110-
Rlp encoded = codec.Encode(header);
111-
byte[] bytes = encoded.Bytes;
58+
var (header, encodedBytes) = BuildHeaderAndDefaultEncode(codec);
11259

11360
// compare to GetLength
11461
int expectedTotal = codec.GetLength(header, RlpBehaviors.None);
115-
Assert.That(bytes.Length, Is.EqualTo(expectedTotal), "Encoded total length should match GetLength().");
62+
Assert.That(encodedBytes.Length, Is.EqualTo(expectedTotal), "Encoded total length should match GetLength().");
11663
}
11764

11865
[Test]
11966
public void Encode_ForSealing_Omits_MixHash_And_Nonce()
12067
{
12168
var codec = new XdcHeaderDecoder();
122-
XdcBlockHeader header = MakeHeader(includeBaseFee: true);
123-
124-
// Full encoding
125-
Rlp full = codec.Encode(header, RlpBehaviors.None);
126-
int fullLen = full.Bytes.Length;
69+
var (header, encodedBytes) = BuildHeaderAndDefaultEncode(codec);
70+
int fullLen = encodedBytes.Length;
12771

12872
// ForSealing encoding
12973
Rlp sealing = codec.Encode(header, RlpBehaviors.ForSealing);

src/Nethermind/Nethermind.Xdc/XdcHeaderDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Nethermind.Int256;
88
using Nethermind.Serialization.Rlp;
99
namespace Nethermind.Xdc;
10-
public sealed class XdcHeaderDecoder : IHeaderRlpCodec
10+
public sealed class XdcHeaderDecoder : IHeaderDecoder
1111
{
1212
private const int NonceLength = 8;
1313

src/Nethermind/Nethermind.slnx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<Project Path="Nethermind.Trie.Test/Nethermind.Trie.Test.csproj" />
8080
<Project Path="Nethermind.TxPool.Test/Nethermind.TxPool.Test.csproj" />
8181
<Project Path="Nethermind.Wallet.Test/Nethermind.Wallet.Test.csproj" />
82-
<Project Path="Nethermind.Xdc.Test\Nethermind.Xdc.Test.csproj" Type="Classic C#" />
82+
<Project Path="Nethermind.Xdc.Test\Nethermind.Xdc.Test.csproj" />
8383
</Folder>
8484
<Project Path="Nethermind.Abi/Nethermind.Abi.csproj" />
8585
<Project Path="Nethermind.Api/Nethermind.Api.csproj" />
@@ -131,4 +131,4 @@
131131
<Project Path="Nethermind.TxPool/Nethermind.TxPool.csproj" />
132132
<Project Path="Nethermind.Wallet/Nethermind.Wallet.csproj" />
133133
<Project Path="Nethermind.Xdc/Nethermind.Xdc.csproj" />
134-
</Solution>
134+
</Solution>

0 commit comments

Comments
 (0)