-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
239 changed files
with
49,296 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Neo.VM | ||
{ | ||
public static class Benchmarks | ||
{ | ||
public static void NeoIssue2528() | ||
{ | ||
// https://github.com/neo-project/neo/issues/2528 | ||
// L01: INITSLOT 1, 0 | ||
// L02: NEWARRAY0 | ||
// L03: DUP | ||
// L04: DUP | ||
// L05: PUSHINT16 2043 | ||
// L06: STLOC 0 | ||
// L07: PUSH1 | ||
// L08: PACK | ||
// L09: LDLOC 0 | ||
// L10: DEC | ||
// L11: STLOC 0 | ||
// L12: LDLOC 0 | ||
// L13: JMPIF_L L07 | ||
// L14: PUSH1 | ||
// L15: PACK | ||
// L16: APPEND | ||
// L17: PUSHINT32 38000 | ||
// L18: STLOC 0 | ||
// L19: PUSH0 | ||
// L20: PICKITEM | ||
// L21: LDLOC 0 | ||
// L22: DEC | ||
// L23: STLOC 0 | ||
// L24: LDLOC 0 | ||
// L25: JMPIF_L L19 | ||
// L26: DROP | ||
Run(nameof(NeoIssue2528), "VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U="); | ||
} | ||
|
||
public static void NeoVMIssue418() | ||
{ | ||
// https://github.com/neo-project/neo-vm/issues/418 | ||
// L00: NEWARRAY0 | ||
// L01: PUSH0 | ||
// L02: PICK | ||
// L03: PUSH1 | ||
// L04: PACK | ||
// L05: PUSH1 | ||
// L06: PICK | ||
// L07: PUSH1 | ||
// L08: PACK | ||
// L09: INITSSLOT 1 | ||
// L10: PUSHINT16 510 | ||
// L11: DEC | ||
// L12: STSFLD0 | ||
// L13: PUSH1 | ||
// L14: PICK | ||
// L15: PUSH1 | ||
// L16: PICK | ||
// L17: PUSH2 | ||
// L18: PACK | ||
// L19: REVERSE3 | ||
// L20: PUSH2 | ||
// L21: PACK | ||
// L22: LDSFLD0 | ||
// L23: DUP | ||
// L24: JMPIF L11 | ||
// L25: DROP | ||
// L26: ROT | ||
// L27: DROP | ||
Run(nameof(NeoVMIssue418), "whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU="); | ||
} | ||
|
||
public static void NeoIssue2723() | ||
{ | ||
// L00: INITSSLOT 1 | ||
// L01: PUSHINT32 130000 | ||
// L02: STSFLD 0 | ||
// L03: PUSHINT32 1048576 | ||
// L04: NEWBUFFER | ||
// L05: DROP | ||
// L06: LDSFLD 0 | ||
// L07: DEC | ||
// L08: DUP | ||
// L09: STSFLD 0 | ||
// L10: JMPIF L03 | ||
Run(nameof(NeoIssue2723), "VgEC0PsBAGcAAgAAEACIRV8AnUpnACTz"); | ||
} | ||
|
||
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}"); | ||
} | ||
} | ||
} |
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>Neo.VM</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Neo.VM\Neo.VM.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Neo.VM; | ||
using System.Reflection; | ||
|
||
foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static)) | ||
{ | ||
method.CreateDelegate<Action>().Invoke(); | ||
} |
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,31 @@ | ||
// Copyright (C) 2016-2023 The Neo Project. | ||
// | ||
// The neo-vm is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System; | ||
|
||
namespace Neo.VM | ||
{ | ||
/// <summary> | ||
/// Represents the exception thrown when the bad script is parsed. | ||
/// </summary> | ||
public class BadScriptException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BadScriptException"/> class. | ||
/// </summary> | ||
public BadScriptException() { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BadScriptException"/> class with a specified error message. | ||
/// </summary> | ||
/// <param name="message">The message that describes the error.</param> | ||
public BadScriptException(string message) : base(message) { } | ||
} | ||
} |
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,21 @@ | ||
// Copyright (C) 2016-2023 The Neo Project. | ||
// | ||
// The neo-vm is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System; | ||
|
||
namespace Neo.VM | ||
{ | ||
public class CatchableException : Exception | ||
{ | ||
public CatchableException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
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,129 @@ | ||
// Copyright (C) 2016-2023 The Neo Project. | ||
// | ||
// The neo-vm is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
namespace Neo.VM.Collections | ||
{ | ||
internal class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue> | ||
where TKey : notnull | ||
{ | ||
private class TItem | ||
{ | ||
public readonly TKey Key; | ||
public TValue Value; | ||
|
||
public TItem(TKey key, TValue value) | ||
{ | ||
this.Key = key; | ||
this.Value = value; | ||
} | ||
} | ||
|
||
private class InternalCollection : KeyedCollection<TKey, TItem> | ||
{ | ||
protected override TKey GetKeyForItem(TItem item) | ||
{ | ||
return item.Key; | ||
} | ||
} | ||
|
||
private readonly InternalCollection collection = new(); | ||
|
||
public int Count => collection.Count; | ||
public bool IsReadOnly => false; | ||
public ICollection<TKey> Keys => collection.Select(p => p.Key).ToArray(); | ||
public ICollection<TValue> Values => collection.Select(p => p.Value).ToArray(); | ||
|
||
public TValue this[TKey key] | ||
{ | ||
get | ||
{ | ||
return collection[key].Value; | ||
} | ||
set | ||
{ | ||
if (collection.TryGetValue(key, out var entry)) | ||
entry.Value = value; | ||
else | ||
Add(key, value); | ||
} | ||
} | ||
|
||
public void Add(TKey key, TValue value) | ||
{ | ||
collection.Add(new TItem(key, value)); | ||
} | ||
|
||
public bool ContainsKey(TKey key) | ||
{ | ||
return collection.Contains(key); | ||
} | ||
|
||
public bool Remove(TKey key) | ||
{ | ||
return collection.Remove(key); | ||
} | ||
|
||
// supress warning of value parameter nullability mismatch | ||
#pragma warning disable CS8767 | ||
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) | ||
#pragma warning restore CS8767 | ||
{ | ||
if (collection.TryGetValue(key, out var entry)) | ||
{ | ||
value = entry.Value; | ||
return true; | ||
} | ||
value = default; | ||
return false; | ||
} | ||
|
||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) | ||
{ | ||
Add(item.Key, item.Value); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
collection.Clear(); | ||
} | ||
|
||
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return collection.Contains(item.Key); | ||
} | ||
|
||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) | ||
{ | ||
for (int i = 0; i < collection.Count; i++) | ||
array[i + arrayIndex] = new KeyValuePair<TKey, TValue>(collection[i].Key, collection[i].Value); | ||
} | ||
|
||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return collection.Remove(item.Key); | ||
} | ||
|
||
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() | ||
{ | ||
return collection.Select(p => new KeyValuePair<TKey, TValue>(p.Key, p.Value)).GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return collection.Select(p => new KeyValuePair<TKey, TValue>(p.Key, p.Value)).GetEnumerator(); | ||
} | ||
} | ||
} |
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 @@ | ||
// Copyright (C) 2016-2023 The Neo Project. | ||
// | ||
// The neo-vm is free software distributed under the MIT software license, | ||
// see the accompanying file LICENSE in the main directory of the | ||
// project or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace Neo.VM.Cryptography | ||
{ | ||
#if !NET5_0_OR_GREATER | ||
static class BitOperations | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static uint RotateLeft(uint value, int offset) | ||
=> (value << offset) | (value >> (32 - offset)); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static ulong RotateLeft(ulong value, int offset) | ||
=> (value << offset) | (value >> (64 - offset)); | ||
} | ||
#endif | ||
} |
Oops, something went wrong.