Skip to content

Commit

Permalink
add --witness-scope option to contract invoke + deploy (#119)
Browse files Browse the repository at this point in the history
* add --witness-scope option to contract invoke + deploy

* update release action version in release workflow

* release workflow upload-artifact

Co-authored-by: Harry <[email protected]>
  • Loading branch information
devhawk and Harry authored Mar 23, 2021
1 parent cca0649 commit 3a0cdd9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 20 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ jobs:
run: dotnet restore
- name: Pack
run: dotnet pack --output ./out --configuration Release --no-restore --verbosity normal
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: packages
path: ./out
- name: Create Release
uses: marvinpinto/[email protected].0
uses: marvinpinto/[email protected].1
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: ${{ contains(env.NUGET_PACKAGE_VERSION, '-preview') }}
Expand Down
9 changes: 9 additions & 0 deletions src/neoxp/Commands/BatchCommand.BatchFileCommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.ComponentModel.DataAnnotations;
using McMaster.Extensions.CommandLineUtils;
using Neo.Network.P2P.Payloads;

namespace NeoExpress.Commands
{
Expand Down Expand Up @@ -60,6 +62,9 @@ internal class Deploy
[Required]
internal string Account { get; init; } = string.Empty;

[Option(Description = "Witness Scope to use for transaction signer (Default: CalledByEntry)")]
[AllowedValues(StringComparison.OrdinalIgnoreCase, "None", "CalledByEntry", "Global")]
internal WitnessScope WitnessScope { get; init; } = WitnessScope.CalledByEntry;
}

[Command("invoke")]
Expand All @@ -72,6 +77,10 @@ internal class Invoke
[Argument(1, Description = "Account to pay contract invocation GAS fee")]
[Required]
internal string Account { get; init; } = string.Empty;

[Option(Description = "Witness Scope to use for transaction signer (Default: CalledByEntry)")]
[AllowedValues(StringComparison.OrdinalIgnoreCase, "None", "CalledByEntry", "Global")]
internal WitnessScope WitnessScope { get; init; } = WitnessScope.CalledByEntry;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/neoxp/Commands/BatchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ await ContractCommand.Deploy.ExecuteAsync(
fileSystem,
cmd.Model.Contract,
cmd.Model.Account,
cmd.Model.WitnessScope,
writer).ConfigureAwait(false);
break;
}
Expand All @@ -116,6 +117,7 @@ await ContractCommand.Invoke.ExecuteTxAsync(
expressNode,
cmd.Model.InvocationFile,
cmd.Model.Account,
cmd.Model.WitnessScope,
fileSystem,
writer).ConfigureAwait(false);
break;
Expand Down
11 changes: 8 additions & 3 deletions src/neoxp/Commands/ContractCommand.Deploy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO.Abstractions;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;

