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

ApplicationEngine helper to get engine error info #3541

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions src/Neo/SmartContract/ApplicationEngine.Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ApplicationEngine.Helper.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neo.SmartContract
{
public partial class ApplicationEngine : ExecutionEngine
{
public string GetEngineErrorInfo()
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
{
if (State != VMState.FAULT || FaultException == null)
return null;
StringBuilder traceback = new();
if (CallingScriptHash != null)
traceback.AppendLine($"CallingScriptHash={CallingScriptHash}[{NativeContract.ContractManagement.GetContract(SnapshotCache, CallingScriptHash)?.Manifest.Name}]");
traceback.AppendLine($"CurrentScriptHash={CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(SnapshotCache, CurrentScriptHash)?.Manifest.Name}]");
traceback.AppendLine($"EntryScriptHash={EntryScriptHash}");

foreach (ExecutionContext context in InvocationStack.Reverse())
{
UInt160 contextScriptHash = context.GetScriptHash();
string contextContractName = NativeContract.ContractManagement.GetContract(SnapshotCache, contextScriptHash)?.Manifest.Name;
traceback.AppendLine($"\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]");
}
Exception baseException = FaultException.GetBaseException();
traceback.AppendLine(baseException.StackTrace);
traceback.AppendLine(baseException.Message);
Copy link
Contributor

Choose a reason for hiding this comment

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

also can you please make this a seperate method to attach the exception to the error. Or make the above information a seperate method that we can use in other method.


return traceback.ToString();
}
}
}
7 changes: 7 additions & 0 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,15 @@ public void TestSystem_Contract_Call_Permissions()
};
var currentScriptHash = engine.EntryScriptHash;

Assert.IsNull(engine.GetEngineErrorInfo());
Assert.AreEqual(VMState.FAULT, engine.Execute());
Assert.IsTrue(engine.FaultException.ToString().Contains($"Cannot Call Method disallowed Of Contract {scriptHash.ToString()}"));
string traceback = engine.GetEngineErrorInfo();
Assert.IsTrue(traceback.Contains($"Cannot Call Method disallowed Of Contract {scriptHash.ToString()}"));
Assert.IsTrue(traceback.Contains("CurrentScriptHash"));
Assert.IsTrue(traceback.Contains("EntryScriptHash"));
Assert.IsTrue(traceback.Contains("InstructionPointer"));
Assert.IsTrue(traceback.Contains("OpCode SYSCALL, Script Length="));
}

// Allowed method call.
Expand Down
Loading