diff --git a/src/Neo/Persistence/MemorySnapshot.cs b/src/Neo/Persistence/MemorySnapshot.cs index 756a4c83b2..b2ac191498 100644 --- a/src/Neo/Persistence/MemorySnapshot.cs +++ b/src/Neo/Persistence/MemorySnapshot.cs @@ -65,12 +65,22 @@ public void Put(byte[] key, byte[] value) public byte[] TryGet(byte[] key) { - immutableData.TryGetValue(key, out byte[] value); + if (writeBatch.TryGetValue(key, out var value)) + { + return value; + } + + immutableData.TryGetValue(key, out value); return value?[..]; } public bool Contains(byte[] key) { + if (writeBatch.TryGetValue(key, out var value)) + { + return value != null; + } + return immutableData.ContainsKey(key); } } diff --git a/tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs b/tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs index 50da353f7c..ca42d316c3 100644 --- a/tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs +++ b/tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs @@ -19,6 +19,31 @@ namespace Neo.UnitTests.Persistence [TestClass] public class UT_MemoryStore { + [TestMethod] + public void SnapshotTest() + { + using var store = new MemoryStore(); + + store.Put(new byte[] { 1 }, new byte[] { 1, 2, 3 }); + + var snapshot = store.GetSnapshot(); + + snapshot.Put(new byte[] { 1 }, new byte[] { 1, 2, 3, 4 }); + + // Test TryGet + + CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, store.TryGet(new byte[] { 1 })); + CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, snapshot.TryGet(new byte[] { 1 })); + + // Test Contains + + Assert.IsFalse(store.Contains(new byte[] { 2 })); + Assert.IsFalse(snapshot.Contains(new byte[] { 2 })); + snapshot.Put(new byte[] { 2 }, new byte[] { 1, 2, 3, 4 }); + Assert.IsFalse(store.Contains(new byte[] { 2 })); + Assert.IsTrue(snapshot.Contains(new byte[] { 2 })); + } + [TestMethod] public void StoreTest() {