Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup/Deduplicate blockdownloader code #8147

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ public class MergeSynchronizerModule : Module
protected override void Load(ContainerBuilder builder)
{
builder
.RegisterType<MergeBlockDownloader>()
.As<BlockDownloader>()
.As<ISyncDownloader<BlocksRequest>>()
.InstancePerLifetimeScope();

builder
.Add<IPosTransitionHook, PosTransitionHook>()
.AddDecorator<BlockDownloader, MergeBlockDownloader>()
.AddSingleton<ISynchronizer, MergeSynchronizer>()
.AddSingleton<IChainLevelHelper, ChainLevelHelper>()
.AddScoped<IPeerAllocationStrategyFactory<BlocksRequest>, MergeBlocksSyncPeerAllocationStrategyFactory>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Linq;
using Nethermind.Blockchain;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Core.Extensions;
using Nethermind.Logging;
using Nethermind.Synchronization.Blocks;
using Nethermind.Synchronization.Peers;

namespace Nethermind.Merge.Plugin.Synchronization;

public class PosTransitionHook(IBlockTree blockTree, IPoSSwitcher poSSwitcher, ILogManager logManager) : IPosTransitionHook
{
private readonly ILogger _logger = logManager.GetClassLogger<PosTransitionHook>();

public void TryUpdateTerminalBlock(BlockHeader currentHeader, bool shouldProcess)
{
if (shouldProcess == false) // if we're processing the block we will find TerminalBlock after processing
poSSwitcher.TryUpdateTerminalBlock(currentHeader);
}

public bool ImprovementRequirementSatisfied(PeerInfo? bestPeer)
{
return bestPeer!.TotalDifficulty > (blockTree.BestSuggestedHeader?.TotalDifficulty ?? 0) &&
poSSwitcher.HasEverReachedTerminalBlock() == false;
}

public IOwnedReadOnlyList<BlockHeader> FilterPosHeader(IOwnedReadOnlyList<BlockHeader> response)
{
// Override PoW's RequestHeaders so that it won't request beyond PoW.
// This fixes `Incremental Sync` hive test.
if (response.Count > 0)
{
BlockHeader lastBlockHeader = response[^1];
bool lastBlockIsPostMerge = poSSwitcher.GetBlockConsensusInfo(response[^1]).IsPostMerge;
if (lastBlockIsPostMerge) // Initial check to prevent creating new array every time
{
response = response
.TakeWhile(header => !poSSwitcher.GetBlockConsensusInfo(header).IsPostMerge)
.ToPooledList(response.Count);
if (_logger.IsInfo) _logger.Info($"Last block is post merge. {lastBlockHeader.Hash}. Trimming to {response.Count} sized batch.");
}
}
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Nethermind.Synchronization.Blocks;
using Nethermind.Synchronization.Peers;
using Nethermind.Synchronization.Peers.AllocationStrategies;
using Nethermind.TxPool;
using NSubstitute;
using NSubstitute.ClearExtensions;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public BlockDownloadContext(ISpecProvider specProvider, PeerInfo syncPeer, IRead

public List<Hash256> NonEmptyBlockHashes { get; }

public bool DownloadReceipts => _downloadReceipts;

public IReadOnlyList<Hash256> GetHashesByOffset(int offset, int maxLength)
{
var hashesToRequest =
Expand Down
375 changes: 238 additions & 137 deletions src/Nethermind/Nethermind.Synchronization/Blocks/BlockDownloader.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Synchronization.Peers;

namespace Nethermind.Synchronization.Blocks;

public interface IPosTransitionHook
{
void TryUpdateTerminalBlock(BlockHeader currentHeader, bool shouldProcess);
bool ImprovementRequirementSatisfied(PeerInfo? peerInfo);
IOwnedReadOnlyList<BlockHeader> FilterPosHeader(IOwnedReadOnlyList<BlockHeader> headers);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Synchronization.Peers;

namespace Nethermind.Synchronization.Blocks;

public class NoPosTransition(IBlockTree blockTree) : IPosTransitionHook
{
public void TryUpdateTerminalBlock(BlockHeader currentHeader, bool shouldProcess)
{
}

public bool ImprovementRequirementSatisfied(PeerInfo? bestPeer)
{
return bestPeer!.TotalDifficulty > (blockTree.BestSuggestedHeader?.TotalDifficulty ?? 0);
}

public IOwnedReadOnlyList<BlockHeader> FilterPosHeader(IOwnedReadOnlyList<BlockHeader> headers)
{
return headers;
}
}
8 changes: 7 additions & 1 deletion src/Nethermind/Nethermind.Synchronization/Synchronizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,13 @@ protected override void Load(ContainerBuilder builder)

// For blocks. There are two block scope, Fast and Full
.AddScoped<SyncFeedComponent<BlocksRequest>>()
.AddScoped<ISyncDownloader<BlocksRequest>, BlockDownloader>()

// The direct implementation is decorated by merge plugin (not the interface)
// so its declared on its own and other use is binded.
.AddScoped<BlockDownloader>()
.Add<IPosTransitionHook, NoPosTransition>()
.Bind<ISyncDownloader<BlocksRequest>, BlockDownloader>()

.AddScoped<IPeerAllocationStrategyFactory<BlocksRequest>, BlocksSyncPeerAllocationStrategyFactory>()
.AddScoped<SyncDispatcher<BlocksRequest>>()

Expand Down