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

Feature/Era1 import/export #6547

Open
wants to merge 327 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
327 commits
Select commit Hold shift + click to select a range
b06fddc
Merge branch 'master' into feature/era-cli
ak88 Jan 10, 2024
5e08c9c
build fix
ak88 Jan 10, 2024
9b42260
build fix
ak88 Jan 10, 2024
90d57a0
removed checksumming
ak88 Jan 10, 2024
3464926
refactor export
ak88 Jan 11, 2024
fecd8a3
eraexporter unittests
ak88 Jan 11, 2024
4ae0c08
EraAdminService
ak88 Jan 14, 2024
bcfcf4f
Merge branch 'feature/era-cli' into feature/era-import
ak88 Jan 14, 2024
b806a23
missing files
ak88 Jan 14, 2024
1e9a490
era import test
ak88 Jan 15, 2024
5706897
update snappier
ak88 Jan 15, 2024
4668b49
include epoch in progress
ak88 Jan 16, 2024
8a78aad
revert LoadGenesisBlock
ak88 Jan 16, 2024
1c5c16d
remove ancient dir
ak88 Jan 16, 2024
76b6149
Merge branch 'master' into feature/era-import
ak88 Jan 16, 2024
7ec3697
Update src/Nethermind/Nethermind.Era.Test/Nethermind.Era1.Test.csproj
ak88 Jan 16, 2024
7d2079d
Update src/Nethermind/Nethermind.Era.Test/Nethermind.Era1.Test.csproj
ak88 Jan 16, 2024
1521c3f
Update src/Nethermind/Nethermind.Era1/Nethermind.Era1.csproj
ak88 Jan 16, 2024
15b7fa5
Update src/Nethermind/Nethermind.Era.Test/Nethermind.Era1.Test.csproj
ak88 Jan 16, 2024
2965f71
Update src/Nethermind/Nethermind.Era1/E2Store.cs
ak88 Jan 16, 2024
1f8bcc3
Update src/Nethermind/Directory.Packages.props
ak88 Jan 16, 2024
ac2a68c
Update src/Nethermind/Directory.Packages.props
ak88 Jan 16, 2024
4382d07
build fix
ak88 Jan 16, 2024
37fc0f7
format whitespace
ak88 Jan 16, 2024
7c714a4
fix unittest
ak88 Jan 16, 2024
858061b
revert deployment scripts
ak88 Jan 16, 2024
81a93d1
Merge branch 'master' into feature/era-import
ak88 Jul 9, 2024
5d98dbe
fixes
ak88 Jul 9, 2024
40c554a
Merge branch 'master' into feature/era-import
ak88 Jul 19, 2024
edeb3af
refactor continued
ak88 Jul 31, 2024
9f1e7aa
refactor
ak88 Aug 2, 2024
e75d8d1
Merge branch 'master' into feature/era-import
ak88 Aug 2, 2024
9453950
fix
ak88 Aug 2, 2024
3de43a3
refactor conti.
ak88 Aug 6, 2024
5d66d34
barrier on header sync
ak88 Aug 10, 2024
4d7529b
Test fix
ak88 Aug 13, 2024
879a968
revert launch
ak88 Aug 13, 2024
d487bca
format
ak88 Aug 13, 2024
edb03fd
rpc docs
ak88 Aug 13, 2024
043daa8
roll back header barriers
ak88 Oct 1, 2024
f9c523d
format whitespace
ak88 Oct 1, 2024
a271354
fix era test
ak88 Oct 1, 2024
0d8eea4
Merge branch 'master' into fix/era-branch
ak88 Oct 1, 2024
5d65d39
fix build
ak88 Oct 1, 2024
e6220d4
test fixes
ak88 Oct 1, 2024
5c32db9
Dont specify potision direcctly
asdacap Oct 9, 2024
a47c884
Move metadata out
asdacap Oct 9, 2024
2f987b2
Use entry
asdacap Oct 9, 2024
5d6dbf3
Smaller interface
asdacap Oct 9, 2024
fc358e1
More cutting things off
asdacap Oct 9, 2024
50eb83b
Minor cleanup
asdacap Oct 9, 2024
e1f19a3
Fix index
asdacap Oct 9, 2024
61b6d22
Fix more tests
asdacap Oct 9, 2024
4cf444c
Fix some tests broke some tests.
asdacap Oct 9, 2024
e934d34
Use BinaryPrimitives
asdacap Oct 9, 2024
b97e6bb
Add different test case
asdacap Oct 9, 2024
f0eaf5e
Deduplicate code
asdacap Oct 9, 2024
6a06d76
Use valuehash directly
asdacap Oct 9, 2024
b264253
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Oct 10, 2024
1f44275
Fix build
asdacap Oct 10, 2024
27ed9be
Reducing changes
asdacap Oct 10, 2024
96e862b
Reducing changes
asdacap Oct 10, 2024
da987e2
Reducing change
asdacap Oct 10, 2024
2c38b45
Remove cortex
asdacap Oct 11, 2024
b823ec6
Fix span index
asdacap Oct 11, 2024
81977d0
Remove unnecessary change
asdacap Oct 11, 2024
b9e309c
Fix build
asdacap Oct 11, 2024
3928574
Add mainnet test
asdacap Oct 15, 2024
2b1a2f4
Log directly
asdacap Oct 15, 2024
9b2a3a5
Log progress directly
asdacap Oct 15, 2024
33b0079
Remove more unnecessary code
asdacap Oct 15, 2024
8bc7b8f
Move to its own step
asdacap Oct 16, 2024
cf06f45
Remove StreamSegment
asdacap Oct 16, 2024
7b9076d
Use same block body decoder as eth
asdacap Oct 16, 2024
621d176
Consolidate block body logic
asdacap Oct 16, 2024
4d28304
Whitespace
asdacap Oct 16, 2024
854c372
Fix test
asdacap Oct 16, 2024
ec7d908
Merge branch 'refactor/consolidate-block-body-logic' into feature/era…
asdacap Oct 18, 2024
b1e6eeb
Using memory mapped file now
asdacap Oct 19, 2024
17e5fd0
Remove EraMetadata
asdacap Oct 19, 2024
841dedf
Rename classes
asdacap Oct 19, 2024
884c4b7
Minor test cleanup
asdacap Oct 21, 2024
f35d1bb
Simplify era writer
asdacap Oct 21, 2024
86ad9d1
No need to add total difficulty inside
asdacap Oct 21, 2024
8b0addf
No need to have separate total difficulty
asdacap Oct 21, 2024
0156b88
Simplify tsets
asdacap Oct 21, 2024
f7e756d
Read and verify in one method
asdacap Oct 21, 2024
bf82a66
Slight cleanup
asdacap Oct 21, 2024
e70aaaf
Minor cleanup
asdacap Oct 21, 2024
2f6a8d4
Mode files to era package
asdacap Oct 21, 2024
20695e0
Move more code into era package
asdacap Oct 21, 2024
da6081f
Minor cli remove
asdacap Oct 21, 2024
933f068
Create a dedicated cconfig for era
asdacap Oct 21, 2024
d29eb72
Reduce missed change
asdacap Oct 21, 2024
db95f85
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Oct 21, 2024
7f2870a
Minor cleanup
asdacap Oct 21, 2024
16dda05
Whitespace
asdacap Oct 21, 2024
79adc94
Export via cli
asdacap Oct 23, 2024
f28e315
Parallel exporter
asdacap Oct 23, 2024
c3139e5
Disable receipt recovery
asdacap Oct 23, 2024
2a946dd
Fix accumulator missing
asdacap Oct 23, 2024
4e13cac
Suggest
asdacap Oct 24, 2024
6fe233c
Blocktree suggest pacer
asdacap Oct 24, 2024
ba0272d
Dispose properly
asdacap Oct 24, 2024
2a789df
Merge branch 'feature/blocking-blocktree-suggester' into feature/era-…
asdacap Oct 24, 2024
9976f5a
Fix premature dispose
asdacap Oct 24, 2024
51261e0
Add pacer
asdacap Oct 24, 2024
2beda2c
Simplify interface
asdacap Oct 24, 2024
eb08363
Verify as part of import
asdacap Oct 24, 2024
8cdd97f
Reduce test slightly
asdacap Oct 24, 2024
2f9142a
Not sure if this helps
asdacap Oct 24, 2024
54c72be
No need to recalculate header hash
asdacap Oct 24, 2024
d3bc0b4
Probably not needed
asdacap Oct 24, 2024
aacfd59
Slight change to log
asdacap Oct 24, 2024
dbfb04f
No need to verify here
asdacap Oct 24, 2024
413f33a
Parallelize verification
asdacap Oct 24, 2024
e39c688
Combine the two import
asdacap Oct 24, 2024
8d08603
Enable heavy write when importing
asdacap Oct 24, 2024
d7918c7
Parallelize
asdacap Oct 25, 2024
bddd941
Fix export
asdacap Oct 25, 2024
fb87aab
Fix log
asdacap Oct 25, 2024
ec85308
Do not redownload unneded block
asdacap Oct 26, 2024
a9f509e
Separate block store and badd block store
asdacap Oct 26, 2024
0b1c573
Separate block store and bad block store
asdacap Oct 26, 2024
4d11ef5
Fix test
asdacap Oct 26, 2024
b67f9f1
MMeerge branch 'refactor/separate-block-store' into feature/dont-down…
asdacap Oct 26, 2024
f2ba192
Dont' redownload existing blocks
asdacap Oct 28, 2024
e648304
Fix build
asdacap Oct 28, 2024
c80ea79
Merge branch 'refactor/separate-block-store' into feature/dont-downlo…
asdacap Oct 28, 2024
7c54f94
Bodies sync feed unit test
asdacap Oct 28, 2024
f0c862f
Maybe add it to IBlockFinder
asdacap Oct 28, 2024
39f8da4
Unit tests
asdacap Oct 28, 2024
43ab1ff
Disalbe optimize filter for hits
asdacap Oct 28, 2024
14d5a90
Whitespace
asdacap Oct 28, 2024
93a0224
Merge branch 'feature/dont-download-existing-block' into feature/era-…
asdacap Oct 28, 2024
a84ecdf
Fix conflict
asdacap Oct 28, 2024
5bf4b93
Fastsync test
asdacap Oct 29, 2024
0924c52
Clean admin, change encoding again
asdacap Oct 29, 2024
5832483
Match geth epoch behaviour
asdacap Oct 29, 2024
c3c5802
Era store factory
asdacap Oct 29, 2024
5bb8cde
Network name from config
asdacap Oct 29, 2024
617d0ff
Add comments
asdacap Oct 29, 2024
fddcc6d
Checksum support
asdacap Oct 29, 2024
203159a
Cli runner test
asdacap Oct 29, 2024
36b39d8
Minor comment
asdacap Oct 29, 2024
df8a91b
Move admin era service to era directory
asdacap Oct 29, 2024
d66eb61
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Oct 29, 2024
f89e644
Whitespace
asdacap Oct 29, 2024
67e2a8c
Fix test
asdacap Oct 29, 2024
31b469f
Add option to not recover sender
asdacap Oct 29, 2024
a654d9a
Merge branch 'feature/allow-skipping-sender-recovery' into feature/er…
asdacap Oct 29, 2024
4d5944e
Fix tests, disable sender ccalculatioon
asdacap Oct 29, 2024
7028b52
Add option to specify write flags
asdacap Oct 29, 2024
3833ff5
Merge branch 'feature/allow-skipping-sender-recovery' into feature/er…
asdacap Oct 29, 2024
d417ac0
More robust validation
asdacap Oct 29, 2024
4df3719
Whitespace
asdacap Oct 30, 2024
097fd7a
Add tests for admin era service
asdacap Oct 30, 2024
d9e5f0e
More consistent name
asdacap Oct 30, 2024
69fbf66
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Nov 1, 2024
1683e87
Whitespace
asdacap Nov 1, 2024
a6a4fb9
Cleanup
asdacap Nov 1, 2024
a8e8154
Fix misleading cli
asdacap Nov 1, 2024
2476a51
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Nov 9, 2024
240f5c8
Update src/Nethermind/Nethermind.Era1/E2StoreWriter.cs
asdacap Nov 9, 2024
4f0021e
Update src/Nethermind/Nethermind.Era1/E2StoreWriter.cs
asdacap Nov 9, 2024
08cf69c
Update src/Nethermind/Nethermind.Era1/AccumulatorCalculator.cs
asdacap Nov 9, 2024
f6aca3d
Fix build
asdacap Nov 17, 2024
3fa9295
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Nov 17, 2024
d9afb87
Use RandomAccessFile
asdacap Nov 17, 2024
338f969
Fix test
asdacap Nov 18, 2024
f58a4c5
Missed log
asdacap Nov 18, 2024
ccc205b
Addressing comment
asdacap Nov 19, 2024
db6818f
Use arraypool
asdacap Nov 19, 2024
9391acb
Address comoment
asdacap Nov 19, 2024
9027a72
Parallelize checksum
asdacap Nov 19, 2024
95f9489
Whitespace
asdacap Nov 19, 2024
af2be08
Fix receipt issue
asdacap Nov 20, 2024
0062e2f
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Nov 20, 2024
fe71a86
Whitespace
asdacap Nov 20, 2024
b1b1a08
Fix error msg
asdacap Nov 20, 2024
bc2daad
Fix non optional rpc param
asdacap Nov 25, 2024
774d8dd
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Nov 25, 2024
8e487ec
Add back global.json
asdacap Nov 25, 2024
c6d3d0c
Better log
asdacap Nov 25, 2024
e52c0cb
Throw on empty era directory
asdacap Nov 27, 2024
685e927
Fix fast sync after import
asdacap Nov 28, 2024
2945364
Persist lowest inserted header
asdacap Nov 28, 2024
8ed3ee4
Only set lowest inserted header in fast header
asdacap Nov 28, 2024
ea2b491
Fix build
asdacap Nov 28, 2024
e938df4
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Nov 29, 2024
6dfdd44
Fix build
asdacap Nov 29, 2024
7aeebf1
Isolate enque batch
asdacap Nov 29, 2024
a2514c0
Down download persisted blocks
asdacap Nov 29, 2024
724ecfd
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Nov 29, 2024
b192ced
Merge remote-tracking branch 'origin/master' into change/use-persiste…
asdacap Dec 2, 2024
76001c2
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Dec 2, 2024
0ed716e
Fix test
asdacap Dec 3, 2024
2bb720b
Minor cleanup
asdacap Dec 3, 2024
9f8bf78
Minor comment
asdacap Dec 3, 2024
de2d398
Unit test persisted header
asdacap Dec 3, 2024
89e1416
Fix test
asdacap Dec 3, 2024
d3116aa
Fix test
asdacap Dec 3, 2024
8ef72d7
Whitespace
asdacap Dec 3, 2024
24dac75
No need for on lowest inserted headers
asdacap Dec 3, 2024
0a4548f
Reseet bench precombiles
asdacap Dec 3, 2024
dc6e7b4
Fix test
asdacap Dec 3, 2024
8e3c899
Whitespace
asdacap Dec 3, 2024
0b7cbe1
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Dec 3, 2024
42964e9
Merge remote-tracking branch 'origin/master' into change/use-persiste…
asdacap Dec 3, 2024
4545c10
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Dec 3, 2024
e5397b9
Fix empty blocktree edge case
asdacap Dec 3, 2024
6569082
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Dec 5, 2024
7c2efad
Merge branch 'master' into change/use-persisted-pointer-for-fast-header
asdacap Dec 5, 2024
744691b
More log on sync pointers
asdacap Dec 11, 2024
b2f021a
Fix old headers does not finish
asdacap Dec 11, 2024
63def2e
Merge branch 'change/use-persisted-pointer-for-fast-header' into feat…
asdacap Dec 11, 2024
edd99a1
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Dec 13, 2024
7c8d8d7
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Dec 18, 2024
f61c0ee
Move log logic to measured progress
asdacap Dec 18, 2024
590a0dc
Skipped rate
asdacap Dec 18, 2024
9b38d1b
Renamed to progress logger
asdacap Dec 18, 2024
a2018fa
Fix some measurement
asdacap Dec 18, 2024
99aca3f
Fix receipt barrier
asdacap Dec 18, 2024
2b405e2
Fix test
asdacap Dec 18, 2024
8ae228d
Unit test progress logger
asdacap Dec 18, 2024
6aabd04
Merge branch 'feature/skipped-progress' into feature/era-import
asdacap Dec 18, 2024
6d88215
Whitespacet
asdacap Dec 18, 2024
5d1b1aa
Fix alignment
asdacap Dec 18, 2024
be0ecba
Fix test
asdacap Dec 18, 2024
4c76773
How about now?
asdacap Dec 18, 2024
1d3d936
Merge branch 'feature/skipped-progress' into feature/era-import
asdacap Dec 19, 2024
88b00f9
Code cleanup
asdacap Dec 19, 2024
5ab4f19
Use values for report state
asdacap Dec 19, 2024
3e202c7
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Dec 19, 2024
e76d6ec
Reduce change
asdacap Dec 19, 2024
c380949
Reduce change
asdacap Dec 19, 2024
954cf0a
Merge branch 'feature/skipped-progress' into feature/era-import
asdacap Dec 19, 2024
d165f29
Cleanup
asdacap Dec 19, 2024
98ced0b
Fix unbounded memory usage with era imported node
asdacap Dec 20, 2024
b806644
Merge branch 'fix/unbound-loaded-dependencies' into feature/era-import
asdacap Dec 20, 2024
065908f
Fix build
asdacap Dec 20, 2024
3c4c1aa
Fix double insert
asdacap Dec 20, 2024
f9bcbab
Merge remote-tracking branch 'origin/master' into feature/skipped-pro…
asdacap Dec 20, 2024
a4c8a82
Fix conflict
asdacap Dec 20, 2024
436f4f3
Merge branch 'feature/skipped-progress' into feature/era-import
asdacap Dec 20, 2024
7121f3e
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Dec 23, 2024
d6a2e90
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Dec 23, 2024
8876dc6
Merge remote-tracking branch 'origin/master' into feature/era-import
asdacap Jan 9, 2025
fba86c7
Missed merge marker
asdacap Jan 9, 2025
faa8d45
Use single container
asdacap Jan 9, 2025
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
Empty file modified scripts/deployment/archive-packages.sh
100755 → 100644
rubo marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
Empty file modified scripts/deployment/build-runner.sh
100755 → 100644
rubo marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
7 changes: 6 additions & 1 deletion src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="Colorful.Console" Version="1.2.15" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
<PackageVersion Include="ConcurrentHashSet" Version="1.3.0" />
<PackageVersion Include="Cortex.SimpleSerialize" Version="0.2.0" />
asdacap marked this conversation as resolved.
Show resolved Hide resolved
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="Crc32.NET" Version="1.2.0" />
<PackageVersion Include="DnsClient" Version="1.7.0" />
Expand Down Expand Up @@ -65,14 +66,18 @@
<PackageVersion Include="RocksDB" Version="8.3.2.39829" />
<PackageVersion Include="SCrypt" Version="2.0.0.2" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="Snappier" Version="1.1.4" />
<PackageVersion Include="Snappy.Standard" Version="0.2.0" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.TextWriterTraceListener" Version="4.3.0" />
<PackageVersion Include="System.IO.Abstractions.TestingHelpers" Version="19.2.87" />
ak88 marked this conversation as resolved.
Show resolved Hide resolved
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.2.0" />
<PackageVersion Include="System.IO.Compression.ZipFile" Version="4.3.0" />
ak88 marked this conversation as resolved.
Show resolved Hide resolved
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="20.0.4" />
<PackageVersion Include="TestableIO.System.IO.Abstractions.Wrappers" Version="20.0.4" />
<PackageVersion Include="Websocket.Client" Version="5.0.0" />
</ItemGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Api/IBasicApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using System.Linq;
using Nethermind.Abi;
using Nethermind.Api.Extensions;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Timers;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Era1;
using Nethermind.KeyStore;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
Expand Down Expand Up @@ -46,6 +48,7 @@ public interface IBasicApi
ITimestamper Timestamper { get; }
ITimerFactory TimerFactory { get; }
IProcessExitSource? ProcessExit { get; set; }
IProcessExitToken? ProcessExitToken { get; set; }

