Skip to content

Commit

Permalink
More consistent name
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Oct 30, 2024
1 parent 097fd7a commit d9e5f0e
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Era.Test/Era1ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public async Task TestEraBuilderCreatesCorrectIndex()

for (int i = 0; i < fileReader.BlockCount; i++)
{
long blockOffset = fileReader.BlockOffset(fileReader.StartBlock + i);
long blockOffset = fileReader.BlockOffset(fileReader.First + i);

using (var accessor = mmf.CreateViewAccessor(blockOffset, 2))
{
Expand Down Expand Up @@ -319,8 +319,8 @@ public async Task EraExportAndImport(bool fastSync, long start, long end, long h
})
.AddSingleton<IEraConfig>(new EraConfig()
{
Start = start,
End = end,
From = start,
To = end,
ImportDirectory = tmpDir.DirectoryPath,
TrustedAccumulatorFile = Path.Join(tmpDir.DirectoryPath, EraExporter.AccumulatorFileName),
MaxEra1Size = 16,
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Era.Test/EraCliRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void WhenImportDirectoryIsSpecified_ThenCallEraImporter()
IEraConfig eraConfig = new EraConfig()
{
ImportDirectory = "import dir",
Start = 99,
End = 999
From = 99,
To = 999
};
EraCliRunner cliRunner = new EraCliRunner(eraConfig, eraImporter, Substitute.For<IEraExporter>(), Substitute.For<IProcessExitSource>(), LimboLogs.Instance);

Expand All @@ -33,8 +33,8 @@ public void WhenExportDirectoryIsSpecified_ThenCallEraExporter()
IEraConfig eraConfig = new EraConfig()
{
ExportDirectory = "export dir",
Start = 99,
End = 999
From = 99,
To = 999
};
EraCliRunner cliRunner = new EraCliRunner(eraConfig, Substitute.For<IEraImporter>(), eraExporter, Substitute.For<IProcessExitSource>(), LimboLogs.Instance);

Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Era1/E2StoreReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void EnsureIndexAvailable()
private long? _startBlock;
private long _blockCount;

public long StartBlock
public long First
{
get
{
Expand All @@ -170,7 +170,7 @@ public long StartBlock
}
}

public long LastBlock => StartBlock + _blockCount - 1;
public long LastBlock => First + _blockCount - 1;

public long AccumulatorOffset
{
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Era1/EraCliRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task Run(CancellationToken token)

private async Task Export(CancellationToken cancellation)
{
var start = eraConfig.Start;
var end = eraConfig.End;
var start = eraConfig.From;
var end = eraConfig.To;

try
{
Expand All @@ -56,7 +56,7 @@ private async Task Import()
{
try
{
await eraImporter.Import(eraConfig.ImportDirectory!, eraConfig.Start, eraConfig.End, eraConfig.TrustedAccumulatorFile, processExitSource.Token);
await eraImporter.Import(eraConfig.ImportDirectory!, eraConfig.From, eraConfig.To, eraConfig.TrustedAccumulatorFile, processExitSource.Token);
}
catch (Exception e) when (e is TaskCanceledException or OperationCanceledException)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Era1/EraConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class EraConfig : IEraConfig
{
public string? ImportDirectory { get; set; } = null;
public string? ExportDirectory { get; set; }
public long Start { get; set; }
public long End { get; set; }
public long From { get; set; }
public long To { get; set; }
public string? TrustedAccumulatorFile { get; set; }
public int MaxEra1Size { get; set; } = EraWriter.MaxEra1Size;
public string? NetworkName { get; set; }
Expand Down
32 changes: 16 additions & 16 deletions src/Nethermind/Nethermind.Era1/EraExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ public EraExporter(

public Task Export(
string destinationPath,
long start,
long end,
long from,
long to,
CancellationToken cancellation = default)
{
if (destinationPath is null) throw new ArgumentNullException(nameof(destinationPath));
if (_fileSystem.File.Exists(destinationPath)) throw new ArgumentException($"Cannot be a file.", nameof(destinationPath));
if (end == 0) end = _blockTree.Head?.Number ?? 0;
if (end > (_blockTree.Head?.Number ?? 0)) throw new ArgumentException($"Cannot export to a block after head block {_blockTree.Head?.Number ?? 0}.", nameof(end));
if (start > end) throw new ArgumentException("Start must be before end block", nameof(start));
if (to == 0) to = _blockTree.Head?.Number ?? 0;
if (to > (_blockTree.Head?.Number ?? 0)) throw new ArgumentException($"Cannot export to a block after head block {_blockTree.Head?.Number ?? 0}.", nameof(to));
if (from > to) throw new ArgumentException("Start must be before end block", nameof(from));

return DoExport(destinationPath, start, end, cancellation: cancellation);
return DoExport(destinationPath, from, to, cancellation: cancellation);
}

private async Task DoExport(
string destinationPath,
long start,
long end,
long from,
long to,
CancellationToken cancellation = default)
{
if (_logger.IsInfo) _logger.Info($"Exporting block {start} to block {end} as Era files to {destinationPath}");
if (_logger.IsInfo) _logger.Info($"Exporting block {from} to block {to} as Era files to {destinationPath}");
if (!_fileSystem.Directory.Exists(destinationPath))
{
//TODO look into permission settings - should it be set?
Expand All @@ -79,9 +79,9 @@ private async Task DoExport(
int processedSinceLast = 0;
int txProcessedSinceLast = 0;

long epochCount = (long)Math.Ceiling((end - start + 1) / (decimal)_era1Size);
long epochCount = (long)Math.Ceiling((to - from + 1) / (decimal)_era1Size);

long startEpoch = start / _era1Size;
long startEpoch = from / _era1Size;

using ArrayPoolList<long> epochIdxs = new((int)epochCount);
for (long i = 0; i < epochCount; i++)
Expand All @@ -106,29 +106,29 @@ await Parallel.ForEachAsync(epochIdxs, cancellation, async (epochIdx, cancel) =>
await _fileSystem.File.WriteAllLinesAsync(checksumPath, checksums.Select((v) => v.ToString()), cancellation);

LogExportProgress(
end - start,
to - from,
totalProcessed,
processedSinceLast,
txProcessedSinceLast,
DateTime.Now.Subtract(lastProgress),
DateTime.Now.Subtract(startTime));

if (_logger.IsInfo) _logger.Info($"Finished history export from {start} to {end}");
if (_logger.IsInfo) _logger.Info($"Finished history export from {from} to {to}");

async Task WriteEpoch(long epochIdx)
{
// Yes, it offset a bit so a block that is at the end of an epoch would be at the start of another epoch
// if the start is not of module _era1Size. This seems to match geth's behaviour.
long epoch = startEpoch + epochIdx;
long startingIndex = start + epochIdx * _era1Size;
long startingIndex = from + epochIdx * _era1Size;

string filePath = Path.Combine(
destinationPath,
EraWriter.Filename(_networkName, epoch, Keccak.Zero));

using EraWriter builder = new EraWriter(_fileSystem.File.Create(filePath), _specProvider);

for (var y = startingIndex; y < startingIndex + _era1Size && y <= end; y++)
for (var y = startingIndex; y < startingIndex + _era1Size && y <= to; y++)
{
Block? block = _blockTree.FindBlock(y, BlockTreeLookupOptions.DoNotCreateLevelIfMissing);
if (block is null)
Expand All @@ -155,7 +155,7 @@ async Task WriteEpoch(long epochIdx)
if (shouldLog)
{
LogExportProgress(
end - start,
to - from,
totalProcessed,
processedSinceLast,
txProcessedSinceLast,
Expand Down
46 changes: 23 additions & 23 deletions src/Nethermind/Nethermind.Era1/EraImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public EraImporter(
_syncConfig = syncConfig;
}

public Task Import(string src, long start, long end, string? accumulatorFile, CancellationToken cancellation = default)
public Task Import(string src, long from, long to, string? accumulatorFile, CancellationToken cancellation = default)
{
if (!_fileSystem.Directory.Exists(src))
throw new ArgumentException("Import directory does not exist", nameof(src));
Expand All @@ -69,32 +69,32 @@ public Task Import(string src, long start, long end, string? accumulatorFile, Ca
IEraStore eraStore = _eraStoreFactory.Create(src, trustedAccumulators);

long lastBlockInStore = eraStore.LastBlock;
if (end == 0) end = long.MaxValue;
if (end != long.MaxValue && lastBlockInStore < end)
if (to == 0) to = long.MaxValue;
if (to != long.MaxValue && lastBlockInStore < to)
{
throw new EraImportException($"The directory given for import '{src}' have highest block number {lastBlockInStore} which is lower then last requested block {end}.");
throw new EraImportException($"The directory given for import '{src}' have highest block number {lastBlockInStore} which is lower then last requested block {to}.");
}
if (end == long.MaxValue)
if (to == long.MaxValue)
{
end = lastBlockInStore;
to = lastBlockInStore;
}

long firstBlockInStore = eraStore.FirstBlock;
if (start == 0 && firstBlockInStore != 0)
if (from == 0 && firstBlockInStore != 0)
{
start = firstBlockInStore;
from = firstBlockInStore;
}
else if (start < firstBlockInStore)
else if (from < firstBlockInStore)
{
throw new EraImportException($"The directory given for import '{src}' have lowest block number {firstBlockInStore} which is lower then last requested block {start}.");
throw new EraImportException($"The directory given for import '{src}' have lowest block number {firstBlockInStore} which is lower then last requested block {from}.");
}
if (start > end && end != 0)
throw new ArgumentException("Start block must not be after end block", nameof(start));
if (from > to && to != 0)
throw new ArgumentException("Start block must not be after end block", nameof(from));

_receiptsDb.Tune(ITunableDb.TuneType.HeavyWrite);
_blocksDb.Tune(ITunableDb.TuneType.HeavyWrite);

return ImportInternal(start, end, eraStore, cancellation)
return ImportInternal(from, to, eraStore, cancellation)
.ContinueWith((t) =>
{
_receiptsDb.Tune(ITunableDb.TuneType.Default);
Expand All @@ -106,24 +106,24 @@ public Task Import(string src, long start, long end, string? accumulatorFile, Ca
}

private async Task ImportInternal(
long start,
long end,
long from,
long to,
IEraStore eraStore,
CancellationToken cancellation)
{
if (_logger.IsInfo) _logger.Info($"Starting history import from {start} to {end}");
if (_logger.IsInfo) _logger.Info($"Starting history import from {from} to {to}");

using IEraStore _ = eraStore;

DateTime lastProgress = DateTime.Now;
DateTime startTime = DateTime.Now;
TimeSpan elapsed = TimeSpan.Zero;
long totalblocks = end - start + 1;
long totalblocks = to - from + 1;
long blocksProcessed = 0;
long blocksProcessedAtLastLog = 0;

using BlockTreeSuggestPacer pacer = new BlockTreeSuggestPacer(_blockTree);
long blockNumber = start;
long blockNumber = from;

long suggestFromBlock = (_blockTree.Head?.Number ?? 0) + 1;
if (_syncConfig.FastSync && suggestFromBlock == 1)
Expand All @@ -143,7 +143,7 @@ private async Task ImportInternal(
// This make the `blockNumber` aligned to era file boundary so that when running parallel, each thread does not
// work on the same era file as other thread.
long nextEraStart = eraStore.NextEraStart(blockNumber);
if (nextEraStart <= end)
if (nextEraStart <= to)
{
for (; blockNumber < nextEraStart; blockNumber++)
{
Expand All @@ -153,10 +153,10 @@ private async Task ImportInternal(

// Earlier part can be parallelized
long partitionSize = _maxEra1Size;
if (start + partitionSize < suggestFromBlock)
if (from + partitionSize < suggestFromBlock)
{
ConcurrentQueue<long> partitionStartBlocks = new ConcurrentQueue<long>();
for (; blockNumber + partitionSize < suggestFromBlock && blockNumber + partitionSize < end; blockNumber += partitionSize)
for (; blockNumber + partitionSize < suggestFromBlock && blockNumber + partitionSize < to; blockNumber += partitionSize)
{
partitionStartBlocks.Enqueue(blockNumber);
}
Expand All @@ -178,14 +178,14 @@ private async Task ImportInternal(
await Task.WhenAll(importTasks);
}

for (; blockNumber <= end; blockNumber++)
for (; blockNumber <= to; blockNumber++)
{
await ImportBlock(blockNumber);
}
elapsed = DateTime.Now.Subtract(lastProgress);
LogImportProgress(DateTime.Now.Subtract(startTime), blocksProcessedAtLastLog, elapsed, blocksProcessed, totalblocks);

if (_logger.IsInfo) _logger.Info($"Finished history import from {start} to {end}");
if (_logger.IsInfo) _logger.Info($"Finished history import from {from} to {to}");

async Task ImportBlock(long blockNumber)
{
Expand Down
14 changes: 7 additions & 7 deletions src/Nethermind/Nethermind.Era1/EraReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EraReader : IAsyncEnumerable<(Block, TxReceipt[])>, IDisposable
private readonly HeaderDecoder _headerDecoder = new();
private readonly E2StoreReader _fileReader;

public long StartBlock => _fileReader.StartBlock;
public long FirstBlock => _fileReader.First;
public long LastBlock => _fileReader.LastBlock;


Expand All @@ -53,7 +53,7 @@ public EraReader(E2StoreReader e2)

private IEnumerable<long> EnumerateBlockNumber()
{
long blockNumber = _fileReader.StartBlock;
long blockNumber = _fileReader.First;
while (blockNumber <= _fileReader.LastBlock)
{
yield return blockNumber;
Expand All @@ -74,7 +74,7 @@ public async Task<ValueHash256> VerifyContent(ISpecProvider specProvider, IBlock

ValueHash256 accumulator = ReadAccumulator();

long startBlock = _fileReader.StartBlock;
long startBlock = _fileReader.First;
int blockCount = (int)_fileReader.BlockCount;
using ArrayPoolList<(Hash256, UInt256)> blockHashes = new(blockCount, blockCount);

Expand Down Expand Up @@ -135,8 +135,8 @@ public ValueHash256 ReadAccumulator()

public async Task<(Block, TxReceipt[])> GetBlockByNumber(long number, CancellationToken cancellation = default)
{
if (number < _fileReader.StartBlock)
throw new ArgumentOutOfRangeException(nameof(number), $"Cannot be less than the first block number {_fileReader.StartBlock}. Number is {number}.");
if (number < _fileReader.First)
throw new ArgumentOutOfRangeException(nameof(number), $"Cannot be less than the first block number {_fileReader.First}. Number is {number}.");
if (number > _fileReader.LastBlock)
throw new ArgumentOutOfRangeException(nameof(number), $"Cannot be more than the last block number {_fileReader.LastBlock}. Number is {number}.");
EntryReadResult result = await ReadBlockAndReceipts(number, false, cancellation);
Expand All @@ -145,8 +145,8 @@ public ValueHash256 ReadAccumulator()

private async Task<EntryReadResult> ReadBlockAndReceipts(long blockNumber, bool computeHeaderHash, CancellationToken cancellationToken)
{
if (blockNumber < _fileReader.StartBlock
|| blockNumber > _fileReader.StartBlock + _fileReader.BlockCount)
if (blockNumber < _fileReader.First
|| blockNumber > _fileReader.First + _fileReader.BlockCount)
throw new ArgumentOutOfRangeException("Value is outside the range of the archive.", blockNumber, nameof(blockNumber));

long position = _fileReader.BlockOffset(blockNumber);
Expand Down
Loading

0 comments on commit d9e5f0e

Please sign in to comment.