From 4cc97175c124f3760f607184fdcfd89088d548ff Mon Sep 17 00:00:00 2001 From: doubiliu Date: Wed, 18 Mar 2020 13:13:02 -0500 Subject: [PATCH] Fix bug in contract update/destory (#1483) * first commit * format * commit again * format --- src/neo/SmartContract/InteropService.Contract.cs | 4 ++-- tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs | 3 +++ tests/neo.UnitTests/SmartContract/UT_InteropService.cs | 3 +++ tests/neo.UnitTests/TestUtils.cs | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/neo/SmartContract/InteropService.Contract.cs b/src/neo/SmartContract/InteropService.Contract.cs index b6cd2862b7..f0fb6bfef1 100644 --- a/src/neo/SmartContract/InteropService.Contract.cs +++ b/src/neo/SmartContract/InteropService.Contract.cs @@ -81,7 +81,7 @@ private static bool Contract_Update(ApplicationEngine engine) contract = engine.Snapshot.Contracts.GetAndChange(contract.ScriptHash); contract.Manifest = ContractManifest.Parse(manifest); if (!contract.Manifest.IsValid(contract.ScriptHash)) return false; - if (!contract.HasStorage && engine.Snapshot.Storages.Find(engine.CurrentScriptHash.ToArray()).Any()) return false; + if (!contract.HasStorage && engine.Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id)).Any()) return false; } return true; @@ -94,7 +94,7 @@ private static bool Contract_Destroy(ApplicationEngine engine) if (contract == null) return true; engine.Snapshot.Contracts.Delete(hash); if (contract.HasStorage) - foreach (var (key, _) in engine.Snapshot.Storages.Find(hash.ToArray())) + foreach (var (key, _) in engine.Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id))) engine.Snapshot.Storages.Delete(key); return true; } diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs index 814f55cb67..c187a08043 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs @@ -13,6 +13,7 @@ using Neo.VM; using Neo.VM.Types; using Neo.Wallets; +using System; using System.Linq; using VMArray = Neo.VM.Types.Array; @@ -218,6 +219,7 @@ public void TestContract_Update() Signature = signature } }; + manifest.Features = ContractFeatures.HasStorage; var snapshot = Blockchain.Singleton.GetSnapshot(); var state = TestUtils.GetContract(); state.Manifest.Features = ContractFeatures.HasStorage; @@ -239,6 +241,7 @@ public void TestContract_Update() engine.CurrentContext.EvaluationStack.Push(manifest.ToString()); engine.CurrentContext.EvaluationStack.Push(script); InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeTrue(); + engine.Snapshot.Storages.Find(BitConverter.GetBytes(state.Id)).ToList().Count().Should().Be(1); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index 7ad570ef0f..fb8d668c10 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -848,6 +848,7 @@ public void TestContract_Destroy() engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); engine.LoadScript(new byte[0]); InteropService.Invoke(engine, InteropService.Contract.Destroy).Should().BeTrue(); + engine.Snapshot.Storages.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse(); //storages are removed snapshot = Blockchain.Singleton.GetSnapshot(); @@ -856,6 +857,8 @@ public void TestContract_Destroy() engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); engine.LoadScript(new byte[0]); InteropService.Invoke(engine, InteropService.Contract.Destroy).Should().BeTrue(); + engine.Snapshot.Storages.Find(BitConverter.GetBytes(0x43000000)).Any().Should().BeFalse(); + } public static void LogEvent(object sender, LogEventArgs args) diff --git a/tests/neo.UnitTests/TestUtils.cs b/tests/neo.UnitTests/TestUtils.cs index dfbd973a26..cf3a59c92a 100644 --- a/tests/neo.UnitTests/TestUtils.cs +++ b/tests/neo.UnitTests/TestUtils.cs @@ -60,6 +60,7 @@ internal static ContractState GetContract() { return new ContractState { + Id = 0x43000000, Script = new byte[] { 0x01, 0x01, 0x01, 0x01 }, Manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01")) };