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

Fix TotalDifficulty for post-merge networks in genesis #6407

Merged
merged 3 commits into from
Dec 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public async Task forkChoiceUpdatedV1_unknown_block_initiates_syncing()
chain.BeaconSync.IsBeaconSyncHeadersFinished().Should().BeFalse();
chain.BeaconSync.IsBeaconSyncFinished(chain.BlockTree.FindBlock(block.Hash!)?.Header).Should().BeFalse();
AssertBeaconPivotValues(chain.BeaconPivot, block.Header);

block.Header.TotalDifficulty = 0;
pointers.LowestInsertedBeaconHeader = block.Header;
pointers.BestKnownBeaconBlock = block.Number;
pointers.LowestInsertedHeader = block.Header;
Expand Down Expand Up @@ -151,6 +153,7 @@ public async Task forkChoiceUpdatedV1_unknown_block_parent_while_syncing_initiat
chain.BeaconSync.IsBeaconSyncFinished(chain.BlockTree.FindBlock(block.Hash!)?.Header).Should().BeFalse();
AssertBeaconPivotValues(chain.BeaconPivot, block.Header);

block.Header.TotalDifficulty = 0;
pointers.LowestInsertedBeaconHeader = block.Header;
pointers.BestKnownBeaconBlock = block.Number;
pointers.LowestInsertedHeader = block.Header;
Expand All @@ -167,6 +170,7 @@ public async Task forkChoiceUpdatedV1_unknown_block_parent_while_syncing_initiat
chain.BeaconSync.IsBeaconSyncFinished(chain.BlockTree.FindBlock(nextUnconnectedBlock.Hash!)?.Header).Should().BeFalse();
AssertBeaconPivotValues(chain.BeaconPivot, nextUnconnectedBlock.Header);

nextUnconnectedBlock.Header.TotalDifficulty = 0;
pointers.LowestInsertedBeaconHeader = nextUnconnectedBlock.Header;
pointers.BestKnownBeaconBlock = nextUnconnectedBlock.Number;
AssertBlockTreePointers(chain.BlockTree, pointers);
Expand Down Expand Up @@ -967,7 +971,7 @@ private void AssertBeaconPivotValues(IBeaconPivot beaconPivot, BlockHeader block
beaconPivot.BeaconPivotExists().Should().BeTrue();
beaconPivot.PivotNumber.Should().Be(blockHeader.Number);
beaconPivot.PivotHash.Should().Be(blockHeader.Hash ?? blockHeader.CalculateHash());
beaconPivot.PivotTotalDifficulty.Should().Be(null);
beaconPivot.PivotTotalDifficulty.Should().Be((UInt256)0);
}

private class BlockTreePointers
Expand Down
40 changes: 40 additions & 0 deletions src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.IO;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;
Expand Down Expand Up @@ -222,6 +223,45 @@ public void Can_load_parameters_after_the_restart()
Assert.That(newPoSSwitcher.HasEverReachedTerminalBlock(), Is.EqualTo(true));
}

[Test]
public void No_final_difficulty_if_conditions_are_not_met()
{
AssertFinalTotalDifficulty(10005, 10000, 10000, null);
}

[TestCase(0, 1)]
[TestCase(0, 0)]
[TestCase(5000, 6000)]
public void Can_set_final_total_difficulty_for_post_merge_networks(long ttd, long genesisDifficulty)
{
AssertFinalTotalDifficulty(ttd, genesisDifficulty, null, genesisDifficulty);
}

[TestCase(0, 1)]
[TestCase(0, 0)]
[TestCase(5000, 6000)]
public void Can_set_final_total_difficulty_based_on_sync_pivot(long ttd, long pivotTotalDifficulty)
{
AssertFinalTotalDifficulty(ttd, 0, pivotTotalDifficulty, pivotTotalDifficulty);
}

private void AssertFinalTotalDifficulty(long ttd, long genesisDifficulty, long? pivotTotalDifficulty, long? expectedFinalTotalDifficulty)
{
TestSpecProvider specProvider = new(London.Instance);
specProvider.TerminalTotalDifficulty = (UInt256)ttd;
Block genesisBlock = Build.A.Block.WithNumber(0).WithDifficulty((UInt256)genesisDifficulty).TestObject;
BlockTree blockTree = Build.A.BlockTree(genesisBlock, specProvider).OfChainLength(4).TestObject;
SyncConfig syncConfig = new();
if (pivotTotalDifficulty != null)
syncConfig = new SyncConfig() { PivotTotalDifficulty = $"{(UInt256)pivotTotalDifficulty}" };
PoSSwitcher poSSwitcher = new PoSSwitcher(new MergeConfig(), syncConfig, new MemDb(), blockTree, specProvider, LimboLogs.Instance);
if (expectedFinalTotalDifficulty != null)
poSSwitcher.FinalTotalDifficulty.Should().Be((UInt256)expectedFinalTotalDifficulty);
else
poSSwitcher.FinalTotalDifficulty.Should().BeNull();
}


private static PoSSwitcher CreatePosSwitcher(IBlockTree blockTree, IDb? db = null, ISpecProvider? specProvider = null)
{
db ??= new MemDb();
Expand Down
13 changes: 12 additions & 1 deletion src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ private void LoadFinalTotalDifficulty()
{
_finalTotalDifficulty = _mergeConfig.FinalTotalDifficultyParsed;

if (TerminalTotalDifficulty is null)
return;

// pivot post TTD, so we know FinalTotalDifficulty
if (_syncConfig.PivotTotalDifficultyParsed != 0 && TerminalTotalDifficulty is not null && _syncConfig.PivotTotalDifficultyParsed >= TerminalTotalDifficulty)
if (_syncConfig.PivotTotalDifficultyParsed != 0 && _syncConfig.PivotTotalDifficultyParsed >= TerminalTotalDifficulty)
{
_finalTotalDifficulty = _syncConfig.PivotTotalDifficultyParsed;
}
else
{
UInt256 genesisDifficulty = _blockTree.Genesis?.Difficulty ?? 0;
if (genesisDifficulty >= TerminalTotalDifficulty) // networks with the merge in genesis
{
_finalTotalDifficulty = genesisDifficulty;
}
}
}

private void CheckIfTerminalBlockReached(object? sender, BlockEventArgs e)
Expand Down
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Runner/configs/holesky.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"EngineEnabledModules": "net,eth,subscribe,engine,web3,client"
},
"Merge": {
"Enabled": true,
"FinalTotalDifficulty": "1"
"Enabled": true
}
}
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Runner/configs/holesky_archive.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"EngineEnabledModules": "net,eth,subscribe,engine,web3,client"
},
"Merge": {
"Enabled": true,
"FinalTotalDifficulty": "1"
"Enabled": true
}
}