refactor(consensus): move IBlockProductionPolicy to DI#12099
Conversation
Register IBlockProductionPolicy in BlockProcessingModule and apply the merge wrap via AddDecorator in MergePluginModule and AuRaMergeModule, instead of mutating the api field in MergePlugin.InitNetworkProtocol. Optimism and Taiko register their AlwaysStart/NeverStart policies in their own modules, dropping the InitializeBlockchain.CreateBlockProductionPolicy override. The mutable IApiWithBlockchain.BlockProductionPolicy property is removed; InitializeBlockProducer and StartBlockProducer inject the policy instead. Also drops a duplicate MergeBlockProductionPolicy decorator in TestMergeModule that previously double-wrapped the policy. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Claude finished @asdacap's task in 6m 21s —— View job Code Review
Overall this is a well-executed refactor. The design is sound and behaviour-preserving. DI wiring correctness
The comment in
|
| // Block production related. | ||
| .AddDecorator<IBlockProductionPolicy, MergeBlockProductionPolicy>() | ||
| .AddScoped<PostMergeBlockProducerFactory>() | ||
| .AddDecorator<IBlockProducerFactory, MergeBlockProducerFactory>() |
There was a problem hiding this comment.
Medium – pre-existing double-decoration of IBlockProducerFactory
This PR correctly removes the two duplicate AddDecorator<IBlockProductionPolicy, MergeBlockProductionPolicy>() lines, but there's still a double-decoration for IBlockProducerFactory:
MergePluginModule(loaded above via.AddModule(new MergePluginModule())) already registers.AddDecorator<IBlockProducerFactory, MergeBlockProducerFactory>().- This line adds it again.
Result in Autofac: MergeBlockProducerFactory(MergeBlockProducerFactory(base)) — the outer factory's baseBlockProducerFactory is another MergeBlockProducerFactory, so InitBlockProducer() produces a MergeBlockProducer whose pre-merge producer is itself a MergeBlockProducer. This isn't caught by Is.InstanceOf<MergeBlockProducer>() but is wrong behaviour.
This was pre-existing before this PR; this review just flags it since the PR is already tidying up similar duplicate decorators. Consider dropping this line (the one from MergePluginModule is sufficient), or filing a follow-up issue.
| new PostMergeBlockProducerFactory(specProvider, sealEngine, timestamper, blocksConfig, logManager)) | ||
| .AddDecorator<IBlockProducerFactory, MergeBlockProducerFactory>() | ||
| .AddDecorator<IBlockProducerRunnerFactory, MergeBlockProducerRunnerFactory>() | ||
| // Wraps the base policy. Kept here (and in AuRaMergeModule) rather than BaseMergePluginModule |
- Drop the explanatory comment on the IBlockProductionPolicy decorator (per @flcl42). - Remove the duplicate IBlockProducerFactory decorator in TestMergeModule, which double-wrapped the factory already provided by MergePluginModule (per @claude[bot]). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…-protocol # Conflicts: # src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs # src/Nethermind/Nethermind.Api/NethermindApi.cs
Clears IDE0005 flagged by the code-lint CI after merging master: - InitializeBlockchain.cs: using Nethermind.Consensus - TestMergeModule.cs: using Nethermind.Consensus.Producers Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…-protocol # Conflicts: # src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Changes
IBlockProductionPolicyin DI (BlockProcessingModule) and apply the post-merge wrap viaAddDecorator<IBlockProductionPolicy, MergeBlockProductionPolicy>()inMergePluginModuleandAuRaMergeModule, instead of mutating_api.BlockProductionPolicyinsideMergePlugin.InitNetworkProtocol.AlwaysStartBlockProductionPolicy/NeverStartBlockProductionPolicyin their own modules, dropping theInitializeBlockchain.CreateBlockProductionPolicyvirtual and its overrides.IApiWithBlockchain.BlockProductionPolicyproperty (a DI registration plus a settable api field is rejected byFallbackToFieldFromApi);InitializeBlockProducerandStartBlockProducernow injectIBlockProductionPolicy.MergePlugin.InitNetworkProtocolnow only handles network capabilities and merge finalization.MergeBlockProductionPolicydecorator inTestMergeModulethat previously double-wrapped the policy.Types of changes
What types of changes does your code introduce?
Testing
Requires testing
If yes, did you write tests?
Notes on testing
Behaviour-preserving refactor covered by the existing merge/Optimism/Taiko suites. Extended
MergePluginTests.Initializes_correctlyto assert the resolvedIBlockProductionPolicyisMergeBlockProductionPolicy. The full solution builds clean (0 warnings, 0 errors);Nethermind.Api.Test,Nethermind.Optimism.Test,Nethermind.Taiko.Test,Nethermind.Merge.Plugin.TestandNethermind.Merge.AuRa.Testpass — the only failures observed were pre-existing onmaster(Runner.TestArena_order_is_defaulttest-state pollution) or contention-only block-improvement timing tests that pass in isolation.Documentation
Requires documentation update
Requires explanation in Release Notes
🤖 Generated with Claude Code