Skip to content

Commit

Permalink
Remove branch values
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Dec 5, 2024
1 parent 438549a commit b8dd54a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 68 deletions.
13 changes: 9 additions & 4 deletions src/Nethermind/Ethereum.Trie.Test/TrieTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class TrieTests
[SetUp]
public void Setup()
{
TrieNode.AllowBranchValues = true;
_db = new MemDb();
}

Expand Down Expand Up @@ -59,7 +58,9 @@ private static IEnumerable<TrieTest> LoadTests()
{
return TestLoader.LoadFromFile<Dictionary<string, TrieTestArraysJson>, TrieTest>(
"trietest.json",
dwj => dwj.Select(Convert));
dwj => dwj.Select(Convert))
// Remove branch value tests
.Where(t => t.Input.All(kvp => kvp.Key.Length == 32));
}

private static IEnumerable<TrieTest> LoadSecureTests()
Expand All @@ -73,15 +74,19 @@ private static IEnumerable<TrieTest> LoadAnyOrderTests()
{
IEnumerable<TrieTest> tests = TestLoader.LoadFromFile<Dictionary<string, TrieTestJson>, TrieTest>(
"trieanyorder.json",
dwj => dwj.Select(p => new TrieTest(p.Key, p.Value.In.ToList(), p.Value.Root)));
dwj => dwj.Select(p => new TrieTest(p.Key, p.Value.In.ToList(), p.Value.Root)))
// Remove branch value tests
.Where(t => t.Input.All(kvp => kvp.Key.Length == 32));
return GetTestPermutations(tests);
}

private static IEnumerable<TrieTest> LoadHexEncodedSecureTests()
{
IEnumerable<TrieTest> tests = TestLoader.LoadFromFile<Dictionary<string, TrieTestJson>, TrieTest>(
"hex_encoded_securetrie_test.json",
dwj => dwj.Select(p => new TrieTest(p.Key, p.Value.In.ToList(), p.Value.Root)));
dwj => dwj.Select(p => new TrieTest(p.Key, p.Value.In.ToList(), p.Value.Root)))
// Remove branch value tests
.Where(t => t.Input.All(kvp => kvp.Key.Length == 32));
return GetTestPermutations(tests);
}

Expand Down
13 changes: 0 additions & 13 deletions src/Nethermind/Nethermind.State.Test/NodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ namespace Nethermind.Store.Test
[TestFixture, Parallelizable(ParallelScope.Children)]
public class NodeTest
{
[OneTimeSetUp]
public void Setup()
{
TrieNode.AllowBranchValues = true;
}

[OneTimeTearDown]
public void TearDown()
{
TrieNode.AllowBranchValues = false;
}

[Test]
public void Two_children_store_encode()
{
Expand Down Expand Up @@ -141,7 +129,6 @@ public void Child_and_value_store_encode()

private static ITrieNodeResolver BuildATreeFromNode(TrieNode node)
{
TrieNode.AllowBranchValues = true;
TreePath emptyPath = TreePath.Empty;
CappedArray<byte> rlp = node.RlpEncode(null, ref emptyPath);
node.ResolveKey(null, ref emptyPath, true);
Expand Down
11 changes: 2 additions & 9 deletions src/Nethermind/Nethermind.Trie/TrieNode.Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,15 @@ public static CappedArray<byte> RlpEncodeBranch(TrieNode item, ITrieNodeResolver
{
Metrics.TreeNodeRlpEncodings++;

int valueRlpLength = AllowBranchValues ? Rlp.LengthOf(item.Value.AsSpan()) : 1;
const int valueRlpLength = 1;
int contentLength = valueRlpLength + (UseParallel(canBeParallel) ? GetChildrenRlpLengthForBranchParallel(tree, ref path, item, pool) : GetChildrenRlpLengthForBranch(tree, ref path, item, pool));
int sequenceLength = Rlp.LengthOfSequence(contentLength);
CappedArray<byte> result = pool.SafeRentBuffer(sequenceLength);
Span<byte> resultSpan = result.AsSpan();
int position = Rlp.StartSequence(resultSpan, 0, contentLength);
WriteChildrenRlpBranch(tree, ref path, item, resultSpan.Slice(position, contentLength - valueRlpLength), pool);
position = sequenceLength - valueRlpLength;
if (AllowBranchValues)
{
Rlp.Encode(resultSpan, position, item.Value);
}
else
{
result.AsSpan()[position] = 128;
}
result.AsSpan()[position] = 128;

return result;

Expand Down
46 changes: 4 additions & 42 deletions src/Nethermind/Nethermind.Trie/TrieNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ public sealed partial class TrieNode
private object?[]? _data;
private int _isDirty;

/// <summary>
/// Ethereum Patricia Trie specification allows for branch values,
/// although branched never have values as all the keys are of equal length.
/// Keys are of length 64 for TxTrie and ReceiptsTrie and StateTrie.
///
/// We leave this switch for testing purposes.
/// </summary>
public static bool AllowBranchValues { private get; set; }

/// <summary>
/// Sealed node is the one that is already immutable except for reference counting and resolving existing data
/// </summary>
Expand Down Expand Up @@ -134,32 +125,8 @@ public ref readonly CappedArray<byte> ValueRef
return ref Unsafe.Unbox<CappedArray<byte>>(data);
}

if (!AllowBranchValues)
{
// branches that we use for state will never have value set as all the keys are equal length
return ref CappedArray<byte>.Empty;
}

ref object? obj = ref _data![BranchesCount];
if (obj is null)
{
RlpFactory rlp = _rlp;
if (rlp is null)
{
obj = CappedArray<byte>.EmptyBoxed;
return ref CappedArray<byte>.Empty;
}
else
{
ValueRlpStream rlpStream = rlp.GetRlpStream();
SeekChild(ref rlpStream, BranchesCount);
byte[]? bArr = rlpStream.DecodeByteArray();
obj = new CappedArray<byte>(bArr);
return ref Unsafe.Unbox<CappedArray<byte>>(obj);
}
}

return ref Unsafe.Unbox<CappedArray<byte>>(obj);
// branches that we use for state will never have value set as all the keys are equal length
return ref CappedArray<byte>.Empty;
}
}

Expand Down Expand Up @@ -198,7 +165,7 @@ private void SetValue(in CappedArray<byte> value, bool overrideSealed = false)
ThrowAlreadySealed();
}

if (IsBranch && !AllowBranchValues)
if (IsBranch)
{
// in Ethereum all paths are of equal length, hence branches will never have values
// so we decided to save 1/17th of the array size in memory
Expand Down Expand Up @@ -246,11 +213,6 @@ public bool IsValidWithOneNodeLess
}
}

if (AllowBranchValues)
{
nonEmptyNodes += Value.Length > 0 ? 1 : 0;
}

return nonEmptyNodes > 2;
}
}
Expand Down Expand Up @@ -1077,7 +1039,7 @@ void Initialize(NodeType nodeType)
var data = nodeType switch
{
NodeType.Unknown => ThrowCannotResolveException(),
NodeType.Branch => new object[AllowBranchValues ? BranchesCount + 1 : BranchesCount],
NodeType.Branch => new object[BranchesCount],
_ => new object[2],
};

Expand Down

0 comments on commit b8dd54a

Please sign in to comment.