From 6ee22a82cf559e6bc8d7633569c6f61569d7a883 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Tue, 22 Oct 2019 12:30:59 +0200 Subject: [PATCH 1/6] Change Add() to never allow overwriting existing values --- src/neo/IO/Caching/DataCache.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index 1665c8f238..6ce2f850fe 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -47,13 +47,17 @@ public void Add(TKey key, TValue value) { lock (dictionary) { - if (dictionary.TryGetValue(key, out Trackable trackable) && trackable.State != TrackState.Deleted) + if ((dictionary.TryGetValue(key, out Trackable trackable) && trackable.State != TrackState.Deleted) || + TryGetInternal(key) != null) + { throw new ArgumentException(); + } + dictionary[key] = new Trackable { Key = key, Item = value, - State = trackable == null ? TrackState.Added : TrackState.Changed + State = TrackState.Added }; } } From 11bd6a7f179d3b388c598ce80f42b1de26db027c Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Tue, 22 Oct 2019 12:45:23 +0200 Subject: [PATCH 2/6] fix lint error --- src/neo/IO/Caching/DataCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index 6ce2f850fe..f23f5c2c49 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -52,7 +52,7 @@ public void Add(TKey key, TValue value) { throw new ArgumentException(); } - + dictionary[key] = new Trackable { Key = key, From be31a2a04af749ac7731a3eccd4fa81585440351 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Tue, 22 Oct 2019 13:30:04 +0200 Subject: [PATCH 3/6] take Deleted -> Added scenario into account --- src/neo/IO/Caching/DataCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index f23f5c2c49..dcd3f30dd3 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -57,7 +57,7 @@ public void Add(TKey key, TValue value) { Key = key, Item = value, - State = TrackState.Added + State = trackable == null ? TrackState.Added : TrackState.Changed }; } } From 20c6e409700fa5d7eee96ab2f21a1764e51c4cde Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Tue, 22 Oct 2019 14:40:44 +0200 Subject: [PATCH 4/6] fixes for unittests --- src/neo/IO/Caching/DataCache.cs | 11 ++++++++--- src/neo/SmartContract/Native/PolicyContract.cs | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/neo/IO/Caching/DataCache.cs b/src/neo/IO/Caching/DataCache.cs index dcd3f30dd3..589cb1369f 100644 --- a/src/neo/IO/Caching/DataCache.cs +++ b/src/neo/IO/Caching/DataCache.cs @@ -47,10 +47,15 @@ public void Add(TKey key, TValue value) { lock (dictionary) { - if ((dictionary.TryGetValue(key, out Trackable trackable) && trackable.State != TrackState.Deleted) || - TryGetInternal(key) != null) + if (dictionary.TryGetValue(key, out Trackable trackable)) + { + if (trackable.State != TrackState.Deleted) + throw new ArgumentException(); + } + else { - throw new ArgumentException(); + if (TryGetInternal(key) != null) + throw new ArgumentException(); } dictionary[key] = new Trackable diff --git a/src/neo/SmartContract/Native/PolicyContract.cs b/src/neo/SmartContract/Native/PolicyContract.cs index d51e1a75b3..57aa085850 100644 --- a/src/neo/SmartContract/Native/PolicyContract.cs +++ b/src/neo/SmartContract/Native/PolicyContract.cs @@ -47,19 +47,19 @@ private bool CheckValidators(ApplicationEngine engine) internal override bool Initialize(ApplicationEngine engine) { if (!base.Initialize(engine)) return false; - engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_MaxBlockSize), new StorageItem + engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_MaxBlockSize), () => new StorageItem { Value = BitConverter.GetBytes(1024u * 256u) }); - engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_MaxTransactionsPerBlock), new StorageItem + engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_MaxTransactionsPerBlock), () => new StorageItem { Value = BitConverter.GetBytes(512u) }); - engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_FeePerByte), new StorageItem + engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_FeePerByte), () => new StorageItem { Value = BitConverter.GetBytes(1000L) }); - engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_BlockedAccounts), new StorageItem + engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_BlockedAccounts), () => new StorageItem { Value = new UInt160[0].ToByteArray() }); From 29cebaf645ffa3ce5d4e25c73e0b965963b9ac98 Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 9 Dec 2019 19:24:34 -0300 Subject: [PATCH 5/6] Fixing mempool UT to GetAndChange --- tests/neo.UnitTests/Ledger/UT_MemoryPool.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs index 38a456bcf3..6ff5f56289 100644 --- a/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs +++ b/tests/neo.UnitTests/Ledger/UT_MemoryPool.cs @@ -487,8 +487,14 @@ public void TestUpdatePoolForBlockPersisted() var key2 = CreateStorageKey(Prefix_FeePerByte); key1.ScriptHash = NativeContract.Policy.Hash; key2.ScriptHash = NativeContract.Policy.Hash; - snapshot.Storages.Add(key1, item1); - snapshot.Storages.Add(key2, item2); + snapshot.Storages.GetAndChange(key1, () => new StorageItem + { + Value = item1.Value + }); + snapshot.Storages.GetAndChange(key2, () => new StorageItem + { + Value = item2.Value + }); var tx1 = CreateTransaction(); var tx2 = CreateTransaction(); From 115372aa1f543a53605c77e22ed6a28cf3243ea5 Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 9 Dec 2019 19:47:28 -0300 Subject: [PATCH 6/6] Removing wrong test on TestCheckPolicy of Blocked Accounts --- .../SmartContract/Native/UT_PolicyContract.cs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index 0b15d0732d..ba62006be8 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -10,6 +10,10 @@ using Neo.VM; using System.Linq; + +using Neo.Cryptography; +using System; + namespace Neo.UnitTests.SmartContract.Native { [TestClass] @@ -231,21 +235,6 @@ public void TestCheckPolicy() { Transaction tx = Blockchain.GenesisBlock.Transactions[0]; var snapshot = Blockchain.Singleton.GetSnapshot(); - - StorageKey storageKey = new StorageKey - { - ScriptHash = NativeContract.Policy.Hash, - Key = new byte[sizeof(byte)] - }; - storageKey.Key[0] = 15; - snapshot.Storages.Add(storageKey, new StorageItem - { - Value = new UInt160[] { tx.Sender }.ToByteArray(), - }); - - NativeContract.Policy.CheckPolicy(tx, snapshot).Should().BeFalse(); - - snapshot = Blockchain.Singleton.GetSnapshot(); NativeContract.Policy.CheckPolicy(tx, snapshot).Should().BeTrue(); } }