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

Logging commit added for debug purposes #454

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
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
72 changes: 70 additions & 2 deletions src/Paprika/Merkle/ComputeMerkleBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Paprika.Store;
using Paprika.Utils;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Paprika.Merkle;

Expand Down Expand Up @@ -984,7 +985,8 @@ private static DeleteStatus TransformExtension(in Key childKey, ICommit commit,
}

[SkipLocalsInit]
private static void MarkPathDirty(in NibblePath path, in Span<byte> rlpMemoWorkingSet, ICommit commit, CacheBudget budget)
private static void MarkPathDirty(in NibblePath path, in Span<byte> rlpMemoWorkingSet, ICommit commit,
CacheBudget budget)
{
// Flag forcing the leaf creation, that saves one get of the non-existent value.
var createLeaf = false;
Expand Down Expand Up @@ -1307,7 +1309,8 @@ public void DoWork(ICommit commit)
}
}

private static Account CalculateStorageRoot(in Keccak keccak, ComputeMerkleBehavior behavior, CacheBudget budget,
private static Account CalculateStorageRoot(in Keccak keccak, ComputeMerkleBehavior behavior,
CacheBudget budget,
PrefixingCommit prefixed, ICommit commit)
{
// Don't parallelize this work as it would be counter-productive to have parallel over parallel.
Expand Down Expand Up @@ -1349,6 +1352,71 @@ private static Account CalculateStorageRoot(in Keccak keccak, ComputeMerkleBehav
public override string ToString() => $"{AccountKeccak}: {Written}";
}

/// <summary>
/// The logging commit not used by the release code.
/// Useful to diagnose which operations are called on the wrapped commit.
/// Capable of deciphering Merkle values.
/// </summary>
public class LoggingCommit(string name, ICommit commit) : ICommit
{
private readonly StringBuilder _logs = new();

public ReadOnlySpanOwnerWithMetadata<byte> Get(scoped in Key key)
{
ReadOnlySpanOwnerWithMetadata<byte> owner = commit.Get(in key);

Log(nameof(Get), key, owner.Span);

return owner;
}

private void Log(string method, in Key key, ReadOnlySpan<byte> span)
{
var path = key.Path.ToString();
string value = "";
if (span.IsEmpty)
{
value = "EMPTY";
}
else
{
Node.ReadFrom(out var type, out var leaf, out var ext, out var branch, span);
switch (type)
{
case Node.Type.Leaf:
value = $"LEAF: {leaf.Path.ToString()}";
break;
case Node.Type.Extension:
value = $"EXTENSION: {ext.Path.ToString()}";
break;
case Node.Type.Branch:
value = $"BRANCH: {branch.Children.ToString()}";
break;
}
}

_logs.AppendLine($"{method}({path}): {value}");
}

public void Set(in Key key, in ReadOnlySpan<byte> payload, EntryType type = EntryType.Persistent)
{
Log(nameof(Set), key, payload);
commit.Set(in key, in payload, type);
}

public void Set(in Key key, in ReadOnlySpan<byte> payload0, in ReadOnlySpan<byte> payload1,
EntryType type = EntryType.Persistent)
{
Log(nameof(Set), key, payload0);
commit.Set(in key, in payload0, in payload1, type);
}

public IChildCommit GetChild() => throw new NotImplementedException();

public bool Owns(object? actualSpanOwner) => commit.Owns(actualSpanOwner);

public override string ToString() => $"{name}: \n {_logs}";
}

public bool CanPrefetch => true;

Expand Down
Loading