From 30b7c89ba6a42bef4faab4421a06f67e2047ca58 Mon Sep 17 00:00:00 2001 From: ZhangTao1596 Date: Thu, 11 Nov 2021 10:41:32 +0800 Subject: [PATCH 1/5] add data when deploy and update --- neo-cli/CLI/MainService.Contracts.cs | 37 ++++++++-------------------- neo-cli/CLI/MainService.cs | 30 ++++++++++++++++------ 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index 3311081cb..fd19a605a 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -1,10 +1,10 @@ // Copyright (C) 2016-2021 The Neo Project. -// -// The neo-cli is free software distributed under the MIT software +// +// The neo-cli is free software distributed under the MIT software // license, see the accompanying file LICENSE in the main directory of -// the project or http://www.opensource.org/licenses/mit-license.php +// the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -27,21 +27,12 @@ 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 - { - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); - } - catch (InvalidOperationException e) - { - Console.WriteLine("Error: " + GetExceptionMessage(e)); - return; - } + Transaction tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name); @@ -62,7 +53,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(); @@ -91,16 +82,8 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes Witnesses = Array.Empty() }; - try - { - byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, out var nef, out var manifest); - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); - } - catch (InvalidOperationException e) - { - ConsoleHelper.Error(GetExceptionMessage(e)); - return; - } + byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); + tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); 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..086842811 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -1,10 +1,10 @@ // Copyright (C) 2016-2021 The Neo Project. -// -// The neo-cli is free software distributed under the MIT software +// +// The neo-cli is free software distributed under the MIT software // license, see the accompanying file LICENSE in the main directory of -// the project or http://www.opensource.org/licenses/mit-license.php +// the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -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,10 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, nef = stream.ReadSerializable(); } + ContractParameter dataParameter = null; + if (data is not null) + dataParameter = ContractParameter.FromJson(data); + // Basic script checks Script script = new Script(nef.Script); @@ -278,12 +282,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 +320,10 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m nef = stream.ReadSerializable(); } + ContractParameter dataParameter = null; + if (data is not null) + dataParameter = ContractParameter.FromJson(data); + // Basic script checks Script script = new Script(nef.Script); @@ -332,7 +343,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(); } } From 0669cb210e2df9ca1b0a66d5c8602a9d563c4d7b Mon Sep 17 00:00:00 2001 From: ZhangTao1596 Date: Fri, 12 Nov 2021 09:46:42 +0800 Subject: [PATCH 2/5] revert license format --- neo-cli/CLI/MainService.Contracts.cs | 8 ++++---- neo-cli/CLI/MainService.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index fd19a605a..e98ad0e0e 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -1,10 +1,10 @@ // Copyright (C) 2016-2021 The Neo Project. -// -// The neo-cli is free software distributed under the MIT software +// +// The neo-cli is free software distributed under the MIT software // license, see the accompanying file LICENSE in the main directory of -// the project or http://www.opensource.org/licenses/mit-license.php +// the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 086842811..8e1213d52 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -1,10 +1,10 @@ // Copyright (C) 2016-2021 The Neo Project. -// -// The neo-cli is free software distributed under the MIT software +// +// The neo-cli is free software distributed under the MIT software // license, see the accompanying file LICENSE in the main directory of -// the project or http://www.opensource.org/licenses/mit-license.php +// the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. From 2a97a531bab8ee29fd1ac0ae490a4708a9a8f964 Mon Sep 17 00:00:00 2001 From: ZhangTao1596 Date: Wed, 17 Nov 2021 18:23:10 +0800 Subject: [PATCH 3/5] catch data format exception --- neo-cli/CLI/MainService.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 8e1213d52..6fa7853e8 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -261,7 +261,15 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath, ContractParameter dataParameter = null; if (data is not null) - dataParameter = ContractParameter.FromJson(data); + try + { + dataParameter = ContractParameter.FromJson(data); + } + catch + { + throw new FormatException("invalid data"); + } + // Basic script checks @@ -322,7 +330,14 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m ContractParameter dataParameter = null; if (data is not null) - dataParameter = ContractParameter.FromJson(data); + try + { + dataParameter = ContractParameter.FromJson(data); + } + catch + { + throw new FormatException("invalid data"); + } // Basic script checks From 2b68502e2e1875d17b46c482092807d39ec23343 Mon Sep 17 00:00:00 2001 From: ZhangTao1596 Date: Thu, 18 Nov 2021 16:16:39 +0800 Subject: [PATCH 4/5] revert error print --- neo-cli/CLI/MainService.Contracts.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index e98ad0e0e..210d9813d 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -31,9 +31,16 @@ private void OnDeployCommand(string filePath, string manifestPath = null, JObjec { if (NoWallet()) return; byte[] script = LoadDeploymentScript(filePath, manifestPath, data, out var nef, out var manifest); - - Transaction tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); - + Transaction tx; + try + { + tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); + } + catch (InvalidOperationException e) + { + ConsoleHelper.Error(GetExceptionMessage(e)); + return; + } UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name); ConsoleHelper.Info("Contract hash: ", $"{hash}"); @@ -83,8 +90,15 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes }; byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); - + try + { + tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); + } + catch (InvalidOperationException e) + { + ConsoleHelper.Error(GetExceptionMessage(e)); + return; + } ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash); if (contract == null) { From 1e299284db26a0df099aa6ca3bdd46c80a7b09e9 Mon Sep 17 00:00:00 2001 From: ZhangTao1596 Date: Thu, 18 Nov 2021 18:50:26 +0800 Subject: [PATCH 5/5] recover --- neo-cli/CLI/MainService.Contracts.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index 210d9813d..1a32e3f5c 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -89,9 +89,9 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes Witnesses = Array.Empty() }; - byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); try { + byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); } catch (InvalidOperationException e)