Skip to content

Commit

Permalink
Remove dependency on HashSet in SyncBlockState
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-orzechowski committed Jan 17, 2025
1 parent 630674d commit a00b229
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/Paprika/Chain/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,6 @@ public override string ToString() =>
private class SyncBlockState : BlockState
{
private readonly PooledSpanDictionary _proofKeys;
private readonly HashSet<ulong> _proofHashSet;

public SyncBlockState(Keccak parentStateRoot, IReadOnlyBatch batch, CommittedBlockState[] ancestors,
Blockchain blockchain) : base(parentStateRoot, batch, ancestors, blockchain)
Expand All @@ -1428,15 +1427,14 @@ public SyncBlockState(Keccak parentStateRoot, IReadOnlyBatch batch, CommittedBlo
_proofKeys.Dispose();

_proofKeys = new PooledSpanDictionary(Pool, true);
_proofHashSet = new HashSet<ulong>();
}

public override ReadOnlySpanOwnerWithMetadata<byte> Get(scoped in Key key)
{
var hash = GetHash(key);
var keyWritten = key.WriteTo(stackalloc byte[key.MaxByteLength]);

if (_proofHashSet.Contains(hash) && _proofKeys.TryGet(keyWritten, hash, out var span))
if (_proofKeys.TryGet(keyWritten, hash, out var span))
{
AcquireLease();
return new ReadOnlySpanOwner<byte>(span, this).WithDepth(0);
Expand All @@ -1450,17 +1448,16 @@ protected override void SetImpl(in Key key, in ReadOnlySpan<byte> payload, Entry
_hash = null;

var hash = GetHash(key);
var k = key.WriteTo(stackalloc byte[key.MaxByteLength]);
var keyWritten = key.WriteTo(stackalloc byte[key.MaxByteLength]);

if (type == EntryType.Proof || (type == EntryType.Persistent && _proofHashSet.Contains(hash)))
if (type == EntryType.Proof || (type == EntryType.Persistent && _proofKeys.TryGet(keyWritten, hash, out _)))
{
_proofKeys.Set(k, hash, payload, (byte)type);
_proofHashSet.Add(hash);
_proofKeys.Set(keyWritten, hash, payload, (byte)type);
return;
}

_filter.Add(hash);
dict.Set(k, hash, payload, (byte)type);
dict.Set(keyWritten, hash, payload, (byte)type);
}

protected override void SetImpl(in Key key, in ReadOnlySpan<byte> payload0, in ReadOnlySpan<byte> payload1, EntryType type, PooledSpanDictionary dict)
Expand All @@ -1469,22 +1466,20 @@ protected override void SetImpl(in Key key, in ReadOnlySpan<byte> payload0, in R
_hash = null;

var hash = GetHash(key);
var k = key.WriteTo(stackalloc byte[key.MaxByteLength]);
var keyWritten = key.WriteTo(stackalloc byte[key.MaxByteLength]);

if (type == EntryType.Proof || (type == EntryType.Persistent && _proofHashSet.Contains(hash)))
if (type == EntryType.Proof || (type == EntryType.Persistent && _proofKeys.TryGet(keyWritten, hash, out _)))
{
_proofKeys.Set(k, hash, payload0, payload1, (byte)type);
_proofHashSet.Add(hash);
_proofKeys.Set(keyWritten, hash, payload0, payload1, (byte)type);
return;
}

_filter.Add(hash);
dict.Set(k, hash, payload0, payload1, (byte)type);
dict.Set(keyWritten, hash, payload0, payload1, (byte)type);
}

protected override void CleanUp()
{
_proofHashSet.Clear();
_proofKeys.Dispose();
base.CleanUp();
}
Expand Down Expand Up @@ -1550,8 +1545,7 @@ public void ProcessProofNodes(Keccak accountKeccak, Span<byte> packedProofPaths,
var childHash = GetHash(childKey);

bool isPersisted;
//double check due to potential hash collision
if (_proofHashSet.Contains(childHash) && proofNodeResults.TryGetValue(childHash, out bool thisRoundPersisted))
if (proofNodeResults.TryGetValue(childHash, out bool thisRoundPersisted))
{
isPersisted = thisRoundPersisted;
}
Expand Down Expand Up @@ -1588,7 +1582,7 @@ public void ProcessProofNodes(Keccak accountKeccak, Span<byte> packedProofPaths,
: Key.Merkle(extChildPath);
var extChildHash = GetHash(extChildKey);

if (_proofHashSet.Contains(extChildHash) && proofNodeResults[extChildHash] || IsPersisted(accountKeccak, extChildPath))
if (proofNodeResults[extChildHash] || IsPersisted(accountKeccak, extChildPath))
{
proofNodeResults[hash] = true;
_preCommit.Set(k, hash, ext.WriteTo(nodeWorkingSpan), (byte)EntryType.Persistent);
Expand Down

0 comments on commit a00b229

Please sign in to comment.