From c81d577cadd8b3407e4a00c84e4401c33329209c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Moraczy=C5=84ski?= Date: Thu, 28 Dec 2023 14:49:34 +0100 Subject: [PATCH] Fix TotalDifficulty for post-merge networks in genesis (#6407) * Fix TotalDifficulty for post-merge devnets * Fix whitespaces * test fixes --- .../EngineModuleTests.Synchronization.cs | 6 ++- .../PoSSwitcherTests.cs | 40 +++++++++++++++++++ .../Nethermind.Merge.Plugin/PoSSwitcher.cs | 13 +++++- .../Nethermind.Runner/configs/holesky.cfg | 3 +- .../configs/holesky_archive.cfg | 3 +- 5 files changed, 59 insertions(+), 6 deletions(-) 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 diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs index c76c2a59063..9f2992c46ed 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 0f6db0d5193..819128ed83c 100644 --- a/src/Nethermind/Nethermind.Runner/configs/holesky.cfg +++ b/src/Nethermind/Nethermind.Runner/configs/holesky.cfg @@ -27,7 +27,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 } }