-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathBlockhashProviderTests.cs
More file actions
484 lines (401 loc) · 22.7 KB
/
Copy pathBlockhashProviderTests.cs
File metadata and controls
484 lines (401 loc) · 22.7 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Headers;
using Nethermind.Config;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Builders;
using Nethermind.Logging;
using Nethermind.Specs;
using Nethermind.Specs.Forks;
using Nethermind.Specs.Test;
using Nethermind.Evm;
using Nethermind.Evm.State;
using Nethermind.Core.Eip2930;
using Nethermind.Int256;
using NSubstitute;
using NUnit.Framework;
namespace Nethermind.Blockchain.Test;
[Parallelizable(ParallelScope.All)]
public class BlockhashProviderTests
{
private static (IWorldState, Hash256) CreateWorldState()
{
IWorldState worldState = TestWorldStateFactory.CreateForTest();
using IDisposable _ = worldState.BeginScope(IWorldState.PreGenesis);
worldState.CreateAccount(Eip2935Constants.BlockHashHistoryAddress, 0, 1);
worldState.Commit(Frontier.Instance);
worldState.CommitTree(0);
return (worldState, worldState.StateRoot);
}
private static IWorldState CreateWorldStateWithHistoryContract(IReleaseSpec spec)
{
IWorldState worldState = TestWorldStateFactory.CreateForTest();
using IDisposable _ = worldState.BeginScope(IWorldState.PreGenesis);
worldState.CreateAccount(Eip2935Constants.BlockHashHistoryAddress, 0, 1);
byte[] code = [1, 2, 3];
worldState.InsertCode(Eip2935Constants.BlockHashHistoryAddress, ValueKeccak.Compute(code), code, spec);
worldState.Commit(spec);
worldState.CommitTree(0);
return worldState;
}
[TestCase(true, false, true, true, TestName = "GetAccessList_WithDeployedContract_CoversTheParentHashSlot")]
[TestCase(false, false, true, false, TestName = "GetAccessList_BeforeEip2935_IsNull")]
[TestCase(true, true, true, false, TestName = "GetAccessList_ForGenesis_IsNull")]
[TestCase(true, false, false, false, TestName = "GetAccessList_WithoutDeployedContract_IsNull")]
public void GetAccessList_AtGivenForkAndState_HintsExactlyTheParentHashSlot(
bool eip2935Enabled, bool isGenesis, bool contractDeployed, bool expectList)
{
IReleaseSpec spec = eip2935Enabled ? Prague.Instance : Cancun.Instance;
(IWorldState worldState, Hash256 stateRoot) = CreateWorldState();
Block parent = Build.A.Block.WithNumber(41).TestObject;
Block current = isGenesis
? Build.A.Block.Genesis.WithStateRoot(stateRoot).TestObject
: Build.A.Block.WithParent(parent).WithStateRoot(stateRoot).TestObject;
using IDisposable scope = worldState.BeginScope(current.Header);
if (contractDeployed)
{
byte[] code = [1, 2, 3];
worldState.InsertCode(Eip2935Constants.BlockHashHistoryAddress, ValueKeccak.Compute(code), code, spec);
}
AccessList? accessList = new BlockhashStore(worldState).GetAccessList(current, spec);
if (!expectList)
{
Assert.That(accessList, Is.Null);
return;
}
UInt256 expectedSlot = new((current.Number - 1) % spec.Eip2935RingBufferSize);
Assert.That(accessList, Is.Not.Null);
foreach ((Address address, AccessList.StorageKeysEnumerable storageKeys) in accessList!)
{
Assert.That(address, Is.EqualTo(Eip2935Constants.BlockHashHistoryAddress));
foreach (UInt256 storageKey in storageKeys)
{
Assert.That(storageKey, Is.EqualTo(expectedSlot), "the hint must cover exactly the ring-buffer slot the block writes");
}
}
}
private static BlockhashProvider CreateBlockHashProvider(IHeaderFinder headerFinder, IReleaseSpec spec)
{
(IWorldState worldState, Hash256 _) = CreateWorldState();
BlockhashProvider provider = new(new BlockhashCache(headerFinder, LimboLogs.Instance), worldState, LimboLogs.Instance);
return provider;
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Can_get_parent_only_headers()
{
const int chainLength = 512;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfHeadersOnly.OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
BlockHeader? head = tree.FindHeader(chainLength - 1, BlockTreeLookupOptions.None);
Block current = Build.A.Block.WithParent(head!).TestObject;
Hash256? result = provider.GetBlockhash(current.Header, chainLength - 1, Frontier.Instance);
Assert.That(result, Is.EqualTo(head?.Hash));
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Can_lookup_up_to_256_before_with_headers_only()
{
const int chainLength = 512;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfHeadersOnly.OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
BlockHeader head = tree.FindHeader(chainLength - 1, BlockTreeLookupOptions.None)!;
Block current = Build.A.Block.WithParent(head).TestObject;
Hash256? result = provider.GetBlockhash(current.Header, chainLength - 256, Frontier.Instance);
Assert.That(result, Is.EqualTo(tree.FindHeader(256, BlockTreeLookupOptions.None)!.Hash));
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Can_lookup_correctly_on_disconnected_main_chain()
{
const int chainLength = 512;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
BlockHeader notCanonParent = tree.FindHeader(chainLength - 4, BlockTreeLookupOptions.None)!;
BlockHeader expectedHeader = tree.FindHeader(chainLength - 3, BlockTreeLookupOptions.None)!;
Block headParent = tree.FindBlock(chainLength - 2, BlockTreeLookupOptions.None)!;
Block head = tree.FindBlock(chainLength - 1, BlockTreeLookupOptions.None)!;
Block branch = Build.A.Block.WithParent(notCanonParent).WithTransactions(Build.A.Transaction.TestObject).TestObject;
Assert.That(tree.Insert(branch, BlockTreeInsertBlockOptions.SaveHeader), Is.EqualTo(AddBlockResult.Added));
tree.TryUpdateMainChain(branch.Header, true, preloadedBlocks: new[] { branch }); // Update branch
tree.TryUpdateMainChain(head.Header, true, preloadedBlocks: new[] { headParent, head }); // Update back to original again, but skipping the branch block.
Block current = Build.A.Block.WithParent(head).TestObject; // At chainLength
Hash256? result = provider.GetBlockhash(current.Header, chainLength - 3, Frontier.Instance);
Assert.That(result, Is.EqualTo(expectedHeader.Hash));
}
[MaxTime(Timeout.MaxTestTime)]
[TestCase(0, TestName = "Can_lookup_up_to_256_before_with_headers_only_and_competing_branches")]
[TestCase(1, TestName = "Can_lookup_up_to_256_before_soon_after_fast_sync")]
[TestCase(6, TestName = "Can_lookup_up_to_256_before_some_blocks_after_fast_sync")]
public void Lookup_with_competing_branches_after_fast_sync(int additionalBlocks)
{
const int chainLength = 512;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfHeadersOnly
.OfChainLength(out Block headBlock, chainLength)
.OfChainLength(out Block _, chainLength, 1);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
Block current = Build.A.Block.WithParent(headBlock).TestObject;
for (int i = 0; i < additionalBlocks; i++)
{
tree.SuggestBlock(current);
tree.TryUpdateMainChain(current.Header, true, preloadedBlocks: new[] { current });
current = Build.A.Block.WithParent(current).TestObject;
}
ulong lookupNumber = current.Number - 256ul;
Hash256? result = provider.GetBlockhash(current.Header, lookupNumber, Frontier.Instance);
Assert.That(result, Is.Not.Null);
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Can_handle_non_main_chain_in_fast_sync()
{
const int chainLength = 512;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfHeadersOnly
.OfChainLength(out Block headBlock, chainLength)
.OfChainLength(out Block _, chainLength, 1);
BlockTree tree = blockTreeBuilder.TestObject;
Block current = Build.A.Block.WithParent(headBlock).TestObject;
for (int i = 0; i < 6; i++)
{
tree.SuggestBlock(current);
tree.TryUpdateMainChain(current.Header, true, preloadedBlocks: new[] { current });
current = Build.A.Block.WithParent(current).TestObject;
}
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
Hash256? result = provider.GetBlockhash(current.Header, 509, Frontier.Instance);
Assert.That(result, Is.Not.Null);
}
[MaxTime(Timeout.MaxTestTime)]
[TestCase(512ul, -1, true, TestName = "Can_get_parent_hash")]
[TestCase(512ul, 0, false, TestName = "Cannot_ask_for_self")]
[TestCase(512ul, 1, false, TestName = "Cannot_ask_about_future")]
[TestCase(512ul, -256, true, TestName = "Can_lookup_up_to_256_before")]
[TestCase(512ul, -257, false, TestName = "No_lookup_more_than_256_before")]
public void Blockhash_lookup_with_full_chain(ulong chainLength, int lookupOffset, bool expectNonNull)
{
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
BlockHeader head = tree.FindHeader(chainLength - 1ul, BlockTreeLookupOptions.None)!;
Block current = Build.A.Block.WithParent(head).TestObject;
ulong lookupNumber = (ulong)((long)chainLength + lookupOffset);
Hash256? result = provider.GetBlockhash(current.Header, lookupNumber, Frontier.Instance);
if (expectNonNull)
{
Hash256 expected = tree.FindHeader(lookupNumber, BlockTreeLookupOptions.None)!.Hash!;
Assert.That(result, Is.EqualTo(expected));
}
else
{
Assert.That(result, Is.Null);
}
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void UInt_256_overflow()
{
const int chainLength = 128;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockhashProvider provider = CreateBlockHashProvider(blockTreeBuilder.HeaderStore, Frontier.Instance);
BlockHeader head = tree.FindHeader(chainLength - 1, BlockTreeLookupOptions.None)!;
Block current = Build.A.Block.WithParent(head).TestObject;
Hash256? result = provider.GetBlockhash(current.Header, 127, Frontier.Instance);
Assert.That(result, Is.EqualTo(head.Hash));
}
[MaxTime(Timeout.MaxTestTime)]
[TestCase(1ul)]
[TestCase(512ul)]
[TestCase(8192ul)]
[TestCase(8193ul)]
public void Eip2935_enabled_Eip7709_disabled_and_then_get_hash(ulong chainLength)
{
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis).OfHeadersOnly.OfChainLength(chainLength);
BlockTree tree = blockTreeBuilder.TestObject;
BlockHeader? head = tree.FindHeader(chainLength - 1ul, BlockTreeLookupOptions.None);
// number = chainLength
(IWorldState worldState, Hash256 stateRoot) = CreateWorldState();
Block current = Build.A.Block.WithParent(head!).WithStateRoot(stateRoot).TestObject;
tree.SuggestHeader(current.Header);
CustomSpecProvider specProvider = new(
(new ForkActivation(0, genesis.Timestamp), Frontier.Instance),
(new ForkActivation(0, current.Timestamp), Prague.Instance));
BlockhashProvider provider = new(new BlockhashCache(blockTreeBuilder.HeaderStore, LimboLogs.Instance), worldState, LimboLogs.Instance);
BlockhashStore store = new(worldState);
using IDisposable _ = worldState.BeginScope(current.Header);
Hash256? result = provider.GetBlockhash(current.Header, chainLength - 1, Frontier.Instance);
Assert.That(result, Is.EqualTo(head?.Hash));
AssertGenesisHash(Prague.Instance, provider, current.Header, genesis.Hash);
head = current.Header;
// number = chainLength + 1
current = Build.A.Block.WithParent(head!).TestObject;
tree.SuggestHeader(current.Header);
store.ApplyBlockhashStateChanges(current.Header, specProvider.GetSpec(current.Header));
result = provider.GetBlockhash(current.Header, chainLength, Frontier.Instance);
Assert.That(result, Is.EqualTo(head?.Hash));
AssertGenesisHash(Prague.Instance, provider, current.Header, genesis.Hash);
}
private static void AssertGenesisHash(IReleaseSpec spec, BlockhashProvider provider, BlockHeader currentHeader, Hash256? genesisHash)
{
Hash256? result = provider.GetBlockhash(currentHeader, 0, spec);
Assert.That(result, (spec.IsEip7709Enabled && currentHeader.Number > Eip2935Constants.RingBufferSize) || currentHeader.Number > 256
? Is.Null
: Is.EqualTo(genesisHash));
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Eip2935_poc_trimmed_hashes()
{
ulong chainLength = 42ul;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTree tree = Build.A.BlockTree(genesis).OfHeadersOnly.OfChainLength(chainLength).TestObject;
BlockHeader? head = tree.FindHeader(chainLength - 1ul, BlockTreeLookupOptions.None);
// number = chainLength
(IWorldState worldState, Hash256 stateRoot) = CreateWorldState();
Block current = Build.A.Block.WithParent(head!).WithStateRoot(stateRoot).TestObject;
tree.SuggestHeader(current.Header);
ISpecProvider specProvider = new CustomSpecProvider(
(new ForkActivation(0, genesis.Timestamp), Frontier.Instance),
(new ForkActivation(0, current.Timestamp), Prague.Instance));
BlockhashStore store = new(worldState);
using IDisposable _ = worldState.BeginScope(current.Header);
// 1. Set some code to pass IsContract check
byte[] code = [1, 2, 3];
worldState.InsertCode(Eip2935Constants.BlockHashHistoryAddress, ValueKeccak.Compute(code), code, Prague.Instance);
current.Header.ParentHash = new Hash256("0x0011111111111111111111111111111111111111111111111111111111111111");
// 2. Store parent hash with leading zeros
store.ApplyBlockhashStateChanges(current.Header, specProvider.GetSpec(current.Header));
// 3. Try to retrieve the parent hash from the state
Hash256? result = store.GetBlockHashFromState(current.Header, current.Header.Number - 1, specProvider.GetSpec(current.Header));
Assert.That(result, Is.EqualTo(current.Header.ParentHash));
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void BlockAccessListManager_blockhash_state_changes_match_BlockhashStore()
{
IReleaseSpec spec = Amsterdam.Instance;
IWorldState legacyWorldState = CreateWorldStateWithHistoryContract(spec);
IWorldState balWorldState = CreateWorldStateWithHistoryContract(spec);
Block parent = Build.A.Block.WithNumber(41).TestObject;
Block current = Build.A.Block.WithParent(parent).TestObject;
UInt256 parentBlockIndex = new((current.Number - 1) % spec.Eip2935RingBufferSize);
StorageCell storageCell = new(Eip2935Constants.BlockHashHistoryAddress, parentBlockIndex);
using IDisposable legacyScope = legacyWorldState.BeginScope(current.Header);
new BlockhashStore(legacyWorldState).ApplyBlockhashStateChanges(current.Header, spec);
byte[] expectedStoredHash = legacyWorldState.Get(storageCell).ToArray();
using IDisposable balScope = balWorldState.BeginScope(current.Header);
TestSingleReleaseSpecProvider specProvider = new(spec);
BlockAccessListManager balManager = new(
balWorldState,
specProvider,
Substitute.For<IBlockhashProvider>(),
LimboLogs.Instance,
new BlocksConfig { ParallelExecution = false },
new WithdrawalProcessorFactory(LimboLogs.Instance),
CodeInfoRepositoryFactories.Caching);
balManager.PrepareForProcessing(current, spec, ProcessingOptions.None);
balManager.SetBlockExecutionContext(new BlockExecutionContext(current.Header, spec));
balManager.Setup(current);
balManager.ApplyBlockhashStateChanges(current.Header, spec);
balManager.NextTransaction();
Assert.That(balWorldState.Get(storageCell).ToArray(), Is.EqualTo(expectedStoredHash));
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void BlockhashStore_uses_custom_ring_buffer_size()
{
const int customRingBufferSize = 100;
const int chainLength = 150;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTree tree = Build.A.BlockTree(genesis).OfHeadersOnly.OfChainLength(chainLength).TestObject;
BlockHeader? head = tree.FindHeader(chainLength - 1, BlockTreeLookupOptions.None);
(IWorldState worldState, Hash256 stateRoot) = CreateWorldState();
Block current = Build.A.Block.WithParent(head!).WithStateRoot(stateRoot).TestObject;
tree.SuggestHeader(current.Header);
// Custom spec with non-standard ring buffer size
ReleaseSpec customSpec = new()
{
IsEip2935Enabled = true,
IsEip7709Enabled = true,
Eip2935RingBufferSize = customRingBufferSize
};
CustomSpecProvider specProvider = new((new ForkActivation(0, genesis.Timestamp), customSpec));
BlockhashStore store = new(worldState);
using IDisposable _ = worldState.BeginScope(current.Header);
// Insert code (account already created by CreateWorldState)
byte[] code = [1, 2, 3];
worldState.InsertCode(Eip2935Constants.BlockHashHistoryAddress, ValueKeccak.Compute(code), code, customSpec);
// Process current block (150) - stores block 149's hash
store.ApplyBlockhashStateChanges(current.Header, specProvider.GetSpec(current.Header));
// Simulate processing blocks 51-149 to fill the ring buffer
// At block 150 with buffer size 100, we need hashes for blocks 50-149
// Block 150 stores block 149's hash (already done above)
// Now process blocks 51-149 from the tree to store blocks 50-148
for (ulong blockNum = chainLength - customRingBufferSize + 1; blockNum < chainLength; blockNum++)
{
BlockHeader? header = tree.FindHeader(blockNum, BlockTreeLookupOptions.None);
Assert.That(header, Is.Not.Null, $"Block {blockNum} should exist in tree");
store.ApplyBlockhashStateChanges(header!, specProvider.GetSpec(header));
}
// Now verify all blocks behave correctly with custom ring buffer size
// At block 150 with buffer size 100, only blocks [50, 149] should be retrievable
for (ulong blockNum = 1ul; blockNum < (chainLength - customRingBufferSize); blockNum++)
{
Hash256? result = store.GetBlockHashFromState(current.Header, blockNum, customSpec);
Assert.That(result, Is.Null,
$"Block {blockNum} should be outside custom ring buffer of size {customRingBufferSize} (proves custom size is used, not default 8191)");
}
for (ulong blockNum = chainLength - customRingBufferSize; blockNum < chainLength; blockNum++)
{
BlockHeader? expectedHeader = tree.FindHeader(blockNum, BlockTreeLookupOptions.None);
Hash256? result = store.GetBlockHashFromState(current.Header, blockNum, customSpec);
Assert.That(result, Is.EqualTo(expectedHeader!.Hash),
$"Block {blockNum} should be retrievable within custom ring buffer of size {customRingBufferSize}");
}
}
[Test, MaxTime(Timeout.MaxTestTime)]
public void Throws_when_in_window_hash_cannot_be_resolved()
{
IHeaderFinder headerFinder = Substitute.For<IHeaderFinder>();
BlockhashProvider provider = CreateBlockHashProvider(headerFinder, Frontier.Instance);
BlockHeader current = Build.A.BlockHeader.WithNumber(300).WithParentHash(TestItem.KeccakA).TestObject;
Assert.Throws<InvalidDataException>(() => provider.GetBlockhash(current, 100, Frontier.Instance));
}
[Test, MaxTime(Timeout.MaxTestTime)]
[NonParallelizable]
public async Task Prefetches_come_in_wrong_order()
{
const int chainLength = 261;
Block genesis = Build.A.Block.Genesis.TestObject;
BlockTreeBuilder blockTreeBuilder = Build.A.BlockTree(genesis)
.OfHeadersOnly
.OfChainLength(chainLength);
SlowHeaderStore slowHeaderStore = new(blockTreeBuilder.HeaderStore) { SlowBlockNumber = 2 };
BlockTree tree = blockTreeBuilder.TestObject;
BlockHeader head = tree.FindHeader(chainLength - 1, BlockTreeLookupOptions.None)!;
ulong expectedBlockNumber = head.Number - 2;
BlockHeader expected = tree.FindHeader(expectedBlockNumber, BlockTreeLookupOptions.None)!;
BlockHeader previousHead = tree.FindHeader(chainLength - 4, BlockTreeLookupOptions.None)!;
BlockhashProvider provider = CreateBlockHashProvider(slowHeaderStore, Frontier.Instance);
CancellationTokenSource cts = new(TimeSpan.FromMilliseconds(5));
Task previousHeadTask = provider.Prefetch(previousHead, cts.Token);
Task headTask = provider.Prefetch(head, CancellationToken.None);
await Task.WhenAll(previousHeadTask, headTask);
Hash256? result = provider.GetBlockhash(head, expectedBlockNumber, Frontier.Instance);
Assert.That(result, Is.EqualTo(expected.Hash));
}
}