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 contract update/destory #1483

Merged
merged 5 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/neo/SmartContract/InteropService.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -203,7 +204,7 @@ public void TestContract_Update()
engine.CurrentContext.EvaluationStack.Push(manifestStr);
engine.CurrentContext.EvaluationStack.Push(script);
InteropService.Invoke(engine, InteropService.Contract.Update).Should().BeFalse();

//right
var manifest = ContractManifest.CreateDefault(script.ToScriptHash());
byte[] privkey = { 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
Expand All @@ -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;
Expand All @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
};
Expand Down