Skip to content

Commit

Permalink
[Neo MemStore] fix memstore commit bug (#3288)
Browse files Browse the repository at this point in the history
* fix memstore commit issue

* update name and format
  • Loading branch information
Jim8y authored Jun 3, 2024
1 parent 68d4005 commit fd1edf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Neo/Persistence/SnapshotCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override void DeleteInternal(StorageKey key)
public override void Commit()
{
base.Commit();
snapshot.Commit();
snapshot?.Commit();
}

protected override bool ContainsInternal(StorageKey key)
Expand Down
49 changes: 33 additions & 16 deletions tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Persistence;
using Neo.SmartContract;
using System;
using System.Linq;
using System.Text;

namespace Neo.UnitTests.Persistence
{
Expand All @@ -30,27 +32,42 @@ public void StoreTest()
{
using var store = new MemoryStore();

store.Delete(new byte[] { 1 });
Assert.AreEqual(null, store.TryGet(new byte[] { 1 }));
store.Put(new byte[] { 1 }, new byte[] { 1, 2, 3 });
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, store.TryGet(new byte[] { 1 }));
store.Delete([1]);
Assert.AreEqual(null, store.TryGet([1]));
store.Put([1], [1, 2, 3]);
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, store.TryGet([1]));

store.Put(new byte[] { 2 }, new byte[] { 4, 5, 6 });
CollectionAssert.AreEqual(new byte[] { 1 }, store.Seek(Array.Empty<byte>(), SeekDirection.Forward).Select(u => u.Key).First());
CollectionAssert.AreEqual(new byte[] { 2 }, store.Seek(new byte[] { 2 }, SeekDirection.Backward).Select(u => u.Key).First());
CollectionAssert.AreEqual(new byte[] { 1 }, store.Seek(new byte[] { 1 }, SeekDirection.Backward).Select(u => u.Key).First());
store.Put([2], [4, 5, 6]);
CollectionAssert.AreEqual(new byte[] { 1 }, store.Seek(Array.Empty<byte>()).Select(u => u.Key).First());
CollectionAssert.AreEqual(new byte[] { 2 }, store.Seek([2], SeekDirection.Backward).Select(u => u.Key).First());
CollectionAssert.AreEqual(new byte[] { 1 }, store.Seek([1], SeekDirection.Backward).Select(u => u.Key).First());

store.Delete(new byte[] { 1 });
store.Delete(new byte[] { 2 });
store.Delete([1]);
store.Delete([2]);

store.Put(new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x00 });
store.Put(new byte[] { 0x00, 0x00, 0x01 }, new byte[] { 0x01 });
store.Put(new byte[] { 0x00, 0x00, 0x02 }, new byte[] { 0x02 });
store.Put(new byte[] { 0x00, 0x00, 0x03 }, new byte[] { 0x03 });
store.Put(new byte[] { 0x00, 0x00, 0x04 }, new byte[] { 0x04 });
store.Put([0x00, 0x00, 0x00], [0x00]);
store.Put([0x00, 0x00, 0x01], [0x01]);
store.Put([0x00, 0x00, 0x02], [0x02]);
store.Put([0x00, 0x00, 0x03], [0x03]);
store.Put([0x00, 0x00, 0x04], [0x04]);

var entries = store.Seek(Array.Empty<byte>(), SeekDirection.Backward).ToArray();
Assert.AreEqual(entries.Count(), 0);
Assert.AreEqual(entries.Length, 0);
}

[TestMethod]
public void NeoSystemStoreViewTest()
{
var neoSystem = new NeoSystem(TestProtocolSettings.Default, new MemoryStoreProvider());
Assert.IsNotNull(neoSystem.StoreView);
var store = neoSystem.StoreView;
var key = new StorageKey(Encoding.UTF8.GetBytes("testKey"));
var value = new StorageItem(Encoding.UTF8.GetBytes("testValue"));
store.Add(key, value);
store.Commit();
var result = store.TryGet(key);
Assert.AreEqual("testValue", Encoding.UTF8.GetString(result.Value.ToArray()));
}

}
}

0 comments on commit fd1edf0

Please sign in to comment.