From 0d3018bf64ae3ca4b5ca055b869cda84bf5c3a9f Mon Sep 17 00:00:00 2001 From: Hecate2 <2474101468@qq.com> Date: Fri, 12 Aug 2022 18:26:21 +0800 Subject: [PATCH] neo 3.4.0 support --- Fairy.Coverage.cs | 2 +- Fairy.Debugger.Breakpoint.cs | 14 ++++++------- Fairy.Debugger.DebugInfo.cs | 14 ++++++------- Fairy.Debugger.Snapshot.cs | 4 ++-- Fairy.Debugger.cs | 40 ++++++++++++++++++------------------ Fairy.Tester.Snapshot.cs | 20 +++++++++--------- Fairy.Tester.cs | 6 +++--- Fairy.Utils.cs | 9 ++++---- Fairy.Wallet.cs | 6 +++--- Fairy.csproj | 5 +++-- README.md | 7 +++++++ 11 files changed, 68 insertions(+), 59 deletions(-) diff --git a/Fairy.Coverage.cs b/Fairy.Coverage.cs index 1a98522..1198bc4 100644 --- a/Fairy.Coverage.cs +++ b/Fairy.Coverage.cs @@ -1,5 +1,5 @@ using Neo; -using Neo.IO.Json; +using Neo.Json; namespace Neo.Plugins { diff --git a/Fairy.Debugger.Breakpoint.cs b/Fairy.Debugger.Breakpoint.cs index a5931ab..61e05e3 100644 --- a/Fairy.Debugger.Breakpoint.cs +++ b/Fairy.Debugger.Breakpoint.cs @@ -1,4 +1,4 @@ -using Neo.IO.Json; +using Neo.Json; using Neo.SmartContract.Native; using System.Collections.Concurrent; @@ -10,7 +10,7 @@ public partial class Fairy readonly ConcurrentDictionary> contractScriptHashToSourceCodeBreakpoints = new(); [RpcMethod] - protected virtual JObject SetAssemblyBreakpoints(JArray _params) + protected virtual JToken SetAssemblyBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) @@ -38,7 +38,7 @@ protected virtual JObject SetAssemblyBreakpoints(JArray _params) } [RpcMethod] - protected virtual JObject ListAssemblyBreakpoints(JArray _params) + protected virtual JToken ListAssemblyBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) @@ -57,7 +57,7 @@ protected virtual JObject ListAssemblyBreakpoints(JArray _params) } [RpcMethod] - protected virtual JObject DeleteAssemblyBreakpoints(JArray _params) + protected virtual JToken DeleteAssemblyBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) @@ -90,7 +90,7 @@ protected virtual JObject DeleteAssemblyBreakpoints(JArray _params) } [RpcMethod] - protected virtual JObject SetSourceCodeBreakpoints(JArray _params) + protected virtual JToken SetSourceCodeBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) @@ -130,7 +130,7 @@ protected virtual JObject SetSourceCodeBreakpoints(JArray _params) } [RpcMethod] - protected virtual JObject ListSourceCodeBreakpoints(JArray _params) + protected virtual JToken ListSourceCodeBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) @@ -151,7 +151,7 @@ protected virtual JObject ListSourceCodeBreakpoints(JArray _params) } [RpcMethod] - protected virtual JObject DeleteSourceCodeBreakpoints(JArray _params) + protected virtual JToken DeleteSourceCodeBreakpoints(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); if (!contractScriptHashToInstructionPointerToSourceLineNum.ContainsKey(scriptHash)) diff --git a/Fairy.Debugger.DebugInfo.cs b/Fairy.Debugger.DebugInfo.cs index ff8e812..9e67b65 100644 --- a/Fairy.Debugger.DebugInfo.cs +++ b/Fairy.Debugger.DebugInfo.cs @@ -1,4 +1,4 @@ -using Neo.IO.Json; +using Neo.Json; using Neo.VM; using Neo.SmartContract.Native; using System.IO.Compression; @@ -43,14 +43,14 @@ public static string Unzip(byte[] zippedBuffer) } [RpcMethod] - protected virtual JObject SetDebugInfo(JArray _params) + protected virtual JToken SetDebugInfo(JArray _params) { string param0 = _params[0].AsString(); UInt160 scriptHash = UInt160.Parse(param0); // nccs YourContractProject.csproj --debug // find .nefdbgnfo beside your .nef contract, and // give me the base64encode(content) of .nefdbgnfo file - JObject nefDbgNfo = JObject.Parse(Unzip(Convert.FromBase64String(_params[1].AsString()))); + JObject nefDbgNfo = (JObject)JObject.Parse(Unzip(Convert.FromBase64String(_params[1].AsString()))); contractScriptHashToNefDbgNfo[scriptHash] = nefDbgNfo; // https://github.com/devhawk/DumpNef // dumpnef contract.nef > contract.nef.txt @@ -110,7 +110,7 @@ protected virtual JObject SetDebugInfo(JArray _params) } [RpcMethod] - protected virtual JObject ListDebugInfo(JArray _params) + protected virtual JToken ListDebugInfo(JArray _params) { JArray scriptHashes = new JArray(); foreach (UInt160 s in contractScriptHashToInstructionPointerToSourceLineNum.Keys) @@ -121,7 +121,7 @@ protected virtual JObject ListDebugInfo(JArray _params) } [RpcMethod] - protected virtual JObject ListFilenamesOfContract(JArray _params) + protected virtual JToken ListFilenamesOfContract(JArray _params) { string scriptHashStr = _params[0].AsString(); UInt160 scriptHash = UInt160.Parse(scriptHashStr); @@ -136,7 +136,7 @@ protected virtual JObject ListFilenamesOfContract(JArray _params) } [RpcMethod] - protected virtual JObject DeleteDebugInfo(JArray _params) + protected virtual JToken DeleteDebugInfo(JArray _params) { JObject json = new(); foreach (var s in _params) @@ -158,7 +158,7 @@ protected virtual JObject DeleteDebugInfo(JArray _params) } [RpcMethod] - protected virtual JObject GetMethodByInstructionPointer(JArray _params) + protected virtual JToken GetMethodByInstructionPointer(JArray _params) { UInt160 scriptHash = UInt160.Parse(_params[0].AsString()); uint instrcutionPointer = uint.Parse(_params[1].AsString()); diff --git a/Fairy.Debugger.Snapshot.cs b/Fairy.Debugger.Snapshot.cs index b5f6717..42b391d 100644 --- a/Fairy.Debugger.Snapshot.cs +++ b/Fairy.Debugger.Snapshot.cs @@ -1,4 +1,4 @@ -using Neo.IO.Json; +using Neo.Json; using System.Collections.Concurrent; namespace Neo.Plugins @@ -8,7 +8,7 @@ public partial class Fairy public readonly ConcurrentDictionary debugSessionToEngine = new(); [RpcMethod] - protected virtual JObject ListDebugSnapshots(JArray _params) + protected virtual JToken ListDebugSnapshots(JArray _params) { JArray session = new JArray(); foreach (string s in debugSessionToEngine.Keys) diff --git a/Fairy.Debugger.cs b/Fairy.Debugger.cs index 5673eb3..d407ac1 100644 --- a/Fairy.Debugger.cs +++ b/Fairy.Debugger.cs @@ -1,5 +1,5 @@ using Neo.IO; -using Neo.IO.Json; +using Neo.Json; using Neo.Network.P2P.Payloads; using Neo.Persistence; using Neo.SmartContract; @@ -21,13 +21,13 @@ enum BreakReason } [RpcMethod] - protected virtual JObject DebugFunctionWithSession(JArray _params) + protected virtual JToken DebugFunctionWithSession(JArray _params) { string session = _params[0].AsString(); bool writeSnapshot = _params[1].AsBoolean(); UInt160 script_hash = UInt160.Parse(_params[2].AsString()); string operation = _params[3].AsString(); - ContractParameter[] args = _params.Count >= 5 ? ((JArray)_params[4]).Select(p => ContractParameter.FromJson(p)).ToArray() : System.Array.Empty(); + ContractParameter[] args = _params.Count >= 5 ? ((JArray)_params[4]).Select(p => ContractParameter.FromJson((JObject)p)).ToArray() : System.Array.Empty(); Signers? signers = _params.Count >= 6 ? SignersFromJson((JArray)_params[5], system.Settings) : null; byte[] script; @@ -72,7 +72,7 @@ protected virtual JObject DebugFunctionWithSession(JArray _params) } [RpcMethod] - protected virtual JObject DebugContinue(JArray _params) + protected virtual JToken DebugContinue(JArray _params) { string session = _params[0].AsString(); FairyEngine newEngine = debugSessionToEngine[session]; @@ -110,7 +110,7 @@ private JObject DumpDebugResultJson(JObject json, FairyEngine newEngine, BreakRe traceback += newEngine.FaultException.StackTrace; foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack) { - traceback += $"\r\nInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction.OpCode}, Script Length={context.Script.Length}"; + traceback += $"\r\nInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}"; } if (!logs.IsEmpty) { @@ -239,7 +239,7 @@ private FairyEngine StepInto(FairyEngine engine, out BreakReason breakReason) } [RpcMethod] - protected virtual JObject DebugStepInto(JArray _params) + protected virtual JToken DebugStepInto(JArray _params) { string session = _params[0].AsString(); FairyEngine newEngine = debugSessionToEngine[session]; @@ -262,7 +262,7 @@ private FairyEngine StepOut(FairyEngine engine, out BreakReason breakReason) } [RpcMethod] - protected virtual JObject DebugStepOut(JArray _params) + protected virtual JToken DebugStepOut(JArray _params) { string session = _params[0].AsString(); FairyEngine newEngine = debugSessionToEngine[session]; @@ -296,7 +296,7 @@ private FairyEngine StepOverSourceCode(FairyEngine engine, out BreakReason break } [RpcMethod] - protected virtual JObject DebugStepOverSourceCode(JArray _params) + protected virtual JToken DebugStepOverSourceCode(JArray _params) { string session = _params[0].AsString(); FairyEngine newEngine = debugSessionToEngine[session]; @@ -309,7 +309,7 @@ protected virtual JObject DebugStepOverSourceCode(JArray _params) } [RpcMethod] - protected virtual JObject DebugStepOverAssembly(JArray _params) + protected virtual JToken DebugStepOverAssembly(JArray _params) { string session = _params[0].AsString(); FairyEngine newEngine = debugSessionToEngine[session]; @@ -322,13 +322,13 @@ protected virtual JObject DebugStepOverAssembly(JArray _params) } [RpcMethod] - protected virtual JObject DebugStepOver(JArray _params) + protected virtual JToken DebugStepOver(JArray _params) { return DebugStepOverSourceCode(_params); } [RpcMethod] - protected virtual JObject GetLocalVariables(JArray _params) + protected virtual JToken GetLocalVariables(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -337,7 +337,7 @@ protected virtual JObject GetLocalVariables(JArray _params) } [RpcMethod] - protected virtual JObject GetArguments(JArray _params) + protected virtual JToken GetArguments(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -346,7 +346,7 @@ protected virtual JObject GetArguments(JArray _params) } [RpcMethod] - protected virtual JObject GetStaticFields(JArray _params) + protected virtual JToken GetStaticFields(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -355,7 +355,7 @@ protected virtual JObject GetStaticFields(JArray _params) } [RpcMethod] - protected virtual JObject GetEvaluationStack(JArray _params) + protected virtual JToken GetEvaluationStack(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -364,7 +364,7 @@ protected virtual JObject GetEvaluationStack(JArray _params) } [RpcMethod] - protected virtual JObject GetInstructionPointer(JArray _params) + protected virtual JToken GetInstructionPointer(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -373,7 +373,7 @@ protected virtual JObject GetInstructionPointer(JArray _params) } [RpcMethod] - protected virtual JObject GetVariableValueByName(JArray _params) + protected virtual JToken GetVariableValueByName(JArray _params) { string session = _params[0].AsString(); string variableName = _params[1].AsString(); @@ -382,7 +382,7 @@ protected virtual JObject GetVariableValueByName(JArray _params) } [RpcMethod] - protected virtual JObject GetVariableNamesAndValues(JArray _params) + protected virtual JToken GetVariableNamesAndValues(JArray _params) { string session = _params[0].AsString(); int invocationStackIndex = _params.Count > 1 ? int.Parse(_params[1].AsString()) : 0; @@ -390,7 +390,7 @@ protected virtual JObject GetVariableNamesAndValues(JArray _params) Neo.VM.ExecutionContext invocationStackItem = newEngine.InvocationStack.ElementAt(invocationStackIndex); UInt160 invocationStackScriptHash = invocationStackItem.GetScriptHash(); int instructionPointer = invocationStackItem.InstructionPointer; - JObject method = GetMethodByInstructionPointer(new JArray(invocationStackScriptHash.ToString(), instructionPointer)); + JToken method = GetMethodByInstructionPointer(new JArray(invocationStackScriptHash.ToString(), instructionPointer)); JObject returnedJson = new(); JArray staticVariables = (JArray)contractScriptHashToNefDbgNfo[invocationStackScriptHash]["static-variables"]; foreach (JObject param in staticVariables) @@ -401,13 +401,13 @@ protected virtual JObject GetVariableNamesAndValues(JArray _params) } if (method != JObject.Null) { - foreach (JObject param in (JArray)method["params"]) + foreach (JToken param in (JArray)method["params"]) { string[] nameTypeAndIndex = param.AsString().Split(','); int index = int.Parse(nameTypeAndIndex[2]); returnedJson[nameTypeAndIndex[0]] = invocationStackItem.Arguments[index].ToJson(); } - foreach (JObject param in (JArray)method["variables"]) + foreach (JToken param in (JArray)method["variables"]) { string[] nameTypeAndIndex = param.AsString().Split(','); int index = int.Parse(nameTypeAndIndex[2]); diff --git a/Fairy.Tester.Snapshot.cs b/Fairy.Tester.Snapshot.cs index 2f8d92c..c939ca3 100644 --- a/Fairy.Tester.Snapshot.cs +++ b/Fairy.Tester.Snapshot.cs @@ -1,4 +1,4 @@ -using Neo.IO.Json; +using Neo.Json; using System.Collections.Concurrent; using System.Numerics; @@ -25,7 +25,7 @@ private FairyEngine BuildSnapshotWithDummyScript(FairyEngine engine = null) } [RpcMethod] - protected virtual JObject NewSnapshotsFromCurrentSystem(JArray _params) + protected virtual JToken NewSnapshotsFromCurrentSystem(JArray _params) { JObject json = new(); foreach (var param in _params) @@ -42,7 +42,7 @@ protected virtual JObject NewSnapshotsFromCurrentSystem(JArray _params) } [RpcMethod] - protected virtual JObject DeleteSnapshots(JArray _params) + protected virtual JToken DeleteSnapshots(JArray _params) { JObject json = new(); foreach (var s in _params) @@ -54,7 +54,7 @@ protected virtual JObject DeleteSnapshots(JArray _params) } [RpcMethod] - protected virtual JObject ListSnapshots(JArray _params) + protected virtual JToken ListSnapshots(JArray _params) { JArray session = new JArray(); foreach (string s in sessionToEngine.Keys) @@ -65,7 +65,7 @@ protected virtual JObject ListSnapshots(JArray _params) } [RpcMethod] - protected virtual JObject RenameSnapshot(JArray _params) + protected virtual JToken RenameSnapshot(JArray _params) { string from = _params[0].AsString(); string to = _params[1].AsString(); @@ -79,7 +79,7 @@ protected virtual JObject RenameSnapshot(JArray _params) } [RpcMethod] - protected virtual JObject CopySnapshot(JArray _params) + protected virtual JToken CopySnapshot(JArray _params) { string from = _params[0].AsString(); string to = _params[1].AsString(); @@ -91,7 +91,7 @@ protected virtual JObject CopySnapshot(JArray _params) } [RpcMethod] - protected virtual JObject SetSnapshotTimestamp(JArray _params) + protected virtual JToken SetSnapshotTimestamp(JArray _params) { string session = _params[0].AsString(); ulong timestamp = ulong.Parse(_params[1].AsString()); @@ -104,7 +104,7 @@ protected virtual JObject SetSnapshotTimestamp(JArray _params) } [RpcMethod] - protected virtual JObject GetSnapshotTimeStamp(JArray _params) + protected virtual JToken GetSnapshotTimeStamp(JArray _params) { JObject json = new(); foreach (var s in _params) @@ -116,7 +116,7 @@ protected virtual JObject GetSnapshotTimeStamp(JArray _params) } //[RpcMethod] - //protected virtual JObject SetSnapshotRandom(JArray _params) + //protected virtual JToken SetSnapshotRandom(JArray _params) //{ // string session = _params[0].AsString(); // string? designatedRandomString = _params[1]?.AsString(); @@ -139,7 +139,7 @@ protected virtual JObject GetSnapshotTimeStamp(JArray _params) //} //[RpcMethod] - //protected virtual JObject GetSnapshotRandom(JArray _params) + //protected virtual JToken GetSnapshotRandom(JArray _params) //{ // JObject json = new(); // foreach (var s in _params) diff --git a/Fairy.Tester.cs b/Fairy.Tester.cs index 095545b..430eb64 100644 --- a/Fairy.Tester.cs +++ b/Fairy.Tester.cs @@ -1,5 +1,5 @@ using Neo.IO; -using Neo.IO.Json; +using Neo.Json; using Neo.Network.P2P.Payloads; using Neo.Persistence; using Neo.SmartContract; @@ -26,7 +26,7 @@ protected virtual JObject InvokeFunctionWithSession(JArray _params) bool writeSnapshot = _params[1].AsBoolean(); UInt160 script_hash = UInt160.Parse(_params[2].AsString()); string operation = _params[3].AsString(); - ContractParameter[] args = _params.Count >= 5 ? ((JArray)_params[4]).Select(p => ContractParameter.FromJson(p)).ToArray() : System.Array.Empty(); + ContractParameter[] args = _params.Count >= 5 ? ((JArray)_params[4]).Select(p => ContractParameter.FromJson((JObject)p)).ToArray() : System.Array.Empty(); Signers signers = _params.Count >= 6 ? SignersFromJson((JArray)_params[5], system.Settings) : null; byte[] script; @@ -132,7 +132,7 @@ private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, b traceback += newEngine.FaultException.StackTrace; foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack) { - traceback += $"\r\nInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction.OpCode}, Script Length={context.Script.Length}"; + traceback += $"\r\nInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length}"; } if(!logs.IsEmpty) { diff --git a/Fairy.Utils.cs b/Fairy.Utils.cs index 09ccdfd..529aec5 100644 --- a/Fairy.Utils.cs +++ b/Fairy.Utils.cs @@ -1,6 +1,6 @@ using Neo.Cryptography.ECC; using Neo.IO; -using Neo.IO.Json; +using Neo.Json; using Neo.Network.P2P.Payloads; using Neo.Persistence; using Neo.SmartContract; @@ -34,9 +34,10 @@ protected virtual JObject VirtualDeploy(JArray _params) JObject json = new(); try { - Transaction tx = fairyWallet.MakeTransaction(snapshot.CreateSnapshot(), script); + Block dummyBlock = CreateDummyBlockWithTimestamp(oldEngine.Snapshot, system.Settings, timestamp: sessionToRuntimeArgs.GetValueOrDefault(session, new RuntimeArgs()).timestamp); + Transaction tx = fairyWallet.MakeTransaction(snapshot.CreateSnapshot(), script, persistingBlock: dummyBlock); UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name); - sessionToEngine[session] = FairyEngine.Run(script, snapshot.CreateSnapshot(), persistingBlock: CreateDummyBlockWithTimestamp(oldEngine.Snapshot, system.Settings, timestamp: sessionToRuntimeArgs.GetValueOrDefault(session, new RuntimeArgs()).timestamp), container: tx, settings: system.Settings, gas: settings.MaxGasInvoke); + sessionToEngine[session] = FairyEngine.Run(script, snapshot.CreateSnapshot(), persistingBlock: dummyBlock, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke); json[session] = hash.ToString(); } catch (InvalidOperationException ex) @@ -51,7 +52,7 @@ protected virtual JObject VirtualDeploy(JArray _params) } else { - throw ex; + throw ex.InnerException; } } return json; diff --git a/Fairy.Wallet.cs b/Fairy.Wallet.cs index 4b9b0c6..b831f40 100644 --- a/Fairy.Wallet.cs +++ b/Fairy.Wallet.cs @@ -1,5 +1,5 @@ using Neo.IO; -using Neo.IO.Json; +using Neo.Json; using Neo.Network.P2P.Payloads; using Neo.SmartContract; using Neo.Wallets; @@ -33,7 +33,7 @@ public override void Save() { } protected Wallet? fairyWallet = null; [RpcMethod] - protected virtual JObject OpenFairyWallet(JArray _params) + protected virtual JToken OpenFairyWallet(JArray _params) { string path = _params[0].AsString(); string password = _params[1].AsString(); @@ -58,7 +58,7 @@ protected virtual JObject OpenFairyWallet(JArray _params) } [RpcMethod] - protected virtual JObject CloseFairyWallet(JArray _params) + protected virtual JToken CloseFairyWallet(JArray _params) { fairyWallet = null; return true; diff --git a/Fairy.csproj b/Fairy.csproj index 61e9907..97bf866 100644 --- a/Fairy.csproj +++ b/Fairy.csproj @@ -7,8 +7,9 @@ - - + + + diff --git a/README.md b/README.md index 8ccf566..035d94b 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,10 @@ Non official client: https://github.com/Hecate2/neo-test-client Instructions for running neo-cli and RpcServer from full source codes for debugging: https://github.com/Hecate2/how-to-debug-neo +#### Building + +Consider cloning [neo-modules](https://github.com/neo-project/neo-modules) and building such a directory: `neo-modules/src/Fairy` and place everything in my repo into your `neo-modules/src/Fairy`. Build `Fairy.csproj`. + +Alternatively you can use `Fairy.sln` to build your own solution. + +You probably have to change the directories of dependencies in `Fairy.csproj` \ No newline at end of file