Skip to content

Commit

Permalink
Clone byte array and add ut (#2573)
Browse files Browse the repository at this point in the history
* clone byte array and add ut

* fix ut

* optimize

* Update UT_CloneCache.cs

Co-authored-by: Erik Zhang <[email protected]>
  • Loading branch information
ZhangTao1596 and erikzhang authored Aug 15, 2021
1 parent 260031e commit a48e79a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/neo/Persistence/ClonedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ClonedCache(DataCache innerCache)

protected override void AddInternal(StorageKey key, StorageItem value)
{
innerCache.Add(key, value);
innerCache.Add(key, value.Clone());
}

protected override void DeleteInternal(StorageKey key)
Expand Down
34 changes: 32 additions & 2 deletions tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -35,14 +36,14 @@ public void TestAddInternal()
clonedCache[new MyKey("key1")].Should().Be(new MyValue("value1"));

clonedCache.Commit();
myDataCache[new MyKey("key1")].Should().Be(new MyValue("value1"));
Assert.IsTrue(myDataCache[new MyKey("key1")].Value.SequenceEqual(new MyValue("value1").Value));
}

[TestMethod]
public void TestDeleteInternal()
{
myDataCache.Add(new MyKey("key1"), new MyValue("value1"));
clonedCache.Delete(new MyKey("key1")); // trackable.State = TrackState.Deleted
clonedCache.Delete(new MyKey("key1")); // trackable.State = TrackState.Deleted
clonedCache.Commit();

clonedCache.TryGet(new MyKey("key1")).Should().BeNull();
Expand Down Expand Up @@ -124,5 +125,34 @@ public void TestUpdateInternal()
new MyValue("value_new_3").Should().Be(clonedCache[new MyKey("key3")]);
new MyValue("value_new_2").Should().Be(clonedCache[new MyKey("key2")]);
}

[TestMethod]
public void TestCacheOverrideIssue2572()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var storages = snapshot.CreateSnapshot();

storages.Add
(
new StorageKey() { Key = new byte[] { 0x00, 0x01 }, Id = 0 },
new StorageItem() { Value = Array.Empty<byte>() }
);
storages.Add
(
new StorageKey() { Key = new byte[] { 0x01, 0x01 }, Id = 0 },
new StorageItem() { Value = new byte[] { 0x05 } }
);

storages.Commit();

var item = storages.GetAndChange(new StorageKey() { Key = new byte[] { 0x01, 0x01 }, Id = 0 });
item.Value = new byte[] { 0x06 };

var res = snapshot.TryGet(new StorageKey() { Key = new byte[] { 0x01, 0x01 }, Id = 0 });
Assert.AreEqual("05", res.Value.ToHexString());
storages.Commit();
res = snapshot.TryGet(new StorageKey() { Key = new byte[] { 0x01, 0x01 }, Id = 0 });
Assert.AreEqual("06", res.Value.ToHexString());
}
}
}

0 comments on commit a48e79a

Please sign in to comment.