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

Interface and logic to support snap sync for Paprika #349

Merged
merged 68 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
11e1fde
Multiple changes to support snap sync via RawState:
damian-orzechowski May 23, 2024
1cc6be1
Enabled flag SNAP_SYNC_SUPPORT
damian-orzechowski May 23, 2024
db58fdc
Removed duplicated call to EnsureHash
damian-orzechowski May 23, 2024
6291439
New IRawState implementation to easier parallelize. Elements to suppo…
damian-orzechowski May 31, 2024
8a559dd
Multiple changes to support snap sync via RawState:
damian-orzechowski May 23, 2024
8896044
Enabled flag SNAP_SYNC_SUPPORT
damian-orzechowski May 23, 2024
22e0b24
Removed duplicated call to EnsureHash
damian-orzechowski May 23, 2024
f724335
New IRawState implementation to easier parallelize. Elements to suppo…
damian-orzechowski May 31, 2024
7a4b5a6
Merge branch 'snap-sync' of https://github.com/NethermindEth/Paprika …
damian-orzechowski Jun 3, 2024
9f824eb
Fix method call after merge
damian-orzechowski Jun 5, 2024
25e516a
Dispose CommittedBlockState during commit.
damian-orzechowski Jun 5, 2024
6b08b6d
Added SNAP_SYNC_SUPPORT for release
damian-orzechowski Jun 5, 2024
c8b14ea
Added ReadOnlySyncWorldStateAccessor. Changed visitor to hanlde RlpMe…
damian-orzechowski Jun 12, 2024
f2c0a91
Merge branch 'main' into snap-sync
damian-orzechowski Jun 12, 2024
493782a
Fixes for leaves at last level.
damian-orzechowski Jun 19, 2024
a190c72
Merge main
damian-orzechowski Jun 19, 2024
358f668
Merge branch 'main' into snap-sync
damian-orzechowski Jun 24, 2024
cefbd0a
Hash calc fixes and changes for arbitrary paths and snap sync specific.
damian-orzechowski Jul 26, 2024
80990b2
Hash calc changes. New methods to create nodes from proof data.
damian-orzechowski Aug 5, 2024
64f5d07
Proof creation changes and sync accessor changes.
damian-orzechowski Aug 21, 2024
2e4079e
Multiple changes to support snap sync via RawState:
damian-orzechowski May 23, 2024
de71851
Enabled flag SNAP_SYNC_SUPPORT
damian-orzechowski May 23, 2024
3828dea
Removed duplicated call to EnsureHash
damian-orzechowski May 23, 2024
b057ad7
New IRawState implementation to easier parallelize. Elements to suppo…
damian-orzechowski May 31, 2024
3fd2a20
Multiple changes to support snap sync via RawState:
damian-orzechowski May 23, 2024
cfa14fb
New IRawState implementation to easier parallelize. Elements to suppo…
damian-orzechowski May 31, 2024
16ff325
Fix method call after merge
damian-orzechowski Jun 5, 2024
5ed822f
Dispose CommittedBlockState during commit.
damian-orzechowski Jun 5, 2024
7fd4708
Added SNAP_SYNC_SUPPORT for release
damian-orzechowski Jun 5, 2024
971edb0
Added ReadOnlySyncWorldStateAccessor. Changed visitor to hanlde RlpMe…
damian-orzechowski Jun 12, 2024
1e7cb63
Fixes for leaves at last level.
damian-orzechowski Jun 19, 2024
3585487
Hash calc fixes and changes for arbitrary paths and snap sync specific.
damian-orzechowski Jul 26, 2024
1599c11
Hash calc changes. New methods to create nodes from proof data.
damian-orzechowski Aug 5, 2024
5eabd92
Proof creation changes and sync accessor changes.
damian-orzechowski Aug 21, 2024
ab3ec0b
Fixing merge
damian-orzechowski Aug 22, 2024
9afc3eb
Merge
damian-orzechowski Aug 22, 2024
b083d5b
Merge main with delete by prefix
damian-orzechowski Aug 23, 2024
e935afb
Add RegisterDeleteByPrefix to RawStateMT
damian-orzechowski Aug 23, 2024
6e22ef9
Cleanup
damian-orzechowski Sep 4, 2024
2b8da89
Whitespace format
damian-orzechowski Sep 4, 2024
6790a86
Merge branch 'main' into snap-sync
damian-orzechowski Sep 9, 2024
06a216f
Use RlpMemo for parallel branch hash calc. Removed unused code.
damian-orzechowski Sep 12, 2024
84f1a7c
Format fix
damian-orzechowski Sep 12, 2024
441f504
Remove unused proof leaves logic.
damian-orzechowski Sep 12, 2024
0285a42
More code cleanup
damian-orzechowski Sep 12, 2024
86c55b0
Merge branch 'main' into snap-sync
damian-orzechowski Sep 30, 2024
3352c1a
Merge main
damian-orzechowski Oct 18, 2024
4015253
Do not override Proof entries as Persistent
damian-orzechowski Oct 18, 2024
653435c
Don't persist Proof entry type. New methods to check persistance and …
damian-orzechowski Oct 18, 2024
c703c48
Whitespace format
damian-orzechowski Oct 18, 2024
4a6bc3b
Merge main
damian-orzechowski Oct 25, 2024
67dc124
ApplyRaw doesn't recalculate root hash
damian-orzechowski Oct 26, 2024
42b137f
Move Proof entry type handling out of dictionary into a specific sync…
damian-orzechowski Oct 26, 2024
f5ad75a
Merge main
damian-orzechowski Oct 26, 2024
fdb1f91
Move processing proof nodes into SyncBlockState to avoid 2 commits wh…
damian-orzechowski Oct 27, 2024
e3b5b1a
Measure recalculation of storage tree
damian-orzechowski Oct 27, 2024
43e668b
Allow parallel store trie recalculation for a single storage trie.
damian-orzechowski Oct 27, 2024
57354c6
Do not open new read only transaction after RawState commit - draft c…
damian-orzechowski Oct 27, 2024
47e38db
Merge main
damian-orzechowski Oct 28, 2024
c7232ef
Merged main
damian-orzechowski Nov 28, 2024
336238a
Fix test, fix whitespace
damian-orzechowski Nov 28, 2024
7fa72d5
Merge main
damian-orzechowski Dec 9, 2024
0b028ae
Apply suggestions from code review
damian-orzechowski Dec 23, 2024
d12725b
Refactoring and cleanup
damian-orzechowski Dec 23, 2024
43f8054
Merged main
damian-orzechowski Dec 23, 2024
2be41ce
Merkle trie visitor refactoring
damian-orzechowski Dec 23, 2024
b358a7b
no compiler constant, hint used for Merkle behavior instead
Scooletz Jan 3, 2025
fdd5079
Changes to RecalculateStorageHash for snap sync RlpMemo dependency. A…
damian-orzechowski Jan 3, 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
2 changes: 1 addition & 1 deletion src/Paprika.Tests/Chain/BlockchainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ private static Keccak BuildKey(int i)

