Skip to content

Commit c6b8a24

Browse files
authored
fix terminal block checking (#4318)
* fix terminal block checking * add test * fix + tests
1 parent cb44613 commit c6b8a24

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/Nethermind/Nethermind.Merge.Plugin/PoSSwitcher.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ public void ForkchoiceUpdated(BlockHeader newHeadHash, Keccak finalizedHash)
176176
_logger.Trace(
177177
$"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}");
178178

179+
if ((header.TotalDifficulty ?? 0) != 0 && dontTrustTotalDifficulty && header.IsGenesis == false)
180+
{
181+
BlockHeader? parentHeader = _blockTree.FindParentHeader(header, BlockTreeLookupOptions.None);
182+
if (parentHeader != null && parentHeader.TotalDifficulty != 0)
183+
header.TotalDifficulty = parentHeader.TotalDifficulty + header.Difficulty;
184+
else
185+
header.TotalDifficulty = null;
186+
}
187+
179188
bool isTerminal = false, isPostMerge;
180189
if (header.IsPostMerge) // block from Engine API, there is no need to check more cases
181190
{
@@ -206,25 +215,9 @@ public void ForkchoiceUpdated(BlockHeader newHeadHash, Keccak finalizedHash)
206215
}
207216
else
208217
{
209-
if (header.TotalDifficulty >= _specProvider.TerminalTotalDifficulty && dontTrustTotalDifficulty && header.IsGenesis == false)
210-
{
211-
BlockHeader? parentHeader = _blockTree.FindParentHeader(header, BlockTreeLookupOptions.None);
212-
if (parentHeader != null && parentHeader.TotalDifficulty != 0)
213-
header.TotalDifficulty = parentHeader.TotalDifficulty + header.Difficulty;
214-
else
215-
header.TotalDifficulty = null;
216-
}
217-
218-
if (header.TotalDifficulty == null)
219-
{
220-
isPostMerge = header.Difficulty == 0;
221-
isTerminal = false; // we can't say if block isTerminal if we don't have TD
222-
}
223-
else
224-
{
225-
isTerminal = header.IsTerminalBlock(_specProvider); // we're checking if block is terminal if not it should be PostMerge block
226-
isPostMerge = !isTerminal;
227-
}
218+
219+
isTerminal = header.IsTerminalBlock(_specProvider); // we're checking if block is terminal if not it should be PostMerge block
220+
isPostMerge = !isTerminal;
228221
}
229222
}
230223

src/Nethermind/Nethermind.Synchronization.Test/SyncServerTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Nethermind.Core.Crypto;
2424
using Nethermind.Core.Test.Builders;
2525
using Nethermind.Db;
26+
using Nethermind.Int256;
2627
using Nethermind.Logging;
2728
using Nethermind.Merge.Plugin;
2829
using Nethermind.Merge.Plugin.Handlers;
@@ -206,15 +207,15 @@ public void Terminal_block_with_lower_td_should_not_change_best_suggested_but_sh
206207
Assert.AreEqual(remoteBestBlock.Hash, localBlockTree.FindBlock(remoteBestBlock.Hash, BlockTreeLookupOptions.None)!.Hash);
207208
}
208209

209-
[Test]
210-
public void Terminal_blocks_with_incorrect_td_from_peer()
210+
[TestCase(10000000)]
211+
[TestCase(20000000)]
212+
public void Fake_total_difficulty_from_peer_does_not_trick_the_node(long ttd)
211213
{
212214
Context ctx = new();
213215
BlockTree remoteBlockTree = Build.A.BlockTree().OfChainLength(10).TestObject;
214216
BlockTree localBlockTree = Build.A.BlockTree().OfChainLength(9).TestObject;
215217
TestSpecProvider testSpecProvider = new(London.Instance);
216-
testSpecProvider.TerminalTotalDifficulty = 10000000;
217-
218+
testSpecProvider.TerminalTotalDifficulty = (UInt256)ttd;
218219

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

0 commit comments

Comments
 (0)