Skip to content

Commit

Permalink
Merge branch 'standardize-exception' of github.com:Jim8y/neo into sta…
Browse files Browse the repository at this point in the history
…ndardize-exception

* 'standardize-exception' of github.com:Jim8y/neo:
  Clean using
  [Neo Core Add] add char support (neo-project#3441)
  [rpc] Extend `getversion` RPC response with additional protocol settings (neo-project#3443)
  update benchmark system (neo-project#3442)
  [`Optimization`] Parsing Smart Contract Script Analysis (neo-project#3420)
  `[Move]` Part-3 Classes into Different Library - `Neo.Extensions` (neo-project#3400)
  • Loading branch information
Jim8y committed Aug 2, 2024
2 parents 472122c + bca2ff6 commit 449adc0
Show file tree
Hide file tree
Showing 70 changed files with 481 additions and 251 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.cs file belongs to the neo project and is free
// Benchmarks.POC.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
Expand All @@ -9,19 +9,21 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.VM;
using System.Diagnostics;

namespace Neo;
namespace Neo.Benchmark;

static class Benchmarks
public class Benchmarks_PoCs
{
private static readonly ProtocolSettings protocol = ProtocolSettings.Load("config.json");
private static readonly NeoSystem system = new(protocol, (string)null);

public static void NeoIssue2725()
[Benchmark]
public void NeoIssue2725()
{
// https://github.com/neo-project/neo/issues/2725
// L00: INITSSLOT 1
Expand Down Expand Up @@ -70,10 +72,7 @@ private static void Run(string name, string poc)
using var snapshot = system.GetSnapshotCache();
using var engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, system.GenesisBlock, protocol, tx.SystemFee);
engine.LoadScript(tx.Script);
Stopwatch stopwatch = Stopwatch.StartNew();
engine.Execute();
stopwatch.Stop();
Debug.Assert(engine.State == VMState.FAULT);
Console.WriteLine($"Benchmark: {name},\tTime: {stopwatch.Elapsed}");
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Neo.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 3 additions & 6 deletions benchmarks/Neo.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo;
using System.Reflection;
using BenchmarkDotNet.Running;
using Neo.Benchmark;

foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
{
method.CreateDelegate<Action>().Invoke();
}
BenchmarkRunner.Run<Benchmarks_PoCs>();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.cs file belongs to the neo project and is free
// Benchmarks.POC.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
Expand All @@ -9,13 +9,15 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using System.Diagnostics;

namespace Neo.VM
namespace Neo.VM.Benchmark
{
public static class Benchmarks
public class Benchmarks_PoCs
{
public static void NeoIssue2528()
[Benchmark]
public void NeoIssue2528()
{
// https://github.com/neo-project/neo/issues/2528
// L01: INITSLOT 1, 0
Expand Down Expand Up @@ -47,7 +49,8 @@ public static void NeoIssue2528()
Run(nameof(NeoIssue2528), "VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U=");
}

public static void NeoVMIssue418()
[Benchmark]
public void NeoVMIssue418()
{
// https://github.com/neo-project/neo-vm/issues/418
// L00: NEWARRAY0
Expand Down Expand Up @@ -81,7 +84,8 @@ public static void NeoVMIssue418()
Run(nameof(NeoVMIssue418), "whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU=");
}

public static void NeoIssue2723()
[Benchmark]
public void NeoIssue2723()
{
// L00: INITSSLOT 1
// L01: PUSHINT32 130000
Expand All @@ -102,11 +106,8 @@ private static void Run(string name, string poc)
byte[] script = Convert.FromBase64String(poc);
using ExecutionEngine engine = new();
engine.LoadScript(script);
Stopwatch stopwatch = Stopwatch.StartNew();
engine.Execute();
stopwatch.Stop();
Debug.Assert(engine.State == VMState.HALT);
Console.WriteLine($"Benchmark: {name},\tTime: {stopwatch.Elapsed}");
}
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.VM.Benchmarks/Neo.VM.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Neo.VM\Neo.VM.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

</Project>
9 changes: 3 additions & 6 deletions benchmarks/Neo.VM.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.VM;
using System.Reflection;
using BenchmarkDotNet.Running;
using Neo.VM.Benchmark;

foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
{
method.CreateDelegate<Action>().Invoke();
}
BenchmarkRunner.Run<Benchmarks_PoCs>();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Extensions.cs file belongs to the neo project and is free
// AssemblyExtensions.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
Expand All @@ -17,7 +17,7 @@ namespace Neo
/// <summary>
/// Extension methods
/// </summary>
internal static class Extensions
internal static class AssemblyExtensions
{
public static string GetVersion(this Assembly assembly)
{
Expand Down
68 changes: 17 additions & 51 deletions src/Neo.CLI/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.SmartContract;
using Neo.VM;
using Neo.Wallets;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -56,7 +56,8 @@ private void OnParseCommand(string value)

if (result != null)
{
Console.WriteLine($"{pair.Key,-30}\t{result}");
ConsoleHelper.Info("", "-----", pair.Key, "-----");
ConsoleHelper.Info("", result, Environment.NewLine);
any = true;
}
}
Expand Down Expand Up @@ -417,62 +418,27 @@ private static string Base64Fixed(string str)
[ParseFunction("Base64 Smart Contract Script Analysis")]
private string? ScriptsToOpCode(string base64)
{
Script script;
try
{
var scriptData = Convert.FromBase64String(base64);
script = new Script(scriptData.ToArray(), true);
}
catch (Exception)
{
return null;
}
return ScriptsToOpCode(script);
}
var bytes = Convert.FromBase64String(base64);
var sb = new StringBuilder();
var line = 0;

private string ScriptsToOpCode(Script script)
{
//Initialize all InteropService
var dic = new Dictionary<uint, string>();
ApplicationEngine.Services.ToList().ForEach(p => dic.Add(p.Value.Hash, p.Value.Name));

//Analyzing Scripts
var ip = 0;
Instruction instruction;
var result = new List<string>();
while (ip < script.Length && (instruction = script.GetInstruction(ip)) != null)
{
ip += instruction.Size;

var op = instruction.OpCode;

if (op.ToString().StartsWith("PUSHINT"))
{
var operand = instruction.Operand.ToArray();
result.Add($"{op} {new BigInteger(operand)}");
}
else if (op == OpCode.SYSCALL)
foreach (var instruct in new VMInstruction(bytes))
{
var operand = instruction.Operand.ToArray();
result.Add($"{op} {dic[BitConverter.ToUInt32(operand)]}");
}
else
{
if (!instruction.Operand.IsEmpty && instruction.Operand.Length > 0)
{
var operand = instruction.Operand.ToArray();
var ascii = Encoding.Default.GetString(operand);
ascii = ascii.Any(p => p < '0' || p > 'z') ? operand.ToHexString() : ascii;

result.Add($"{op} {(operand.Length == 20 ? new UInt160(operand).ToString() : ascii)}");
}
if (instruct.OperandSize == 0)
sb.AppendFormat("L{0:D04}:{1:X04} {2}{3}", line, instruct.Position, instruct.OpCode, Environment.NewLine);
else
{
result.Add($"{op}");
}
sb.AppendFormat("L{0:D04}:{1:X04} {2,-10}{3}{4}", line, instruct.Position, instruct.OpCode, instruct.DecodeOperand(), Environment.NewLine);
line++;
}

return sb.ToString();
}
catch
{
return null;
}
return Environment.NewLine + string.Join("\r\n", result.ToArray());
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Vote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.Actor;
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.Actor;
using Neo.ConsoleService;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
Expand Down
1 change: 1 addition & 0 deletions src/Neo.CLI/Neo.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<ProjectReference Include="..\Neo.ConsoleService\Neo.ConsoleService.csproj" />
<ProjectReference Include="..\Neo.Extensions\Neo.Extensions.csproj" />
<ProjectReference Include="..\Neo\Neo.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 449adc0

Please sign in to comment.