Skip to content

Commit 7802202

Browse files
authored
[Bug] Fix MemorySnapshot bug (#3953)
* Fix MemorySnapshot bug * new ut
1 parent e45a913 commit 7802202

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Neo/Persistence/Providers/MemorySnapshot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ internal class MemorySnapshot : IStoreSnapshot
3131

3232
public IStore Store { get; }
3333

34+
internal int WriteBatchLength => _writeBatch.Count;
35+
3436
internal MemorySnapshot(MemoryStore store, ConcurrentDictionary<byte[], byte[]> innerData)
3537
{
3638
Store = store;
@@ -46,6 +48,8 @@ public void Commit()
4648
_innerData.TryRemove(pair.Key, out _);
4749
else
4850
_innerData[pair.Key] = pair.Value;
51+
52+
_writeBatch.Clear();
4953
}
5054

5155
public void Delete(byte[] key)

tests/Neo.UnitTests/Persistence/UT_MemorySnapshot.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,51 @@ public void CleanUp()
3434
_memoryStore.Reset();
3535
}
3636

37+
[TestMethod]
38+
public void TestDobleCommit()
39+
{
40+
var key1 = new byte[] { 0x05, 0x02 };
41+
var value1 = new byte[] { 0x06, 0x04 };
42+
43+
var snapshot = (MemorySnapshot)_memoryStore.GetSnapshot();
44+
Assert.AreEqual(0, snapshot.WriteBatchLength);
45+
46+
snapshot.Put(key1, value1);
47+
Assert.AreEqual(1, snapshot.WriteBatchLength);
48+
49+
snapshot.Delete(key1);
50+
Assert.AreEqual(1, snapshot.WriteBatchLength);
51+
snapshot.Commit();
52+
53+
Assert.AreEqual(0, snapshot.WriteBatchLength);
54+
}
55+
56+
[TestMethod]
57+
public void TestDobleCommitTwo()
58+
{
59+
var key1 = new byte[] { 0x51, 0x02 };
60+
var value1 = new byte[] { 0x06, 0x04 };
61+
62+
var snapshotA = (MemorySnapshot)_memoryStore.GetSnapshot();
63+
var snapshotB = (MemorySnapshot)_memoryStore.GetSnapshot();
64+
65+
Assert.IsFalse(_memoryStore.Contains(key1));
66+
snapshotA.Put(key1, value1);
67+
snapshotA.Commit();
68+
Assert.IsTrue(_memoryStore.Contains(key1));
69+
70+
snapshotB.Delete(key1);
71+
snapshotB.Commit();
72+
Assert.IsFalse(_memoryStore.Contains(key1));
73+
74+
snapshotA.Put(key1, value1);
75+
snapshotA.Commit();
76+
Assert.IsTrue(_memoryStore.Contains(key1));
77+
78+
snapshotB.Commit(); // Already committed
79+
Assert.IsTrue(_memoryStore.Contains(key1)); // It fails before #3953
80+
}
81+
3782
[TestMethod]
3883
public void SingleSnapshotTest()
3984
{

0 commit comments

Comments
 (0)