From 80b64e90e70e130aab93536f152e134db1d031e5 Mon Sep 17 00:00:00 2001 From: scooletz Date: Thu, 19 Dec 2024 15:18:21 +0100 Subject: [PATCH] Logging commit added for debug purposes --- src/Paprika/Merkle/ComputeMerkleBehavior.cs | 72 ++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Paprika/Merkle/ComputeMerkleBehavior.cs b/src/Paprika/Merkle/ComputeMerkleBehavior.cs index 3dae3a99..409e7a51 100644 --- a/src/Paprika/Merkle/ComputeMerkleBehavior.cs +++ b/src/Paprika/Merkle/ComputeMerkleBehavior.cs @@ -11,6 +11,7 @@ using Paprika.Store; using Paprika.Utils; using System.Diagnostics.CodeAnalysis; +using System.Text; namespace Paprika.Merkle; @@ -984,7 +985,8 @@ private static DeleteStatus TransformExtension(in Key childKey, ICommit commit, } [SkipLocalsInit] - private static void MarkPathDirty(in NibblePath path, in Span rlpMemoWorkingSet, ICommit commit, CacheBudget budget) + private static void MarkPathDirty(in NibblePath path, in Span rlpMemoWorkingSet, ICommit commit, + CacheBudget budget) { // Flag forcing the leaf creation, that saves one get of the non-existent value. var createLeaf = false; @@ -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. @@ -1349,6 +1352,71 @@ private static Account CalculateStorageRoot(in Keccak keccak, ComputeMerkleBehav public override string ToString() => $"{AccountKeccak}: {Written}"; } + /// + /// 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. + /// + public class LoggingCommit(string name, ICommit commit) : ICommit + { + private readonly StringBuilder _logs = new(); + + public ReadOnlySpanOwnerWithMetadata Get(scoped in Key key) + { + ReadOnlySpanOwnerWithMetadata owner = commit.Get(in key); + + Log(nameof(Get), key, owner.Span); + + return owner; + } + + private void Log(string method, in Key key, ReadOnlySpan 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 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 payload0, in ReadOnlySpan 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;