Skip to content

Commit

Permalink
add invoked contract (#657)
Browse files Browse the repository at this point in the history
* init

* upgrade neo

* useDiagnostic

* Update RpcServer.SmartContract.cs

* invoke tree

* add event

* Add storage changes

* Update nuget

* rename event state

* Unify

* update neofs

* fix json

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Erik Zhang <erik@neo.org>
Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 15, 2021
1 parent 456c980 commit 08ef3b7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.1.0" />
<PackageReference Include="Neo" Version="3.1.0-CI01329" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/OracleService/OracleService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Neo.ConsoleService" Version="1.2.0" />
<PackageReference Include="NeoFS.API" Version="3.1.0" />
<PackageReference Include="NeoFS.API" Version="3.1.0-CI01329" />
</ItemGroup>

<ItemGroup>
Expand Down
53 changes: 49 additions & 4 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.IO;
using System.Linq;
using Neo.IO.Caching;

namespace Neo.Plugins
{
Expand Down Expand Up @@ -59,20 +60,36 @@ public void SerializeUnsigned(BinaryWriter writer)
}
}

private JObject GetInvokeResult(byte[] script, Signers signers = null)
private JObject GetInvokeResult(byte[] script, Signers signers = null, bool useDiagnostic = false)
{
Transaction tx = signers == null ? null : new Transaction
{
Signers = signers.GetSigners(),
Attributes = System.Array.Empty<TransactionAttribute>(),
Witnesses = signers.Witnesses,
};
using ApplicationEngine engine = ApplicationEngine.Run(script, system.StoreView, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke);
using ApplicationEngine engine = ApplicationEngine.Run(script, system.StoreView, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke, diagnostic: useDiagnostic ? new Diagnostic() : null);
JObject json = new();
json["script"] = Convert.ToBase64String(script);
json["state"] = engine.State;
json["gasconsumed"] = engine.GasConsumed.ToString();
json["exception"] = GetExceptionMessage(engine.FaultException);
json["notifications"] = new JArray(engine.Notifications.Select(n =>
{
var obj = new JObject();
obj["eventname"] = n.EventName;
obj["contract"] = n.ScriptHash.ToString();
obj["state"] = ToJson(n.State, settings.MaxIteratorResultItems);
return obj;
}));
if (useDiagnostic)
{
json["diagnostics"] = new JObject()
{
["invokedcontracts"] = ToJson(engine.Diagnostic.InvocationTree.Root),
["storagechanges"] = ToJson(engine.Snapshot.GetChangeSet())
};
}
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
Expand All @@ -88,6 +105,32 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
return json;
}

private static JObject ToJson(TreeNode<UInt160> node)
{
JObject json = new();
json["hash"] = node.Item.ToString();
if (node.Children.Any())
{
json["call"] = new JArray(node.Children.Select(ToJson));
}
return json;
}

private static JObject ToJson(System.Collections.Generic.IEnumerable<DataCache.Trackable> changes)
{
JArray array = new();
foreach (var entry in changes)
{
array.Add(new JObject
{
["state"] = entry.State.ToString(),
["key"] = Convert.ToBase64String(entry.Key.ToArray()),
["value"] = Convert.ToBase64String(entry.Item.Value.ToArray())
});
}
return array;
}

private static JObject ToJson(StackItem item, int max)
{
JObject json = item.ToJson();
Expand Down Expand Up @@ -143,21 +186,23 @@ protected virtual JObject InvokeFunction(JArray _params)
string operation = _params[1].AsString();
ContractParameter[] args = _params.Count >= 3 ? ((JArray)_params[2]).Select(p => ContractParameter.FromJson(p)).ToArray() : System.Array.Empty<ContractParameter>();
Signers signers = _params.Count >= 4 ? SignersFromJson((JArray)_params[3], system.Settings) : null;
bool useDiagnostic = _params.Count >= 5 && _params[4].GetBoolean();

byte[] script;
using (ScriptBuilder sb = new())
{
script = sb.EmitDynamicCall(script_hash, operation, args).ToArray();
}
return GetInvokeResult(script, signers);
return GetInvokeResult(script, signers, useDiagnostic);
}

[RpcMethod]
protected virtual JObject InvokeScript(JArray _params)
{
byte[] script = Convert.FromBase64String(_params[0].AsString());
Signers signers = _params.Count >= 2 ? SignersFromJson((JArray)_params[1], system.Settings) : null;
return GetInvokeResult(script, signers);
bool useDiagnostic = _params.Count >= 3 && _params[2].GetBoolean();
return GetInvokeResult(script, signers, useDiagnostic);
}

[RpcMethod]
Expand Down

0 comments on commit 08ef3b7

Please sign in to comment.