-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathBlockCachePreWarmer.cs
More file actions
692 lines (598 loc) · 28.6 KB
/
Copy pathBlockCachePreWarmer.cs
File metadata and controls
692 lines (598 loc) · 28.6 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Collections.Pooled;
using Microsoft.Extensions.ObjectPool;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Core.Threading;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Evm.State;
using Nethermind.Core.Eip2930;
using Nethermind.Core.BlockAccessLists;
using Nethermind.Core.Collections;
using Nethermind.Core.Extensions;
using Nethermind.Trie;
using PrewarmMetrics = Nethermind.Consensus.Processing.Prewarming.Metrics;
namespace Nethermind.Consensus.Processing;
public sealed class BlockCachePreWarmer : IBlockCachePreWarmer
{
private readonly int _concurrencyLevel;
// Speculative warming runs in the idle gap alongside RPC, so it is capped below the reactive level to leave cores free.
private readonly int _speculativeConcurrencyLevel;
private readonly bool _parallelExecutionBatchRead;
private readonly ObjectPool<IReadOnlyTxProcessorSource> _envPool;
private readonly ILogger _logger;
private readonly PreBlockCaches _preBlockCaches;
private readonly NodeStorageCache _nodeStorageCache;
private readonly bool _parallelExecutionEnabled;
private int _mainThreadTxIndex = -1;
internal int MainThreadTxIndex => Volatile.Read(ref _mainThreadTxIndex);
// A session is always joined (under _speculativeLock) before the reactive path touches the shared caches.
private readonly Lock _speculativeLock = new();
private CancellationTokenSource? _speculativeCts;
private Task _speculativeTask = Task.CompletedTask;
private long _speculativeGeneration = long.MinValue;
// Written only by the loop thread and read after it is joined, so the marker and its tx-hash set need no further sync.
private WarmMarker? _warmMarker;
private readonly PooledSet<Hash256> _warmedTxHashes = [];
private readonly IHasAccessList[] _systemAccessLists;
public BlockCachePreWarmer(
PrewarmerEnvFactory envFactory,
IBlocksConfig blocksConfig,
NodeStorageCache nodeStorageCache,
PreBlockCaches preBlockCaches,
ILogManager logManager,
IHasAccessList[]? systemAccessLists = null
) : this(
new ReadOnlyTxProcessingEnvPooledObjectPolicy(envFactory, preBlockCaches),
Environment.ProcessorCount * 2,
blocksConfig.PreWarmStateConcurrency,
blocksConfig.ParallelExecutionBatchRead,
nodeStorageCache,
preBlockCaches,
logManager,
blocksConfig.MempoolPreWarmConcurrency,
systemAccessLists) => _parallelExecutionEnabled = blocksConfig.ParallelExecution;
internal BlockCachePreWarmer(
IPooledObjectPolicy<IReadOnlyTxProcessorSource> poolPolicy,
int maxPoolSize,
int concurrency,
bool parallelExecutionBatchRead,
NodeStorageCache nodeStorageCache,
PreBlockCaches preBlockCaches,
ILogManager logManager,
int speculativeConcurrency = 0,
IHasAccessList[]? systemAccessLists = null)
{
_systemAccessLists = systemAccessLists ?? [];
_concurrencyLevel = concurrency == 0 ? Math.Min(Environment.ProcessorCount - 1, 16) : concurrency;
_speculativeConcurrencyLevel = speculativeConcurrency == 0 ? Math.Max(1, _concurrencyLevel / 2) : speculativeConcurrency;
_parallelExecutionBatchRead = parallelExecutionBatchRead;
_envPool = new DefaultObjectPoolProvider { MaximumRetained = maxPoolSize }.Create(poolPolicy);
_logger = logManager.GetClassLogger<BlockCachePreWarmer>();
_preBlockCaches = preBlockCaches;
_nodeStorageCache = nodeStorageCache;
}
public Task PreWarmCaches(Block suggestedBlock, BlockHeader? parent, IReleaseSpec spec, CancellationToken cancellationToken = default)
{
if (_preBlockCaches is null || !ShouldPreWarm(spec)) return Task.CompletedTask;
CancelAndJoinSpeculative();
if (TryConsumeWarmMarker(suggestedBlock.ParentHash, spec, out ISet<Hash256>? speculativelyWarmed))
{
PrewarmMetrics.MempoolPrewarmHandoffs++;
_nodeStorageCache.Enabled = true;
}
else
{
CacheType result = _preBlockCaches.ClearCaches();
_nodeStorageCache.ClearCaches();
_nodeStorageCache.Enabled = true;
if (result != default)
{
if (_logger.IsWarn) _logger.Warn($"Caches {result} are not empty. Clearing them.");
}
}
return WarmCaches(suggestedBlock, parent, spec, speculativelyWarmed, cancellationToken, _systemAccessLists);
}
private Task WarmCaches(Block suggestedBlock, BlockHeader? parent, IReleaseSpec spec, ISet<Hash256>? speculativelyWarmed, CancellationToken cancellationToken, ReadOnlySpan<IHasAccessList> systemAccessLists)
{
if (parent is null || _concurrencyLevel <= 1 || cancellationToken.IsCancellationRequested) return Task.CompletedTask;
(BlockState blockState, ParallelOptions parallelOptions, AddressWarmer addressWarmer) = PrepareWarm(suggestedBlock, parent, spec, speculativelyWarmed, _concurrencyLevel, cancellationToken, systemAccessLists);
// Run address warmer ahead of transactions warmer, but queue to ThreadPool so it doesn't block the txs
ThreadPool.UnsafeQueueUserWorkItem(addressWarmer, preferLocal: false);
// Do not pass the cancellation token to the task, we don't want exceptions to be thrown in the main processing thread
return Task.Run(() => PreWarmCachesParallel(blockState, suggestedBlock, parent, spec, parallelOptions, addressWarmer, cancellationToken));
}
private void WarmDeltaSync(Block delta, BlockHeader head, IReleaseSpec spec, CancellationToken token)
{
(BlockState blockState, ParallelOptions parallelOptions, AddressWarmer addressWarmer) = PrepareWarm(delta, head, spec, speculativelyWarmed: null, _speculativeConcurrencyLevel, token, systemAccessLists: default);
ThreadPool.UnsafeQueueUserWorkItem(addressWarmer, preferLocal: false);
PreWarmCachesParallel(blockState, delta, head, spec, parallelOptions, addressWarmer, token);
}
private (BlockState BlockState, ParallelOptions ParallelOptions, AddressWarmer AddressWarmer) PrepareWarm(Block block, BlockHeader parent, IReleaseSpec spec, ISet<Hash256>? speculativelyWarmed, int maxDegreeOfParallelism, CancellationToken token, ReadOnlySpan<IHasAccessList> systemAccessLists)
{
BlockState blockState = new(this, block, parent, spec, speculativelyWarmed);
// Safe for the speculative caller: it never overlaps main execution (joined before ProcessOne).
Volatile.Write(ref _mainThreadTxIndex, -1);
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = token };
// BAL makes speculative tx execution redundant — when BAL-based read warming is in use, drive warmup
// directly off the block's access list.
ReadOnlyBlockAccessList? bal = IsBalReadWarmingEnabled(spec) ? block.BlockAccessList : null;
AddressWarmer addressWarmer = new(parallelOptions, block, parent, spec, systemAccessLists, this, bal);
return (blockState, parallelOptions, addressWarmer);
}
public Task StartSpeculativePreWarm(BlockHeader head, IReleaseSpec spec, long generation, Func<CancellationToken, Block?> nextDelta, int idlePassDelayMs, CancellationToken cancellationToken)
{
if (_preBlockCaches is null || !ShouldPreWarm(spec) || _concurrencyLevel <= 1) return Task.CompletedTask;
if (head.Hash is not Hash256 headHash) return Task.CompletedTask;
lock (_speculativeLock)
{
// An equal-or-newer session already started (out-of-order work item); don't clobber it.
if (generation <= _speculativeGeneration) return _speculativeTask;
_speculativeGeneration = generation;
CancelAndJoinSpeculativeLocked();
CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_speculativeCts = cts;
CancellationToken token = cts.Token;
ClearWarmMarker();
_warmedTxHashes.Clear();
_preBlockCaches.ClearCaches();
_nodeStorageCache.ClearCaches();
_nodeStorageCache.Enabled = true;
return _speculativeTask = Task.Run(() => RunSpeculativeLoop(headHash, head, spec, nextDelta, idlePassDelayMs, token));
}
}
private void RunSpeculativeLoop(Hash256 headHash, BlockHeader head, IReleaseSpec spec, Func<CancellationToken, Block?> nextDelta, int idlePassDelayMs, CancellationToken token)
{
// _warmedTxHashes is reused across sessions (cleared at session start); only the small marker is per-session.
WarmMarker marker = new(headHash, spec, _warmedTxHashes);
try
{
int delay = Math.Max(1, idlePassDelayMs);
while (!token.IsCancellationRequested)
{
Block? delta = nextDelta(token);
if (token.IsCancellationRequested) break;
if (delta is not null && delta.Transactions.Length > 0)
{
WarmDeltaSync(delta, head, spec, token);
// Don't record a delta cancelled mid-warm, or the reactive pass would skip a half-warmed sender.
if (token.IsCancellationRequested) break;
foreach (Transaction tx in delta.Transactions)
{
if (tx.Hash is Hash256 hash) _warmedTxHashes.Add(hash);
}
Volatile.Write(ref _warmMarker, marker);
}
// Rate-limit every pass so a churning mempool can't keep tx selection continuously in flight.
if (token.WaitHandle.WaitOne(delay)) break;
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
_logger.DebugWarn($"Error during speculative pre-warming. {ex}");
}
}
// For tests: true once a session has published its handoff marker.
internal bool SpeculativeMarkerPublished => Volatile.Read(ref _warmMarker) is not null;
private void CancelAndJoinSpeculative()
{
lock (_speculativeLock)
{
CancelAndJoinSpeculativeLocked();
}
}
private void CancelAndJoinSpeculativeLocked()
{
if (_speculativeCts is null) return;
_speculativeCts.Cancel();
try
{
_speculativeTask.GetAwaiter().GetResult();
}
catch
{
// Warming failures are already logged inside the pass; nothing actionable here.
}
_speculativeCts.Dispose();
_speculativeCts = null;
_speculativeTask = Task.CompletedTask;
}
private void ClearWarmMarker() => Volatile.Write(ref _warmMarker, null);
private bool TryConsumeWarmMarker(Hash256? parentHash, IReleaseSpec spec, out ISet<Hash256>? warmedTxHashes)
{
WarmMarker? marker = Volatile.Read(ref _warmMarker);
// ReferenceEquals on the per-fork spec singleton: a mismatch only disables the handoff, never a correctness issue.
if (marker is not null && parentHash is not null && marker.ParentHash == parentHash && ReferenceEquals(marker.Spec, spec))
{
warmedTxHashes = marker.WarmedTxHashes;
Volatile.Write(ref _warmMarker, null);
return true;
}
warmedTxHashes = null;
return false;
}
private bool ShouldPreWarm(IReleaseSpec spec)
=> !_parallelExecutionEnabled
|| !spec.BlockLevelAccessListsEnabled
|| IsBalReadWarmingEnabled(spec);
public bool IsBalReadWarmingEnabled(IReleaseSpec spec)
=> _parallelExecutionBatchRead && spec.BlockLevelAccessListsEnabled;
/// <summary>Reports main-thread progress (called via <see cref="PrewarmerTxAdapter"/>) so warming can skip already-started txs.</summary>
/// <remarks>Only the single main execution thread writes, in ascending tx order, so a plain release store publishes progress to the polling warmup workers — no interlocked read-modify-write is needed.</remarks>
public void OnBeforeTxExecution() => Volatile.Write(ref _mainThreadTxIndex, _mainThreadTxIndex + 1);
public CacheType ClearCaches()
{
if (_logger.IsDebug) _logger.Debug("Clearing caches");
CancelAndJoinSpeculative();
ClearWarmMarker();
CacheType cachesCleared = _preBlockCaches?.ClearCaches() ?? default;
cachesCleared |= _nodeStorageCache.ClearCaches() ? CacheType.Rlp : CacheType.None;
if (_logger.IsDebug) _logger.Debug($"Cleared caches: {cachesCleared}");
return cachesCleared;
}
public void Dispose()
{
CancelAndJoinSpeculative();
_warmedTxHashes.Dispose();
(_envPool as IDisposable)?.Dispose();
}
private void PreWarmCachesParallel(BlockState blockState, Block suggestedBlock, BlockHeader parent, IReleaseSpec spec, ParallelOptions parallelOptions, AddressWarmer addressWarmer, CancellationToken cancellationToken)
{
try
{
if (cancellationToken.IsCancellationRequested) return;
if (_logger.IsDebug) _logger.Debug($"Started pre-warming caches for block {suggestedBlock.Number}.");
if (!addressWarmer.HasBal)
{
WarmupTransactions(blockState, parallelOptions);
WarmupWithdrawals(parallelOptions, spec, suggestedBlock, parent);
}
if (_logger.IsDebug) _logger.Debug($"Finished pre-warming caches for block {suggestedBlock.Number}.");
}
catch (Exception ex)
{
_logger.DebugWarn($"Error pre-warming {suggestedBlock.Number}. {ex}");
}
finally
{
// Don't complete the task until address warmer is also done.
addressWarmer.Wait();
addressWarmer.Dispose();
}
}
private void WarmupWithdrawals(ParallelOptions parallelOptions, IReleaseSpec spec, Block block, BlockHeader? parent)
{
if (parallelOptions.CancellationToken.IsCancellationRequested) return;
try
{
if (spec.WithdrawalsEnabled && block.Withdrawals is not null)
{
ParallelUnbalancedWork.For(0, block.Withdrawals.Length, parallelOptions, (EnvPool: _envPool, Block: block, Parent: parent),
static (i, state) =>
{
IReadOnlyTxProcessorSource env = state.EnvPool.Get();
try
{
using IReadOnlyTxProcessingScope scope = env.Build(state.Parent);
scope.WorldState.WarmUp(state.Block.Withdrawals![i].Address);
}
catch (MissingTrieNodeException)
{
}
finally
{
state.EnvPool.Return(env);
}
return state;
});
}
}
catch (OperationCanceledException)
{
// Ignore, block completed cancel
}
catch (Exception ex)
{
_logger.DebugError("Error pre-warming withdrawal", ex);
}
}
private void WarmupTransactions(BlockState blockState, ParallelOptions parallelOptions)
{
if (parallelOptions.CancellationToken.IsCancellationRequested) return;
try
{
Block block = blockState.Block;
if (block.Transactions.Length == 0) return;
// Group transactions by sender to process same-sender transactions sequentially
// This ensures state changes (balance, storage) from tx[N] are visible to tx[N+1]
Dictionary<AddressAsKey, ArrayPoolList<(int Index, Transaction Tx)>>? senderGroups = GroupTransactionsBySender(block);
try
{
// Convert to array for parallel iteration
using ArrayPoolList<ArrayPoolList<(int Index, Transaction Tx)>> groupArray = senderGroups.Values.ToPooledList();
// Parallel across different senders, sequential within the same sender
ParallelUnbalancedWork.For(
0,
groupArray.Count,
parallelOptions,
(blockState, groupArray, parallelOptions.CancellationToken),
static (groupIndex, tupleState) =>
{
(BlockState? blockState, ArrayPoolList<ArrayPoolList<(int Index, Transaction Tx)>> groups, CancellationToken token) = tupleState;
ArrayPoolList<(int Index, Transaction Tx)>? txList = groups[groupIndex];
// Whole group already warmed speculatively — skip; leave the rest to the reactive pass.
if (blockState.SpeculativelyWarmed is { } warmed)
{
if (AllSpeculativelyWarmed(txList, warmed))
{
Interlocked.Increment(ref PrewarmMetrics.MempoolPrewarmSendersSkipped);
return tupleState;
}
Interlocked.Increment(ref PrewarmMetrics.MempoolPrewarmSendersWarmed);
}
IReadOnlyTxProcessorSource env = blockState.PreWarmer._envPool.Get();
try
{
using IReadOnlyTxProcessingScope scope = env.Build(blockState.Parent);
BlockExecutionContext context = new(blockState.Block.Header, blockState.Spec);
scope.TransactionProcessor.SetBlockExecutionContext(context);
// Sequential within the same sender-state changes propagate correctly
foreach ((int txIndex, Transaction? tx) in txList.AsSpan())
{
if (token.IsCancellationRequested) return tupleState;
WarmupSingleTransaction(scope, tx, txIndex, blockState, token);
}
}
finally
{
blockState.PreWarmer._envPool.Return(env);
}
return tupleState;
});
}
finally
{
foreach (KeyValuePair<AddressAsKey, ArrayPoolList<(int Index, Transaction Tx)>> kvp in senderGroups)
kvp.Value.Dispose();
}
}
catch (OperationCanceledException)
{
// Ignore, block completed cancel
}
catch (Exception ex)
{
_logger.DebugError("Error pre-warming transactions", ex);
}
}
private static Dictionary<AddressAsKey, ArrayPoolList<(int Index, Transaction Tx)>> GroupTransactionsBySender(Block block)
{
Dictionary<AddressAsKey, ArrayPoolList<(int, Transaction)>> groups = [];
for (int i = 0; i < block.Transactions.Length; i++)
{
Transaction tx = block.Transactions[i];
if (tx.SenderAddress is not Address sender)
{
// Invalid signature leaves the sender null; the block will be rejected — nothing to warm.
continue;
}
if (!groups.TryGetValue(sender, out ArrayPoolList<(int, Transaction)> list))
{
list = new(4);
groups[sender] = list;
}
list.Add((i, tx));
}
return groups;
}
private static bool AllSpeculativelyWarmed(ArrayPoolList<(int Index, Transaction Tx)> group, ISet<Hash256> warmed)
{
foreach ((int _, Transaction tx) in group.AsSpan())
{
if (tx.Hash is not Hash256 hash || !warmed.Contains(hash)) return false;
}
return true;
}
private static void WarmupSingleTransaction(
IReadOnlyTxProcessingScope scope,
Transaction tx,
int txIndex,
BlockState blockState,
CancellationToken cancellationToken)
{
try
{
// Already started by the main thread — warming it now is redundant and contends; skip.
if (blockState.PreWarmer.MainThreadTxIndex >= txIndex) return;
// Non-null guaranteed: GroupTransactionsBySender filters null-sender txs
Address senderAddress = tx.SenderAddress!;
IWorldState worldState = scope.WorldState;
if (!worldState.AccountExists(senderAddress))
{
worldState.CreateAccountIfNotExists(senderAddress, UInt256.Zero);
}
// eip-2930; cancellation-responsive so an over-declared access list can't stall the end-of-block join.
if (blockState.Spec.UseTxAccessLists)
{
worldState.WarmUp(tx.AccessList, cancellationToken);
}
TransactionResult result = scope.TransactionProcessor.Warmup(tx, NullTxTracer.Instance);
if (blockState.PreWarmer._logger.IsTrace) blockState.PreWarmer._logger.Trace($"Finished pre-warming cache for tx[{txIndex}] {tx.Hash} with {result}");
}
catch (Exception ex) when (ex is EvmException or OverflowException)
{
// Ignore, regular tx processing exceptions
}
catch (Exception ex)
{
blockState.PreWarmer._logger.DebugError($"Error pre-warming cache {tx.Hash}", ex);
}
}
private class AddressWarmer(ParallelOptions parallelOptions, Block block, BlockHeader parent, IReleaseSpec spec, ReadOnlySpan<IHasAccessList> systemAccessLists, BlockCachePreWarmer preWarmer, ReadOnlyBlockAccessList? bal = null)
: IThreadPoolWorkItem, IDisposable
{
private readonly Block Block = block;
private readonly BlockCachePreWarmer PreWarmer = preWarmer;
private readonly ReadOnlyBlockAccessList? Bal = bal;
private readonly ArrayPoolList<AccessList>? SystemTxAccessLists = GetAccessLists(block, spec, systemAccessLists);
private readonly ManualResetEventSlim _doneEvent = new(initialState: false);
public bool HasBal => Bal is not null;
public void Wait() => _doneEvent.Wait();
public void Dispose() => _doneEvent.Dispose();
private static ArrayPoolList<AccessList>? GetAccessLists(Block block, IReleaseSpec spec, ReadOnlySpan<IHasAccessList> systemAccessLists)
{
if (systemAccessLists.Length == 0) return null;
ArrayPoolList<AccessList> list = new(systemAccessLists.Length);
foreach (IHasAccessList systemAccessList in systemAccessLists)
{
list.Add(systemAccessList.GetAccessList(block, spec));
}
return list;
}
void IThreadPoolWorkItem.Execute()
{
try
{
if (parallelOptions.CancellationToken.IsCancellationRequested) return;
WarmupAddresses(parallelOptions, Block);
}
catch (Exception ex)
{
PreWarmer._logger.DebugError("Error pre-warming addresses", ex);
}
finally
{
_doneEvent.Set();
}
}
private void WarmupAddresses(ParallelOptions parallelOptions, Block block)
{
if (parallelOptions.CancellationToken.IsCancellationRequested)
{
SystemTxAccessLists?.Dispose();
return;
}
ObjectPool<IReadOnlyTxProcessorSource> envPool = PreWarmer._envPool;
try
{
Address? beneficiary = block.Header.GasBeneficiary;
if (SystemTxAccessLists is not null || beneficiary is not null)
{
IReadOnlyTxProcessorSource env = envPool.Get();
try
{
using IReadOnlyTxProcessingScope scope = env.Build(parent);
WarmupSender(beneficiary, null, scope.WorldState);
if (SystemTxAccessLists is not null)
{
foreach (AccessList list in SystemTxAccessLists.AsSpan())
{
scope.WorldState.WarmUp(list);
}
}
}
finally
{
envPool.Return(env);
SystemTxAccessLists?.Dispose();
}
}
// BAL warmup is driven from BlockProcessor.HintBal; skip speculative warming here.
if (Bal is null)
{
WarmingState<Block> baseState = new(envPool, block, parent);
ParallelUnbalancedWork.For(
0,
block.Transactions.Length,
parallelOptions,
baseState.InitThreadState,
static (i, state) =>
{
Transaction tx = state.Payload.Transactions[i];
WarmupSender(tx.SenderAddress, tx.To, state.Scope!.WorldState);
return state;
},
WarmingState<Block>.FinallyAction);
}
}
catch (OperationCanceledException)
{
// Ignore, block completed cancel
}
}
private static void WarmupSender(Address? sender, Address? to, IWorldState worldState)
{
try
{
if (sender is not null)
{
worldState.WarmUp(sender);
}
if (to is not null)
{
worldState.WarmUp(to);
}
}
catch (MissingTrieNodeException)
{
}
}
}
private readonly struct WarmingState<TPayload>(ObjectPool<IReadOnlyTxProcessorSource> envPool, TPayload payload, BlockHeader parent) : IDisposable
{
public static Action<WarmingState<TPayload>> FinallyAction { get; } = DisposeThreadState;
private readonly ObjectPool<IReadOnlyTxProcessorSource> EnvPool = envPool;
private readonly IReadOnlyTxProcessorSource? Env;
public readonly TPayload Payload = payload;
public readonly IReadOnlyTxProcessingScope? Scope;
private WarmingState(ObjectPool<IReadOnlyTxProcessorSource> envPool, TPayload payload, BlockHeader parent, IReadOnlyTxProcessorSource env, IReadOnlyTxProcessingScope scope) : this(envPool, payload, parent)
{
Env = env;
Scope = scope;
}
public WarmingState<TPayload> InitThreadState()
{
IReadOnlyTxProcessorSource env = EnvPool.Get();
return new(EnvPool, Payload, parent, env, scope: env.Build(parent));
}
public void Dispose()
{
Scope?.Dispose();
if (Env is not null)
{
EnvPool.Return(Env);
}
}
private static void DisposeThreadState(WarmingState<TPayload> state) => state.Dispose();
}
/// <summary>
/// Pool policy for <see cref="IReadOnlyTxProcessorSource"/> envs used by the prewarmer.
/// </summary>
internal class ReadOnlyTxProcessingEnvPooledObjectPolicy(PrewarmerEnvFactory envFactory, PreBlockCaches _preBlockCaches) : IPooledObjectPolicy<IReadOnlyTxProcessorSource>
{
public IReadOnlyTxProcessorSource Create() => envFactory.Create(_preBlockCaches);
/// <remarks>
/// Always returns true — the env is valid for reuse. The pool that owns this policy
/// must call <see cref="IDisposable.Dispose"/> on any item it cannot retain; failing
/// to do so leaks resources held by the env for the lifetime of the process.
/// </remarks>
public bool Return(IReadOnlyTxProcessorSource obj) => true;
}
private record BlockState(BlockCachePreWarmer PreWarmer, Block Block, BlockHeader Parent, IReleaseSpec Spec, ISet<Hash256>? SpeculativelyWarmed = null);
private sealed record WarmMarker(Hash256 ParentHash, IReleaseSpec Spec, ISet<Hash256> WarmedTxHashes);
}