Skip to content

Commit

Permalink
Process iterator in RPC result (#583)
Browse files Browse the repository at this point in the history
* Process iterator

* Fix

* Use while

* Preserve type

* Update src/RpcServer/RpcServer.SmartContract.cs

Co-authored-by: Satoshi Chou <82646971+satoshichou@users.noreply.github.com>

* Update src/RpcServer/RpcServer.SmartContract.cs

* Include other items in warning

* < instead of <=

* Optimize

* Rename

Co-authored-by: Erik Zhang <erik@neo.org>
Co-authored-by: Satoshi Chou <82646971+satoshichou@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 20, 2021
1 parent 057ff46 commit 4359882
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
25 changes: 22 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,11 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
json["exception"] = GetExceptionMessage(engine.FaultException);
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToJson()));
json["stack"] = new JArray(engine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
json["stack"] = "error: invalid operation";
}
if (engine.State != VMState.FAULT)
{
Expand All @@ -89,6 +91,23 @@ private JObject GetInvokeResult(byte[] script, Signers signers = null)
return json;
}

private static JObject ToJson(StackItem item, int max)
{
JObject json = item.ToJson();
if (item is InteropInterface interopInterface && interopInterface.GetInterface<object>() is IIterator iterator)
{
JArray array = new();
while (max > 0 && iterator.Next())
{
array.Add(iterator.Value().ToJson());
max--;
}
json["iterator"] = array;
json["truncated"] = iterator.Next();
}
return json;
}

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,
"MaxIteratorResultItems": 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 @@ -30,6 +30,7 @@ public record RpcServerSettings
public string RpcPass { get; init; }
public long MaxGasInvoke { get; init; }
public long MaxFee { get; init; }
public int MaxIteratorResultItems { get; init; }
public string[] DisabledMethods { get; init; }

public static RpcServerSettings Default { get; } = new RpcServerSettings
Expand All @@ -41,6 +42,7 @@ public record RpcServerSettings
MaxGasInvoke = (long)new BigDecimal(10M, NativeContract.GAS.Decimals).Value,
MaxFee = (long)new BigDecimal(0.1M, NativeContract.GAS.Decimals).Value,
TrustedAuthorities = Array.Empty<string>(),
MaxIteratorResultItems = 100,
DisabledMethods = Array.Empty<string>(),
MaxConcurrentConnections = 40,
};
Expand All @@ -57,6 +59,7 @@ public record RpcServerSettings
RpcPass = section.GetSection("RpcPass").Value,
MaxGasInvoke = (long)new BigDecimal(section.GetValue<decimal>("MaxGasInvoke", Default.MaxGasInvoke), NativeContract.GAS.Decimals).Value,
MaxFee = (long)new BigDecimal(section.GetValue<decimal>("MaxFee", Default.MaxFee), NativeContract.GAS.Decimals).Value,
MaxIteratorResultItems = section.GetValue("MaxIteratorResultItems", Default.MaxIteratorResultItems),
DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).ToArray(),
MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", Default.MaxConcurrentConnections),
};
Expand Down

0 comments on commit 4359882

Please sign in to comment.