Skip to content

Commit

Permalink
DeleteByPrefixes in RawState
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Aug 22, 2024
1 parent 6b1703c commit 1fdfe59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/Paprika/Chain/Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -1632,6 +1633,7 @@ public async ValueTask DisposeAsync()
/// </summary>
private class RawState : IRawState
{
private ArrayBufferWriter<byte> _prefixesToDelete = new();
private readonly Blockchain _blockchain;
private readonly IDb _db;
private BlockState _current;
Expand Down Expand Up @@ -1698,6 +1700,13 @@ public void SetStorage(in Keccak address, in Keccak storage, ReadOnlySpan<byte>

public void DestroyAccount(in Keccak address) => _current.DestroyAccount(address);

public void RegisterDeleteByPrefix(in Key prefix)
{
var span = _prefixesToDelete.GetSpan(prefix.MaxByteLength);
var leftover = prefix.WriteTo(span);
_prefixesToDelete.Advance(span.Length - leftover.Length);
}

public void Commit()
{
ThrowOnFinalized();
Expand All @@ -1714,6 +1723,8 @@ public void Commit()

using var batch = _db.BeginNextBatch();

DeleteByPrefixes(batch);

var committed = _current.CommitRaw();
committed.Apply(batch);
_current.Dispose();
Expand All @@ -1727,6 +1738,17 @@ public void Commit()
_current = new BlockState(Keccak.Zero, read, ancestors, _blockchain);
}

private void DeleteByPrefixes(IBatch batch)
{
var prefixes = _prefixesToDelete.WrittenSpan;
while (prefixes.IsEmpty == false)
{
prefixes = Key.ReadFrom(prefixes, out var prefixToDelete);
batch.DeleteByPrefix(prefixToDelete);
}
_prefixesToDelete.ResetWrittenCount();
}

public void Finalize(uint blockNumber)
{
ThrowOnFinalized();
Expand Down
6 changes: 6 additions & 0 deletions src/Paprika/Chain/IRawState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public interface IRawState : IReadOnlyWorldState

void DestroyAccount(in Keccak address);

/// <summary>
/// Registers a deletion that will be applied when <see cref="Commit"/> is called.
/// </summary>
/// <param name="prefix"></param>
void RegisterDeleteByPrefix(in Key prefix);

/// <summary>
/// Commits the pending changes.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions src/Paprika/Data/Key.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Diagnostics;
using System.IO.Hashing;
using System.Numerics;
using System.Runtime.CompilerServices;
using Paprika.Crypto;
using Paprika.Store;

namespace Paprika.Data;

Expand Down Expand Up @@ -98,7 +96,6 @@ public static ReadOnlySpan<byte> ReadFrom(ReadOnlySpan<byte> source, out Key key
public bool IsState => Type == DataType.Account ||
(Type == DataType.Merkle && Path.Length < NibblePath.KeccakNibbleCount);


[SkipLocalsInit]
public override int GetHashCode()
{
Expand Down

0 comments on commit 1fdfe59

Please sign in to comment.