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

Fix stack exception name #855

Merged
merged 8 commits into from
Dec 6, 2023
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
34 changes: 22 additions & 12 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ public static JObject TxLogToJson(Blockchain.ApplicationExecuted appExec)
trigger["vmstate"] = appExec.VMState;
trigger["exception"] = GetExceptionMessage(appExec.Exception);
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
var stack = new JArray();
foreach (var item in appExec.Stack)
{
trigger["stack"] = appExec.Stack.Select(q => q.ToJson(Settings.Default.MaxStackSize)).ToArray();
}
catch (Exception ex)
{
trigger["exception"] = ex.Message;
try
{
stack.Add(item.ToJson(Settings.Default.MaxStackSize));
superboyiii marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex)
{
stack.Add("error: " + ex.Message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stack.Add("error: " + ex.Message);
stack.Add(item.GetInterface<object>().GetType().Name);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can break applicationlog getting the stack:
3e8148970abed45ba6ef0e98c4161c3
But you can see it's on-chain:
7723fcd8ecae99fdf1a629501952ada

}
}
trigger["stack"] = stack;
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
Expand Down Expand Up @@ -133,14 +138,19 @@ public static JObject BlockLogToJson(Block block, IReadOnlyList<Blockchain.Appli
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
var stack = new JArray();
foreach (var item in appExec.Stack)
{
trigger["stack"] = appExec.Stack.Select(q => q.ToJson(Settings.Default.MaxStackSize)).ToArray();
}
catch (Exception ex)
{
trigger["exception"] = ex.Message;
try
{
stack.Add(item.ToJson(Settings.Default.MaxStackSize));
}
catch (Exception ex)
{
stack.Add("error: " + ex.Message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stack.Add("error: " + ex.Message);
stack.Add(item.GetInterface<object>().GetType().Name);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error means that there was a stackitem, but it can't be serialized and therefore can't be presented in JSON output. Replacing it with NULL or any other element (empty/string with a name) would be wrong because that's not what's on the stack. Proper stack item (like suggested) would be misinterpreted as a real output from the execution, it has to be some error string in case of serialization failure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the ones that can't be output to json are iterators (storage); at least what i have ran into (on testnet).

}
}
trigger["stack"] = stack;
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
Expand Down
17 changes: 11 additions & 6 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ private JObject GetInvokeResult(byte[] script, Signer[] signers = null, Witness[
["storagechanges"] = ToJson(session.Engine.Snapshot.GetChangeSet())
};
}
try
var stack = new JArray();
foreach (var item in session.Engine.ResultStack)
{
json["stack"] = new JArray(session.Engine.ResultStack.Select(p => ToJson(p, session)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: invalid operation";
try
{
stack.Add(ToJson(item, session));
}
catch (Exception ex)
{
stack.Add("error: " + ex.Message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stack.Add("error: " + ex.Message);
stack.Add(item.GetInterface<object>().GetType().Name);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this way, it will throw exception directly but can't get result stack!
725922486f0147421b02d13395e875b
Compare to current code:
302dc02b1c0fea24c7e69980f02256f

}
}
json["stack"] = stack;
if (session.Engine.State != VMState.FAULT)
{
ProcessInvokeWithWallet(json, signers);
Expand Down
42 changes: 21 additions & 21 deletions tests/Neo.Network.RPC.Tests/Neo.Network.RPC.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Neo.Network.RPC.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RpcClient\RpcClient.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="RpcTestCases.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Neo.Network.RPC.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RpcClient\RpcClient.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="RpcTestCases.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>