private class PreCommit : IPreCommitBehavior
{
public Keccak BeforeCommit(ICommitWithStats commit, CacheBudget budget)
public Keccak BeforeCommit(ICommitWithStats commit, CacheBudget budget, bool isSnapSync = false)
{
var hashCode = RuntimeHelpers.GetHashCode(commit);
Keccak hash = default;
Expand Down
2 changes: 1 addition & 1 deletion src/Paprika.Tests/Chain/PreCommitBehaviorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AssertingKeysPreCommit(HashSet<Keccak> keccaks)
_found = new HashSet<Keccak>();
}

public Keccak BeforeCommit(ICommitWithStats commit, CacheBudget budget)
public Keccak BeforeCommit(ICommitWithStats commit, CacheBudget budget, bool isSnapSync = false)
{
_found.Clear();

Expand Down
130 changes: 89 additions & 41 deletions src/Paprika.Tests/Chain/RawStateTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using FluentAssertions;
using Nethermind.Int256;
using NUnit.Framework;
using Paprika.Chain;
using Paprika.Crypto;
using Paprika.Data;
using Paprika.Merkle;
using Paprika.Store;
using Paprika.RLP;

namespace Paprika.Tests.Chain;

Expand All @@ -29,39 +29,13 @@ public async Task Raw_access_spin()
}

raw.DestroyAccount(account);
raw.Commit();
raw.Commit(keepOpened: true);

raw.Finalize(1);

raw.Hash.Should().Be(Keccak.EmptyTreeHash);
}

[Test]
public async Task Snap_boundary()
{
var account = Values.Key1;
var keccak = Values.Key2;

using var db = PagedDb.NativeMemoryDb(256 * 1024, 2);
var merkle = new ComputeMerkleBehavior();

await using var blockchain = new Blockchain(db, merkle);
using var raw = blockchain.StartRaw();

raw.SetBoundary(NibblePath.FromKey(account).SliceTo(1), keccak);
raw.Commit();

var root1 = raw.Hash;

raw.SetAccount(account, new Account(1, 1));
raw.Commit();

raw.Finalize(1);

var root2 = raw.Hash;

root1.Should().NotBe(root2);
}

[Test]
public async Task Metadata_are_preserved()
Expand All @@ -81,13 +55,13 @@ public async Task Metadata_are_preserved()

using var raw = blockchain.StartRaw();
raw.SetAccount(a, new Account(valueA, valueA));
raw.Commit();
raw.Commit(keepOpened: true);

raw.SetAccount(b, new Account(valueB, valueB));
raw.Commit();
raw.Commit(keepOpened: true);

raw.SetAccount(c, new Account(valueC, valueC));
raw.Commit();
raw.Commit(keepOpened: true);

var root = raw.Hash;

Expand Down Expand Up @@ -116,11 +90,11 @@ public async Task Disposal()
for (uint i = 0; i < 1_000; i++)
{
raw.SetAccount(account, new Account(i, i));
raw.Commit();
raw.Commit(keepOpened: true);
}

raw.DestroyAccount(account);
raw.Commit();
raw.Commit(keepOpened: true);

