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

Merge/fix orphaned blocks #4226

Merged
merged 13 commits into from
Jul 1, 2022
18 changes: 14 additions & 4 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ public AddBlockResult SuggestBlock(Block block, BlockTreeSuggestOptions options

bool totalDifficultyNeeded = (options & BlockTreeLookupOptions.TotalDifficultyNotNeeded) ==
BlockTreeLookupOptions.None;
bool calculateTotalDifficulty = (options & BlockTreeLookupOptions.DoNotCalculateTotalDifficulty) ==
BlockTreeLookupOptions.None;
bool requiresCanonical = (options & BlockTreeLookupOptions.RequireCanonical) ==
BlockTreeLookupOptions.RequireCanonical;

Expand All @@ -874,8 +876,11 @@ public AddBlockResult SuggestBlock(Block block, BlockTreeSuggestOptions options
if (_logger.IsTrace)
_logger.Trace(
$"Entering missing block info in {nameof(FindHeader)} scope when head is {Head?.ToString(Block.Format.Short)}");
SetTotalDifficulty(header);
blockInfo = new BlockInfo(header.Hash, header.TotalDifficulty!.Value);
if (calculateTotalDifficulty)
{
SetTotalDifficulty(header);
}
blockInfo = new BlockInfo(header.Hash, header.TotalDifficulty ?? UInt256.Zero);
level = UpdateOrCreateLevel(header.Number, header.Hash, blockInfo);
}
else
Expand Down Expand Up @@ -1850,6 +1855,8 @@ private bool ShouldCache(long number)

bool totalDifficultyNeeded = (options & BlockTreeLookupOptions.TotalDifficultyNotNeeded) ==
BlockTreeLookupOptions.None;
bool calculateTotalDifficulty = (options & BlockTreeLookupOptions.DoNotCalculateTotalDifficulty) ==
BlockTreeLookupOptions.None;
bool requiresCanonical = (options & BlockTreeLookupOptions.RequireCanonical) ==
BlockTreeLookupOptions.RequireCanonical;

Expand All @@ -1863,8 +1870,11 @@ private bool ShouldCache(long number)
if (_logger.IsTrace)
_logger.Trace(
$"Entering missing block info in {nameof(FindBlock)} scope when head is {Head?.ToString(Block.Format.Short)}");
SetTotalDifficulty(block.Header);
blockInfo = new BlockInfo(block.Hash, block.TotalDifficulty!.Value);
if (calculateTotalDifficulty)
{
SetTotalDifficulty(block.Header);
}
blockInfo = new BlockInfo(block.Hash, block.TotalDifficulty ?? UInt256.Zero);
level = UpdateOrCreateLevel(block.Number, block.Hash, blockInfo);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum BlockTreeLookupOptions
None = 0,
TotalDifficultyNotNeeded = 1,
RequireCanonical = 2,
All = 3
DoNotCalculateTotalDifficulty = 4,
All = 7
}

[Flags]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private bool IsInconsistent(Keccak blockHash)

private Block? GetBlock(Keccak headBlockHash)
{
Block? block = _blockTree.FindBlock(headBlockHash, BlockTreeLookupOptions.None);
Block? block = _blockTree.FindBlock(headBlockHash, BlockTreeLookupOptions.DoNotCalculateTotalDifficulty);
if (block is null)
{
if (_logger.IsInfo) _logger.Info($"Syncing... Block {headBlockHash} not found.");
Expand Down Expand Up @@ -280,7 +280,7 @@ private bool IsInconsistent(Keccak blockHash)
return null;
}

BlockHeader? blockHeader = _blockTree.FindHeader(blockHash, BlockTreeLookupOptions.None);
BlockHeader? blockHeader = _blockTree.FindHeader(blockHash, BlockTreeLookupOptions.DoNotCalculateTotalDifficulty);
if (blockHeader is null)
{
errorMessage = $"Block {blockHash} not found.";
Expand All @@ -297,7 +297,7 @@ private bool TryGetBranch(Block newHeadBlock, out Block[] blocks)

while (true)
{
predecessor = _blockTree.FindParent(predecessor, BlockTreeLookupOptions.None);
predecessor = _blockTree.FindParent(predecessor, BlockTreeLookupOptions.DoNotCalculateTotalDifficulty);
if (predecessor == null)
{
blocks = Array.Empty<Block>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task<ResultWrapper<PayloadStatusV1>> HandleAsync(ExecutionPayloadV1

block.Header.TotalDifficulty = _poSSwitcher.FinalTotalDifficulty;

BlockHeader? parentHeader = _blockTree.FindHeader(request.ParentHash, BlockTreeLookupOptions.None);
BlockHeader? parentHeader = _blockTree.FindHeader(request.ParentHash, BlockTreeLookupOptions.DoNotCalculateTotalDifficulty);
if (parentHeader is null)
{
// possible that headers sync finished before this was called, so blocks in cache weren't inserted
Expand Down
28 changes: 14 additions & 14 deletions src/Nethermind/Nethermind.Runner/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
<logger name="JsonWebAPI*" minlevel="Error" writeTo="file-async" />
<logger name="JsonWebAPI*" minlevel="Error" writeTo="auto-colored-console-async" final="true" />
<logger name="JsonWebAPI*" final="true" />
<!-- <logger name="Merge.Plugin.PoSSwitcher" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Merge.Plugin.PoSSwitcher" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Merge.Plugin.PoSSwitcher" final="true"/> -->
<!-- -->
<!-- <logger name="Merge.Plugin.Synchronization.*" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Merge.Plugin.Synchronization.*" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Merge.Plugin.Synchronization.*" final="true"/> -->

<!-- <logger name="Merge.Plugin.PoSSwitcher" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Merge.Plugin.PoSSwitcher" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Merge.Plugin.PoSSwitcher" final="true"/> -->
<!-- -->
<!-- <logger name="Merge.Plugin.Synchronization.*" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Merge.Plugin.Synchronization.*" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Merge.Plugin.Synchronization.*" final="true"/> -->

<!-- <logger name="Synchronization.*" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Synchronization.*" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
Expand Down Expand Up @@ -93,17 +93,17 @@
<!-- <logger name="Synchronization.Blocks.BlockDownloader" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Synchronization.Blocks.BlockDownloader" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Synchronization.Blocks.BlockDownloader" final="true"/> -->
<!-- <logger name="Consensus.Processing.BlockchainProcessor" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Consensus.Processing.BlockchainProcessor" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Consensus.Processing.BlockchainProcessor" final="true"/> -->

<!-- <logger name="Consensus.Processing.BlockchainProcessor" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Consensus.Processing.BlockchainProcessor" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Consensus.Processing.BlockchainProcessor" final="true"/> -->

<!-- if sync get stuck this is the best thing to enable the Trace on -->
<!-- <logger name="Synchronization.ParallelSync.MultiSyncModeSelector" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Synchronization.ParallelSync.MultiSyncModeSelector" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Synchronization.ParallelSync.MultiSyncModeSelector" final="true"/> -->
<!-- if sync get stuck this is the best thing to enable the Trace on -->

<!-- if sync get stuck this is the best thing to enable the Trace on -->
<!-- <logger name="Synchronization.SyncServer" minlevel="Trace" writeTo="file-async"/> -->
<!-- <logger name="Synchronization.SyncServer" minlevel="Trace" writeTo="auto-colored-console-async"/> -->
<!-- <logger name="Synchronization.SyncServer" final="true"/> -->
Expand Down