Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in MemorySnapshot #3133

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Neo/Persistence/MemorySnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?[..];
Comment on lines +68 to 74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return null? If not then change writeBatch to do the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it returns null when it's not found

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see the difference? In the returns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we creating a new pointer?

}

public bool Contains(byte[] key)
{
if (writeBatch.TryGetValue(key, out var value))
{
return value != null;
}

return immutableData.ContainsKey(key);
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down