1
- using System ;
1
+ using FluentAssertions ;
2
2
using NUnit . Framework ;
3
3
using Nethermind . Core ;
4
- using Nethermind . Core . Crypto ;
5
4
using Nethermind . Int256 ;
6
5
using Nethermind . Serialization . Rlp ;
7
6
7
+ using Nethermind . Core . Test . Builders ;
8
+
8
9
namespace Nethermind . Xdc . Test
9
10
{
10
11
[ TestFixture ]
11
12
public class XdcHeaderRlpCodecTests
12
13
{
13
- private static XdcBlockHeader MakeHeader ( bool includeBaseFee = true )
14
+ private static ( XdcBlockHeader Header , byte [ ] Bytes ) BuildHeaderAndDefaultEncode ( XdcHeaderDecoder codec , bool includeBaseFee = true )
14
15
{
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 ;
42
18
43
- return header ;
19
+ Rlp encoded = codec . Encode ( header ) ;
20
+ return ( header , encoded . Bytes ) ;
44
21
}
45
22
46
23
[ Test ]
47
24
public void EncodeDecode_RoundTrip_Matches_AllFields ( )
48
25
{
49
26
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 ) ;
55
28
56
29
// Decode
57
- var stream = new RlpStream ( bytes ) ;
30
+ var stream = new RlpStream ( encodedBytes ) ;
58
31
BlockHeader ? decodedBase = codec . Decode ( stream ) ;
59
32
Assert . That ( decodedBase , Is . Not . Null , "The decoded header should not be null." ) ;
60
33
Assert . That ( decodedBase , Is . InstanceOf < XdcBlockHeader > ( ) , "The decoded header should be an instance of XdcBlockHeader." ) ;
61
34
62
35
var decoded = ( XdcBlockHeader ) decodedBase ! ;
63
36
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 ) ) ;
84
39
}
85
40
86
41
[ Test ]
87
42
public void No_BaseFee ( )
88
43
{
89
44
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 ) ;
95
46
96
47
// Decode back
97
- var stream = new RlpStream ( bytes ) ;
48
+ var stream = new RlpStream ( encodedBytes ) ;
98
49
var decoded = ( XdcBlockHeader ) codec . Decode ( stream ) ! ;
99
50
100
51
Assert . That ( decoded . BaseFeePerGas . IsZero , "BaseFeePerGas should be zero when omitted." ) ;
@@ -104,26 +55,19 @@ public void No_BaseFee()
104
55
public void TotalLength_Equals_GetLength ( )
105
56
{
106
57
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 ) ;
112
59
113
60
// compare to GetLength
114
61
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()." ) ;
116
63
}
117
64
118
65
[ Test ]
119
66
public void Encode_ForSealing_Omits_MixHash_And_Nonce ( )
120
67
{
121
68
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 ;
127
71
128
72
// ForSealing encoding
129
73
Rlp sealing = codec . Encode ( header , RlpBehaviors . ForSealing ) ;
0 commit comments