Skip to content

Commit da8533a

Browse files
jmederosalvaradobrbrr
authored andcommitted
Fix OP Stack header validation / Remove snapshot from snap-synced chains (#7588)
1 parent 511d862 commit da8533a

File tree

9 files changed

+85
-19
lines changed

9 files changed

+85
-19
lines changed

src/Nethermind/Chains/op-mainnet.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"rip7212TransitionTimestamp": "0x668eb001",
7474
"opGraniteTransitionTimestamp": "0x66e1be81",
7575

76-
"terminalTotalDifficulty": "0"
76+
"terminalTotalDifficulty": "210470125"
7777
},
7878
"genesis": {
7979
"seal": {
@@ -93,6 +93,11 @@
9393
"stateRoot": "0xeddb4c1786789419153a27c4c80ff44a2226b6eda04f7e22ce5bae892ea568eb"
9494
},
9595
"nodes": [
96+
"enode://87a32fd13bd596b2ffca97020e31aef4ddcc1bbd4b95bb633d16c1329f654f34049ed240a36b449fda5e5225d70fe40bc667f53c304b71f8e68fc9d448690b51@3.231.138.188:30301",
97+
"enode://ca21ea8f176adb2e229ce2d700830c844af0ea941a1d8152a9513b966fe525e809c3a6c73a2c18a12b74ed6ec4380edf91662778fe0b79f6a591236e49e176f9@184.72.129.189:30301",
98+
"enode://acf4507a211ba7c1e52cdf4eef62cdc3c32e7c9c47998954f7ba024026f9a6b2150cd3f0b734d9c78e507ab70d59ba61dfe5c45e1078c7ad0775fb251d7735a2@3.220.145.177:30301",
99+
"enode://8a5a5006159bf079d06a04e5eceab2a1ce6e0f721875b2a9c96905336219dbe14203d38f70f3754686a6324f786c2f9852d8c0dd3adac2d080f4db35efc678c5@3.231.11.52:30301",
100+
"enode://cdadbe835308ad3557f9a1de8db411da1a260a98f8421d62da90e71da66e55e98aaa8e90aa7ce01b408a54e4bd2253d701218081ded3dbe5efbbc7b41d7cef79@54.198.153.150:30301",
96101
"enode://ca2774c3c401325850b2477fd7d0f27911efbf79b1e8b335066516e2bd8c4c9e0ba9696a94b1cb030a88eac582305ff55e905e64fb77fe0edcd70a4e5296d3ec@34.65.175.185:30305",
97102
"enode://dd751a9ef8912be1bfa7a5e34e2c3785cc5253110bd929f385e07ba7ac19929fb0e0c5d93f77827291f4da02b2232240fbc47ea7ce04c46e333e452f8656b667@34.65.107.0:30305",
98103
"enode://c5d289b56a77b6a2342ca29956dfd07aadf45364dde8ab20d1dc4efd4d1bc6b4655d902501daea308f4d8950737a4e93a4dfedd17b49cd5760ffd127837ca965@34.65.202.239:30305",

src/Nethermind/Nethermind.Consensus/Validators/HeaderValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private bool ValidateGasUsed(BlockHeader header, ref string? error)
135135
return true;
136136
}
137137

138-
private bool ValidateParent(BlockHeader header, BlockHeader? parent, ref string? error)
138+
protected bool ValidateParent(BlockHeader header, BlockHeader? parent, ref string? error)
139139
{
140140
if (parent is null)
141141
{

src/Nethermind/Nethermind.Merge.Plugin/MergeHeaderValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Nethermind.Merge.Plugin
1616
{
17-
public sealed class MergeHeaderValidator : HeaderValidator
17+
public class MergeHeaderValidator : HeaderValidator
1818
{
1919
// https://eips.ethereum.org/EIPS/eip-3675#constants
2020
private const int MaxExtraDataBytes = 32;

src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeBlockDownloader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ bool HasMoreToSync(out BlockHeader[]? headers, out int headersToRequest)
218218
}
219219

220220
// can move this to block tree now?
221-
if (!_blockValidator.ValidateSuggestedBlock(currentBlock, out _))
221+
if (!_blockValidator.ValidateSuggestedBlock(currentBlock, out string? errorMessage))
222222
{
223-
string message = InvalidBlockHelper.GetMessage(currentBlock, "invalid block sent by peer") +
223+
string message = InvalidBlockHelper.GetMessage(currentBlock, $"invalid block sent by peer. {errorMessage}") +
224224
$" PeerInfo {bestPeer}";
225225
if (_logger.IsWarn) _logger.Warn(message);
226226
throw new EthSyncException(message);

src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ protected override ITransactionProcessor CreateTransactionProcessor(CodeInfoRepo
5151
protected override IHeaderValidator CreateHeaderValidator()
5252
{
5353
if (api.InvalidChainTracker is null) throw new StepDependencyException(nameof(api.InvalidChainTracker));
54+
if (api.BlockTree is null) throw new StepDependencyException(nameof(api.BlockTree));
55+
if (api.SealValidator is null) throw new StepDependencyException(nameof(api.SealValidator));
56+
if (api.SpecProvider is null) throw new StepDependencyException(nameof(api.SpecProvider));
57+
if (api.SpecHelper is null) throw new StepDependencyException(nameof(api.SpecHelper));
58+
if (api.LogManager is null) throw new StepDependencyException(nameof(api.LogManager));
5459

5560
OptimismHeaderValidator opHeaderValidator = new(
61+
api.PoSSwitcher,
5662
api.BlockTree,
5763
api.SealValidator,
5864
api.SpecProvider,
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4+
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
46
using Nethermind.Blockchain;
57
using Nethermind.Consensus;
8+
using Nethermind.Consensus.Messages;
69
using Nethermind.Consensus.Validators;
710
using Nethermind.Core;
811
using Nethermind.Core.Specs;
12+
using Nethermind.Int256;
913
using Nethermind.Logging;
14+
using Nethermind.Merge.Plugin;
1015

1116
namespace Nethermind.Optimism;
1217

13-
public class OptimismHeaderValidator(
18+
public class PreBedrockHeaderValidator(
1419
IBlockTree? blockTree,
1520
ISealValidator? sealValidator,
1621
ISpecProvider? specProvider,
17-
ILogManager? logManager)
18-
: HeaderValidator(blockTree, sealValidator, specProvider, logManager)
22+
ILogManager? logManager) : HeaderValidator(blockTree, sealValidator, specProvider, logManager)
23+
{
24+
public override bool Validate(BlockHeader header, BlockHeader? parent, bool isUncle, [NotNullWhen(false)] out string? error)
25+
{
26+
error = null;
27+
return ValidateParent(header, parent, ref error);
28+
}
29+
}
30+
31+
public class OptimismHeaderValidator(
32+
IPoSSwitcher poSSwitcher,
33+
IBlockTree blockTree,
34+
ISealValidator sealValidator,
35+
ISpecProvider specProvider,
36+
ILogManager logManager)
37+
: MergeHeaderValidator(
38+
poSSwitcher,
39+
new PreBedrockHeaderValidator(blockTree, sealValidator, specProvider, logManager),
40+
blockTree, specProvider, sealValidator, logManager)
1941
{
2042
protected override bool ValidateGasLimitRange(BlockHeader header, BlockHeader parent, IReleaseSpec spec, ref string? error) => true;
2143
}

src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Nethermind.Specs.ChainSpecStyle;
3030
using Nethermind.Serialization.Rlp;
3131
using Nethermind.Optimism.Rpc;
32+
using Nethermind.Db;
3233

3334
namespace Nethermind.Optimism;
3435

@@ -99,7 +100,9 @@ public Task Init(INethermindApi api)
99100
ArgumentNullException.ThrowIfNull(_api.BlockTree);
100101
ArgumentNullException.ThrowIfNull(_api.EthereumEcdsa);
101102

102-
_api.PoSSwitcher = AlwaysPoS.Instance;
103+
ArgumentNullException.ThrowIfNull(_api.SpecProvider);
104+
105+
_api.PoSSwitcher = new OptimismPoSSwitcher(_api.SpecProvider, _api.ChainSpec.Optimism.BedrockBlockNumber);
103106

104107
_blockCacheService = new BlockCacheService();
105108
_api.EthereumEcdsa = new OptimismEthereumEcdsa(_api.EthereumEcdsa);
@@ -133,6 +136,7 @@ public Task InitSynchronization()
133136
ArgumentNullException.ThrowIfNull(_api.SyncPeerPool);
134137
ArgumentNullException.ThrowIfNull(_api.NodeStatsManager);
135138
ArgumentNullException.ThrowIfNull(_api.BlockchainProcessor);
139+
ArgumentNullException.ThrowIfNull(_api.BetterPeerStrategy);
136140

137141
ArgumentNullException.ThrowIfNull(_blockCacheService);
138142
ArgumentNullException.ThrowIfNull(_invalidChainTracker);
@@ -144,7 +148,7 @@ public Task InitSynchronization()
144148

145149
_beaconPivot = new BeaconPivot(_syncConfig, _api.DbProvider.MetadataDb, _api.BlockTree, _api.PoSSwitcher, _api.LogManager);
146150
_beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _api.PoSSwitcher, _api.LogManager);
147-
_api.BetterPeerStrategy = new MergeBetterPeerStrategy(null!, _api.PoSSwitcher, _beaconPivot, _api.LogManager);
151+
_api.BetterPeerStrategy = new MergeBetterPeerStrategy(_api.BetterPeerStrategy, _api.PoSSwitcher, _beaconPivot, _api.LogManager);
148152
_api.Pivot = _beaconPivot;
149153

150154
MergeBlockDownloaderFactory blockDownloaderFactory = new MergeBlockDownloaderFactory(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Nethermind.Consensus;
3+
using Nethermind.Core;
4+
using Nethermind.Core.Crypto;
5+
using Nethermind.Core.Specs;
6+
using Nethermind.Int256;
7+
using Nethermind.Optimism;
8+
9+
public class OptimismPoSSwitcher(ISpecProvider specProvider, long bedrockBlockNumber) : IPoSSwitcher
10+
{
11+
public UInt256? TerminalTotalDifficulty => specProvider.TerminalTotalDifficulty;
12+
13+
public UInt256? FinalTotalDifficulty => TerminalTotalDifficulty;
14+
15+
public bool TransitionFinished => true;
16+
17+
public Hash256? ConfiguredTerminalBlockHash => null;
18+
19+
public long? ConfiguredTerminalBlockNumber => null;
20+
21+
public event EventHandler TerminalBlockReached { add { } remove { } }
22+
23+
public void ForkchoiceUpdated(BlockHeader newHeadHash, Hash256 finalizedHash) { }
24+
25+
public (bool IsTerminal, bool IsPostMerge) GetBlockConsensusInfo(BlockHeader header)
26+
{
27+
return (header.Number == bedrockBlockNumber - 1, header.IsPostMerge = header.Number >= bedrockBlockNumber);
28+
}
29+
30+
public bool HasEverReachedTerminalBlock() => true;
31+
32+
public bool IsPostMerge(BlockHeader header) => GetBlockConsensusInfo(header).IsPostMerge;
33+
34+
public bool TryUpdateTerminalBlock(BlockHeader header) => false;
35+
}

src/Nethermind/Nethermind.Runner/configs/op-mainnet.cfg

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"AncientBodiesBarrier": 105235063,
1717
"AncientReceiptsBarrier": 105235063,
1818
"FastSyncCatchUpHeightDelta": "10000000000",
19-
"PivotNumber": 125970000,
20-
"PivotHash": "0xc77d1d26b1d3511a1440cb4f55222a30d79bc7ac9d806e30de5399c5415ecfb0",
19+
"PivotNumber": 126270000,
20+
"PivotHash": "0xd3404c9404e47cbbcd688c1620119495cad1dab37631b556cbbb6d4ce0cea54f",
2121
"PivotTotalDifficulty": "210470125",
2222
"MaxAttemptsToUpdatePivot": 0
2323
},
@@ -39,13 +39,7 @@
3939
"Merge": {
4040
"Enabled": true
4141
},
42-
"Snapshot": {
43-
"Enabled": true,
44-
"DownloadUrl": "http://optimism-snapshot.nethermind.io/op-mainnet-genesis-v2.zip",
45-
"SnapshotFileName": "op-mainnet-genesis-v2.zip",
46-
"Checksum": "0x9b08eeb974c46f257c8bca812b119849590d92975ac86e4215d0d5647c26b147"
47-
},
4842
"Optimism": {
4943
"SequencerUrl": "https://mainnet-sequencer.optimism.io"
5044
}
51-
}
45+
}

0 commit comments

Comments
 (0)