public IConsensusPlugin? GetConsensusPlugin() =>
Plugins
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Api/NethermindApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public ISealEngine SealEngine
public IList<IPublisher> Publishers { get; } = new List<IPublisher>(); // this should be called publishers
public CompositePruningTrigger PruningTrigger { get; } = new();
public IProcessExitSource? ProcessExit { get; set; }
public IProcessExitToken? ProcessExitToken { get; set; }
public CompositeTxGossipPolicy TxGossipPolicy { get; } = new();
public IEraStore? EraStore { get; set; }
}
}
182 changes: 182 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/Era1/EraExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.IO.Abstractions;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using System;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
using Nethermind.Era1;
using System.Linq;
using Nethermind.Blockchain.Era1;

namespace Nethermind.Blockchain;
public class EraExporter : IEraExporter
{
private const int MergeBlock = 15537393;
private readonly IFileSystem _fileSystem;
private readonly IBlockTree _blockTree;
private readonly IReceiptStorage _receiptStorage;
private readonly ISpecProvider _specProvider;
private readonly string _networkName;

public event EventHandler<ExportProgressArgs> ExportProgress;
public event EventHandler<VerificationProgressArgs> VerificationProgress;

public string NetworkName => _networkName;

public EraExporter(
IFileSystem fileSystem,
IBlockTree blockTree,
IReceiptStorage receiptStorage,
ISpecProvider specProvider,
string networkName)
{
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_receiptStorage = receiptStorage ?? throw new ArgumentNullException(nameof(receiptStorage));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
if (string.IsNullOrWhiteSpace(networkName)) throw new ArgumentException("Cannot be null or whitespace.", nameof(specProvider));
_networkName = networkName.Trim().ToLower();
}

public async Task Export(
string destinationPath,
long start,
long end,
int size = EraWriter.MaxEra1Size,
CancellationToken cancellation = default)
{
if (destinationPath is null) throw new ArgumentNullException(nameof(destinationPath));
if (_fileSystem.File.Exists(destinationPath)) throw new ArgumentException(nameof(destinationPath), $"Cannot be a file.");
if (size < 1) throw new ArgumentOutOfRangeException(nameof(size), size, $"Must be greater than 0.");
if (size > EraWriter.MaxEra1Size) throw new ArgumentOutOfRangeException(nameof(size), size, $"Cannot be greater than {EraWriter.MaxEra1Size}.");
if (!_fileSystem.Directory.Exists(destinationPath))
{
//TODO look into permission settings - should it be set?
_fileSystem.Directory.CreateDirectory(destinationPath);
}

DateTime startTime = DateTime.Now;
DateTime lastProgress = DateTime.Now;

int totalProcessed = 0;
int processedSinceLast = 0;
int txProcessedSinceLast = 0;

for (long i = start; i <= end; i += size)
{
string filePath = Path.Combine(
destinationPath,
EraWriter.Filename(_networkName, i / size, Keccak.Zero));
using EraWriter? builder = EraWriter.Create(_fileSystem.File.Create(filePath), _specProvider);

//TODO read directly from RocksDb with range reads
for (var y = i; y <= end; y++)
{
Block? block = _blockTree.FindBlock(y, BlockTreeLookupOptions.DoNotCreateLevelIfMissing);

if (block == null)
{
throw new EraException($"Could not find a block with number {y}.");
}

TxReceipt[]? receipts = _receiptStorage.Get(block);
if (receipts == null)
{
//Can this even happen?
throw new EraException($"Could not find receipts for block {block.ToString(Block.Format.FullHashAndNumber)}");
}
if (!await builder.Add(block, receipts, cancellation) || y == end)
{
byte[] root = await builder.Finalize();
builder.Dispose();
string rename = Path.Combine(
destinationPath,
EraWriter.Filename(_networkName, i / size, new Hash256(root)));
_fileSystem.File.Move(
filePath,
rename, true);
break;
}
totalProcessed++;
txProcessedSinceLast += block.Transactions.Length;
processedSinceLast++;
TimeSpan elapsed = DateTime.Now.Subtract(lastProgress);
if (elapsed.TotalSeconds > TimeSpan.FromSeconds(10).TotalSeconds)
{
ExportProgress?.Invoke(this, new ExportProgressArgs(
end - start,
totalProcessed,
processedSinceLast,
txProcessedSinceLast,
elapsed,
DateTime.Now.Subtract(startTime)));
lastProgress = DateTime.Now;
processedSinceLast = 0;
txProcessedSinceLast = 0;
}
}
}
}
/// <summary>
/// Verifies all era1 archives from a directory, with an expected accumulator list from a hex encoded file.
/// </summary>
/// <param name="eraDirectory"></param>
/// <param name="accumulatorFile"></param>
/// <param name="cancellation"></param>
/// <exception cref="EraVerificationException">If the verification fails.</exception>
public async Task VerifyEraFiles(string eraDirectory, string accumulatorFile, CancellationToken cancellation = default)
{
string[] eraFiles = EraReader.GetAllEraFiles(eraDirectory, _networkName).ToArray();
string[] lines = await _fileSystem.File.ReadAllLinesAsync(accumulatorFile, cancellation);

byte[][] accumulators = lines.Select(s => Bytes.FromHexString(s)).ToArray();

await VerifyEraFiles(eraFiles, accumulators);
}
/// <summary>
/// Verifies all era1 files, with an expected accumulator list.
/// </summary>
/// <param name="eraFiles"></param>
/// <param name="expectedAccumulators"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="EraVerificationException">If the verification fails.</exception>
public async Task VerifyEraFiles(string[] eraFiles, byte[][] expectedAccumulators, CancellationToken cancellation = default)
{
if (expectedAccumulators is null) throw new ArgumentNullException(nameof(expectedAccumulators));
if (eraFiles is null) throw new ArgumentNullException(nameof(eraFiles));
if (eraFiles.Length == 0)
throw new ArgumentException("Must have at least one file.", nameof(eraFiles));
if (eraFiles.Length != expectedAccumulators.Length)
throw new ArgumentException("Must have an equal amount of files and accumulators.", nameof(eraFiles));
if (expectedAccumulators.Any(a=>a.Length != 32))
throw new ArgumentException("All accumulators must have a length of 32 bytes.", nameof(eraFiles));

DateTime startTime = DateTime.Now;
DateTime lastProgress = DateTime.Now;
for (int i = 0; i < eraFiles.Length; i++)
{
using EraReader reader = await EraReader.Create(_fileSystem.File.OpenRead(eraFiles[i]), cancellation);
if (!await reader.VerifyAccumulator(expectedAccumulators[i], _specProvider, cancellation))
{
throw new EraVerificationException($"The accumulator for the archive '{eraFiles[i]}' does not match the expected accumulator '{expectedAccumulators[i].ToHexString()}'.");
}

TimeSpan elapsed = DateTime.Now.Subtract(lastProgress);
if (elapsed.TotalSeconds > TimeSpan.FromSeconds(10).TotalSeconds)
{
VerificationProgress?.Invoke(this, new VerificationProgressArgs(i, eraFiles.Length, DateTime.Now.Subtract(startTime)));
lastProgress = DateTime.Now;
}
}
}
}
100 changes: 100 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/Era1/EraStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Era1;

