Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Parse vote commands' result #581

Merged
merged 11 commits into from
May 9, 2020
49 changes: 0 additions & 49 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,55 +41,6 @@ private void OnDeployCommand(string filePath, string manifestPath = null)
SignAndSendTx(tx);
}

/// <summary>
/// Process "invoke" command
/// </summary>
/// <param name="scriptHash">Script hash</param>
/// <param name="operation">Operation</param>
/// <param name="verificable">Transaction</param>
/// <param name="contractParameters">Contract parameters</param>
private VM.Types.StackItem OnInvokeWithResult(UInt160 scriptHash, string operation, IVerifiable verificable = null, JArray contractParameters = null)
{
List<ContractParameter> parameters = new List<ContractParameter>();

if (contractParameters != null)
{
foreach (var contractParameter in contractParameters)
{
parameters.Add(ContractParameter.FromJson(contractParameter));
}
}

byte[] script;

using (ScriptBuilder scriptBuilder = new ScriptBuilder())
{
scriptBuilder.EmitAppCall(scriptHash, operation, parameters.ToArray());
script = scriptBuilder.ToArray();
Console.WriteLine($"Invoking script with: '{script.ToHexString()}'");
}

if (verificable is Transaction tx)
{
tx.Script = script;
}

using (ApplicationEngine engine = ApplicationEngine.Run(script, verificable, testMode: true))
{
Console.WriteLine($"VM State: {engine.State}");
Console.WriteLine($"Gas Consumed: {new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Result Stack: {new JArray(engine.ResultStack.Select(p => p.ToJson()))}");

if (engine.State.HasFlag(VMState.FAULT) || !engine.ResultStack.TryPop(out VM.Types.StackItem ret))
{
Console.WriteLine("Engine faulted.");
return null;
}

return ret;
}
}

/// <summary>
/// Process "invoke" command
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions neo-cli/CLI/MainService.NEP5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ private void OnBalanceOfCommand(UInt160 tokenHash, UInt160 address)
arg["type"] = "Hash160";
arg["value"] = address.ToString();

var result = OnInvokeWithResult(tokenHash, "balanceOf", null, new JArray(arg));
var asset = new AssetDescriptor(tokenHash);

Console.WriteLine($"Result : {((PrimitiveType)result).GetBigInteger()}");
var balanceResult = OnInvokeWithResult(tokenHash, "balanceOf", null, new JArray(arg));
var balance = new BigDecimal(((PrimitiveType)balanceResult).GetBigInteger(), asset.Decimals);

Console.WriteLine();
Console.WriteLine($"{asset.AssetName} balance : {balance}");
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
103 changes: 66 additions & 37 deletions neo-cli/CLI/MainService.Vote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.VM;
using System;
using Neo.Cryptography.ECC;
using Neo.VM.Types;

namespace Neo.CLI
{
Expand All @@ -12,9 +13,9 @@ partial class MainService
/// Process "register candidate" command
/// </summary>
/// <param name="senderAccount">Sender account</param>
/// <param name="publicKey">Register publicKey</param>
/// <param name="senderPublicKey">Register publicKey</param>
[ConsoleCommand("register candidate", Category = "Vote Commands")]
private void OnRegisterCandidateCommand(UInt160 senderAccount, ECPoint publicKey)
private void OnRegisterCandidateCommand(UInt160 senderAccount, ECPoint senderPublicKey)
{
if (NoWallet())
{
Expand All @@ -25,29 +26,13 @@ private void OnRegisterCandidateCommand(UInt160 senderAccount, ECPoint publicKey
byte[] script;
using (ScriptBuilder scriptBuilder = new ScriptBuilder())
{
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "registerCandidate", publicKey);
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "registerCandidate", senderPublicKey);
script = scriptBuilder.ToArray();
}

SendTransaction(script, senderAccount);
}

/// <summary>
/// Process "get candidates"
/// </summary>
[ConsoleCommand("get candidates", Category = "Vote Commands")]
private void OnGetCandidatesCommand()
{
byte[] script;
using (ScriptBuilder scriptBuilder = new ScriptBuilder())
{
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "getCandidates");
script = scriptBuilder.ToArray();
}

SendTransaction(script);
}

/// <summary>
/// Process "unregister candidate" command
/// </summary>
Expand Down Expand Up @@ -96,20 +81,52 @@ private void OnVoteCommand(UInt160 senderAccount, ECPoint publicKey)
SendTransaction(script, senderAccount);
}

/// <summary>
/// Process "get candidates"
/// </summary>
[ConsoleCommand("get candidates", Category = "Vote Commands")]
private void OnGetCandidatesCommand()
{
var result = OnInvokeWithResult(NativeContract.NEO.Hash, "getCandidates", null, null, false);

var resJArray = (VM.Types.Array)result;

if (resJArray.Count > 0)
{
Console.WriteLine();
Console.WriteLine("Candidates:");

foreach (var item in resJArray)
{
var value = (VM.Types.Array)item;

Console.Write(((ByteString)value?[0])?.GetSpan().ToHexString());
Console.Write(" ");
Console.WriteLine(((Integer)value?[1]).ToBigInteger());
}
}
}

