diff --git a/Libplanet.Store/Trie/ITrie.cs b/Libplanet.Store/Trie/ITrie.cs index 58e2a96c973..f6264051dad 100644 --- a/Libplanet.Store/Trie/ITrie.cs +++ b/Libplanet.Store/Trie/ITrie.cs @@ -58,7 +58,8 @@ public interface ITrie /// /// Removes the value at the path corresponding to given - /// in memory. + /// in memory. If there is no at , + /// this does nothing. /// /// The unique key to associate with the . /// Returns new updated . diff --git a/Libplanet.Tests/Store/Trie/MerkleTrieTest.cs b/Libplanet.Tests/Store/Trie/MerkleTrieTest.cs index d815faf32f9..b18b8f58154 100644 --- a/Libplanet.Tests/Store/Trie/MerkleTrieTest.cs +++ b/Libplanet.Tests/Store/Trie/MerkleTrieTest.cs @@ -396,7 +396,7 @@ public void SetValueToFullNode() public void RemoveValue() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); - IStateStore stateStore = new TrieStateStore(new MemoryKeyValueStore()); + IStateStore stateStore = new TrieStateStore(keyValueStore); ITrie trie = stateStore.GetStateRoot(null); HashDigest nullTrieHash = trie.Hash; @@ -529,5 +529,35 @@ public void RemoveValue() Assert.Empty(expected); Assert.Null(trie.Root); } + + [Fact] + public void RemoveValueNoOp() + { + IStateStore stateStore = new TrieStateStore(new MemoryKeyValueStore()); + ITrie trie = stateStore.GetStateRoot(null); + + KeyBytes key00 = new KeyBytes(new byte[] { 0x00 }); + KeyBytes key0000 = new KeyBytes(new byte[] { 0x00, 0x00 }); + IValue value0000 = new Text("0000"); + KeyBytes key0011 = new KeyBytes(new byte[] { 0x00, 0x11 }); + IValue value0011 = new Text("0011"); + KeyBytes key000000 = new KeyBytes(new byte[] { 0x00, 0x00, 0x00 }); + + trie = trie.Set(key0000, value0000); + trie = trie.Set(key0011, value0011); + trie = stateStore.Commit(trie); + int expectedNodeCount = trie.IterateNodes().Count(); + int expectedValueCount = trie.IterateValues().Count(); + HashDigest expectedHash = trie.Hash; + + // Does nothing without throwing an exception when trying to remove value from + // a path where there is a node without value or a non-existent path. + trie = trie.Remove(key00); + trie = trie.Remove(key000000); + trie = stateStore.Commit(trie); + Assert.Equal(expectedNodeCount, trie.IterateNodes().Count()); + Assert.Equal(expectedValueCount, trie.IterateValues().Count()); + Assert.Equal(expectedHash, trie.Hash); + } } }