Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jan 18, 2024
1 parent 46bdf35 commit 495d6a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Libplanet.Store/IStateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public interface IStateStore : IDisposable
/// </summary>
/// <param name="survivingStateRootHashes">The state root hashes <em>not</em> to prune.
/// These state root hashes are guaranteed to survive after pruning.</param>
/// <remarks>
/// As <see cref="Bencodex"/> fingerprinting has been removed and is no longer supported,
/// any <see cref="IStateStore"/> with fingerprinted states stored may no longer work
/// as expected once pruned.
/// </remarks>
void PruneStates(IImmutableSet<HashDigest<SHA256>> survivingStateRootHashes);

/// <summary>
Expand Down
31 changes: 11 additions & 20 deletions Libplanet.Store/TrieStateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,17 @@ public TrieStateStore(IKeyValueStore stateKeyValueStore)
/// <inheritdoc cref="IStateStore.PruneStates(IImmutableSet{HashDigest{SHA256}})"/>
public void PruneStates(IImmutableSet<HashDigest<SHA256>> survivingStateRootHashes)
{
// TODO: As MerkleTrie now have two levels of Merkle trees (one for accounts and one for
// Bencodex values), it needs to be fixed so that it can prune offloaded Bencodex
// values too. https://github.com/planetarium/libplanet/issues/1653
var stopwatch = new Stopwatch();
_logger.Verbose("Started {MethodName}()", nameof(PruneStates));
var survivalNodes = new HashSet<KeyBytes>();
var survivalKeys = new HashSet<KeyBytes>();
foreach (HashDigest<SHA256> stateRootHash in survivingStateRootHashes)
{
var stateTrie = new MerkleTrie(
StateKeyValueStore,
new HashNode(stateRootHash));
var trie = (MerkleTrie)GetStateRoot(stateRootHash);
_logger.Debug("Started to iterate hash nodes");
stopwatch.Start();
foreach ((KeyBytes key, byte[] _) in stateTrie.IterateKeyValuePairs())
foreach ((KeyBytes key, byte[] _) in trie.IterateKeyValuePairs())
{
survivalNodes.Add(key);
survivalKeys.Add(key);
}

_logger.Debug(
Expand All @@ -60,28 +55,24 @@ public void PruneStates(IImmutableSet<HashDigest<SHA256>> survivingStateRootHash
stopwatch.Stop();
}

_logger.Debug("{Count} hash nodes will survive", survivalNodes.Count);
_logger.Debug("{Count} hash nodes will survive", survivalKeys.Count);

// Clean up nodes.
long deleteCount = 0;
_logger.Debug("Started to clean up states...");
stopwatch.Restart();
foreach (var stateKey in StateKeyValueStore.ListKeys())
foreach (var key in StateKeyValueStore.ListKeys())
{
// FIXME: Bencodex fingerprints also should be tracked.
// https://github.com/planetarium/libplanet/issues/1653
if (survivalNodes.Contains(stateKey))
if (!survivalKeys.Contains(key))
{
continue;
StateKeyValueStore.Delete(key);
deleteCount++;
}

StateKeyValueStore.Delete(stateKey);
deleteCount++;
}

_logger.Debug(
"Finished to clean up {DeleteCount} state hashes " +
"(elapsed: {ElapsedMilliseconds} ms)",
"Finished cleaning up by removing {Count} key value pairs " +
"in {ElapsedMilliseconds} ms",
deleteCount,
stopwatch.ElapsedMilliseconds);
stopwatch.Stop();
Expand Down

0 comments on commit 495d6a6

Please sign in to comment.