Skip to content

Commit

Permalink
fix terminal block checking (#4318)
Browse files Browse the repository at this point in the history
* fix terminal block checking

* add test

* fix + tests
  • Loading branch information
MarekM25 authored Jul 27, 2022
1 parent cb44613 commit c6b8a24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
31 changes: 12 additions & 19 deletions src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ public void ForkchoiceUpdated(BlockHeader newHeadHash, Keccak finalizedHash)
_logger.Trace(
$"GetBlockConsensusInfo {header.ToString(BlockHeader.Format.FullHashAndNumber)} header.IsPostMerge: {header.IsPostMerge} header.TotalDifficulty {header.TotalDifficulty} header.Difficulty {header.Difficulty} TTD: {_specProvider.TerminalTotalDifficulty} MergeBlockNumber {_specProvider.MergeBlockNumber}, TransitionFinished: {TransitionFinished}");

if ((header.TotalDifficulty ?? 0) != 0 && dontTrustTotalDifficulty && header.IsGenesis == false)
{
BlockHeader? parentHeader = _blockTree.FindParentHeader(header, BlockTreeLookupOptions.None);
if (parentHeader != null && parentHeader.TotalDifficulty != 0)
header.TotalDifficulty = parentHeader.TotalDifficulty + header.Difficulty;
else
header.TotalDifficulty = null;
}

bool isTerminal = false, isPostMerge;
if (header.IsPostMerge) // block from Engine API, there is no need to check more cases
{
Expand Down Expand Up @@ -206,25 +215,9 @@ public void ForkchoiceUpdated(BlockHeader newHeadHash, Keccak finalizedHash)
}
else
{
if (header.TotalDifficulty >= _specProvider.TerminalTotalDifficulty && dontTrustTotalDifficulty && header.IsGenesis == false)
{
BlockHeader? parentHeader = _blockTree.FindParentHeader(header, BlockTreeLookupOptions.None);
if (parentHeader != null && parentHeader.TotalDifficulty != 0)
header.TotalDifficulty = parentHeader.TotalDifficulty + header.Difficulty;
else
header.TotalDifficulty = null;
}

if (header.TotalDifficulty == null)
{
isPostMerge = header.Difficulty == 0;
isTerminal = false; // we can't say if block isTerminal if we don't have TD
}
else
{
isTerminal = header.IsTerminalBlock(_specProvider); // we're checking if block is terminal if not it should be PostMerge block
isPostMerge = !isTerminal;
}

isTerminal = header.IsTerminalBlock(_specProvider); // we're checking if block is terminal if not it should be PostMerge block
isPostMerge = !isTerminal;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Merge.Plugin;
using Nethermind.Merge.Plugin.Handlers;
Expand Down Expand Up @@ -206,15 +207,15 @@ public void Terminal_block_with_lower_td_should_not_change_best_suggested_but_sh
Assert.AreEqual(remoteBestBlock.Hash, localBlockTree.FindBlock(remoteBestBlock.Hash, BlockTreeLookupOptions.None)!.Hash);
}

[Test]
public void Terminal_blocks_with_incorrect_td_from_peer()
[TestCase(10000000)]
[TestCase(20000000)]
public void Fake_total_difficulty_from_peer_does_not_trick_the_node(long ttd)
{
Context ctx = new();
BlockTree remoteBlockTree = Build.A.BlockTree().OfChainLength(10).TestObject;
BlockTree localBlockTree = Build.A.BlockTree().OfChainLength(9).TestObject;
TestSpecProvider testSpecProvider = new(London.Instance);
testSpecProvider.TerminalTotalDifficulty = 10000000;

testSpecProvider.TerminalTotalDifficulty = (UInt256)ttd;

PoSSwitcher poSSwitcher = new(new MergeConfig() { Enabled = true }, new SyncConfig(), new MemDb(), localBlockTree, testSpecProvider, LimboLogs.Instance);
MergeSealEngine sealEngine = new MergeSealEngine(new SealEngine(new NethDevSealEngine(), Always.Valid), poSSwitcher, new MergeSealValidator(poSSwitcher, Always.Valid), LimboLogs.Instance);
Expand Down

0 comments on commit c6b8a24

Please sign in to comment.