Skip to content

Commit 449adc0

Browse files
committed
Merge branch 'standardize-exception' of github.com:Jim8y/neo into standardize-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)
2 parents 472122c + bca2ff6 commit 449adc0

File tree

70 files changed

+481
-251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+481
-251
lines changed

benchmarks/Neo.Benchmarks/Benchmarks.cs benchmarks/Neo.Benchmarks/Benchmarks.POC.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (C) 2015-2024 The Neo Project.
22
//
3-
// Benchmarks.cs file belongs to the neo project and is free
3+
// Benchmarks.POC.cs file belongs to the neo project and is free
44
// software distributed under the MIT software license, see the
55
// accompanying file LICENSE in the main directory of the
66
// repository or http://www.opensource.org/licenses/mit-license.php
@@ -9,19 +9,21 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using BenchmarkDotNet.Attributes;
1213
using Neo.Network.P2P.Payloads;
1314
using Neo.SmartContract;
1415
using Neo.VM;
1516
using System.Diagnostics;
1617

17-
namespace Neo;
18+
namespace Neo.Benchmark;
1819

19-
static class Benchmarks
20+
public class Benchmarks_PoCs
2021
{
2122
private static readonly ProtocolSettings protocol = ProtocolSettings.Load("config.json");
2223
private static readonly NeoSystem system = new(protocol, (string)null);
2324

24-
public static void NeoIssue2725()
25+
[Benchmark]
26+
public void NeoIssue2725()
2527
{
2628
// https://github.com/neo-project/neo/issues/2725
2729
// L00: INITSSLOT 1
@@ -70,10 +72,7 @@ private static void Run(string name, string poc)
7072
using var snapshot = system.GetSnapshotCache();
7173
using var engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, system.GenesisBlock, protocol, tx.SystemFee);
7274
engine.LoadScript(tx.Script);
73-
Stopwatch stopwatch = Stopwatch.StartNew();
7475
engine.Execute();
75-
stopwatch.Stop();
7676
Debug.Assert(engine.State == VMState.FAULT);
77-
Console.WriteLine($"Benchmark: {name},\tTime: {stopwatch.Elapsed}");
7877
}
7978
}

benchmarks/Neo.Benchmarks/Neo.Benchmarks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
12+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
1213
</ItemGroup>
1314

1415
<ItemGroup>

benchmarks/Neo.Benchmarks/Program.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12-
using Neo;
13-
using System.Reflection;
12+
using BenchmarkDotNet.Running;
13+
using Neo.Benchmark;
1414

15-
foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
16-
{
17-
method.CreateDelegate<Action>().Invoke();
18-
}
15+
BenchmarkRunner.Run<Benchmarks_PoCs>();

benchmarks/Neo.VM.Benchmarks/Benchmarks.cs benchmarks/Neo.VM.Benchmarks/Benchmarks.POC.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (C) 2015-2024 The Neo Project.
22
//
3-
// Benchmarks.cs file belongs to the neo project and is free
3+
// Benchmarks.POC.cs file belongs to the neo project and is free
44
// software distributed under the MIT software license, see the
55
// accompanying file LICENSE in the main directory of the
66
// repository or http://www.opensource.org/licenses/mit-license.php
@@ -9,13 +9,15 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using BenchmarkDotNet.Attributes;
1213
using System.Diagnostics;
1314

14-
namespace Neo.VM
15+
namespace Neo.VM.Benchmark
1516
{
16-
public static class Benchmarks
17+
public class Benchmarks_PoCs
1718
{
18-
public static void NeoIssue2528()
19+
[Benchmark]
20+
public void NeoIssue2528()
1921
{
2022
// https://github.com/neo-project/neo/issues/2528
2123
// L01: INITSLOT 1, 0
@@ -47,7 +49,8 @@ public static void NeoIssue2528()
4749
Run(nameof(NeoIssue2528), "VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U=");
4850
}
4951

50-
public static void NeoVMIssue418()
52+
[Benchmark]
53+
public void NeoVMIssue418()
5154
{
5255
// https://github.com/neo-project/neo-vm/issues/418
5356
// L00: NEWARRAY0
@@ -81,7 +84,8 @@ public static void NeoVMIssue418()
8184
Run(nameof(NeoVMIssue418), "whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU=");
8285
}
8386

84-
public static void NeoIssue2723()
87+
[Benchmark]
88+
public void NeoIssue2723()
8589
{
8690
// L00: INITSSLOT 1
8791
// L01: PUSHINT32 130000
@@ -102,11 +106,8 @@ private static void Run(string name, string poc)
102106
byte[] script = Convert.FromBase64String(poc);
103107
using ExecutionEngine engine = new();
104108
engine.LoadScript(script);
105-
Stopwatch stopwatch = Stopwatch.StartNew();
106109
engine.Execute();
107-
stopwatch.Stop();
108110
Debug.Assert(engine.State == VMState.HALT);
109-
Console.WriteLine($"Benchmark: {name},\tTime: {stopwatch.Elapsed}");
110111
}
111112
}
112113
}

benchmarks/Neo.VM.Benchmarks/Neo.VM.Benchmarks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<ProjectReference Include="..\..\src\Neo.VM\Neo.VM.csproj" />
13+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
1314
</ItemGroup>
1415

1516
</Project>

benchmarks/Neo.VM.Benchmarks/Program.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12-
using Neo.VM;
13-
using System.Reflection;
12+
using BenchmarkDotNet.Running;
13+
using Neo.VM.Benchmark;
1414

15-
foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
16-
{
17-
method.CreateDelegate<Action>().Invoke();
18-
}
15+
BenchmarkRunner.Run<Benchmarks_PoCs>();