Expand Down Expand Up @@ -30,6 +31,10 @@ public Deploy(IExpressChainManagerFactory chainManagerFactory, IFileSystem fileS
[Required]
internal string Account { get; init; } = string.Empty;

[Option(Description = "Witness Scope to use for transaction signer (Default: CalledByEntry)")]
[AllowedValues(StringComparison.OrdinalIgnoreCase, "None", "CalledByEntry", "Global")]
internal WitnessScope WitnessScope { get; init; } = WitnessScope.CalledByEntry;

[Option(Description = "Path to neo-express data file")]
internal string Input { get; init; } = string.Empty;

Expand All @@ -39,15 +44,15 @@ public Deploy(IExpressChainManagerFactory chainManagerFactory, IFileSystem fileS
[Option(Description = "Output as JSON")]
internal bool Json { get; init; } = false;

internal static async Task ExecuteAsync(IExpressChainManager chainManager, IExpressNode expressNode, IFileSystem fileSystem, string contract, string accountName, System.IO.TextWriter writer, bool json = false)
internal static async Task ExecuteAsync(IExpressChainManager chainManager, IExpressNode expressNode, IFileSystem fileSystem, string contract, string accountName, WitnessScope witnessScope, System.IO.TextWriter writer, bool json = false)
{
if (!chainManager.Chain.TryGetAccount(accountName, out var wallet, out var account, chainManager.ProtocolSettings))
{
throw new Exception($"{accountName} account not found.");
}

var (nefFile, manifest) = await fileSystem.LoadContractAsync(contract).ConfigureAwait(false);
var txHash = await expressNode.DeployAsync(nefFile, manifest, wallet, account.ScriptHash).ConfigureAwait(false);
var txHash = await expressNode.DeployAsync(nefFile, manifest, wallet, account.ScriptHash, witnessScope).ConfigureAwait(false);
await writer.WriteTxHashAsync(txHash, "Deployment", json).ConfigureAwait(false);
}

Expand All @@ -57,7 +62,7 @@ internal async Task<int> OnExecuteAsync(IConsole console)
{
var (chainManager, _) = chainManagerFactory.LoadChain(Input);
using var expressNode = chainManager.GetExpressNode(Trace);
await ExecuteAsync(chainManager, expressNode, fileSystem, Contract, Account, console.Out, Json).ConfigureAwait(false);
await ExecuteAsync(chainManager, expressNode, fileSystem, Contract, Account, WitnessScope, console.Out, Json).ConfigureAwait(false);
return 0;
}
catch (Exception ex)
Expand Down
17 changes: 11 additions & 6 deletions src/neoxp/Commands/ContractCommand.Invoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO.Abstractions;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using Neo.Network.P2P.Payloads;

namespace NeoExpress.Commands
{
Expand All @@ -27,12 +28,13 @@ public Invoke(IExpressChainManagerFactory chainManagerFactory, IFileSystem fileS
[Argument(1, Description = "Account to pay contract invocation GAS fee")]
internal string Account { get; init; } = string.Empty;

[Option(Description = "Witness Scope to use for transaction signer (Default: CalledByEntry)")]
[AllowedValues(StringComparison.OrdinalIgnoreCase, "None", "CalledByEntry", "Global")]
internal WitnessScope WitnessScope { get; init; } = WitnessScope.CalledByEntry;

[Option(Description = "Invoke contract for results (does not cost GAS)")]
internal bool Results { get; init; } = false;

[Option(Description = "Path to neo-express data file")]
internal string Input { get; init; } = string.Empty;

[Option("--gas|-g", CommandOptionType.SingleValue, Description = "Additional GAS to apply to the contract invocation")]
internal decimal AdditionalGas { get; init; } = 0;

Expand All @@ -42,7 +44,10 @@ public Invoke(IExpressChainManagerFactory chainManagerFactory, IFileSystem fileS
[Option(Description = "Output as JSON")]
internal bool Json { get; init; } = false;

internal static async Task ExecuteTxAsync(IExpressChainManager chainManager, IExpressNode expressNode, string invocationFile, string accountName, IFileSystem fileSystem, System.IO.TextWriter writer, bool json = false)
[Option(Description = "Path to neo-express data file")]
internal string Input { get; init; } = string.Empty;

internal static async Task ExecuteTxAsync(IExpressChainManager chainManager, IExpressNode expressNode, string invocationFile, string accountName, WitnessScope witnessScope, IFileSystem fileSystem, System.IO.TextWriter writer, bool json = false)
{
if (!fileSystem.File.Exists(invocationFile))
{
Expand All @@ -56,7 +61,7 @@ internal static async Task ExecuteTxAsync(IExpressChainManager chainManager, IEx

var parser = await expressNode.GetContractParameterParserAsync(chainManager).ConfigureAwait(false);
var script = await parser.LoadInvocationScriptAsync(invocationFile).ConfigureAwait(false);
var txHash = await expressNode.ExecuteAsync(wallet, account.ScriptHash, script).ConfigureAwait(false);
var txHash = await expressNode.ExecuteAsync(wallet, account.ScriptHash, witnessScope, script).ConfigureAwait(false);
await writer.WriteTxHashAsync(txHash, "Deployment", json).ConfigureAwait(false);
}

Expand Down Expand Up @@ -108,7 +113,7 @@ internal async Task<int> OnExecuteAsync(IConsole console)
}
else
{
await ExecuteTxAsync(chainManager, expressNode, InvocationFile, Account, fileSystem, console.Out, Json);
await ExecuteTxAsync(chainManager, expressNode, InvocationFile, Account, WitnessScope, fileSystem, console.Out, Json);
}

return 0;
Expand Down
10 changes: 5 additions & 5 deletions src/neoxp/ExpressNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static async Task<UInt256> TransferAsync(this IExpressNode expressNode, U
var decimals = (byte)(results.Stack[0].GetInteger());
var value = quantity.AsT0.ToBigInteger(decimals);
var script = asset.MakeScript("transfer", senderHash, receiverHash, value, null);
return await expressNode.ExecuteAsync(sender, senderHash, script).ConfigureAwait(false);
return await expressNode.ExecuteAsync(sender, senderHash, WitnessScope.CalledByEntry, script).ConfigureAwait(false);
}
else
{
Expand All @@ -135,11 +135,11 @@ public static async Task<UInt256> TransferAsync(this IExpressNode expressNode, U
sb.EmitPush("transfer");
sb.EmitPush(asset);
sb.EmitSysCall(ApplicationEngine.System_Contract_Call);
return await expressNode.ExecuteAsync(sender, senderHash, sb.ToArray()).ConfigureAwait(false);
return await expressNode.ExecuteAsync(sender, senderHash, WitnessScope.CalledByEntry, sb.ToArray()).ConfigureAwait(false);
}
}

public static async Task<UInt256> DeployAsync(this IExpressNode expressNode, NefFile nefFile, ContractManifest manifest, Wallet wallet, UInt160 accountHash)
public static async Task<UInt256> DeployAsync(this IExpressNode expressNode, NefFile nefFile, ContractManifest manifest, Wallet wallet, UInt160 accountHash, WitnessScope witnessScope)
{
// check for bad opcodes (logic borrowed from neo-cli LoadDeploymentScript)
Neo.VM.Script script = nefFile.Script;
Expand All @@ -163,7 +163,7 @@ public static async Task<UInt256> DeployAsync(this IExpressNode expressNode, Nef

using var sb = new ScriptBuilder();
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nefFile.ToArray(), manifest.ToJson().ToString());
return await expressNode.ExecuteAsync(wallet, accountHash, sb.ToArray()).ConfigureAwait(false);
return await expressNode.ExecuteAsync(wallet, accountHash, witnessScope, sb.ToArray()).ConfigureAwait(false);
}

public static async Task<UInt256> DesignateOracleRolesAsync(this IExpressNode expressNode, Wallet wallet, UInt160 accountHash, IEnumerable<ECPoint> oracles)
Expand All @@ -178,7 +178,7 @@ public static async Task<UInt256> DesignateOracleRolesAsync(this IExpressNode ex

using var sb = new ScriptBuilder();
sb.EmitDynamicCall(NativeContract.RoleManagement.Hash, "designateAsRole", roleParam, oraclesParam);
return await expressNode.ExecuteAsync(wallet, accountHash, sb.ToArray()).ConfigureAwait(false);
return await expressNode.ExecuteAsync(wallet, accountHash, WitnessScope.CalledByEntry, sb.ToArray()).ConfigureAwait(false);
}

public static async Task<ECPoint[]> GetOracleNodesAsync(this IExpressNode expressNode)
Expand Down
2 changes: 1 addition & 1 deletion src/neoxp/IExpressNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal interface IExpressNode : IDisposable
ProtocolSettings ProtocolSettings { get; }

Task<bool> CreateCheckpointAsync(string checkPointPath);
Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, Script script, decimal additionalGas = 0);
Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, WitnessScope witnessScope, Script script, decimal additionalGas = 0);
Task<UInt256> SubmitTransactionAsync(Transaction tx);
Task<RpcInvokeResult> InvokeAsync(Script script);
Task<(RpcNep17Balance balance, Nep17Contract contract)[]> GetBalancesAsync(UInt160 address);
Expand Down
4 changes: 2 additions & 2 deletions src/neoxp/Node/OfflineNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public async Task<RpcInvokeResult> InvokeAsync(Neo.VM.Script script)
return result;
}

public async Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, Neo.VM.Script script, decimal additionalGas = 0)
public async Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, WitnessScope witnessScope, Neo.VM.Script script, decimal additionalGas = 0)
{
if (disposedValue) throw new ObjectDisposedException(nameof(OfflineNode));

var signer = new Signer() { Account = accountHash, Scopes = WitnessScope.CalledByEntry };
var signer = new Signer() { Account = accountHash, Scopes = witnessScope };
var tx = wallet.MakeTransaction(neoSystem.StoreView, script, accountHash, new[] { signer });
if (additionalGas > 0.0m)
{
Expand Down
4 changes: 2 additions & 2 deletions src/neoxp/Node/OnlineNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void Dispose()
{
}

public async Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, Script script, decimal additionalGas = 0)
public async Task<UInt256> ExecuteAsync(Wallet wallet, UInt160 accountHash, WitnessScope witnessScope, Script script, decimal additionalGas = 0)
{
var signers = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = accountHash } };
var signers = new[] { new Signer { Account = accountHash, Scopes = witnessScope } };
var factory = new TransactionManagerFactory(rpcClient);
var tm = await factory.MakeTransactionAsync(script, signers).ConfigureAwait(false);

Expand Down

0 comments on commit 3a0cdd9

Please sign in to comment.