Skip to content

Commit

Permalink
Add data parameter when deploy and update contract (neo-project#837)
Browse files Browse the repository at this point in the history
* add data when deploy and update

* revert license format

* catch data format exception

* revert error print

* recover
  • Loading branch information
ZhangTao authored Nov 19, 2021
1 parent 5c2edbf commit 24bd2cc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
13 changes: 5 additions & 8 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ partial class MainService
/// <param name="filePath">File path</param>
/// <param name="manifestPath">Manifest path</param>
[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));
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}

UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name);

ConsoleHelper.Info("Contract hash: ", $"{hash}");
Expand All @@ -62,7 +60,7 @@ private void OnDeployCommand(string filePath, string manifestPath = null)
/// <param name="filePath">File path</param>
/// <param name="manifestPath">Manifest path</param>
[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<Signer>();

Expand Down Expand Up @@ -93,15 +91,14 @@ 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)
{
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}

ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
if (contract == null)
{
Expand Down
37 changes: 33 additions & 4 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -259,6 +259,18 @@ private byte[] LoadDeploymentScript(string nefFilePath, string manifestFilePath,
nef = stream.ReadSerializable<NefFile>();
}

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);
Expand All @@ -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))
{
Expand Down Expand Up @@ -313,6 +328,17 @@ private byte[] LoadUpdateScript(UInt160 scriptHash, string nefFilePath, string m
nef = stream.ReadSerializable<NefFile>();
}

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);
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 24bd2cc

Please sign in to comment.