src/Neo.CLI/Extensions.cs src/Neo.CLI/AssemblyExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (C) 2015-2024 The Neo Project.
22
//
3-
// Extensions.cs file belongs to the neo project and is free
3+
// AssemblyExtensions.cs file belongs to the neo project and is free
44
// software distributed under the MIT software license, see the
55
// accompanying file LICENSE in the main directory of the
66
// repository or http://www.opensource.org/licenses/mit-license.php
@@ -17,7 +17,7 @@ namespace Neo
1717
/// <summary>
1818
/// Extension methods
1919
/// </summary>
20-
internal static class Extensions
20+
internal static class AssemblyExtensions
2121
{
2222
public static string GetVersion(this Assembly assembly)
2323
{

src/Neo.CLI/CLI/MainService.Tools.cs

+17-51
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
using Neo.ConsoleService;
1313
using Neo.Cryptography.ECC;
14+
using Neo.Extensions;
1415
using Neo.IO;
1516
using Neo.SmartContract;
16-
using Neo.VM;
1717
using Neo.Wallets;
1818
using System;
1919
using System.Collections.Generic;
@@ -56,7 +56,8 @@ private void OnParseCommand(string value)
5656

5757
if (result != null)
5858
{
59-
Console.WriteLine($"{pair.Key,-30}\t{result}");
59+
ConsoleHelper.Info("", "-----", pair.Key, "-----");
60+
ConsoleHelper.Info("", result, Environment.NewLine);
6061
any = true;
6162
}
6263
}
@@ -417,62 +418,27 @@ private static string Base64Fixed(string str)
417418
[ParseFunction("Base64 Smart Contract Script Analysis")]
418419
private string? ScriptsToOpCode(string base64)
419420
{
420-
Script script;
421421
try
422422
{
423-
var scriptData = Convert.FromBase64String(base64);
424-
script = new Script(scriptData.ToArray(), true);
425-
}
426-
catch (Exception)
427-
{
428-
return null;
429-
}
430-
return ScriptsToOpCode(script);
431-
}
423+
var bytes = Convert.FromBase64String(base64);
424+
var sb = new StringBuilder();
425+
var line = 0;
432426

433-
private string ScriptsToOpCode(Script script)
434-
{
435-
//Initialize all InteropService
436-
var dic = new Dictionary<uint, string>();
437-
ApplicationEngine.Services.ToList().ForEach(p => dic.Add(p.Value.Hash, p.Value.Name));
438-
439-
//Analyzing Scripts
440-
var ip = 0;
441-
Instruction instruction;
442-
var result = new List<string>();
443-
while (ip < script.Length && (instruction = script.GetInstruction(ip)) != null)
444-
{
445-
ip += instruction.Size;
446-
447-
var op = instruction.OpCode;
448-
449-
if (op.ToString().StartsWith("PUSHINT"))
450-
{
451-
var operand = instruction.Operand.ToArray();
452-
result.Add($"{op} {new BigInteger(operand)}");
453-
}
454-
else if (op == OpCode.SYSCALL)
427+
foreach (var instruct in new VMInstruction(bytes))
455428
{
456-
var operand = instruction.Operand.ToArray();
457-
result.Add($"{op} {dic[BitConverter.ToUInt32(operand)]}");
458-
}
459-
else
460-
{
461-
if (!instruction.Operand.IsEmpty && instruction.Operand.Length > 0)
462-
{
463-
var operand = instruction.Operand.ToArray();
464-
var ascii = Encoding.Default.GetString(operand);
465-
ascii = ascii.Any(p => p < '0' || p > 'z') ? operand.ToHexString() : ascii;
466-
467-
result.Add($"{op} {(operand.Length == 20 ? new UInt160(operand).ToString() : ascii)}");
468-
}
429+
if (instruct.OperandSize == 0)
430+
sb.AppendFormat("L{0:D04}:{1:X04} {2}{3}", line, instruct.Position, instruct.OpCode, Environment.NewLine);
469431
else
470-
{
471-
result.Add($"{op}");
472-
}
432+
sb.AppendFormat("L{0:D04}:{1:X04} {2,-10}{3}{4}", line, instruct.Position, instruct.OpCode, instruct.DecodeOperand(), Environment.NewLine);
433+
line++;
473434
}
435+
436+
return sb.ToString();
437+
}
438+
catch
439+
{
440+
return null;
474441
}
475-
return Environment.NewLine + string.Join("\r\n", result.ToArray());
476442
}
477443

478444
/// <summary>

src/Neo.CLI/CLI/MainService.Vote.cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using Neo.ConsoleService;
1313
using Neo.Cryptography.ECC;
14+
using Neo.Extensions;
1415
using Neo.Json;
1516
using Neo.SmartContract;
1617
using Neo.SmartContract.Native;

src/Neo.CLI/CLI/MainService.Wallet.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Akka.Actor;
1313
using Neo.ConsoleService;
1414
using Neo.Cryptography.ECC;
15+
using Neo.Extensions;
1516
using Neo.Json;
1617
using Neo.Network.P2P.Payloads;
1718
using Neo.Persistence;

src/Neo.CLI/CLI/MainService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Akka.Actor;
1313
using Neo.ConsoleService;
1414
using Neo.Cryptography.ECC;
15+
using Neo.Extensions;
1516
using Neo.IO;
1617
using Neo.Json;
1718
using Neo.Ledger;

src/Neo.CLI/Neo.CLI.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
<ItemGroup>
3333
<ProjectReference Include="..\Neo.ConsoleService\Neo.ConsoleService.csproj" />
34+
<ProjectReference Include="..\Neo.Extensions\Neo.Extensions.csproj" />
3435
<ProjectReference Include="..\Neo\Neo.csproj" />
3536
</ItemGroup>
3637

0 commit comments

Comments
 (0)