Skip to content

Commit

Permalink
Fixed FinalTotalDifficulty based on genesis (#6435)
Browse files Browse the repository at this point in the history
* Fixed FinalTotalDifficulty based on genesis

* Fix whitespace

* Fix test setup
  • Loading branch information
MarekM25 committed Dec 29, 2023
1 parent f601ede commit 6a9c76d
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.Merge.Plugin.Synchronization;
using Nethermind.Serialization.Rlp;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using Nethermind.Synchronization.Blocks;
using Nethermind.Synchronization.Peers;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void Can_suggest_terminal_block_correctly()
.WithSpecProvider(specProvider)
.OfChainLength(10)
.TestObject;
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, new ChainSpec(), LimboLogs.Instance);

Block? block8 = tree.FindBlock(8, BlockTreeLookupOptions.None);
Assert.False(block8!.IsTerminalBlock(specProvider));
Expand All @@ -95,7 +96,7 @@ public void Suggest_terminal_block_with_lower_number_and_lower_total_difficulty(
.WithSpecProvider(specProvider)
.OfChainLength(10)
.TestObject;
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, new ChainSpec(), LimboLogs.Instance);

Block? block7 = tree.FindBlock(7, BlockTreeLookupOptions.None);
Block newTerminalBlock = Build.A.Block
Expand Down Expand Up @@ -124,7 +125,7 @@ public void Cannot_change_best_suggested_to_terminal_block_after_merge_block()
.OfChainLength(10)
.TestObject;

PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), tree, specProvider, new ChainSpec(), LimboLogs.Instance);

Block? block8 = tree.FindBlock(8, BlockTreeLookupOptions.None);
Assert.False(block8!.Header.IsTerminalBlock(specProvider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Core.Test.Builders;
using Nethermind.Core.Timers;
using Nethermind.Crypto;
using Nethermind.Db;
Expand All @@ -33,6 +34,7 @@
using Nethermind.Merge.Plugin.Handlers;
using Nethermind.Merge.Plugin.Synchronization;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using Nethermind.State;
using Nethermind.Synchronization.ParallelSync;
Expand Down Expand Up @@ -236,7 +238,7 @@ protected override IBlockProcessor CreateBlockProcessor()
protected IBlockValidator CreateBlockValidator()
{
IBlockCacheService blockCacheService = new BlockCacheService();
PoSSwitcher = new PoSSwitcher(MergeConfig, SyncConfig.Default, new MemDb(), BlockTree, SpecProvider, LogManager);
PoSSwitcher = new PoSSwitcher(MergeConfig, SyncConfig.Default, new MemDb(), BlockTree, SpecProvider, new ChainSpec() { Genesis = Core.Test.Builders.Build.A.Block.WithDifficulty(0).TestObject }, LogManager);
SealValidator = new MergeSealValidator(PoSSwitcher, Always.Valid);
HeaderValidator preMergeHeaderValidator = new HeaderValidator(BlockTree, SealValidator, SpecProvider, LogManager);
HeaderValidator = new MergeHeaderValidator(PoSSwitcher, preMergeHeaderValidator, BlockTree, SpecProvider, SealValidator, LogManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Nethermind.Logging;
using Nethermind.Merge.Plugin.Handlers;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -156,7 +157,7 @@ private static PoSSwitcher CreatePosSwitcher()
specProvider.TerminalTotalDifficulty = 2;
MergeConfig? mergeConfig = new() { };
IBlockCacheService blockCacheService = new BlockCacheService();
return new PoSSwitcher(mergeConfig, new SyncConfig(), db, blockTree, specProvider, LimboLogs.Instance);
return new PoSSwitcher(mergeConfig, new SyncConfig(), db, blockTree, specProvider, new ChainSpec(), LimboLogs.Instance);
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.Merge.Plugin.Test/PoSSwitcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Initial_TTD_should_be_null()
{
UInt256? expectedTtd = null;
IBlockTree blockTree = Substitute.For<IBlockTree>();
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), blockTree, TestSpecProvider.Instance, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), blockTree, TestSpecProvider.Instance, new ChainSpec(), LimboLogs.Instance);

Assert.That(poSSwitcher.TerminalTotalDifficulty, Is.EqualTo(expectedTtd));
}
Expand All @@ -45,7 +45,7 @@ public void Read_TTD_from_chainspec_if_not_specified_in_merge_config()
ChainSpec chainSpec = loader.Load(File.ReadAllText(path));

ChainSpecBasedSpecProvider specProvider = new(chainSpec);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), blockTree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig(), new SyncConfig(), new MemDb(), blockTree, specProvider, new ChainSpec(), LimboLogs.Instance);