/// <summary>
/// Process "get validators"
/// </summary>
[ConsoleCommand("get validators", Category = "Vote Commands")]
private void OnGetValidatorsCommand()
{
byte[] script;
using (ScriptBuilder scriptBuilder = new ScriptBuilder())
var result = OnInvokeWithResult(NativeContract.NEO.Hash, "getValidators", null, null, false);

var resJArray = (VM.Types.Array)result;

if (resJArray.Count > 0)
{
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "getValidators");
script = scriptBuilder.ToArray();
}
Console.WriteLine();
Console.WriteLine("Validators:");

SendTransaction(script);
foreach (var item in resJArray)
{
Console.WriteLine(((ByteString)item)?.GetSpan().ToHexString());
}
}
}

/// <summary>
Expand All @@ -118,14 +135,20 @@ private void OnGetValidatorsCommand()
[ConsoleCommand("get committee", Category = "Vote Commands")]
private void OnGetCommitteeCommand()
{
byte[] script;
using (ScriptBuilder scriptBuilder = new ScriptBuilder())
var result = OnInvokeWithResult(NativeContract.NEO.Hash, "getCommittee", null, null, false);

var resJArray = (VM.Types.Array)result;

if (resJArray.Count > 0)
{
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "getCommittee");
script = scriptBuilder.ToArray();
}
Console.WriteLine();
Console.WriteLine("Committee:");

SendTransaction(script);
foreach (var item in resJArray)
{
Console.WriteLine(((ByteString)item)?.GetSpan().ToHexString());
}
}
}

/// <summary>
Expand All @@ -134,14 +157,20 @@ private void OnGetCommitteeCommand()
[ConsoleCommand("get next validators", Category = "Vote Commands")]
private void OnGetNextBlockValidatorsCommand()
{
byte[] script;
using (ScriptBuilder scriptBuilder = new ScriptBuilder())
var result = OnInvokeWithResult(NativeContract.NEO.Hash, "getNextBlockValidators", null, null, false);

var resJArray = (VM.Types.Array)result;

if (resJArray.Count > 0)
{
scriptBuilder.EmitAppCall(NativeContract.NEO.Hash, "getNextBlockValidators");
script = scriptBuilder.ToArray();
}
Console.WriteLine();
Console.WriteLine("Next validators:");

SendTransaction(script);
foreach (var item in resJArray)
{
Console.WriteLine(((ByteString)item)?.GetSpan().ToHexString());
}
}
}
}
}
56 changes: 56 additions & 0 deletions neo-cli/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ private static void WriteLineWithoutFlicker(string message = "", int maxWidth =
Console.WriteLine(new string(' ', spacesToErase));
}

/// <summary>
/// Make and send transaction with script, sender
/// </summary>
/// <param name="script">script</param>
/// <param name="account">sender</param>
private void SendTransaction(byte[] script, UInt160 account = null)
{
List<Cosigner> signCollection = new List<Cosigner>();
Expand Down Expand Up @@ -537,5 +542,56 @@ private void SendTransaction(byte[] script, UInt160 account = null)

return;
}

/// <summary>
/// Process "invoke" command
/// </summary>
/// <param name="scriptHash">Script hash</param>
/// <param name="operation">Operation</param>
/// <param name="verificable">Transaction</param>
/// <param name="contractParameters">Contract parameters</param>
private VM.Types.StackItem OnInvokeWithResult(UInt160 scriptHash, string operation, IVerifiable verificable = null, JArray contractParameters = null, bool showStack = true)
{
List<ContractParameter> parameters = new List<ContractParameter>();

if (contractParameters != null)
{
foreach (var contractParameter in contractParameters)
{
parameters.Add(ContractParameter.FromJson(contractParameter));
}
}

byte[] script;

using (ScriptBuilder scriptBuilder = new ScriptBuilder())
{
scriptBuilder.EmitAppCall(scriptHash, operation, parameters.ToArray());
script = scriptBuilder.ToArray();
Console.WriteLine($"Invoking script with: '{script.ToHexString()}'");
}

if (verificable is Transaction tx)
{
tx.Script = script;
}

using (ApplicationEngine engine = ApplicationEngine.Run(script, verificable, testMode: true))
{
Console.WriteLine($"VM State: {engine.State}");
Console.WriteLine($"Gas Consumed: {new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals)}");

if (showStack)
Console.WriteLine($"Result Stack: {new JArray(engine.ResultStack.Select(p => p.ToJson()))}");

if (engine.State.HasFlag(VMState.FAULT) || !engine.ResultStack.TryPop(out VM.Types.StackItem ret))
{
Console.WriteLine("Engine faulted.");
return null;
}

return ret;
}
}
}
}