-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Test] add instructions to the contract methods (#1191)
* add instructions to the contract methods * update github action * move opcodes position in artifacts * add base64 script to methods * reverse change to TestCleanup. * Fix ut * Reduce location according to the max one * format * Update src/Neo.SmartContract.Testing/Extensions/ArtifactExtensions.cs --------- Co-authored-by: Shargon <[email protected]>
- Loading branch information
Showing
98 changed files
with
16,544 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection.Emit; | ||
using System.Text.RegularExpressions; | ||
using Neo.Json; | ||
using Neo.SmartContract; | ||
using Neo.VM; | ||
using OpCode = Neo.VM.OpCode; | ||
|
||
namespace Neo.Disassembler.CSharp; | ||
|
||
public static class Disassembler | ||
{ | ||
private static readonly Regex RangeRegex = new(@"(\d+)\-(\d+)", RegexOptions.Compiled); | ||
|
||
public static List<Instruction> ConvertScriptToInstructions(byte[] script) | ||
{ | ||
var res = EnumerateInstructions(script); | ||
|
||
return res.Select(x => x.instruction).ToList(); | ||
} | ||
|
||
public static List<(int address, Instruction instruction)> ConvertMethodToInstructions(NefFile nef, JToken DebugInfo, string method) | ||
{ | ||
var (start, end) = GetMethodStartEndAddress(method, DebugInfo); | ||
var instructions = EnumerateInstructions(nef.Script).ToList(); | ||
return instructions.Where( | ||
ai => ai.address >= start && ai.address <= end).Select(ai => (ai.address - start, ai.instruction)).ToList(); | ||
} | ||
|
||
private static (int start, int end) GetMethodStartEndAddress(string name, JToken debugInfo) | ||
{ | ||
name = name.Length == 0 ? string.Empty : string.Concat(name[0].ToString().ToUpper(), name.AsSpan(1)); // first letter uppercase | ||
int start = -1, end = -1; | ||
foreach (var method in (JArray)debugInfo["methods"]!) | ||
{ | ||
var methodName = method!["name"]!.AsString().Split(",")[1]; | ||
if (methodName == name) | ||
{ | ||
var rangeGroups = RangeRegex.Match(method["range"]!.AsString()).Groups; | ||
(start, end) = (int.Parse(rangeGroups[1].ToString()), int.Parse(rangeGroups[2].ToString())); | ||
} | ||
} | ||
return (start, end); | ||
} | ||
|
||
private static IEnumerable<(int address, Instruction instruction)> EnumerateInstructions(this Script script) | ||
{ | ||
var address = 0; | ||
var opcode = OpCode.PUSH0; | ||
Instruction instruction; | ||
for (; address < script.Length; address += instruction.Size) | ||
{ | ||
instruction = script.GetInstruction(address); | ||
opcode = instruction.OpCode; | ||
yield return (address, instruction); | ||
} | ||
if (opcode != OpCode.RET) | ||
yield return (address, Instruction.RET); | ||
} | ||
|
||
public static string InstructionToString(this Instruction instruction) | ||
{ | ||
var opcode = instruction.OpCode.ToString(); | ||
var operand = instruction.Operand; | ||
|
||
if (operand.IsEmpty || operand.Length == 0) | ||
{ | ||
return $"OpCode.{opcode}"; | ||
} | ||
var operandString = BitConverter.ToString(operand.ToArray()).Replace("-", ""); | ||
return $"OpCode.{opcode} {operandString}"; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Neo.Disassembler.CSharp/Neo.Disassembler.CSharp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Title>Neo.Disassembler.CSharp</Title> | ||
<LangVersion>latest</LangVersion> | ||
<AssemblyTitle>Neo.Disassembler.CSharp</AssemblyTitle> | ||
<AssemblyName>Neo.Disassembler.CSharp</AssemblyName> | ||
<RootNamespace>Neo.Disassembler.CSharp</RootNamespace> | ||
<PackageId>Neo.Disassembler.CSharp</PackageId> | ||
<PackageTags>NEO;Blockchain;Smart Contract</PackageTags> | ||
<Description>Disassembler for NEO smart contract.</Description> | ||
<IncludeContentInPack>true</IncludeContentInPack> | ||
<ContentTargetFolders>content</ContentTargetFolders> | ||
<Nullable>enable</Nullable> | ||
<NoWarn>$(NoWarn);NU5128</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\neo\src\Neo.Extensions\Neo.Extensions.csproj" /> | ||
<ProjectReference Include="..\..\neo\src\Neo.IO\Neo.IO.csproj" /> | ||
<ProjectReference Include="..\..\neo\src\Neo.Json\Neo.Json.csproj" /> | ||
<ProjectReference Include="..\..\neo\src\Neo.VM\Neo.VM.csproj" /> | ||
<ProjectReference Include="..\..\neo\src\Neo\Neo.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.