From 8c49f2758a585d68c273b7df2c4ab7d7ca7c9a54 Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Thu, 21 Dec 2023 20:32:14 +0100 Subject: [PATCH 1/3] Fix TotalDifficulty for post-merge devnets --- .../PoSSwitcherTests.cs | 40 +++++++++++++++++++ .../Nethermind.Merge.Plugin/PoSSwitcher.cs | 13 +++++- .../Nethermind.Runner/configs/holesky.cfg | 3 +- .../configs/holesky_archive.cfg | 3 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs index c76c2a59063..95251bf3676 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs @@ -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; @@ -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(); diff --git a/src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs b/src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs index 492bbc20c12..fe860873b6c 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs @@ -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) diff --git a/src/Nethermind/Nethermind.Runner/configs/holesky.cfg b/src/Nethermind/Nethermind.Runner/configs/holesky.cfg index b0409121834..dade4acc070 100644 --- a/src/Nethermind/Nethermind.Runner/configs/holesky.cfg +++ b/src/Nethermind/Nethermind.Runner/configs/holesky.cfg @@ -26,7 +26,6 @@ "EngineEnabledModules": "net,eth,subscribe,engine,web3,client" }, "Merge": { - "Enabled": true, - "FinalTotalDifficulty": "1" + "Enabled": true } } diff --git a/src/Nethermind/Nethermind.Runner/configs/holesky_archive.cfg b/src/Nethermind/Nethermind.Runner/configs/holesky_archive.cfg index bc7aa9030d3..eb2e77a02a9 100644 --- a/src/Nethermind/Nethermind.Runner/configs/holesky_archive.cfg +++ b/src/Nethermind/Nethermind.Runner/configs/holesky_archive.cfg @@ -27,7 +27,6 @@ "EngineEnabledModules": "net,eth,subscribe,engine,web3,client" }, "Merge": { - "Enabled": true, - "FinalTotalDifficulty": "1" + "Enabled": true } } From bae224dacd49eae48723b0961433fc47142b5ef6 Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Thu, 21 Dec 2023 20:43:31 +0100 Subject: [PATCH 2/3] Fix whitespaces --- src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs index 95251bf3676..9f2992c46ed 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs @@ -255,7 +255,7 @@ private void AssertFinalTotalDifficulty(long ttd, long genesisDifficulty, long? 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) + if (expectedFinalTotalDifficulty != null) poSSwitcher.FinalTotalDifficulty.Should().Be((UInt256)expectedFinalTotalDifficulty); else poSSwitcher.FinalTotalDifficulty.Should().BeNull(); From 3d19445b211cf24980fa02e4cb449c5dcdcbcaa1 Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Thu, 21 Dec 2023 21:52:58 +0100 Subject: [PATCH 3/3] test fixes --- .../EngineModuleTests.Synchronization.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs index 335b0707324..ab33e79b4f0 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs @@ -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; @@ -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; @@ -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); @@ -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