-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathBlockchainTestBase.cs
More file actions
552 lines (464 loc) · 23.3 KB
/
BlockchainTestBase.cs
File metadata and controls
552 lines (464 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Find;
using Nethermind.Config;
using Nethermind.Consensus;
using Nethermind.Consensus.Ethash;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Modules;
using Nethermind.Crypto;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Serialization.Rlp;
using Nethermind.Specs;
using Nethermind.Specs.Forks;
using Nethermind.Specs.Test;
using Nethermind.Evm.State;
using Nethermind.Init.Modules;
using NUnit.Framework;
using Nethermind.Merge.Plugin.Data;
using Nethermind.Merge.Plugin;
using Nethermind.JsonRpc;
using System.Reflection;
using Nethermind.State;
namespace Ethereum.Test.Base;
public abstract class BlockchainTestBase
{
private static readonly ILogger _logger;
private static readonly ILogManager _logManager = new TestLogManager(LogLevel.Warn);
private static DifficultyCalculatorWrapper DifficultyCalculator { get; }
private const int _genesisProcessingTimeoutMs = 30000;
static BlockchainTestBase()
{
DifficultyCalculator = new DifficultyCalculatorWrapper();
_logManager ??= LimboLogs.Instance;
_logger = _logManager.GetClassLogger();
}
[SetUp]
public void Setup()
{
}
private class DifficultyCalculatorWrapper : IDifficultyCalculator
{
public IDifficultyCalculator? Wrapped { get; set; }
public UInt256 Calculate(BlockHeader header, BlockHeader parent)
{
if (Wrapped is null)
{
throw new InvalidOperationException(
$"Cannot calculate difficulty before the {nameof(Wrapped)} calculator is set.");
}
return Wrapped.Calculate(header, parent);
}
}
protected async Task<EthereumTestResult> RunTest(BlockchainTest test, Stopwatch? stopwatch = null, bool failOnInvalidRlp = true, ITestBlockTracer? tracer = null)
{
_logger.Info($"Running {test.Name}, Network: [{test.Network!.Name}] at {DateTime.UtcNow:HH:mm:ss.ffffff}");
if (test.NetworkAfterTransition is not null)
_logger.Info($"Network after transition: [{test.NetworkAfterTransition.Name}] at {test.TransitionForkActivation}");
Assert.That(test.LoadFailure, Is.Null, "test data loading failure");
test.Network = ChainUtils.ResolveSpec(test.Network, test.ChainId);
test.NetworkAfterTransition = ChainUtils.ResolveSpec(test.NetworkAfterTransition, test.ChainId);
bool isEngineTest = test.Blocks is null && test.EngineNewPayloads is not null;
// Post-merge pyspec blockchain_test_from_state_test fixtures expect genesis to be processed
// under the target fork rules when the fork requires it (e.g. EIP-7928 sets BlockAccessListHash).
bool genesisUsesTargetFork = test.Network.IsEip7928Enabled;
List<(ForkActivation Activation, IReleaseSpec Spec)> transitions = isEngineTest || genesisUsesTargetFork
? [((ForkActivation)0, test.Network)]
: [((ForkActivation)0, test.GenesisSpec), ((ForkActivation)1, test.Network)]; // genesis block is always initialized with Frontier
if (test.NetworkAfterTransition is not null)
{
transitions.Add((test.TransitionForkActivation!.Value, test.NetworkAfterTransition));
}
ISpecProvider specProvider = new CustomSpecProvider(test.ChainId, test.ChainId, transitions.ToArray());
if (test.Network.IsEip4844Enabled || test.NetworkAfterTransition?.IsEip4844Enabled == true)
{
await KzgPolynomialCommitments.InitializeAsync();
}
DifficultyCalculator.Wrapped = new EthashDifficultyCalculator(specProvider);
IRewardCalculator rewardCalculator = new RewardCalculator(specProvider);
bool isPostMerge = test.Network != London.Instance &&
test.Network != Berlin.Instance &&
test.Network != MuirGlacier.Instance &&
test.Network != Istanbul.Instance &&
test.Network != ConstantinopleFix.Instance &&
test.Network != Constantinople.Instance &&
test.Network != Byzantium.Instance &&
test.Network != SpuriousDragon.Instance &&
test.Network != TangerineWhistle.Instance &&
test.Network != Dao.Instance &&
test.Network != Homestead.Instance &&
test.Network != Frontier.Instance &&
test.Network != Olympic.Instance;
if (isPostMerge)
{
rewardCalculator = NoBlockRewards.Instance;
specProvider.UpdateMergeTransitionInfo(0, 0);
}
IConfigProvider configProvider = new ConfigProvider();
ContainerBuilder containerBuilder = new ContainerBuilder()
.AddModule(new TestNethermindModule(configProvider))
.AddSingleton(specProvider)
.AddSingleton(_logManager)
.AddSingleton(rewardCalculator)
.AddSingleton<IDifficultyCalculator>(DifficultyCalculator);
if (isEngineTest)
{
containerBuilder.AddModule(new TestMergeModule(configProvider));
}
await using IContainer container = containerBuilder.Build();
IMainProcessingContext mainBlockProcessingContext = container.Resolve<IMainProcessingContext>();
IWorldState stateProvider = (mainBlockProcessingContext.WorldState as ParallelWorldState).Inner; // directly access underlying state
BlockchainProcessor blockchainProcessor = (BlockchainProcessor)mainBlockProcessingContext.BlockchainProcessor;
IBlockTree blockTree = container.Resolve<IBlockTree>();
IBlockValidator blockValidator = container.Resolve<IBlockValidator>();
blockchainProcessor.Start();
// Register tracer if provided for blocktest tracing
if (tracer is not null)
{
blockchainProcessor.Tracers.Add(tracer);
}
try
{
BlockHeader parentHeader;
// Genesis processing
using (stateProvider.BeginScope(null))
{
InitializeTestState(test, stateProvider, specProvider);
stopwatch?.Start();
test.GenesisRlp ??= Rlp.Encode(new Block(JsonToEthereumTest.Convert(test.GenesisBlockHeader)));
Block genesisBlock = Rlp.Decode<Block>(test.GenesisRlp.Bytes);
Assert.That(genesisBlock.Header.Hash, Is.EqualTo(new Hash256(test.GenesisBlockHeader.Hash)));
ManualResetEvent genesisProcessed = new(false);
blockTree.NewHeadBlock += (_, args) =>
{
if (args.Block.Number == 0)
{
Assert.That(stateProvider.HasStateForBlock(genesisBlock.Header), Is.True);
genesisProcessed.Set();
}
};
blockchainProcessor.BlockRemoved += (_, args) =>
{
if (args.ProcessingResult != ProcessingResult.Success && args.BlockHash == genesisBlock.Header.Hash)
{
Assert.Fail($"Failed to process genesis block: {args.Exception}");
genesisProcessed.Set();
}
};
blockTree.SuggestBlock(genesisBlock);
genesisProcessed.WaitOne(_genesisProcessingTimeoutMs);
parentHeader = genesisBlock.Header;
// Dispose genesis block's AccountChanges
genesisBlock.DisposeAccountChanges();
}
if (test.Blocks is not null)
{
// blockchain test
parentHeader = SuggestBlocks(test, failOnInvalidRlp, blockValidator, blockTree, parentHeader);
}
else if (test.EngineNewPayloads is not null)
{
// engine test
IEngineRpcModule engineRpcModule = container.Resolve<IEngineRpcModule>();
await RunNewPayloads(test.EngineNewPayloads, engineRpcModule);
}
else
{
Assert.Fail("Invalid blockchain test, did not contain blocks or new payloads.");
}
// NOTE: Tracer removal must happen AFTER StopAsync to ensure all blocks are traced
// Blocks are queued asynchronously, so we need to wait for processing to complete
await blockchainProcessor.StopAsync(true);
stopwatch?.Stop();
IBlockCachePreWarmer? preWarmer = container.Resolve<MainProcessingContext>().LifetimeScope.ResolveOptional<IBlockCachePreWarmer>();
// Caches are cleared async, which is a problem as read for the MainWorldState with prewarmer is not correct if its not cleared.
preWarmer?.ClearCaches();
Block? headBlock = blockTree.RetrieveHeadBlock();
Assert.That(headBlock, Is.Not.Null);
if (headBlock is null)
{
return new EthereumTestResult(test.Name, null, false);
}
List<string> differences;
using (stateProvider.BeginScope(headBlock.Header))
{
differences = RunAssertions(test, headBlock, stateProvider);
}
bool testPassed = differences.Count == 0;
// Write test end marker if using streaming tracer (JSONL format)
// This must be done BEFORE removing tracer and BEFORE Assert to ensure marker is written even on failure
if (tracer is not null)
{
tracer.TestFinished(test.Name, testPassed, test.Network, stopwatch?.Elapsed, headBlock?.StateRoot);
blockchainProcessor.Tracers.Remove(tracer);
}
Assert.That(differences, Is.Empty, "differences");
return new EthereumTestResult(test.Name, null, testPassed);
}
catch (Exception)
{
await blockchainProcessor.StopAsync(true);
throw;
}
}
private static BlockHeader SuggestBlocks(BlockchainTest test, bool failOnInvalidRlp, IBlockValidator blockValidator, IBlockTree blockTree, BlockHeader parentHeader)
{
List<(Block Block, string ExpectedException)> correctRlp = DecodeRlps(test, failOnInvalidRlp);
for (int i = 0; i < correctRlp.Count; i++)
{
// Mimic the actual behaviour where block goes through validating sync manager
correctRlp[i].Block.Header.IsPostMerge = correctRlp[i].Block.Difficulty == 0;
// For tests with reorgs, find the actual parent header from block tree
parentHeader = blockTree.FindHeader(correctRlp[i].Block.ParentHash) ?? parentHeader;
Assert.That(correctRlp[i].Block.Hash, Is.Not.Null, $"null hash in {test.Name} block {i}");
bool expectsException = correctRlp[i].ExpectedException is not null;
// Validate block structure first (mimics SyncServer validation)
if (blockValidator.ValidateSuggestedBlock(correctRlp[i].Block, parentHeader, out string? validationError))
{
Assert.That(!expectsException, $"Expected block {correctRlp[i].Block.Hash} to fail with '{correctRlp[i].ExpectedException}', but it passed validation");
try
{
// All validations passed, suggest the block
blockTree.SuggestBlock(correctRlp[i].Block);
}
catch (InvalidBlockException e)
{
// Exception thrown during block processing
Assert.That(expectsException, $"Unexpected invalid block {correctRlp[i].Block.Hash}: {validationError}, Exception: {e}");
// else: Expected to fail and did fail via exception → this is correct behavior
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception during processing: {e}");
}
finally
{
// Dispose AccountChanges to prevent memory leaks in tests
correctRlp[i].Block.DisposeAccountChanges();
}
}
else
{
// Validation FAILED
Assert.That(expectsException, $"Unexpected invalid block {correctRlp[i].Block.Hash}: {validationError}");
// else: Expected to fail and did fail → this is correct behavior
}
parentHeader = correctRlp[i].Block.Header;
}
return parentHeader;
}
private async static Task RunNewPayloads(TestEngineNewPayloadsJson[]? newPayloads, IEngineRpcModule engineRpcModule)
{
(ExecutionPayloadV4, string[]?, string[]?, int, int)[] payloads = [.. JsonToEthereumTest.Convert(newPayloads)];
// blockchain test engine
foreach ((ExecutionPayload executionPayload, string[]? blobVersionedHashes, string[]? validationError, int newPayloadVersion, int fcuVersion) in payloads)
{
ResultWrapper<PayloadStatusV1> res;
byte[]?[] hashes = blobVersionedHashes is null ? [] : [.. blobVersionedHashes.Select(x => Bytes.FromHexString(x))];
MethodInfo newPayloadMethod = engineRpcModule.GetType().GetMethod($"engine_newPayloadV{newPayloadVersion}");
List<object?> newPayloadParams = [executionPayload];
if (newPayloadVersion >= 3)
{
newPayloadParams.AddRange([hashes, executionPayload.ParentBeaconBlockRoot]);
}
if (newPayloadVersion >= 4)
{
newPayloadParams.Add(executionPayload.ExecutionRequests);
}
res = await (Task<ResultWrapper<PayloadStatusV1>>)newPayloadMethod.Invoke(engineRpcModule, [.. newPayloadParams]);
if (res.Result.ResultType == ResultType.Success)
{
ForkchoiceStateV1 fcuState = new(executionPayload.BlockHash, executionPayload.BlockHash, executionPayload.BlockHash);
MethodInfo fcuMethod = engineRpcModule.GetType().GetMethod($"engine_forkchoiceUpdatedV{fcuVersion}");
await (Task<ResultWrapper<ForkchoiceUpdatedV1Result>>)fcuMethod.Invoke(engineRpcModule, [fcuState, null]);
}
}
}
private static List<(Block Block, string ExpectedException)> DecodeRlps(BlockchainTest test, bool failOnInvalidRlp)
{
List<(Block Block, string ExpectedException)> correctRlp = [];
for (int i = 0; i < test.Blocks!.Length; i++)
{
TestBlockJson testBlockJson = test.Blocks[i];
try
{
byte[] rlpBytes = Bytes.FromHexString(testBlockJson.Rlp!);
Block suggestedBlock = Rlp.Decode<Block>(rlpBytes);
if (testBlockJson.BlockHeader is not null)
{
Assert.That(suggestedBlock.Header.Hash, Is.EqualTo(new Hash256(testBlockJson.BlockHeader.Hash)));
for (int uncleIndex = 0; uncleIndex < suggestedBlock.Uncles.Length; uncleIndex++)
{
Assert.That(suggestedBlock.Uncles[uncleIndex].Hash, Is.EqualTo(new Hash256(testBlockJson.UncleHeaders![uncleIndex].Hash)));
}
correctRlp.Add((suggestedBlock, testBlockJson.ExpectException));
}
}
catch (Exception e)
{
if (testBlockJson.ExpectException is null)
{
string invalidRlpMessage = $"Invalid RLP ({i}) {e}";
Assert.That(!failOnInvalidRlp, invalidRlpMessage);
// ForgedTests don't have ExpectedException and at the same time have invalid rlps
// Don't fail here. If test executed incorrectly will fail at last check
_logger.Warn(invalidRlpMessage);
}
else
{
_logger.Info($"Expected invalid RLP ({i})");
}
}
}
if (correctRlp.Count == 0)
{
using (Assert.EnterMultipleScope())
{
Assert.That(test.GenesisBlockHeader, Is.Not.Null);
Assert.That(test.LastBlockHash, Is.EqualTo(new Hash256(test.GenesisBlockHeader.Hash)));
}
}
return correctRlp;
}
private static void InitializeTestState(BlockchainTest test, IWorldState stateProvider, ISpecProvider specProvider)
{
foreach (KeyValuePair<Address, AccountState> accountState in
(IEnumerable<KeyValuePair<Address, AccountState>>)test.Pre ?? Array.Empty<KeyValuePair<Address, AccountState>>())
{
foreach (KeyValuePair<UInt256, byte[]> storageItem in accountState.Value.Storage)
{
stateProvider.Set(new StorageCell(accountState.Key, storageItem.Key), storageItem.Value);
}
stateProvider.CreateAccount(accountState.Key, accountState.Value.Balance, accountState.Value.Nonce);
stateProvider.InsertCode(accountState.Key, accountState.Value.Code, specProvider.GenesisSpec);
}
stateProvider.Commit(specProvider.GenesisSpec);
stateProvider.CommitTree(0);
stateProvider.Reset();
}
private static List<string> RunAssertions(BlockchainTest test, Block headBlock, IWorldState stateProvider)
{
if (test.PostStateRoot is not null)
{
return test.PostStateRoot != stateProvider.StateRoot ? ["state root mismatch"] : Enumerable.Empty<string>().ToList();
}
List<string> differences = [];
IEnumerable<KeyValuePair<Address, AccountState>> deletedAccounts = test.Pre?
.Where(pre => !(test.PostState?.ContainsKey(pre.Key) ?? false)) ?? Array.Empty<KeyValuePair<Address, AccountState>>();
foreach (KeyValuePair<Address, AccountState> deletedAccount in deletedAccounts)
{
if (stateProvider.AccountExists(deletedAccount.Key))
{
differences.Add($"Pre state account {deletedAccount.Key} was not deleted as expected.");
}
}
foreach ((Address accountAddress, AccountState accountState) in test.PostState!)
{
int differencesBefore = differences.Count;
if (differences.Count > 8)
{
Console.WriteLine("More than 8 differences...");
break;
}
bool accountExists = stateProvider.AccountExists(accountAddress);
UInt256? balance = accountExists ? stateProvider.GetBalance(accountAddress) : null;
UInt256? nonce = accountExists ? stateProvider.GetNonce(accountAddress) : null;
if (accountState.Balance != balance)
{
differences.Add($"{accountAddress} balance exp: {accountState.Balance}, actual: {balance}, diff: {(balance > accountState.Balance ? balance - accountState.Balance : accountState.Balance - balance)}");
}
if (accountState.Nonce != nonce)
{
differences.Add($"{accountAddress} nonce exp: {accountState.Nonce}, actual: {nonce}");
}
byte[] code = accountExists ? stateProvider.GetCode(accountAddress) : [];
if (!Bytes.AreEqual(accountState.Code, code))
{
differences.Add($"{accountAddress} code exp: {accountState.Code?.Length}, actual: {code?.Length}");
}
if (differences.Count != differencesBefore)
{
_logger.Info($"ACCOUNT STATE ({accountAddress}) HAS DIFFERENCES");
}
differencesBefore = differences.Count;
KeyValuePair<UInt256, byte[]>[] clearedStorages = [];
if (test.Pre.ContainsKey(accountAddress))
{
clearedStorages = [.. test.Pre[accountAddress].Storage.Where(s => !accountState.Storage.ContainsKey(s.Key))];
}
foreach (KeyValuePair<UInt256, byte[]> clearedStorage in clearedStorages)
{
ReadOnlySpan<byte> value = !stateProvider.AccountExists(accountAddress) ? Bytes.Empty : stateProvider.Get(new StorageCell(accountAddress, clearedStorage.Key));
if (!value.IsZero())
{
differences.Add($"{accountAddress} storage[{clearedStorage.Key}] exp: 0x00, actual: {value.ToHexString(true)}");
}
}
foreach (KeyValuePair<UInt256, byte[]> storageItem in accountState.Storage)
{
ReadOnlySpan<byte> value = !stateProvider.AccountExists(accountAddress) ? Bytes.Empty : stateProvider.Get(new StorageCell(accountAddress, storageItem.Key));
if (!Bytes.AreEqual(storageItem.Value, value))
{
differences.Add($"{accountAddress} storage[{storageItem.Key}] exp: {storageItem.Value.ToHexString(true)}, actual: {value.ToHexString(true)}");
}
}
if (differences.Count != differencesBefore)
{
_logger.Info($"ACCOUNT STORAGE ({accountAddress}) HAS DIFFERENCES");
}
}
TestBlockHeaderJson? testHeaderJson = test.Blocks?
.Where(b => b.BlockHeader is not null)
.SingleOrDefault(b => new Hash256(b.BlockHeader.Hash) == headBlock.Hash)?.BlockHeader;
if (testHeaderJson is not null)
{
BlockHeader testHeader = JsonToEthereumTest.Convert(testHeaderJson);
BigInteger gasUsed = headBlock.Header.GasUsed;
if ((testHeader?.GasUsed ?? 0) != gasUsed)
{
differences.Add($"GAS USED exp: {testHeader?.GasUsed ?? 0}, actual: {gasUsed}");
}
if (headBlock.Transactions.Length != 0 && testHeader.Bloom.ToString() != headBlock.Header.Bloom.ToString())
{
differences.Add($"BLOOM exp: {testHeader.Bloom}, actual: {headBlock.Header.Bloom}");
}
if (testHeader.StateRoot != stateProvider.StateRoot)
{
differences.Add($"STATE ROOT exp: {testHeader.StateRoot}, actual: {stateProvider.StateRoot}");
}
if (testHeader.TxRoot != headBlock.Header.TxRoot)
{
differences.Add($"TRANSACTIONS ROOT exp: {testHeader.TxRoot}, actual: {headBlock.Header.TxRoot}");
}
if (testHeader.ReceiptsRoot != headBlock.Header.ReceiptsRoot)
{
differences.Add($"RECEIPT ROOT exp: {testHeader.ReceiptsRoot}, actual: {headBlock.Header.ReceiptsRoot}");
}
}
if (test.LastBlockHash != headBlock.Hash)
{
differences.Add($"LAST BLOCK HASH exp: {test.LastBlockHash}, actual: {headBlock.Hash}");
}
foreach (string difference in differences)
{
_logger.Info(difference);
}
return differences;
}
}