From febd01d5fa5ff80f23de9d71f9f7537f1b69eb80 Mon Sep 17 00:00:00 2001 From: Hecate2 <2474101468@qq.com> Date: Thu, 10 Oct 2024 11:52:38 +0800 Subject: [PATCH] use StringBuilder; cancel catch at result stack --- Fairy.Debugger.cs | 39 +++++++++++++++++++-------------------- Fairy.Tester.cs | 42 +++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Fairy.Debugger.cs b/Fairy.Debugger.cs index 929b29b..9610194 100644 --- a/Fairy.Debugger.cs +++ b/Fairy.Debugger.cs @@ -5,6 +5,7 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.VM; +using System.Text; namespace Neo.Plugins { @@ -125,8 +126,9 @@ private JObject DumpDebugResultJson(JObject json, FairyEngine newEngine, BreakRe json["exception"] = GetExceptionMessage(newEngine.FaultException); if (json["exception"] != null) { - string traceback = $"CallingScriptHash={newEngine.CallingScriptHash}\r\nCurrentScriptHash={newEngine.CurrentScriptHash}\r\nEntryScriptHash={newEngine.EntryScriptHash}\r\n"; - traceback += newEngine.FaultException.StackTrace; + StringBuilder traceback = new(); + traceback.Append($"CallingScriptHash={newEngine.CallingScriptHash}\r\nCurrentScriptHash={newEngine.CurrentScriptHash}\r\nEntryScriptHash={newEngine.EntryScriptHash}\r\n"); + traceback.Append(newEngine.FaultException.StackTrace); foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack.Reverse()) { UInt160 contextScriptHash = context.GetScriptHash(); @@ -134,36 +136,33 @@ private JObject DumpDebugResultJson(JObject json, FairyEngine newEngine, BreakRe { if (contractScriptHashToAllInstructionPointerToSourceLineNum.ContainsKey(contextScriptHash) && contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash].ContainsKey((uint)context.InstructionPointer)) { - string sourceCodeTraceback = ""; SourceFilenameAndLineNum sourceCode = contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash][(uint)context.InstructionPointer]; - sourceCodeTraceback += $"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}"; - traceback += sourceCodeTraceback; + traceback.Append($"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}"); } } //catch (Exception _) {; } - traceback += $"\r\n\tScriptHash={contextScriptHash}, InstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}"; + traceback.Append($"\r\n\tScriptHash={contextScriptHash}, InstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}"); } - traceback += $"\r\n{json["exception"]!.GetString()}"; + traceback.Append($"\r\n{json["exception"]!.GetString()}"); if (!logs.IsEmpty) - { - traceback += $"\r\n-------Logs-------({logs.Count})"; - } + traceback.Append($"\r\n-------Logs-------({logs.Count})"); + foreach (LogEventArgs log in logs) { string contractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, log.ScriptHash).Manifest.Name; - traceback += $"\r\n[{log.ScriptHash}] {contractName}: {log.Message}"; + traceback.Append($"\r\n[{log.ScriptHash}] {contractName}: {log.Message}"); } - json["traceback"] = traceback; - } - try - { - json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems))); - } - catch (InvalidOperationException) - { - json["stack"] = "error: invalid operation"; + json["traceback"] = traceback.ToString(); } + //try + //{ + json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems))); + //} + //catch (InvalidOperationException) + //{ + // json["stack"] = "error: invalid operation"; + //} return json; } diff --git a/Fairy.Tester.cs b/Fairy.Tester.cs index 2028aab..619e4ad 100644 --- a/Fairy.Tester.cs +++ b/Fairy.Tester.cs @@ -8,6 +8,7 @@ using Neo.Wallets; using System.Collections.Concurrent; using System.Numerics; +using System.Text; namespace Neo.Plugins { @@ -144,11 +145,11 @@ private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, b json["exception"] = GetExceptionMessage(newEngine.FaultException); if (json["exception"] != null) { - string traceback = ""; - try { if (newEngine.CallingScriptHash != null) traceback += $"CallingScriptHash={newEngine.CallingScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CallingScriptHash)?.Manifest.Name}]\r\n"; } catch { } - try { traceback += $"CurrentScriptHash={newEngine.CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CurrentScriptHash)?.Manifest.Name}]\r\n"; } catch { } - try { traceback += $"EntryScriptHash={newEngine.EntryScriptHash}\r\n"; } catch { } - traceback += newEngine.FaultException.StackTrace; + StringBuilder traceback = new(); + try { if (newEngine.CallingScriptHash != null) traceback.Append($"CallingScriptHash={newEngine.CallingScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CallingScriptHash)?.Manifest.Name}]\r\n"); } catch { } + try { traceback.Append($"CurrentScriptHash={newEngine.CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CurrentScriptHash)?.Manifest.Name}]\r\n"); } catch { } + try { traceback.Append($"EntryScriptHash={newEngine.EntryScriptHash}\r\n"); } catch { } + traceback.Append(newEngine.FaultException.StackTrace); foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack.Reverse()) { UInt160 contextScriptHash = context.GetScriptHash(); @@ -160,33 +161,32 @@ private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, b string sourceCodeTraceback = ""; SourceFilenameAndLineNum sourceCode = contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash][(uint)context.InstructionPointer]; sourceCodeTraceback += $"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}"; - traceback += sourceCodeTraceback; + traceback.Append(sourceCodeTraceback); } } //catch (Exception _) {; } - traceback += $"\r\n\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]"; + traceback.Append($"\r\n\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]"); } - traceback += $"\r\n{json["exception"]!.GetString()}"; + traceback.Append($"\r\n{json["exception"]!.GetString()}"); if (!logs.IsEmpty) - { - traceback += $"\r\n-------Logs-------({logs.Count})"; - } + traceback.Append($"\r\n-------Logs-------({logs.Count})"); + foreach (LogEventArgs log in logs) { string? contractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, log.ScriptHash)?.Manifest.Name; - traceback += $"\r\n[{log.ScriptHash}] {contractName}: {log.Message}"; + traceback.Append($"\r\n[{log.ScriptHash}] {contractName}: {log.Message}"); } - json["traceback"] = traceback; - } - try - { - json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems))); - } - catch (InvalidOperationException) - { - json["stack"] = "error: invalid operation"; + json["traceback"] = traceback.ToString(); } + //try + //{ + json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems))); + //} + //catch (InvalidOperationException) + //{ + // json["stack"] = "error: invalid operation"; + //} if (newEngine.State != VMState.FAULT) { if (witnesses == null)