Skip to content

Commit

Permalink
Don't delete during full pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Feb 19, 2024
1 parent 7946817 commit 37cf01b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public bool ShouldPersist(long blockNumber)
return inPruning;
}

public bool IsFullPruning => _inPruning != 0;

/// <inheritdoc/>
public void Dispose()
{
Expand Down
31 changes: 31 additions & 0 deletions src/Nethermind/Nethermind.Trie.Test/Pruning/TreeStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Nethermind.State;
using Nethermind.State.Witnesses;
using Nethermind.Trie.Pruning;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Trie.Test.Pruning
Expand Down Expand Up @@ -822,6 +823,7 @@ public void After_commit_should_have_has_root()
trieStore.HasRoot(stateTree.RootHash).Should().BeTrue();
}

[Test]
public async Task Will_RemovePastKeys_OnSnapshot()
{
MemDb memDb = new();
Expand Down Expand Up @@ -854,6 +856,35 @@ public async Task Will_RemovePastKeys_OnSnapshot()
}
}

[Test]
public async Task Will_Not_RemovePastKeys_OnSnapshot_DuringFullPruning()
{
MemDb memDb = new();

IPersistenceStrategy isPruningPersistenceStrategy = Substitute.For<IPersistenceStrategy>();
isPruningPersistenceStrategy.IsFullPruning.Returns(true);

using TrieStore fullTrieStore = CreateTrieStore(
kvStore: memDb,
pruningStrategy: new TestPruningStrategy(true, true, 100000),
persistenceStrategy: isPruningPersistenceStrategy,
reorgDepthOverride: 2);

IScopedTrieStore trieStore = fullTrieStore.GetTrieStore(null);

for (int i = 0; i < 64; i++)
{
TrieNode node = new(NodeType.Leaf, TestItem.Keccaks[i], new byte[2]);
trieStore.CommitNode(i, new NodeCommitInfo(node, TreePath.Empty));
trieStore.FinishBlockCommit(TrieType.State, i, node);

// Pruning is done in background
await Task.Delay(TimeSpan.FromMilliseconds(10));
}

memDb.Count.Should().Be(61);
}

[Test]
public async Task Will_NotRemove_ReCommittedNode()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Trie/Pruning/Archive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public bool ShouldPersist(long blockNumber)
{
return true;
}

public bool IsFullPruning => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public IPersistenceStrategy AddStrategy(IPersistenceStrategy strategy)
}

public bool ShouldPersist(long blockNumber) => _strategies.Any(strategy => strategy.ShouldPersist(blockNumber));
public bool IsFullPruning => _strategies.Any(strategy => strategy.IsFullPruning);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Nethermind.Trie.Pruning
public interface IPersistenceStrategy
{
bool ShouldPersist(long blockNumber);
bool IsFullPruning { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public bool ShouldPersist(long blockNumber)
{
return blockNumber % _snapshotInterval == 0;
}

public bool IsFullPruning => false;
}
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Trie/Pruning/NoPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public bool ShouldPersist(long blockNumber)
{
return false;
}

public bool IsFullPruning => false;
}
}
11 changes: 9 additions & 2 deletions src/Nethermind/Nethermind.Trie/Pruning/TrieStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,16 @@ private bool CanPruneCacheFurther()
_commitSetQueue.Enqueue(toAddBack[index]);
}

bool shouldDeletePersistedNode =
// Its disabled
_pastPathHash != null &&
// Full pruning need to visit all node, so can't delete anything.
!_persistenceStrategy.IsFullPruning &&
// If more than one candidate set, its a reorg, we can't remove node as persisted node may not be canonical
candidateSets.Count == 1;

Dictionary<(Hash256?, TinyTreePath), Hash256?>? persistedHashes =
// If its a reorg, we can't remove node as persisted node may not be canonical
_pastPathHash != null && candidateSets.Count == 1
shouldDeletePersistedNode
? new Dictionary<(Hash256?, TinyTreePath), Hash256?>()
: null;

Expand Down

0 comments on commit 37cf01b

Please sign in to comment.