From 38a915cb6e576de78b91997140a73d29bca335b2 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 8 Jul 2024 14:10:11 +0800 Subject: [PATCH] make it more clear --- .../Persistence/UT_MemoryClonedCache.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Neo.UnitTests/Persistence/UT_MemoryClonedCache.cs b/tests/Neo.UnitTests/Persistence/UT_MemoryClonedCache.cs index 8008ef16a1..56afdc31f1 100644 --- a/tests/Neo.UnitTests/Persistence/UT_MemoryClonedCache.cs +++ b/tests/Neo.UnitTests/Persistence/UT_MemoryClonedCache.cs @@ -15,6 +15,22 @@ namespace Neo.UnitTests.Persistence; +/// +/// When adding data to `datacache` , +/// it gets passed to `snapshotcache` during commit. +/// If `snapshotcache` commits, the data is then passed +/// to the underlying store . +/// However, because snapshots are immutable, the new data +/// cannot be retrieved from the snapshot . +/// +/// When deleting data from `datacache` , +/// it won't exist in `datacache` upon commit, and therefore will be removed from `snapshotcache` . +/// Upon `snapshotcache` commit, the data is deleted from the store . +/// However, since the snapshot remains unchanged, the data still exists in the snapshot. +/// If you attempt to read this data from `datacache` or `snapshotcache` , +/// which do not have the data, they will retrieve it from the snapshot instead of the store. +/// Thus, they can still access data that has been deleted. +/// [TestClass] public class UT_MemoryClonedCache { @@ -46,6 +62,7 @@ public void SingleSnapshotCacheTest() var key1 = new KeyBuilder(0, 1); var value1 = new StorageItem([0x03, 0x04]); + Assert.IsFalse(_dataCache.Contains(key1)); _dataCache.Add(key1, value1); Assert.IsTrue(_dataCache.Contains(key1)); @@ -78,6 +95,7 @@ public void SingleSnapshotCacheTest() _snapshotCache = new SnapshotCache(_snapshot); _dataCache = _snapshotCache.CreateSnapshot(); + Assert.IsTrue(_dataCache.Contains(key1)); _dataCache.Delete(key1); Assert.IsFalse(_dataCache.Contains(key1)); @@ -99,6 +117,8 @@ public void SingleSnapshotCacheTest() // so its value after the commit is meaningless and should not be used. _snapshotCache.Commit(); + // The reason that datacache, snapshotcache still contains key1 is because + // they can not find the value from its cache, so they fetch it from the snapshot of the store. Assert.IsTrue(_dataCache.Contains(key1)); Assert.IsTrue(_snapshotCache.Contains(key1)); Assert.IsTrue(_snapshot.Contains(key1.ToArray()));