Skip to content

Commit

Permalink
Small refactors and simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej committed Oct 10, 2024
1 parent 2d260e6 commit c415a53
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 238 deletions.
44 changes: 14 additions & 30 deletions src/Nethermind/Nethermind.Trie/CachedTrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,25 @@ public class CachedTrieStore(IScopedTrieStore @base) : IScopedTrieStore
{
private readonly NonBlocking.ConcurrentDictionary<(TreePath path, Hash256 hash), TrieNode> _cachedNode = new();

public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash)
{
return _cachedNode.GetOrAdd((path, hash), (key) => @base.FindCachedOrUnknown(key.path, key.hash));
}

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return @base.LoadRlp(in path, hash, flags);
}

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return @base.TryLoadRlp(in path, hash, flags);
}

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address)
{
public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash) =>
_cachedNode.GetOrAdd((path, hash), (key) => @base.FindCachedOrUnknown(key.path, key.hash));

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) =>
@base.LoadRlp(in path, hash, flags);

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) =>
@base.TryLoadRlp(in path, hash, flags);

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address) =>
throw new InvalidOperationException("unsupported");
}

public INodeStorage.KeyScheme Scheme => @base.Scheme;

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None)
{
return @base.BeginCommit(trieType, blockNumber, root, writeFlags);
}
public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None) =>
@base.BeginCommit(trieType, blockNumber, root, writeFlags);

public bool IsPersisted(in TreePath path, in ValueHash256 keccak)
{
return @base.IsPersisted(in path, in keccak);
}
public bool IsPersisted(in TreePath path, in ValueHash256 keccak) => @base.IsPersisted(in path, in keccak);

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp)
{
@base.Set(in path, in keccak, rlp);
}
public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp) => @base.Set(in path, in keccak, rlp);
}

3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Trie/NodeCommitInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace Nethermind.Trie
{
public readonly struct NodeCommitInfo
Expand Down Expand Up @@ -27,6 +29,7 @@ public NodeCommitInfo(

public int ChildPositionAtParent { get; }

[MemberNotNullWhen(false, nameof(Node))]
public bool IsEmptyBlockMarker => Node is null;

public bool IsRoot => !IsEmptyBlockMarker && NodeParent is null;
Expand Down
28 changes: 11 additions & 17 deletions src/Nethermind/Nethermind.Trie/PatriciaTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,13 @@ public void Commit(long blockNumber, bool skipRoot = false, WriteFlags writeFlag
ThrowReadOnlyTrieException();
}

int maxLevelForConcurrentCommit = -1;
_writeBeforeCommit /= 64;
if (_writeBeforeCommit > 0)
int maxLevelForConcurrentCommit = _writeBeforeCommit switch
{
maxLevelForConcurrentCommit++; // Ok, we separate at top level
if (_writeBeforeCommit / 16 > 0)
{
maxLevelForConcurrentCommit++; // Another level
}
}
> 64 * 16 => 1, // we separate at two top levels
> 64 => 0, // we separate at top level
_ => -1
};

_writeBeforeCommit = 0;

using (ICommitter committer = TrieStore.BeginCommit(TrieType, blockNumber, RootRef, writeFlags))
Expand Down Expand Up @@ -204,14 +201,11 @@ private void Commit(ICommitter committer, ref TreePath path, NodeCommitInfo node
}
else
{
Task CreateTaskForPath(TreePath childPath, TrieNode childNode, int idx)
Task CreateTaskForPath(TreePath childPath, TrieNode childNode, int idx) => Task.Run(() =>
{
return Task.Run(() =>
{
Commit(committer, ref childPath, new NodeCommitInfo(childNode!, node, idx), maxLevelForConcurrentCommit);
committer.ReturnConcurrencyQuota();
});
}
Commit(committer, ref childPath, new NodeCommitInfo(childNode!, node, idx), maxLevelForConcurrentCommit);
committer.ReturnConcurrencyQuota();
});

ArrayPoolList<Task>? childTasks = null;

Expand Down Expand Up @@ -243,7 +237,7 @@ Task CreateTaskForPath(TreePath childPath, TrieNode childNode, int idx)
}
}

if (childTasks != null)
if (childTasks is not null)
{
Task.WaitAll(childTasks.ToArray());
childTasks.Dispose();
Expand Down
26 changes: 7 additions & 19 deletions src/Nethermind/Nethermind.Trie/Pruning/NullTrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,25 @@ private NullTrieStore() { }

public TrieNode FindCachedOrUnknown(in TreePath treePath, Hash256 hash) => new(NodeType.Unknown, hash);

public byte[] LoadRlp(in TreePath treePath, Hash256 hash, ReadFlags flags = ReadFlags.None) => Array.Empty<byte>();
public byte[] LoadRlp(in TreePath treePath, Hash256 hash, ReadFlags flags = ReadFlags.None) => [];

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) => Array.Empty<byte>();
public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) => [];

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None)
{
return new NullCommitter();
}
public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None) => new NullCommitter();

