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

Process iterator in RPC result #583

Merged
merged 10 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 34 additions & 3 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Iterators;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using Neo.Wallets;
using System;
using System.IO;
Expand Down Expand Up @@ -65,7 +67,7 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
Transaction tx = signers == null ? null : new Transaction
{
Signers = signers.GetSigners(),
Attributes = Array.Empty<TransactionAttribute>(),
Attributes = System.Array.Empty<TransactionAttribute>(),
Witnesses = signers.Witnesses,
};
using ApplicationEngine engine = ApplicationEngine.Run(script, system.StoreView, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke);
Expand All @@ -76,11 +78,12 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
json["exception"] = GetExceptionMessage(engine.FaultException);
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson()));
int max = settings.MaxResultItems;
json["stack"] = new JArray(engine.ResultStack.Select(p => ToJson(p, ref max)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
json["stack"] = "error: invalid operation";
}
if (engine.State != VMState.FAULT)
{
Expand All @@ -89,6 +92,34 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
return json;
}

private JObject ToJson(StackItem p, ref int max)
{
if (max <= 0) throw new InvalidOperationException();
max--;

if (p is InteropInterface result)
{
var iterator = result.GetInterface<IIterator>();
if (iterator != null)
{
VM.Types.Array resultList = new();
while (iterator.Next())
{
resultList.Add(iterator.Value());
max--;
if (max <= 0) throw new InvalidOperationException();
}

JObject ret = new();
ret["type"] = "InteropInterface";
ret["value"] = ToJson(resultList, ref max);
return ret;
}
}

return p.ToJson();
}

private static Signers SignersFromJson(JArray _params, ProtocolSettings settings)
{
var ret = new Signers(_params.Select(u => new Signer()
Expand Down
1 change: 1 addition & 0 deletions src/RpcServer/RpcServer/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"MaxGasInvoke": 20,
"MaxFee": 0.1,
"MaxConcurrentConnections": 40,
"MaxResultItems": 100,
"DisabledMethods": [ "openwallet" ]
}
]
Expand Down
3 changes: 3 additions & 0 deletions src/RpcServer/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public record RpcServerSettings
public long MaxGasInvoke { get; init; }
public long MaxFee { get; init; }
public string[] DisabledMethods { get; init; }
public int MaxResultItems { get; init; }

public static RpcServerSettings Default { get; } = new RpcServerSettings
{
Expand All @@ -43,6 +44,7 @@ public record RpcServerSettings
TrustedAuthorities = Array.Empty<string>(),
DisabledMethods = Array.Empty<string>(),
MaxConcurrentConnections = 40,
MaxResultItems = 100,
};

public static RpcServerSettings Load(IConfigurationSection section) => new RpcServerSettings
Expand All @@ -59,6 +61,7 @@ public record RpcServerSettings
MaxFee = (long)new BigDecimal(section.GetValue<decimal>("MaxFee", Default.MaxFee), NativeContract.GAS.Decimals).Value,
DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).ToArray(),
MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", Default.MaxConcurrentConnections),
MaxResultItems = section.GetValue("MaxResultItems", Default.MaxResultItems),
};
}
}