-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
405 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using FluentAssertions; | ||
using Nethermind.Core.Crypto; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Trie.Test; | ||
|
||
public class TinyTreePathTests | ||
{ | ||
[Test] | ||
public void Should_ConvertFromAndToTreePath() | ||
{ | ||
TreePath path = new TreePath(new ValueHash256("0123456789abcd00000000000000000000000000000000000000000000000000"), 14); | ||
|
||
TinyTreePath tinyPath = new TinyTreePath(path); | ||
|
||
tinyPath.ToTreePath().Should().Be(path); | ||
} | ||
|
||
[Test] | ||
public void When_PathIsTooLong_Should_Throw() | ||
{ | ||
TreePath path = new TreePath(new ValueHash256("0123456789000000000000000000000000000000000000000000000000000000"), 15); | ||
|
||
Action act = () => new TinyTreePath(path); | ||
act.Should().Throw<InvalidOperationException>(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,7 @@ public bool ShouldPrune(in long currentMemory) | |
{ | ||
return false; | ||
} | ||
|
||
public int TrackedPastKeyCount => 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using Nethermind.Core.Crypto; | ||
|
||
namespace Nethermind.Trie; | ||
|
||
/// <summary> | ||
/// Like TreePath, but tiny. Fit in 8 byte, like a long. Can only represent 14 nibble. | ||
/// </summary> | ||
public struct TinyTreePath | ||
{ | ||
public const int MaxNibbleLength = 14; | ||
|
||
long _data; | ||
|
||
// Eh.. readonly? | ||
private Span<byte> AsSpan => MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref _data, 1)); | ||
|
||
public TinyTreePath(in TreePath path) | ||
{ | ||
if (path.Length > MaxNibbleLength) throw new InvalidOperationException("Unable to represent more than 14 nibble"); | ||
Span<byte> pathSpan = path.Path.BytesAsSpan; | ||
Span<byte> selfSpan = AsSpan; | ||
pathSpan[..7].CopyTo(selfSpan); | ||
selfSpan[7] = (byte)path.Length; | ||
} | ||
|
||
public int Length => AsSpan[7]; | ||
|
||
public TreePath ToTreePath() | ||
{ | ||
ValueHash256 rawPath = Keccak.Zero; | ||
Span<byte> pathSpan = rawPath.BytesAsSpan; | ||
Span<byte> selfSpan = AsSpan; | ||
selfSpan[..7].CopyTo(pathSpan); | ||
|
||
return new TreePath(rawPath, selfSpan[7]); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Nethermind/Nethermind.Trie/Pruning/TrackedPastKeyCountStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
namespace Nethermind.Trie.Pruning; | ||
|
||
public class TrackedPastKeyCountStrategy : IPruningStrategy | ||
{ | ||
private IPruningStrategy _baseStrategy; | ||
private readonly int _trackedPastKeyCount; | ||
public bool PruningEnabled => _baseStrategy.PruningEnabled; | ||
|
||
public TrackedPastKeyCountStrategy(IPruningStrategy baseStrategy, int trackedPastKeyCount) | ||
{ | ||
_baseStrategy = baseStrategy; | ||
_trackedPastKeyCount = trackedPastKeyCount; | ||
} | ||
|
||
public bool ShouldPrune(in long currentMemory) | ||
{ | ||
return _baseStrategy.ShouldPrune(in currentMemory); | ||
} | ||
|
||
public int TrackedPastKeyCount => _trackedPastKeyCount; | ||
} |
Oops, something went wrong.