public bool IsPersisted(in TreePath path, in ValueHash256 keccak) => true;

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp)
{
}
public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp) { }

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256 storageRoot)
{
return this;
}
public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256 storageRoot) => this;

public INodeStorage.KeyScheme Scheme => INodeStorage.KeyScheme.HalfPath;

internal class NullCommitter : ICommitter
{
public void Dispose()
{
}
public void Dispose() { }

public void CommitNode(ref TreePath path, NodeCommitInfo nodeCommitInfo)
{
}
public void CommitNode(ref TreePath path, NodeCommitInfo nodeCommitInfo) { }
}
}
}
107 changes: 31 additions & 76 deletions src/Nethermind/Nethermind.Trie/Pruning/ReadOnlyTrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,25 @@ namespace Nethermind.Trie.Pruning
/// <summary>
/// Safe to be reused for the same wrapped store.
/// </summary>
public class ReadOnlyTrieStore : IReadOnlyTrieStore
public class ReadOnlyTrieStore(TrieStore trieStore, INodeStorage? readOnlyStore) : IReadOnlyTrieStore
{
private readonly TrieStore _trieStore;
private readonly INodeStorage? _readOnlyStore;
private readonly TrieStore _trieStore = trieStore ?? throw new ArgumentNullException(nameof(trieStore));
public INodeStorage.KeyScheme Scheme => _trieStore.Scheme;

public ReadOnlyTrieStore(TrieStore trieStore, INodeStorage? readOnlyStore)
{
_trieStore = trieStore ?? throw new ArgumentNullException(nameof(trieStore));
_readOnlyStore = readOnlyStore;
}

public TrieNode FindCachedOrUnknown(Hash256? address, in TreePath treePath, Hash256 hash) =>
_trieStore.FindCachedOrUnknown(address, treePath, hash, true);

public byte[] LoadRlp(Hash256? address, in TreePath treePath, Hash256 hash, ReadFlags flags) =>
_trieStore.LoadRlp(address, treePath, hash, _readOnlyStore, flags);
_trieStore.LoadRlp(address, treePath, hash, readOnlyStore, flags);
public byte[]? TryLoadRlp(Hash256? address, in TreePath treePath, Hash256 hash, ReadFlags flags) =>
_trieStore.TryLoadRlp(address, treePath, hash, _readOnlyStore, flags);
_trieStore.TryLoadRlp(address, treePath, hash, readOnlyStore, flags);

public bool IsPersisted(Hash256? address, in TreePath path, in ValueHash256 keccak) => _trieStore.IsPersisted(address, path, keccak);

public IReadOnlyTrieStore AsReadOnly(INodeStorage nodeStore)
{
return new ReadOnlyTrieStore(_trieStore, nodeStore);
}
public IReadOnlyTrieStore AsReadOnly(INodeStorage nodeStore) => new ReadOnlyTrieStore(_trieStore, nodeStore);

public ICommitter BeginCommit(TrieType trieType, long blockNumber, Hash256? address, TrieNode? root, WriteFlags writeFlags)
{
return new NullTrieStore.NullCommitter();
}
public ICommitter BeginCommit(TrieType trieType, long blockNumber, Hash256? address, TrieNode? root, WriteFlags writeFlags) =>
new NullTrieStore.NullCommitter();

public event EventHandler<ReorgBoundaryReached> ReorgBoundaryReached
{
Expand All @@ -52,69 +40,36 @@ public event EventHandler<ReorgBoundaryReached> ReorgBoundaryReached

public IReadOnlyKeyValueStore TrieNodeRlpStore => _trieStore.TrieNodeRlpStore;

public void Set(Hash256? address, in TreePath path, in ValueHash256 keccak, byte[] rlp)
{
}
public void Set(Hash256? address, in TreePath path, in ValueHash256 keccak, byte[] rlp) { }

public IScopedTrieStore GetTrieStore(Hash256? address)
{
return new ScopedReadOnlyTrieStore(this, address);
}
public IScopedTrieStore GetTrieStore(Hash256? address) => new ScopedReadOnlyTrieStore(this, address);

public bool HasRoot(Hash256 stateRoot)
{
return _trieStore.HasRoot(stateRoot);
}
public bool HasRoot(Hash256 stateRoot) => _trieStore.HasRoot(stateRoot);

public void Dispose() { }

private class ScopedReadOnlyTrieStore : IScopedTrieStore
private class ScopedReadOnlyTrieStore(ReadOnlyTrieStore fullTrieStore, Hash256? address) : IScopedTrieStore
{
private readonly ReadOnlyTrieStore _trieStoreImplementation;
private readonly Hash256? _address;

public ScopedReadOnlyTrieStore(ReadOnlyTrieStore fullTrieStore, Hash256? address)
{
_trieStoreImplementation = fullTrieStore;
_address = address;
}

public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash)
{
return _trieStoreImplementation.FindCachedOrUnknown(_address, path, hash);
}

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return _trieStoreImplementation.LoadRlp(_address, path, hash, flags);
}

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return _trieStoreImplementation.TryLoadRlp(_address, path, hash, flags);
}

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address)
{
if (address == _address) return this;
return new ScopedReadOnlyTrieStore(_trieStoreImplementation, address);
}