raw.Finalize(1);

Expand All @@ -140,10 +114,10 @@ public async Task DeleteByPrefix()
using var raw = blockchain.StartRaw();

raw.SetAccount(account, new Account(1, 1));
raw.Commit();
raw.Commit(keepOpened: true);

raw.RegisterDeleteByPrefix(Key.Account(account));
raw.Commit();
raw.Commit(keepOpened: true);

raw.Finalize(1);

Expand All @@ -169,10 +143,10 @@ public async Task DeleteByShortPrefix()

raw.SetAccount(account1, new Account(1, 1));
raw.SetAccount(account2, new Account(2, 2));
raw.Commit();
raw.Commit(keepOpened: true);

raw.RegisterDeleteByPrefix(Key.Account(NibblePath.FromKey(account2).SliceTo(1)));
raw.Commit();
raw.Commit(keepOpened: true);

raw.Finalize(1);

Expand All @@ -185,10 +159,10 @@ public async Task DeleteByShortPrefix()
using var raw2 = blockchain.StartRaw();

raw2.SetAccount(account2, new Account(2, 2));
raw2.Commit();
raw2.Commit(keepOpened: true);

raw2.RegisterDeleteByPrefix(Key.Account(NibblePath.Empty));
raw2.Commit();
raw2.Commit(keepOpened: true);

raw2.Finalize(2);

Expand All @@ -212,17 +186,91 @@ public void DeleteByPrefixStorage()

raw.SetAccount(account, new Account(1, 1));
raw.SetStorage(account, Values.Key2, new byte[] { 1, 2, 3, 4, 5 });
raw.Commit();
raw.Commit(keepOpened: true);

using var read = db.BeginReadOnlyBatch();
read.TryGet(Key.StorageCell(NibblePath.FromKey(account), Values.Key2), out _).Should().BeTrue();

raw.RegisterDeleteByPrefix(Key.StorageCell(NibblePath.FromKey(account), NibblePath.Empty));
raw.Commit();
raw.Commit(keepOpened: true);

raw.Finalize(1);

using var read2 = db.BeginReadOnlyBatch();
read2.TryGet(Key.StorageCell(NibblePath.FromKey(account), Values.Key2), out _).Should().BeFalse();
}

[Test]
public void CalcRootFromRlpMemoDataState()
{
using var db = PagedDb.NativeMemoryDb(256 * 1024, 2);
var merkle = new ComputeMerkleBehavior();

var blockchain = new Blockchain(db, merkle);

using var raw = blockchain.StartRaw();

var random = GetRandom();

Span<byte> rlp = stackalloc byte[1024];
RlpStream stream = new RlpStream(rlp);
stream.StartSequence(529);

//all children to trigger parallel branch hash calculation
byte[] children = new byte[16];
Keccak[] childHashes = new Keccak[16];

for (int i = 0; i < 16; i++)
{
children[i] = (byte)i;
childHashes[i] = random.NextKeccak();
stream.Encode(childHashes[i]);
}
stream.EncodeEmptyArray();
KeccakOrRlp.FromSpan(rlp.Slice(0, stream.Position), out var checkKeccakOrRlp);

raw.CreateMerkleBranch(Keccak.Zero, NibblePath.Empty, children, childHashes);
Keccak newRootHash = raw.RefreshRootHash(true);

newRootHash.Should().Be(checkKeccakOrRlp.Keccak);
}

[Test]
public void CalcRootFromRlpMemoDataStorage()
{
using var db = PagedDb.NativeMemoryDb(256 * 1024, 2);
var merkle = new ComputeMerkleBehavior();

var blockchain = new Blockchain(db, merkle);

using var raw = blockchain.StartRaw();

var account = Values.Key1;

var random = GetRandom();

Span<byte> rlp = stackalloc byte[1024];
RlpStream stream = new RlpStream(rlp);
stream.StartSequence(529);

//all children to trigger parallel branch hash calculation
byte[] children = new byte[16];
Keccak[] childHashes = new Keccak[16];

for (int i = 0; i < 16; i++)
{
children[i] = (byte)i;
childHashes[i] = random.NextKeccak();
stream.Encode(childHashes[i]);
}
stream.EncodeEmptyArray();
KeccakOrRlp.FromSpan(rlp.Slice(0, stream.Position), out var checkKeccakOrRlp);

raw.CreateMerkleBranch(account, NibblePath.Empty, children, childHashes);
Keccak newRootHash = raw.RecalculateStorageRoot(account, true);

newRootHash.Should().Be(checkKeccakOrRlp.Keccak);
}

private static Random GetRandom() => new(13);
}
2 changes: 1 addition & 1 deletion src/Paprika.Tests/Merkle/AdditionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task Account_destruction_same_block()

using var read = blockchain.StartReadOnly(hash);
var account = read.GetAccount(Key0);
var recalculatedStorage = merkle.CalculateStorageHash(read, Key0);
var recalculatedStorage = merkle.GetStorageHash(read, Key0);

recalculatedStorage.Should().Be(account.StorageRootHash);
}
Expand Down
Loading
Loading