Assert.That(poSSwitcher.TerminalTotalDifficulty, Is.EqualTo(expectedTtd));
Assert.That(specProvider.MergeBlockNumber?.BlockNumber, Is.EqualTo(101));
Expand Down Expand Up @@ -93,7 +93,7 @@ public void Override_TTD_and_number_from_merge_config()
IBlockTree blockTree = Substitute.For<IBlockTree>();
TestSpecProvider specProvider = new(London.Instance);
specProvider.UpdateMergeTransitionInfo(100, 20);
PoSSwitcher poSSwitcher = new(new MergeConfig() { TerminalTotalDifficulty = "340", TerminalBlockNumber = 2000 }, new SyncConfig(), new MemDb(), blockTree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig() { TerminalTotalDifficulty = "340", TerminalBlockNumber = 2000 }, new SyncConfig(), new MemDb(), blockTree, specProvider, new ChainSpec(), LimboLogs.Instance);

Assert.That(poSSwitcher.TerminalTotalDifficulty, Is.EqualTo(expectedTtd));
Assert.That(specProvider.MergeBlockNumber?.BlockNumber, Is.EqualTo(2001));
Expand All @@ -106,7 +106,7 @@ public void Can_update_merge_transition_info()
IBlockTree blockTree = Substitute.For<IBlockTree>();
TestSpecProvider specProvider = new(London.Instance);
specProvider.UpdateMergeTransitionInfo(2001, expectedTtd);
PoSSwitcher poSSwitcher = new(new MergeConfig() { }, new SyncConfig(), new MemDb(), blockTree, specProvider, LimboLogs.Instance);
PoSSwitcher poSSwitcher = new(new MergeConfig() { }, new SyncConfig(), new MemDb(), blockTree, specProvider, new ChainSpec(), LimboLogs.Instance);

