Skip to content

Commit

Permalink
Merge pull request planetarium#1654 from dahlia/fix-prune-states
Browse files Browse the repository at this point in the history
Release 0.23.1 & Fix `TrieStateStore.PruneStates()` throwing `ArgumentOutOfRangeException`
  • Loading branch information
dahlia authored Dec 10, 2021
2 parents 42c1af7 + 00fcb6c commit 8c5e573
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ Libplanet changelog
Version 0.23.1
--------------

To be released.
Released on December 10, 2021.

- Fixed `TrieStateStore.PruneStates()` method's bug that it had thrown
`ArgumentOutOfRangeException`. [[#1653], [#1654]]

[#1653]: https://github.com/planetarium/libplanet/issues/1653
[#1654]: https://github.com/planetarium/libplanet/pull/1654


Version 0.23.0
Expand Down
21 changes: 14 additions & 7 deletions Libplanet.Tests/Store/TrieStateStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,35 @@ public void GetStateRoot(bool secure)
public void PruneStates(bool secure)
{
var values = ImmutableDictionary<string, IValue>.Empty
.Add("foo", (Binary)GetRandomBytes(32))
.Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(32)))
.Add("foo", (Binary)GetRandomBytes(4096))
.Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(2048)))
.Add("baz", (Bencodex.Types.Boolean)false)
.Add("qux", Bencodex.Types.Dictionary.Empty);
.Add("qux", Bencodex.Types.Dictionary.Empty)
.Add(
"zzz",
Bencodex.Types.Dictionary.Empty
.Add("binary", GetRandomBytes(4096))
.Add("text", ByteUtil.Hex(GetRandomBytes(2048))));

var stateStore = new TrieStateStore(_stateKeyValueStore, secure);
ITrie first = stateStore.Commit(null, values);

int prevStatesCount = _stateKeyValueStore.ListKeys().Count();
ImmutableDictionary<string, IValue> nextStates =
values.SetItem("foo", (Binary)GetRandomBytes(32));
values.SetItem("foo", (Binary)GetRandomBytes(4096));
ITrie second = stateStore.Commit(first.Hash, nextStates);

// foo = 0x666f6f
// updated branch node (0x6, aka root) + updated branch node (0x66) +
// updated short node + new value node
Assert.Equal(prevStatesCount + 4, _stateKeyValueStore.ListKeys().Count());
// updated short node + new value nodes
Assert.Equal(prevStatesCount + 5, _stateKeyValueStore.ListKeys().Count());

stateStore.PruneStates(ImmutableHashSet<HashDigest<SHA256>>.Empty.Add(second.Hash));

// It will stay at the same count of nodes.
Assert.Equal(prevStatesCount, _stateKeyValueStore.ListKeys().Count());
// FIXME: Bencodex fingerprints also should be tracked.
// https://github.com/planetarium/libplanet/issues/1653
Assert.Equal(prevStatesCount + 1, _stateKeyValueStore.ListKeys().Count());
}

[Fact]
Expand Down
7 changes: 5 additions & 2 deletions Libplanet/Store/TrieStateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void PruneStates(IImmutableSet<HashDigest<SHA256>> survivingStateRootHash
{
// 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.
// values too. https://github.com/planetarium/libplanet/issues/1653
var stopwatch = new Stopwatch();
_logger.Verbose($"Started {nameof(PruneStates)}()");
var survivalNodes = new HashSet<HashDigest<SHA256>>();
Expand Down Expand Up @@ -72,7 +72,10 @@ public void PruneStates(IImmutableSet<HashDigest<SHA256>> survivingStateRootHash
stopwatch.Restart();
foreach (var stateKey in _stateKeyValueStore.ListKeys())
{
if (survivalNodes.Contains(new HashDigest<SHA256>(stateKey)))
// FIXME: Bencodex fingerprints also should be tracked.
// https://github.com/planetarium/libplanet/issues/1653
if (stateKey.Length != HashDigest<SHA256>.Size ||
survivalNodes.Contains(new HashDigest<SHA256>(stateKey)))
{
continue;
}
Expand Down

0 comments on commit 8c5e573

Please sign in to comment.