From de7b1e08c897d793fb68ad8834a8dbb9e73681a0 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Fri, 24 Apr 2020 18:32:38 +0800 Subject: [PATCH 1/4] Datacache correction --- src/neo/IO/Caching/DataCache.cs | 3 +++ tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs | 3 +++ tests/neo.UnitTests/IO/Caching/UT_DataCache.cs | 3 +++ tests/neo.UnitTests/Wallets/UT_Wallet.cs | 4 ++-- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index d20d1bba2e..a9d1520008 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -80,12 +80,15 @@ public void Commit() { case TrackState.Added: AddInternal(trackable.Key, trackable.Item); + trackable.State = TrackState.None; break; case TrackState.Changed: UpdateInternal(trackable.Key, trackable.Item); + trackable.State = TrackState.None; break; case TrackState.Deleted: DeleteInternal(trackable.Key); + dictionary.Remove(trackable.Key); break; } } diff --git a/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs b/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs index b75a70ee97..8fa1992808 100644 --- a/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs +++ b/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs @@ -35,6 +35,7 @@ public void TestAddInternal() cloneCache.Commit(); myDataCache[new MyKey("key1")].Should().Be(new MyValue("value1")); + cloneCache.Commit(); } [TestMethod] @@ -46,6 +47,7 @@ public void TestDeleteInternal() cloneCache.TryGet(new MyKey("key1")).Should().BeNull(); myDataCache.TryGet(new MyKey("key1")).Should().BeNull(); + cloneCache.Commit(); } [TestMethod] @@ -122,6 +124,7 @@ public void TestUpdateInternal() cloneCache[new MyKey("key2")].Should().Be(new MyValue("value_new_2")); cloneCache[new MyKey("key3")].Should().Be(new MyValue("value_new_3")); myDataCache[new MyKey("key2")].Should().Be(new MyValue("value_new_2")); + cloneCache.Commit(); } } } diff --git a/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs b/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs index cd9752c819..a03f9f4e61 100644 --- a/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs +++ b/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs @@ -225,6 +225,7 @@ public void TestCommit() myDataCache.InnerDict[new MyKey("key1")].Should().Be(new MyValue("value1")); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); myDataCache.InnerDict[new MyKey("key3")].Should().Be(new MyValue("value4")); + myDataCache.Commit(); } [TestMethod] @@ -244,6 +245,7 @@ public void TestDelete() myDataCache.Delete(new MyKey("key2")); myDataCache.Commit(); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); + myDataCache.Commit(); } [TestMethod] @@ -261,6 +263,7 @@ public void TestDeleteWhere() myDataCache.TryGet(new MyKey("key2")).Should().BeNull(); myDataCache.InnerDict.ContainsKey(new MyKey("key1")).Should().BeFalse(); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); + myDataCache.Commit(); } [TestMethod] diff --git a/tests/neo.UnitTests/Wallets/UT_Wallet.cs b/tests/neo.UnitTests/Wallets/UT_Wallet.cs index 63ddb327fe..e93879c536 100644 --- a/tests/neo.UnitTests/Wallets/UT_Wallet.cs +++ b/tests/neo.UnitTests/Wallets/UT_Wallet.cs @@ -298,7 +298,7 @@ public void TestMakeTransaction1() }, UInt160.Zero); action.Should().Throw(); - action = () => wallet.MakeTransaction(new TransferOutput[] + /*action = () => wallet.MakeTransaction(new TransferOutput[] { new TransferOutput() { @@ -307,7 +307,7 @@ public void TestMakeTransaction1() Value = new BigDecimal(1,8) } }, account.ScriptHash); - action.Should().Throw(); + action.Should().Throw();*/ action = () => wallet.MakeTransaction(new TransferOutput[] { From d24b32d1f4e08b48d399e220fcf3224756cf36d6 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 24 Apr 2020 12:47:02 +0200 Subject: [PATCH 2/4] Revert comment --- tests/neo.UnitTests/Wallets/UT_Wallet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/neo.UnitTests/Wallets/UT_Wallet.cs b/tests/neo.UnitTests/Wallets/UT_Wallet.cs index e93879c536..63ddb327fe 100644 --- a/tests/neo.UnitTests/Wallets/UT_Wallet.cs +++ b/tests/neo.UnitTests/Wallets/UT_Wallet.cs @@ -298,7 +298,7 @@ public void TestMakeTransaction1() }, UInt160.Zero); action.Should().Throw(); - /*action = () => wallet.MakeTransaction(new TransferOutput[] + action = () => wallet.MakeTransaction(new TransferOutput[] { new TransferOutput() { @@ -307,7 +307,7 @@ public void TestMakeTransaction1() Value = new BigDecimal(1,8) } }, account.ScriptHash); - action.Should().Throw();*/ + action.Should().Throw(); action = () => wallet.MakeTransaction(new TransferOutput[] { From 0584e6606d556cd950fe7a71a296344413add1d5 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Sun, 26 Apr 2020 12:01:41 +0800 Subject: [PATCH 3/4] Code optimization --- src/neo/IO/Caching/DataCache.cs | 7 ++++++- tests/neo.UnitTests/Wallets/UT_Wallet.cs | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index a9d1520008..dd0d5221c8 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -75,6 +75,7 @@ public void Add(TKey key, TValue value) /// public void Commit() { + LinkedList deletedItem = new LinkedList(); foreach (Trackable trackable in GetChangeSet()) switch (trackable.State) { @@ -88,9 +89,13 @@ public void Commit() break; case TrackState.Deleted: DeleteInternal(trackable.Key); - dictionary.Remove(trackable.Key); + deletedItem.AddFirst(trackable.Key); break; } + foreach (TKey key in deletedItem) + { + dictionary.Remove(key); + } } public DataCache CreateSnapshot() diff --git a/tests/neo.UnitTests/Wallets/UT_Wallet.cs b/tests/neo.UnitTests/Wallets/UT_Wallet.cs index 63ddb327fe..bf35592bf3 100644 --- a/tests/neo.UnitTests/Wallets/UT_Wallet.cs +++ b/tests/neo.UnitTests/Wallets/UT_Wallet.cs @@ -209,6 +209,7 @@ public void TestGetAvailable() wallet.GetAvailable(NativeContract.GAS.Hash).Should().Be(new BigDecimal(1000000000000, 8)); + entry = snapshot.Storages.GetAndChange(key, () => new StorageItem(new Nep5AccountState())); entry.GetInteroperable().Balance = 0; snapshot.Commit(); } @@ -231,6 +232,7 @@ public void TestGetBalance() wallet.GetBalance(UInt160.Zero, new UInt160[] { account.ScriptHash }).Should().Be(new BigDecimal(0, 0)); wallet.GetBalance(NativeContract.GAS.Hash, new UInt160[] { account.ScriptHash }).Should().Be(new BigDecimal(1000000000000, 8)); + entry = snapshot.Storages.GetAndChange(key, () => new StorageItem(new Nep5AccountState())); entry.GetInteroperable().Balance = 0; snapshot.Commit(); } @@ -354,6 +356,8 @@ public void TestMakeTransaction1() }); tx.Should().NotBeNull(); + entry1 = snapshot.Storages.GetAndChange(key, () => new StorageItem(new Nep5AccountState())); + entry2 = snapshot.Storages.GetAndChange(key, () => new StorageItem(new Nep5AccountState())); entry1.GetInteroperable().Balance = 0; entry2.GetInteroperable().Balance = 0; snapshot.Commit(); @@ -383,6 +387,7 @@ public void TestMakeTransaction2() tx = wallet.MakeTransaction(new byte[] { }, null, new TransactionAttribute[] { }); tx.Should().NotBeNull(); + entry = snapshot.Storages.GetAndChange(key, () => new StorageItem(new Nep5AccountState())); entry.GetInteroperable().Balance = 0; snapshot.Commit(); } From fd1c085ae9e70d74a52a5b9024f78561ff12fe43 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Sun, 26 Apr 2020 13:49:32 +0800 Subject: [PATCH 4/4] Code optimization --- tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs | 3 --- tests/neo.UnitTests/IO/Caching/UT_DataCache.cs | 3 --- 2 files changed, 6 deletions(-) diff --git a/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs b/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs index 8fa1992808..b75a70ee97 100644 --- a/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs +++ b/tests/neo.UnitTests/IO/Caching/UT_CloneCache.cs @@ -35,7 +35,6 @@ public void TestAddInternal() cloneCache.Commit(); myDataCache[new MyKey("key1")].Should().Be(new MyValue("value1")); - cloneCache.Commit(); } [TestMethod] @@ -47,7 +46,6 @@ public void TestDeleteInternal() cloneCache.TryGet(new MyKey("key1")).Should().BeNull(); myDataCache.TryGet(new MyKey("key1")).Should().BeNull(); - cloneCache.Commit(); } [TestMethod] @@ -124,7 +122,6 @@ public void TestUpdateInternal() cloneCache[new MyKey("key2")].Should().Be(new MyValue("value_new_2")); cloneCache[new MyKey("key3")].Should().Be(new MyValue("value_new_3")); myDataCache[new MyKey("key2")].Should().Be(new MyValue("value_new_2")); - cloneCache.Commit(); } } } diff --git a/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs b/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs index a03f9f4e61..cd9752c819 100644 --- a/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs +++ b/tests/neo.UnitTests/IO/Caching/UT_DataCache.cs @@ -225,7 +225,6 @@ public void TestCommit() myDataCache.InnerDict[new MyKey("key1")].Should().Be(new MyValue("value1")); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); myDataCache.InnerDict[new MyKey("key3")].Should().Be(new MyValue("value4")); - myDataCache.Commit(); } [TestMethod] @@ -245,7 +244,6 @@ public void TestDelete() myDataCache.Delete(new MyKey("key2")); myDataCache.Commit(); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); - myDataCache.Commit(); } [TestMethod] @@ -263,7 +261,6 @@ public void TestDeleteWhere() myDataCache.TryGet(new MyKey("key2")).Should().BeNull(); myDataCache.InnerDict.ContainsKey(new MyKey("key1")).Should().BeFalse(); myDataCache.InnerDict.ContainsKey(new MyKey("key2")).Should().BeFalse(); - myDataCache.Commit(); } [TestMethod]