Assert.That(poSSwitcher.TerminalTotalDifficulty, Is.EqualTo(expectedTtd));
Assert.That(specProvider.MergeBlockNumber?.BlockNumber, Is.EqualTo(2001));
Expand Down Expand Up @@ -254,7 +254,7 @@ private void AssertFinalTotalDifficulty(long ttd, long genesisDifficulty, long?
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);
PoSSwitcher poSSwitcher = new PoSSwitcher(new MergeConfig(), syncConfig, new MemDb(), blockTree, specProvider, new ChainSpec() { Genesis = genesisBlock }, LimboLogs.Instance);
if (expectedFinalTotalDifficulty != null)
poSSwitcher.FinalTotalDifficulty.Should().Be((UInt256)expectedFinalTotalDifficulty);
else
Expand All @@ -266,7 +266,7 @@ private static PoSSwitcher CreatePosSwitcher(IBlockTree blockTree, IDb? db = nul
{
db ??= new MemDb();
MergeConfig? mergeConfig = new() { };
return new PoSSwitcher(mergeConfig, new SyncConfig(), db, blockTree, specProvider ?? MainnetSpecProvider.Instance, LimboLogs.Instance);
return new PoSSwitcher(mergeConfig, new SyncConfig(), db, blockTree, specProvider ?? MainnetSpecProvider.Instance, new ChainSpec(), LimboLogs.Instance);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Nethermind.Merge.Plugin.InvalidChainTracker;
using Nethermind.Merge.Plugin.Synchronization;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State.Repositories;
using Nethermind.Synchronization;
using Nethermind.Synchronization.FastBlocks;
Expand Down Expand Up @@ -71,7 +72,7 @@ public IBeaconPivot BeaconPivot

private PoSSwitcher? _poSSwitcher;
public PoSSwitcher PoSSwitcher => _poSSwitcher ??= new(MergeConfig, SyncConfig, MetadataDb, BlockTree,
MainnetSpecProvider.Instance, LimboLogs.Instance);
MainnetSpecProvider.Instance, new ChainSpec(), LimboLogs.Instance);

private IInvalidChainTracker? _invalidChainTracker;

Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public virtual Task Init(INethermindApi nethermindApi)
_api.DbProvider.GetDb<IDb>(DbNames.Metadata),
_api.BlockTree,
_api.SpecProvider,
_api.ChainSpec,
_api.LogManager);
_invalidChainTracker = new InvalidChainTracker.InvalidChainTracker(
_poSSwitcher,
Expand Down
8 changes: 7 additions & 1 deletion src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.Serialization.Rlp;
using Nethermind.Specs.ChainSpecStyle;

namespace Nethermind.Merge.Plugin
{
Expand Down Expand Up @@ -39,6 +40,7 @@ public class PoSSwitcher : IPoSSwitcher
private readonly IDb _metadataDb;
private readonly IBlockTree _blockTree;
private readonly ISpecProvider _specProvider;
private readonly ChainSpec _chainSpec;
private readonly ILogger _logger;
private Hash256? _terminalBlockHash;

Expand All @@ -55,13 +57,15 @@ public class PoSSwitcher : IPoSSwitcher
IDb metadataDb,
IBlockTree blockTree,
ISpecProvider specProvider,
ChainSpec chainSpec,
ILogManager logManager)
{
_mergeConfig = mergeConfig;
_syncConfig = syncConfig;
_metadataDb = metadataDb;
_blockTree = blockTree;
_specProvider = specProvider;
_chainSpec = chainSpec;
_logger = logManager.GetClassLogger();

Initialize();
Expand Down Expand Up @@ -98,7 +102,9 @@ private void LoadFinalTotalDifficulty()
}
else
{
UInt256 genesisDifficulty = _blockTree.Genesis?.Difficulty ?? 0;
if (_chainSpec?.Genesis == null) return;

UInt256 genesisDifficulty = _chainSpec.Genesis.Difficulty;
if (genesisDifficulty >= TerminalTotalDifficulty) // networks with the merge in genesis
{
_finalTotalDifficulty = genesisDifficulty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.Merge.Plugin.Synchronization;
using Nethermind.Merge.Plugin.Test;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Stats;
using Nethermind.Stats.Model;
using Nethermind.Synchronization.Blocks;
Expand Down Expand Up @@ -456,6 +457,7 @@ public MergeConfig MergeConfig
MetadataDb,
BlockTree,
SpecProvider,
new ChainSpec(),
LimboLogs.Instance);

protected override IBetterPeerStrategy BetterPeerStrategy => _betterPeerStrategy ??=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Nethermind.Merge.Plugin.Handlers;
using Nethermind.Merge.Plugin.InvalidChainTracker;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using Nethermind.State;
using Nethermind.Stats.Model;
Expand Down Expand Up @@ -180,6 +181,7 @@ public void Terminal_block_with_lower_td_should_not_change_best_suggested_but_sh
new MemDb(),
localBlockTree,
testSpecProvider,
new ChainSpec(),
LimboLogs.Instance);
HeaderValidator headerValidator = new(
localBlockTree,
Expand Down Expand Up @@ -389,6 +391,7 @@ private Context CreateMergeContext(int blockTreeChainLength, UInt256 ttd)
new MemDb(),
localBlockTree,
testSpecProvider,
new ChainSpec(),
LimboLogs.Instance);
MergeSealEngine sealEngine = new(
new SealEngine(new NethDevSealEngine(), Always.Valid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ synchronizerType switch
{
mergeConfig.TerminalTotalDifficulty = UInt256.MaxValue.ToString(CultureInfo.InvariantCulture);
}
PoSSwitcher poSSwitcher = new(mergeConfig, syncConfig, dbProvider.MetadataDb, BlockTree, new TestSingleReleaseSpecProvider(Constantinople.Instance), _logManager);
PoSSwitcher poSSwitcher = new(mergeConfig, syncConfig, dbProvider.MetadataDb, BlockTree, new TestSingleReleaseSpecProvider(Constantinople.Instance), new ChainSpec(), _logManager);
IBeaconPivot beaconPivot = new BeaconPivot(syncConfig, dbProvider.MetadataDb, BlockTree, _logManager);

TrieStore trieStore = new(stateDb, LimboLogs.Instance);
Expand Down

0 comments on commit 6a9c76d

Please sign in to comment.