Skip to content

Commit 679e1f8

Browse files
asdacapclaude
andauthored
refactor(consensus): wire block preprocessor steps through DI (#12258)
* refactor(consensus): wire block preprocessor steps through DI Move IBlockPreprocessorStep wiring off the mutable NethermindApi.BlockPreprocessor composite and onto the OrderedComponents DI DSL, matching the earlier TxGossipPolicy (#10941) and IP2PCapabilityResolver (#12093) migrations: - Register RecoverSignatures (BlockProcessingModule), MergeProcessingRecoveryStep (BaseMergePluginModule, covering Merge/Optimism/Taiko/AuRaMerge) and AuthorRecoveryStep (CliqueModule) via AddFirst/AddLast, preserving execution order. - Drop the imperative api.BlockPreprocessor.AddFirst/AddLast calls, the BlockPreprocessor property on IApiWithBlockchain/NethermindApi, and the IBlockPreprocessorStep bridge in NethermindRunnerModule. Remove CompositeBlockPreprocessorStep entirely: it was a logic-free foreach adapter. Consumers (BlockchainProcessor, BlockchainProcessorFacade, MainProcessingContext) now inject the ordered IReadOnlyList<IBlockPreprocessorStep> directly, consistent with how BlockchainProcessor already takes IEnumerable<IBlockTracer>. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(consensus): address review feedback on preprocessor DI - MergePlugin: register MergeProcessingRecoveryStep via AddLast so RecoverSignatures stays ahead of it, restoring the exact pre-DI ordering [RecoverSignatures, Merge] (the previous AddFirst flipped it; harmless since the steps touch disjoint fields, but now behavior-identical to master). - BlockchainProcessor: rename recoverySteps -> preprocessorSteps and the private RecoverData helper -> Preprocess, now that the steps are generic pre-processors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent f0243f6 commit 679e1f8

22 files changed

Lines changed: 39 additions & 91 deletions

File tree

src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public interface IApiWithBlockchain : IApiWithStores
1818
(IApiWithStores GetFromApi, IApiWithBlockchain SetInApi) ForInit => (this, this);
1919
(IApiWithStores GetFromApi, IApiWithBlockchain SetInApi) ForBlockchain => (this, this);
2020

21-
CompositeBlockPreprocessorStep BlockPreprocessor { get; }
2221
IBlockProducer? BlockProducer { get; set; }
2322
IBlockProducerRunner BlockProducerRunner { get; set; }
2423

src/Nethermind/Nethermind.Api/NethermindApi.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ ILifetimeScope Context
5151
private Dependencies _dependencies = dependencies;
5252

5353
public IBlobTxStorage BlobTxStorage => Context.Resolve<IBlobTxStorage>();
54-
public CompositeBlockPreprocessorStep BlockPreprocessor { get; } = new();
5554
public IBlockProducer? BlockProducer { get; set; }
5655
public IBlockProducerRunner BlockProducerRunner { get; set; } = new NoBlockProducerRunner();
5756
public IBlockTree BlockTree => Context.Resolve<IBlockTree>();

src/Nethermind/Nethermind.Blockchain.Test/BlockchainProcessorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public ProcessingTestContext(bool startProcessor)
224224
.TestObject;
225225
_branchProcessor = new BranchProcessorMock(_logManager, _stateReader);
226226
_recoveryStep = new RecoveryStepMock(_logManager);
227-
_processor = new BlockchainProcessor(_blockTree, _branchProcessor, _recoveryStep, _stateReader, LimboLogs.Instance, BlockchainProcessor.Options.Default, Substitute.For<IProcessingStats>());
227+
_processor = new BlockchainProcessor(_blockTree, _branchProcessor, [_recoveryStep], _stateReader, LimboLogs.Instance, BlockchainProcessor.Options.Default, Substitute.For<IProcessingStats>());
228228
_resetEvent = new AutoResetEvent(false);
229229
_queueEmptyResetEvent = new AutoResetEvent(false);
230230

src/Nethermind/Nethermind.Blockchain.Test/ReorgTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ public void Setup()
117117
_blockchainProcessor = new BlockchainProcessor(
118118
_blockTree,
119119
branchProcessor,
120-
new RecoverSignatures(
120+
[new RecoverSignatures(
121121
ecdsa,
122122
specProvider,
123-
LimboLogs.Instance),
123+
LimboLogs.Instance)],
124124
stateReader,
125125
LimboLogs.Instance,
126126
BlockchainProcessor.Options.Default,

src/Nethermind/Nethermind.Clique.Test/CliqueBlockProducerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = f
7777

7878
.AddModule(new TestNethermindModule())
7979
.AddModule(new CliqueModule())
80-
.AddSingleton<IBlockPreprocessorStep, AuthorRecoveryStep>()
8180
.AddSingleton<IBlockValidator>(Always.Valid)
8281
.AddSingleton<ISpecProvider>(SepoliaSpecProvider.Instance)
8382
.AddSingleton<CliqueChainSpecEngineParameters>(new CliqueChainSpecEngineParameters()

src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using Nethermind.Api;
88
using Nethermind.Api.Extensions;
99
using Nethermind.Blockchain.Services;
10+
using Nethermind.Consensus.Processing;
1011
using Nethermind.Core;
12+
using Nethermind.Core.Container;
1113
using Nethermind.JsonRpc.Modules;
1214
using Nethermind.Specs.ChainSpecStyle;
1315

@@ -23,16 +25,7 @@ public class CliquePlugin(ChainSpec chainSpec) : IConsensusPlugin
2325

2426
public bool Enabled => chainSpec.SealEngineType == SealEngineType;
2527

26-
public Task Init(INethermindApi nethermindApi)
27-
{
28-
(IApiWithStores _, IApiWithBlockchain setInApi) = nethermindApi.ForInit;
29-
30-
ISnapshotManager snapshotManager = nethermindApi.Context.Resolve<ISnapshotManager>();
31-
32-
setInApi.BlockPreprocessor.AddLast(new AuthorRecoveryStep(snapshotManager));
33-
34-
return Task.CompletedTask;
35-
}
28+
public Task Init(INethermindApi nethermindApi) => Task.CompletedTask;
3629

3730
public string SealEngineType => Nethermind.Core.SealEngineType.Clique;
3831

@@ -59,6 +52,7 @@ protected override void Load(ContainerBuilder builder)
5952
})
6053

6154
.AddSingleton<ISnapshotManager, SnapshotManager>()
55+
.AddLast<IBlockPreprocessorStep, AuthorRecoveryStep>()
6256
.AddSingleton<ISealValidator, CliqueSealValidator>()
6357
.AddSingleton<ISealer, CliqueSealer>()
6458

src/Nethermind/Nethermind.Consensus/Processing/BlockchainProcessor.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public sealed class BlockchainProcessor : IBlockchainProcessor, IBlockProcessing
4040
public bool IsMainProcessor { get; init; }
4141

4242
private readonly IBranchProcessor _branchProcessor;
43-
private readonly IBlockPreprocessorStep _recoveryStep;
43+
private readonly IReadOnlyList<IBlockPreprocessorStep> _preprocessorSteps;
4444
private readonly IStateReader _stateReader;
4545
private readonly Options _options;
4646
private readonly IBlockTree _blockTree;
@@ -88,7 +88,7 @@ public sealed class BlockchainProcessor : IBlockchainProcessor, IBlockProcessing
8888
/// </summary>
8989
/// <param name="blockTree"></param>
9090
/// <param name="branchProcessor"></param>
91-
/// <param name="recoveryStep"></param>
91+
/// <param name="preprocessorSteps"></param>
9292
/// <param name="stateReader"></param>
9393
/// <param name="logManager"></param>
9494
/// <param name="options"></param>
@@ -97,7 +97,7 @@ public sealed class BlockchainProcessor : IBlockchainProcessor, IBlockProcessing
9797
public BlockchainProcessor(
9898
IBlockTree blockTree,
9999
IBranchProcessor branchProcessor,
100-
IBlockPreprocessorStep recoveryStep,
100+
IReadOnlyList<IBlockPreprocessorStep> preprocessorSteps,
101101
IStateReader stateReader,
102102
ILogManager logManager,
103103
Options options,
@@ -107,7 +107,7 @@ public BlockchainProcessor(
107107
_logger = logManager.GetClassLogger<BlockchainProcessor>();
108108
_blockTree = blockTree;
109109
_branchProcessor = branchProcessor;
110-
_recoveryStep = recoveryStep;
110+
_preprocessorSteps = preprocessorSteps;
111111
_stateReader = stateReader;
112112
_options = options;
113113

@@ -117,6 +117,14 @@ public BlockchainProcessor(
117117
if (blockTracers is not null) _compositeBlockTracer.AddRange(blockTracers);
118118
}
119119

120+
private void Preprocess(Block block)
121+
{
122+
for (int i = 0; i < _preprocessorSteps.Count; i++)
123+
{
124+
_preprocessorSteps[i].RecoverData(block);
125+
}
126+
}
127+
120128
private void OnNewProcessingStatistics(object? sender, BlockStatistics stats)
121129
=> NewProcessingStatistics?.Invoke(sender, stats);
122130

@@ -283,7 +291,7 @@ private async Task RunRecoveryLoop()
283291
{
284292
Interlocked.Add(ref _currentRecoveryQueueSize, -blockRef.Block!.Transactions.Length);
285293
if (_logger.IsTrace) _logger.Trace($"Recovering addresses for block {blockRef.BlockHash}.");
286-
_recoveryStep.RecoverData(blockRef.Block);
294+
Preprocess(blockRef.Block);
287295

288296
try
289297
{
@@ -679,7 +687,7 @@ private void PrepareBlocksToProcess(Block suggestedBlock, ProcessingOptions opti
679687
for (int i = 0; i < blocksToProcess.Count; i++)
680688
{
681689
/* this can happen if the block was loaded as an ancestor and did not go through the recovery queue */
682-
_recoveryStep.RecoverData(blocksToProcess[i]);
690+
Preprocess(blocksToProcess[i]);
683691
}
684692

685693
// Uncommon logging and throws

src/Nethermind/Nethermind.Consensus/Processing/CompositeBlockPreprocessorStep.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Nethermind/Nethermind.Consensus/Tracing/BlockchainProcessorFacade.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4+
using System.Collections.Generic;
45
using System.Threading;
56
using Nethermind.Consensus.Processing;
67
using Nethermind.Core;
@@ -18,12 +19,15 @@ namespace Nethermind.Consensus.Tracing;
1819
public sealed class BlockchainProcessorFacade(
1920
IBlockProcessor blockProcessor,
2021
ISpecProvider specProvider,
21-
CompositeBlockPreprocessorStep preprocessorStep
22+
IReadOnlyList<IBlockPreprocessorStep> preprocessorSteps
2223
)
2324
{
2425
public Block? Process(Block block, ProcessingOptions options, IBlockTracer tracer, CancellationToken token = default)
2526
{
26-
preprocessorStep.RecoverData(block);
27+
for (int i = 0; i < preprocessorSteps.Count; i++)
28+
{
29+
preprocessorSteps[i].RecoverData(block);
30+
}
2731

2832
IReleaseSpec spec = specProvider.GetSpec(block.Header);
2933

src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class TestBlockchain : IDisposable
6464
public IBlockProcessor BlockProcessor => _fromContainer.MainProcessingContext.BlockProcessor;
6565
public IBlockchainProcessor BlockchainProcessor => _fromContainer.MainProcessingContext.BlockchainProcessor;
6666
public IBlockProcessingQueue BlockProcessingQueue => _fromContainer.MainProcessingContext.BlockProcessingQueue;
67-
public IBlockPreprocessorStep BlockPreprocessorStep => _fromContainer.BlockPreprocessorStep;
67+
public IReadOnlyList<IBlockPreprocessorStep> BlockPreprocessorSteps => _fromContainer.BlockPreprocessorSteps;
6868

6969
public IBlockTree BlockTree => _fromContainer.BlockTree;
7070

@@ -132,7 +132,7 @@ public sealed class FromContainer(
132132
Lazy<IReceiptStorage> receiptStorage,
133133
Lazy<ITxPool> txPool,
134134
Lazy<IWorldStateManager> worldStateManager,
135-
Lazy<IBlockPreprocessorStep> blockPreprocessorStep,
135+
Lazy<IReadOnlyList<IBlockPreprocessorStep>> blockPreprocessorSteps,
136136
Lazy<IBlockTree> blockTree,
137137
Lazy<IBlockFinder> blockFinder,
138138
Lazy<ILogFinder> logFinder,
@@ -162,7 +162,7 @@ Lazy<IForkInfo> forkInfo
162162
public IReceiptStorage ReceiptStorage => receiptStorage.Value;
163163
public ITxPool TxPool => txPool.Value;
164164
public IWorldStateManager WorldStateManager => worldStateManager.Value;
165-
public IBlockPreprocessorStep BlockPreprocessorStep => blockPreprocessorStep.Value;
165+
public IReadOnlyList<IBlockPreprocessorStep> BlockPreprocessorSteps => blockPreprocessorSteps.Value;
166166
public IBlockTree BlockTree => blockTree.Value;
167167
public IBlockFinder BlockFinder => blockFinder.Value;
168168
public ILogFinder LogFinder => logFinder.Value;

0 commit comments

Comments
 (0)