Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception field in invokeresult #364

Merged
merged 20 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/RpcClient/Models/RpcInvokeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public class RpcInvokeResult

public string Tx { get; set; }

public string Exception { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["script"] = Script;
json["state"] = State;
json["gasconsumed"] = GasConsumed;
if (!string.IsNullOrEmpty(Exception))
json["exception"] = Exception;
try
{
json["stack"] = new JArray(Stack.Select(p => p.ToJson()));
Expand All @@ -43,6 +47,8 @@ public static RpcInvokeResult FromJson(JObject json)
invokeScriptResult.Script = json["script"].AsString();
invokeScriptResult.State = json["state"].TryGetEnum<VM.VMState>();
invokeScriptResult.GasConsumed = json["gasconsumed"].AsString();
if (json.ContainsProperty("exception"))
invokeScriptResult.Exception = json["exception"]?.AsString();
try
{
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => Utility.StackItemFromJson(p)).ToArray();
Expand Down
20 changes: 18 additions & 2 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private class Signers : IVerifiable
{
private readonly Signer[] _signers;
public Witness[] Witnesses { get; set; }
public int Size { get; }
public int Size => _signers.Length;

public Signers(Signer[] signers)
{
Expand Down Expand Up @@ -68,6 +68,7 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
json["script"] = Convert.ToBase64String(script);
json["state"] = engine.State;
json["gasconsumed"] = engine.GasConsumed.ToString();
json["exception"] = GetExceptionMessage(engine.FaultException);
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson()));
Expand All @@ -76,7 +77,10 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
{
json["stack"] = "error: recursive reference";
}
ProcessInvokeWithWallet(json, signers);
if (engine.State != VMState.FAULT)
{
ProcessInvokeWithWallet(json, signers);
}
return json;
}

Expand Down Expand Up @@ -142,5 +146,17 @@ protected virtual JObject GetUnclaimedGas(JArray _params)
json["address"] = script_hash.ToAddress();
return json;
}

static string GetExceptionMessage(Exception exception)
{
if (exception == null) return null;

if (exception.InnerException != null)
{
return exception.InnerException.Message;
}

return exception.Message;
}
}
}
10 changes: 9 additions & 1 deletion src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ private void ProcessInvokeWithWallet(JObject result, Signers signers = null)
UInt160 sender = signers.Size > 0 ? signers.GetSigners()[0].Account : null;
if (witnessSigners.Count() > 0)
{
tx = wallet.MakeTransaction(Convert.FromBase64String(result["script"].AsString()), sender, witnessSigners);
try
{
tx = wallet.MakeTransaction(result["script"].AsString().HexToBytes(), sender, witnessSigners);
}
catch (Exception e)
{
result["exception"] = GetExceptionMessage(e);
return;
}
ContractParametersContext context = new ContractParametersContext(tx);
wallet.Sign(context);
if (context.Completed)
Expand Down
2 changes: 2 additions & 0 deletions tests/Neo.Network.RPC.Tests/UT_RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public async Task TestInvokeFunction()
var result = await rpc.InvokeFunctionAsync(test.Request.Params[0].AsString(), test.Request.Params[1].AsString(),
((JArray)test.Request.Params[2]).Select(p => RpcStack.FromJson(p)).ToArray());
Assert.AreEqual(test.Response.Result.ToString(), result.ToJson().ToString());

// TODO test verify method
}

[TestMethod]
Expand Down