From 24bd2ccfb7abd6ccdcb2e539a9896d1c3b02a4a7 Mon Sep 17 00:00:00 2001 From: ZhangTao Date: Fri, 19 Nov 2021 14:06:24 +0800 Subject: [PATCH] Add data parameter when deploy and update contract (#837) * add data when deploy and update * revert license format * catch data format exception * revert error print * recover --- neo-cli/CLI/MainService.Contracts.cs | 13 ++++------ neo-cli/CLI/MainService.cs | 37 +++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index 3311081cb..1a32e3f5c 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -27,11 +27,10 @@ partial class MainService /// File path /// Manifest path [ConsoleCommand("deploy", Category = "Contract Commands")] - private void OnDeployCommand(string filePath, string manifestPath = null) + private void OnDeployCommand(string filePath, string manifestPath = null, JObject data = null) { if (NoWallet()) return; - byte[] script = LoadDeploymentScript(filePath, manifestPath, out var nef, out var manifest); - + byte[] script = LoadDeploymentScript(filePath, manifestPath, data, out var nef, out var manifest); Transaction tx; try { @@ -39,10 +38,9 @@ private void OnDeployCommand(string filePath, string manifestPath = null) } catch (InvalidOperationException e) { - Console.WriteLine("Error: " + GetExceptionMessage(e)); + ConsoleHelper.Error(GetExceptionMessage(e)); return; } - UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name); ConsoleHelper.Info("Contract hash: ", $"{hash}"); @@ -62,7 +60,7 @@ private void OnDeployCommand(string filePath, string manifestPath = null) /// File path /// Manifest path [ConsoleCommand("update", Category = "Contract Commands")] - private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifestPath, UInt160 sender, UInt160[] signerAccounts = null) + private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifestPath, UInt160 sender, UInt160[] signerAccounts = null, JObject data = null) { Signer[] signers = Array.Empty(); @@ -93,7 +91,7 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes try { - byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, out var nef, out var manifest); + byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); } catch (InvalidOperationException e) @@ -101,7 +99,6 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes ConsoleHelper.Error(GetExceptionMessage(e)); return; } - ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash); if (contract == null) { diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 5020f651e..6fa7853e8 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -229,7 +229,7 @@ private bool NoWallet() return true; } - private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, out NefFile nef, out ContractManifest manifest) + private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, JObject data, out NefFile nef, out ContractManifest manifest) { if (string.IsNullOrEmpty(manifestFilePath)) { @@ -259,6 +259,18 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, nef = stream.ReadSerializable(); } + ContractParameter dataParameter = null; + if (data is not null) + try + { + dataParameter = ContractParameter.FromJson(data); + } + catch + { + throw new FormatException("invalid data"); + } + + // Basic script checks Script script = new Script(nef.Script); @@ -278,12 +290,15 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, using (ScriptBuilder sb = new ScriptBuilder()) { - sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString()); + if (dataParameter is not null) + sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString(), dataParameter); + else + sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString()); return sb.ToArray(); } } - private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string manifestFilePath, out NefFile nef, out ContractManifest manifest) + private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string manifestFilePath, JObject data, out NefFile nef, out ContractManifest manifest) { if (string.IsNullOrEmpty(manifestFilePath)) { @@ -313,6 +328,17 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m nef = stream.ReadSerializable(); } + ContractParameter dataParameter = null; + if (data is not null) + try + { + dataParameter = ContractParameter.FromJson(data); + } + catch + { + throw new FormatException("invalid data"); + } + // Basic script checks Script script = new Script(nef.Script); @@ -332,7 +358,10 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m using (ScriptBuilder sb = new ScriptBuilder()) { - sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString()); + if (dataParameter is null) + sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString()); + else + sb.EmitDynamicCall(scriptHash, "update", nef.ToArray(), manifest.ToJson().ToString(), dataParameter); return sb.ToArray(); } }