public INodeStorage.KeyScheme Scheme => _trieStoreImplementation.Scheme;

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None)
{
return new NullTrieStore.NullCommitter();
}

public bool IsPersisted(in TreePath path, in ValueHash256 keccak)
{
return _trieStoreImplementation.IsPersisted(_address, path, in keccak);
}

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp)
{
}
public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash) =>
fullTrieStore.FindCachedOrUnknown(address, path, hash);

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) =>
fullTrieStore.LoadRlp(address, path, hash, flags);

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) => fullTrieStore.TryLoadRlp(address, path, hash, flags);

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address1) =>
address1 == address ? this : new ScopedReadOnlyTrieStore(fullTrieStore, address1);

public INodeStorage.KeyScheme Scheme => fullTrieStore.Scheme;

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None) =>
new NullTrieStore.NullCommitter();

public bool IsPersisted(in TreePath path, in ValueHash256 keccak) =>
fullTrieStore.IsPersisted(address, path, in keccak);

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp) { }
}
}
}
70 changes: 23 additions & 47 deletions src/Nethermind/Nethermind.Trie/Pruning/ScopedTrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,28 @@

namespace Nethermind.Trie.Pruning;

public sealed class ScopedTrieStore : IScopedTrieStore
public sealed class ScopedTrieStore(ITrieStore fullTrieStore, Hash256? address) : IScopedTrieStore
{
private readonly ITrieStore _trieStoreImplementation;
private readonly Hash256? _address;

public ScopedTrieStore(ITrieStore fullTrieStore, Hash256? address)
{
_trieStoreImplementation = fullTrieStore;
_address = address;
}

public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash)
{
return _trieStoreImplementation.FindCachedOrUnknown(_address, path, hash);
}

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return _trieStoreImplementation.LoadRlp(_address, path, hash, flags);
}

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None)
{
return _trieStoreImplementation.TryLoadRlp(_address, path, hash, flags);
}

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address)
{
if (address == _address) return this;
return new ScopedTrieStore(_trieStoreImplementation, address);
}

public INodeStorage.KeyScheme Scheme => _trieStoreImplementation.Scheme;

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None)
{
return _trieStoreImplementation.BeginCommit(trieType, blockNumber, _address, root, writeFlags);
}

public bool IsPersisted(in TreePath path, in ValueHash256 keccak)
{
return _trieStoreImplementation.IsPersisted(_address, path, in keccak);
}

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp)
{
_trieStoreImplementation.Set(_address, path, keccak, rlp);
}
public TrieNode FindCachedOrUnknown(in TreePath path, Hash256 hash) =>
fullTrieStore.FindCachedOrUnknown(address, path, hash);

public byte[]? LoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) =>
fullTrieStore.LoadRlp(address, path, hash, flags);

public byte[]? TryLoadRlp(in TreePath path, Hash256 hash, ReadFlags flags = ReadFlags.None) =>
fullTrieStore.TryLoadRlp(address, path, hash, flags);

public ITrieNodeResolver GetStorageTrieNodeResolver(Hash256? address1) =>
address1 == address ? this : new ScopedTrieStore(fullTrieStore, address1);

public INodeStorage.KeyScheme Scheme => fullTrieStore.Scheme;

public ICommitter BeginCommit(TrieType trieType, long blockNumber, TrieNode? root, WriteFlags writeFlags = WriteFlags.None) =>
fullTrieStore.BeginCommit(trieType, blockNumber, address, root, writeFlags);

public bool IsPersisted(in TreePath path, in ValueHash256 keccak) =>
fullTrieStore.IsPersisted(address, path, in keccak);

public void Set(in TreePath path, in ValueHash256 keccak, byte[] rlp) =>
fullTrieStore.Set(address, path, keccak, rlp);
}
Loading

0 comments on commit c415a53

Please sign in to comment.