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

Zero total difficulty in FindBlock #5581

Merged
merged 2 commits into from
Apr 20, 2023
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
28 changes: 28 additions & 0 deletions src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,34 @@ public void Cannot_insert_genesis()
Assert.Throws<InvalidOperationException>(() => tree.Insert(new[] { genesis }));
}

[Test, Timeout(Timeout.MaxTestTime)]
public void Should_set_zero_total_difficulty()
{
MemDb blocksDb = new();
MemDb blockInfosDb = new();
MemDb headersDb = new();
MemDb metadataDb = new();

long pivotNumber = 0L;

SyncConfig syncConfig = new();
syncConfig.PivotNumber = pivotNumber.ToString();

CustomSpecProvider specProvider = new(((ForkActivation)0, London.Instance));
specProvider.UpdateMergeTransitionInfo(null, 0);

BlockTree tree = new(blocksDb, headersDb, blockInfosDb, metadataDb,
new ChainLevelInfoRepository(blockInfosDb), specProvider,
NullBloomStorage.Instance, syncConfig, LimboLogs.Instance);
Block genesis = Build.A.Block.WithDifficulty(0).TestObject;
tree.SuggestBlock(genesis).Should().Be(AddBlockResult.Added);
tree.FindBlock(genesis.Hash, BlockTreeLookupOptions.None)!.TotalDifficulty.Should().Be(UInt256.Zero);

Block A = Build.A.Block.WithParent(genesis).WithDifficulty(0).TestObject;
tree.SuggestBlock(A).Should().Be(AddBlockResult.Added);
tree.FindBlock(A.Hash, BlockTreeLookupOptions.None)!.TotalDifficulty.Should().Be(UInt256.Zero);
}

[Test, Timeout(Timeout.MaxTestTime)]
public void Can_batch_insert_blocks()
{
Expand Down
37 changes: 30 additions & 7 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,7 @@ public AddBlockResult SuggestBlock(Block block, BlockTreeSuggestOptions options
}
else
{
if (blockInfo.TotalDifficulty != UInt256.Zero || header.IsGenesis)
header.TotalDifficulty = blockInfo.TotalDifficulty;
SetTotalDifficultyFromBlockInfo(header, blockInfo);
}

if (requiresCanonical)
Expand Down Expand Up @@ -1834,8 +1833,7 @@ private bool ShouldCache(long number)
}
else
{
if (blockInfo.TotalDifficulty != UInt256.Zero || block.IsGenesis)
block.Header.TotalDifficulty = blockInfo.TotalDifficulty;
SetTotalDifficultyFromBlockInfo(block.Header, blockInfo);
}

if (requiresCanonical)
Expand All @@ -1854,6 +1852,33 @@ private bool ShouldCache(long number)
return block;
}

private bool IsTotalDifficultyAlwaysZero()
{
// In some Ethereum tests and possible testnets difficulty of all blocks might be zero
// We also checking TTD is zero to ensure that block after genesis have zero difficulty
return Genesis?.Difficulty == 0 && _specProvider.TerminalTotalDifficulty == 0;
}

private void SetTotalDifficultyFromBlockInfo(BlockHeader header, BlockInfo blockInfo)
{
if (header.IsGenesis)
{
header.TotalDifficulty = header.Difficulty;
return;
}

if (blockInfo.TotalDifficulty != UInt256.Zero)
{
header.TotalDifficulty = blockInfo.TotalDifficulty;
return;
}

if (IsTotalDifficultyAlwaysZero())
{
header.TotalDifficulty = 0;
}
}

private void SetTotalDifficulty(BlockHeader header)
{
if (header.IsGenesis)
Expand All @@ -1863,9 +1888,7 @@ private void SetTotalDifficulty(BlockHeader header)
return;
}

// In some Ethereum tests and possible testnets difficulty of all blocks might be zero
// We also checking TTD is zero to ensure that block after genesis have zero difficulty
if (Genesis!.Difficulty == 0 && _specProvider.TerminalTotalDifficulty == 0)
if (IsTotalDifficultyAlwaysZero())
{
header.TotalDifficulty = 0;
if (_logger.IsTrace) _logger.Trace($"Block {header} has zero total difficulty");
Expand Down