From b68ab0ad30dcc9cd9907daf387e5bbfb561b6be5 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Tue, 15 Dec 2020 23:37:51 +0800 Subject: [PATCH 1/2] add contract deploy/update/destory event --- .../Native/ManagementContract.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/neo/SmartContract/Native/ManagementContract.cs b/src/neo/SmartContract/Native/ManagementContract.cs index 124be72a88..5d8bbd6601 100644 --- a/src/neo/SmartContract/Native/ManagementContract.cs +++ b/src/neo/SmartContract/Native/ManagementContract.cs @@ -21,6 +21,56 @@ public sealed class ManagementContract : NativeContract private const byte Prefix_NextAvailableId = 15; private const byte Prefix_Contract = 8; + internal ManagementContract() + { + var events = new List(Manifest.Abi.Events) + { + new ContractEventDescriptor + { + Name = "Deploy", + Parameters = new ContractParameterDefinition[] + { + new ContractParameterDefinition() + { + Name = "Contract", + Type = ContractParameterType.Hash160 + } + } + }, + new ContractEventDescriptor + { + Name = "Update", + Parameters = new ContractParameterDefinition[] + { + new ContractParameterDefinition() + { + Name = "Contract", + Type = ContractParameterType.Hash160 + }, + new ContractParameterDefinition() + { + Name = "UpdateCounter", + Type = ContractParameterType.Integer + } + } + }, + new ContractEventDescriptor + { + Name = "Destory", + Parameters = new ContractParameterDefinition[] + { + new ContractParameterDefinition() + { + Name = "Contract", + Type = ContractParameterType.Hash160 + } + } + } + }; + + Manifest.Abi.Events = events.ToArray(); + } + private int GetNextAvailableId(StoreView snapshot) { StorageItem item = snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_NextAvailableId), () => new StorageItem(1)); @@ -116,6 +166,8 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma if (md != null) engine.CallFromNativeContract(Hash, hash, md.Name, false); + engine.SendNotification(Hash, "Deploy", new VM.Types.Array { contract.Hash.ToArray() }); + return contract; } @@ -154,6 +206,7 @@ private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest) if (md != null) engine.CallFromNativeContract(Hash, contract.Hash, md.Name, true); } + engine.SendNotification(Hash, "Update", new VM.Types.Array { contract.Hash.ToArray(), (int)contract.UpdateCounter }); } [ContractMethod(0_01000000, CallFlags.WriteStates)] @@ -166,6 +219,7 @@ private void Destroy(ApplicationEngine engine) engine.Snapshot.Storages.Delete(ckey); foreach (var (key, _) in engine.Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id))) engine.Snapshot.Storages.Delete(key); + engine.SendNotification(Hash, "Destory", new VM.Types.Array { hash.ToArray() }); } } } From 8e31f2a7616ae083430958ceb8b5d45d306459ec Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 16 Dec 2020 14:17:22 +0800 Subject: [PATCH 2/2] fix --- .../Native/ManagementContract.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/neo/SmartContract/Native/ManagementContract.cs b/src/neo/SmartContract/Native/ManagementContract.cs index 5d8bbd6601..578504f719 100644 --- a/src/neo/SmartContract/Native/ManagementContract.cs +++ b/src/neo/SmartContract/Native/ManagementContract.cs @@ -32,7 +32,7 @@ internal ManagementContract() { new ContractParameterDefinition() { - Name = "Contract", + Name = "Hash", Type = ContractParameterType.Hash160 } } @@ -44,13 +44,8 @@ internal ManagementContract() { new ContractParameterDefinition() { - Name = "Contract", + Name = "Hash", Type = ContractParameterType.Hash160 - }, - new ContractParameterDefinition() - { - Name = "UpdateCounter", - Type = ContractParameterType.Integer } } }, @@ -61,7 +56,7 @@ internal ManagementContract() { new ContractParameterDefinition() { - Name = "Contract", + Name = "Hash", Type = ContractParameterType.Hash160 } } @@ -127,7 +122,7 @@ public IEnumerable ListContracts(StoreView snapshot) return snapshot.Storages.Find(listContractsPrefix).Select(kvp => kvp.Value.GetInteroperable()); } - [ContractMethod(0, CallFlags.WriteStates)] + [ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)] private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] manifest) { if (!(engine.ScriptContainer is Transaction tx)) @@ -171,7 +166,7 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma return contract; } - [ContractMethod(0, CallFlags.WriteStates)] + [ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)] private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest) { if (nefFile is null && manifest is null) throw new ArgumentException(); @@ -206,10 +201,10 @@ private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest) if (md != null) engine.CallFromNativeContract(Hash, contract.Hash, md.Name, true); } - engine.SendNotification(Hash, "Update", new VM.Types.Array { contract.Hash.ToArray(), (int)contract.UpdateCounter }); + engine.SendNotification(Hash, "Update", new VM.Types.Array { contract.Hash.ToArray() }); } - [ContractMethod(0_01000000, CallFlags.WriteStates)] + [ContractMethod(0_01000000, CallFlags.WriteStates | CallFlags.AllowNotify)] private void Destroy(ApplicationEngine engine) { UInt160 hash = engine.CallingScriptHash;