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

Bugfix/request receipts #6413

Merged
merged 8 commits into from
Mar 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ bool HasMoreToSync(out BlockHeader[]? headers, out int headersToRequest)
break;
}

long bestProcessedBlock = 0;

for (int blockIndex = 0; blockIndex < blocks.Length; blockIndex++)
{
if (cancellation.IsCancellationRequested)
Expand All @@ -225,18 +227,23 @@ bool HasMoreToSync(out BlockHeader[]? headers, out int headersToRequest)

if (shouldProcess)
{
// covering edge case during fastSyncTransition when we're trying to SuggestBlock without the state
// An edge case when we've got state already, but still downloading blocks before it.
// We cannot process such blocks, but still we are requested to process them via blocksRequest.Options
// So we'are detecting it and chaing from processing to receipts downloading
bool headIsGenesis = _blockTree.Head?.IsGenesis ?? false;
bool toBeProcessedIsNotBlockOne = currentBlock.Number > 1;
bool isFastSyncTransition = headIsGenesis && toBeProcessedIsNotBlockOne;
bool toBeProcessedHasNoProcessedParent = currentBlock.Number > (bestProcessedBlock + 1);
bool isFastSyncTransition = headIsGenesis && toBeProcessedHasNoProcessedParent;
if (isFastSyncTransition)
{
long bestFullState = _fullStateFinder.FindBestFullState();
shouldProcess = currentBlock.Number > bestFullState && bestFullState != 0;
if (!shouldProcess)
if (!shouldProcess && !downloadReceipts)
{
if (_logger.IsInfo) _logger.Info($"Skipping processing during fastSyncTransition, currentBlock: {currentBlock}, bestFullState: {bestFullState}");
if (_logger.IsInfo) _logger.Info($"Skipping processing during fastSyncTransition, currentBlock: {currentBlock}, bestFullState: {bestFullState}, trying to load receipts");
downloadReceipts = true;
context.SetDownloadReceipts();
await RequestReceipts(bestPeer, cancellation, context);
receipts = context.ReceiptsForBlocks;
}
}
}
Expand Down Expand Up @@ -273,6 +280,10 @@ bool HasMoreToSync(out BlockHeader[]? headers, out int headersToRequest)
{
_blockTree.UpdateMainChain(new[] { currentBlock }, false);
}
else
{
bestProcessedBlock = currentBlock.Number;
}

TryUpdateTerminalBlock(currentBlock.Header, shouldProcess);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,33 @@ public async Task BlockDownloader_works_correctly_with_withdrawals(int options)
await action.Should().NotThrowAsync();
}

[TestCase(2)]
[TestCase(6)]
[TestCase(34)]
[TestCase(129)]
[TestCase(1024)]
public void BlockDownloader_does_not_stop_processing_when_main_chain_is_unknown(long pivot)
{
DownloaderOptions downloaderOptions = DownloaderOptions.Process;
BlockTreeTests.BlockTreeTestScenario.ScenarioBuilder blockTrees = BlockTreeTests.BlockTreeTestScenario
.GoesLikeThis()
.WithBlockTrees(1, (int)(pivot + 1), false, 0)
.InsertBeaconPivot(pivot)
.InsertBeaconHeaders(1, pivot)
.InsertBeaconBlocks(pivot, pivot, BlockTreeTests.BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Null);

PostMergeContext ctx = new()
{
BlockTreeScenario = blockTrees,
MergeConfig = new MergeConfig { TerminalTotalDifficulty = "0" }
};
ctx.BeaconPivot.EnsurePivot(blockTrees.SyncedTree.FindHeader(pivot, BlockTreeLookupOptions.None));
ctx.BeaconPivot.ProcessDestination = blockTrees.SyncedTree.FindHeader(pivot, BlockTreeLookupOptions.None);

SyncPeerMock syncPeer = new(blockTrees.SyncedTree, true, Response.AllCorrect | Response.WithTransactions, 0);
Assert.DoesNotThrowAsync(() => ctx.BlockDownloader.DownloadBlocks(new(syncPeer), new BlocksRequest(downloaderOptions), CancellationToken.None));
}

class PostMergeContext : Context
{
protected override ISpecProvider SpecProvider => _specProvider ??= new MainnetSpecProvider(); // PoSSwitcher changes TTD, so can't use MainnetSpecProvider.Instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@
using Nethermind.Network;
using Nethermind.Specs;
using Nethermind.State.Proofs;
using Nethermind.State.Repositories;
using Nethermind.Stats.Model;
using Nethermind.Db.Blooms;
using Nethermind.Network.P2P.Subprotocols.Eth.V62.Messages;
using Nethermind.Network.P2P.Subprotocols.Eth.V63.Messages;
using Nethermind.Serialization.Rlp;
using Nethermind.Synchronization.Blocks;
using Nethermind.Synchronization.ParallelSync;
using Nethermind.Synchronization.Peers;
using Nethermind.Synchronization.Reporting;
using Nethermind.Synchronization.SnapSync;
using Nethermind.Trie.Pruning;
using NSubstitute;
using NUnit.Framework;
using BlockTree = Nethermind.Blockchain.BlockTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BlockDownloadContext
private readonly Dictionary<int, int> _indexMapping;
private readonly ISpecProvider _specProvider;
private readonly PeerInfo _syncPeer;
private readonly bool _downloadReceipts;
private bool _downloadReceipts;
private readonly IReceiptsRecovery _receiptsRecovery;

public BlockDownloadContext(ISpecProvider specProvider, PeerInfo syncPeer, IReadOnlyList<BlockHeader?> headers,
Expand Down Expand Up @@ -66,7 +66,7 @@ public BlockDownloadContext(ISpecProvider specProvider, PeerInfo syncPeer, IRead

public Block[] Blocks { get; }

public TxReceipt[]?[]? ReceiptsForBlocks { get; }
public TxReceipt[]?[]? ReceiptsForBlocks { get; private set; }

public List<Hash256> NonEmptyBlockHashes { get; }

Expand Down Expand Up @@ -133,5 +133,14 @@ private void ValidateReceipts(Block block, TxReceipt[] blockReceipts)
throw new EthSyncException($"Wrong receipts root for downloaded block {block.ToString(Block.Format.Short)}.");
}
}

public void SetDownloadReceipts()
{
if (!_downloadReceipts)
{
_downloadReceipts = true;
ReceiptsForBlocks = new TxReceipt[Blocks.Length][];
}
}
}
}
Loading