namespace Nethermind.Blockchain;
public class EraStore: IEraStore
{
private readonly char[] _eraSeparator = ['-'];
private readonly Dictionary<long, string> _epochs;
private readonly IFileSystem _fileSystem;

public int EpochCount => _epochs.Count;
public int BiggestEpoch { get; private set; }
public int SmallestEpoch { get; private set; }

public EraStore(string[] eraFiles) : this(eraFiles, new FileSystem()) { }
public EraStore(string[] eraFiles, IFileSystem fileSystem)
{
_epochs = new();
foreach (var file in eraFiles)
{
string[] parts = Path.GetFileName(file).Split(_eraSeparator);
int epoch;
if (parts.Length != 3 || !int.TryParse(parts[1], out epoch) || epoch < 0)
throw new ArgumentException($"Malformed Era1 file '{file}'.",nameof(eraFiles));
_epochs[epoch] = file;
if (epoch > BiggestEpoch)
BiggestEpoch = epoch;
if (epoch < SmallestEpoch)
SmallestEpoch = epoch;
}
_fileSystem = fileSystem;
}
public bool HasEpoch(long epoch) => _epochs.ContainsKey(epoch);

public Task<EraReader> GetReader(long epoch, CancellationToken cancellation = default)
{
GuardMissingEpoch(epoch);
return EraReader.Create(_epochs[epoch], _fileSystem, cancellation);
}

public string GetReaderPath(long epoch)
{
GuardMissingEpoch(epoch);
return _epochs[epoch];
}

public async Task<Block?> FindBlock(long number, CancellationToken cancellation = default)
{
ThrowIfNegative(number);

long partOfEpoch = number == 0 ? 0 : number / EraWriter.MaxEra1Size;
if (!_epochs.ContainsKey(partOfEpoch))
return null;
using (EraReader reader = await EraReader.Create(_fileSystem.File.OpenRead(_epochs[partOfEpoch]), cancellation))
{
(Block b, _, _) = await reader.GetBlockByNumber(number, cancellation);
return b;
}
}
public async Task<(Block?, TxReceipt[]?)> FindBlockAndReceipts(long number, CancellationToken cancellation = default)
{
ThrowIfNegative(number);

long partOfEpoch = number == 0 ? 0 : number / EraWriter.MaxEra1Size;
if (!_epochs.ContainsKey(partOfEpoch))
return (null, null);
using (EraReader reader = await EraReader.Create(_fileSystem.File.OpenRead(_epochs[partOfEpoch]), cancellation))
{
(Block b, TxReceipt[] r, _) = await reader.GetBlockByNumber(number, cancellation);
return (b, r);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ThrowIfNegative(long number)
{
if (number < 0)
throw new ArgumentOutOfRangeException(nameof(number), number, "Cannot be negative.");
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GuardMissingEpoch(long epoch)
{
if (!HasEpoch(epoch))
throw new ArgumentOutOfRangeException($"Does not contain epoch.", epoch, nameof(epoch));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.Serialization;

namespace Nethermind.Blockchain;
public class EraVerificationException : Exception
{
public EraVerificationException()
{
}

public EraVerificationException(string message) : base(message)
{
}

public EraVerificationException(string message, Exception innerException) : base(message, innerException)
{
}
}
Loading
Loading