Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove snapshot; bypass downloading if available locally #7565

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Nethermind/Chains/op-mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"rip7212TransitionTimestamp": "0x668eb001",
"opGraniteTransitionTimestamp": "0x66e1be81",

"terminalTotalDifficulty": "0"
"terminalTotalDifficulty": "210470125"
},
"genesis": {
"seal": {
Expand Down
65 changes: 37 additions & 28 deletions src/Nethermind/Nethermind.Init.Snapshot/InitDatabaseSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,44 +71,53 @@ private async Task InitDbFromSnapshot(CancellationToken cancellationToken)

Directory.CreateDirectory(snapshotConfig.SnapshotDirectory);

if (GetCheckpoint(snapshotConfig) < Stage.Downloaded)
if (!File.Exists(snapshotUrl))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Url and file? Did you mean snapshotFileName? Would be good to abstract file manipulation to some methods instead of dign it directly.

{
while (true)
if (GetCheckpoint(snapshotConfig) < Stage.Downloaded)
{
try
while (true)
{
await DownloadSnapshotTo(snapshotUrl, snapshotFileName, cancellationToken);
break;
try
{
await DownloadSnapshotTo(snapshotUrl, snapshotFileName, cancellationToken);
break;
}
catch (IOException e)
{
if (_logger.IsError)
_logger.Error($"Snapshot download failed. Retrying in 5 seconds. Error: {e}");
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
cancellationToken.ThrowIfCancellationRequested();
}
catch (IOException e)
{
if (_logger.IsError)
_logger.Error($"Snapshot download failed. Retrying in 5 seconds. Error: {e}");
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
cancellationToken.ThrowIfCancellationRequested();
SetCheckpoint(snapshotConfig, Stage.Downloaded);
}
SetCheckpoint(snapshotConfig, Stage.Downloaded);
}

if (GetCheckpoint(snapshotConfig) < Stage.Verified)
{
if (snapshotConfig.Checksum is not null)
if (GetCheckpoint(snapshotConfig) < Stage.Verified)
{
bool isChecksumValid = await VerifyChecksum(snapshotFileName, snapshotConfig.Checksum, cancellationToken);
if (!isChecksumValid)
if (snapshotConfig.Checksum is not null)
{
if (_logger.IsError)
_logger.Error("Snapshot checksum verification failed. Aborting, but will continue running.");
return;
bool isChecksumValid = await VerifyChecksum(snapshotFileName, snapshotConfig.Checksum, cancellationToken);
if (!isChecksumValid)
{
if (_logger.IsError)
_logger.Error("Snapshot checksum verification failed. Aborting, but will continue running.");
return;
}

if (_logger.IsInfo)
_logger.Info("Snapshot checksum verified.");
}

if (_logger.IsInfo)
_logger.Info("Snapshot checksum verified.");
else if (_logger.IsWarn)
_logger.Warn("Snapshot checksum is not configured");
SetCheckpoint(snapshotConfig, Stage.Verified);
}
else if (_logger.IsWarn)
_logger.Warn("Snapshot checksum is not configured");
SetCheckpoint(snapshotConfig, Stage.Verified);
}
else
{
if (_logger.IsWarn)
_logger.Warn("Snapshot is taken from local path, no checksum verification will be applied.");
snapshotFileName = snapshotUrl;
}

await ExtractSnapshotTo(snapshotFileName, dbPath, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ bool HasMoreToSync(out BlockHeader[]? headers, out int headersToRequest)
}

// can move this to block tree now?
if (!_blockValidator.ValidateSuggestedBlock(currentBlock, out _))
if (!_blockValidator.ValidateSuggestedBlock(currentBlock, out string? errorMessage))
{
string message = InvalidBlockHelper.GetMessage(currentBlock, "invalid block sent by peer") +
string message = InvalidBlockHelper.GetMessage(currentBlock, $"invalid block sent by peer. {errorMessage}") +
$" PeerInfo {bestPeer}";
if (_logger.IsWarn) _logger.Warn(message);
throw new EthSyncException(message);
Expand Down
19 changes: 17 additions & 2 deletions src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Serialization.Rlp;
using Nethermind.Optimism.Rpc;
using Nethermind.Db;

namespace Nethermind.Optimism;

Expand Down Expand Up @@ -91,6 +92,12 @@ public Task Init(INethermindApi api)
return Task.CompletedTask;

_api = (OptimismNethermindApi)api;

ArgumentNullException.ThrowIfNull(_api);
ArgumentNullException.ThrowIfNull(_api.DbProvider);
ArgumentNullException.ThrowIfNull(_api.SpecProvider);
ArgumentNullException.ThrowIfNull(_api.BlockTree);

_mergeConfig = _api.Config<IMergeConfig>();
_syncConfig = _api.Config<ISyncConfig>();
_blocksConfig = _api.Config<IBlocksConfig>();
Expand All @@ -99,7 +106,14 @@ public Task Init(INethermindApi api)
ArgumentNullException.ThrowIfNull(_api.BlockTree);
ArgumentNullException.ThrowIfNull(_api.EthereumEcdsa);

_api.PoSSwitcher = AlwaysPoS.Instance;
_api.PoSSwitcher = new PoSSwitcher(
_mergeConfig,
_syncConfig,
_api.DbProvider.GetDb<IDb>(DbNames.Metadata),
_api.BlockTree,
_api.SpecProvider,
_api.ChainSpec,
_api.LogManager);

_blockCacheService = new BlockCacheService();
_api.EthereumEcdsa = new OptimismEthereumEcdsa(_api.EthereumEcdsa);
Expand Down Expand Up @@ -133,6 +147,7 @@ public Task InitSynchronization()
ArgumentNullException.ThrowIfNull(_api.SyncPeerPool);
ArgumentNullException.ThrowIfNull(_api.NodeStatsManager);
ArgumentNullException.ThrowIfNull(_api.BlockchainProcessor);
ArgumentNullException.ThrowIfNull(_api.BetterPeerStrategy);

ArgumentNullException.ThrowIfNull(_blockCacheService);
ArgumentNullException.ThrowIfNull(_invalidChainTracker);
Expand All @@ -144,7 +159,7 @@ public Task InitSynchronization()

_beaconPivot = new BeaconPivot(_syncConfig, _api.DbProvider.MetadataDb, _api.BlockTree, _api.PoSSwitcher, _api.LogManager);
_beaconSync = new BeaconSync(_beaconPivot, _api.BlockTree, _syncConfig, _blockCacheService, _api.PoSSwitcher, _api.LogManager);
_api.BetterPeerStrategy = new MergeBetterPeerStrategy(null!, _api.PoSSwitcher, _beaconPivot, _api.LogManager);
_api.BetterPeerStrategy = new MergeBetterPeerStrategy(_api.BetterPeerStrategy, _api.PoSSwitcher, _beaconPivot, _api.LogManager);
_api.Pivot = _beaconPivot;

MergeBlockDownloaderFactory blockDownloaderFactory = new MergeBlockDownloaderFactory(
Expand Down
11 changes: 3 additions & 8 deletions src/Nethermind/Nethermind.Runner/configs/op-mainnet.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"FastSyncCatchUpHeightDelta": "10000000000",
"PivotNumber": 126270000,
"PivotHash": "0xd3404c9404e47cbbcd688c1620119495cad1dab37631b556cbbb6d4ce0cea54f",
"PivotTotalDifficulty": "0",
"PivotTotalDifficulty": "210470125",
"MaxAttemptsToUpdatePivot": 0
},
"Discovery": {
Expand All @@ -37,15 +37,10 @@
"TargetBlockGasLimit": 30000000
},
"Merge": {
"Enabled": true
},
"Snapshot": {
"Enabled": true,
"DownloadUrl": "http://optimism-snapshot.nethermind.io/op-mainnet-genesis-v2.zip",
"SnapshotFileName": "op-mainnet-genesis-v2.zip",
"Checksum": "0x9b08eeb974c46f257c8bca812b119849590d92975ac86e4215d0d5647c26b147"
"FinalTotalDifficulty": "210470125"
},
"Optimism": {
"SequencerUrl": "https://mainnet-sequencer.optimism.io"
}
}
}
Loading