Skip to content

Commit

Permalink
Merge branch 'master' into HF_Echidna
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Nov 23, 2024
2 parents eb96d14 + af5242f commit 74498e5
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 105 deletions.
13 changes: 4 additions & 9 deletions src/Neo.VM/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ public class Script
/// <summary>
/// The length of the script.
/// </summary>
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _value.Length;
}
}
public int Length { get; }

/// <summary>
/// Gets the <see cref="OpCode"/> at the specified index.
Expand Down Expand Up @@ -74,6 +67,7 @@ public Script(ReadOnlyMemory<byte> script) : this(script, false)
public Script(ReadOnlyMemory<byte> script, bool strictMode)
{
_value = script;
Length = _value.Length;
if (strictMode)
{
for (int ip = 0; ip < script.Length; ip += GetInstruction(ip).Size) { }
Expand Down Expand Up @@ -143,11 +137,12 @@ public Script(ReadOnlyMemory<byte> script, bool strictMode)
/// <param name="ip">The position to get the <see cref="Instruction"/>.</param>
/// <returns>The <see cref="Instruction"/> at the specified position.</returns>
/// <exception cref="ArgumentException">In strict mode, the <see cref="Instruction"/> was not found at the specified position.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Instruction GetInstruction(int ip)
{
if (ip >= Length) throw new ArgumentOutOfRangeException(nameof(ip));
if (!_instructions.TryGetValue(ip, out Instruction? instruction))
{
if (ip >= Length) throw new ArgumentOutOfRangeException(nameof(ip));
if (strictMode) throw new ArgumentException($"ip not found with strict mode", nameof(ip));
instruction = new Instruction(_value, ip);
_instructions.Add(ip, instruction);
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.VM/Types/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Neo.VM.Types
{
Expand All @@ -29,6 +30,7 @@ public class Array : CompoundType, IReadOnlyList<StackItem>
/// <returns>The item at the specified index.</returns>
public StackItem this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _array[index];
set
{
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.VM/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public override bool Equals(StackItem? other)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool GetBoolean()
{
return value;
Expand All @@ -57,6 +58,7 @@ public override int GetHashCode()
return HashCode.Combine(value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BigInteger GetInteger()
{
return value ? BigInteger.One : BigInteger.Zero;
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.VM/Types/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ internal bool Equals(StackItem? other, ref uint limits)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool GetBoolean()
{
if (Size > Integer.MaxSize) throw new InvalidCastException();
return GetSpan().NotZero();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BigInteger GetInteger()
{
if (Size > Integer.MaxSize) throw new InvalidCastException($"Can not convert {nameof(ByteString)} to an integer, MaxSize of {nameof(Types.Integer)} is exceeded: {Size}/{Integer.MaxSize}.");
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.VM/Types/Integer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public override bool Equals(StackItem? other)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool GetBoolean()
{
return !value.IsZero;
Expand All @@ -72,6 +73,7 @@ public override int GetHashCode()
return HashCode.Combine(value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override BigInteger GetInteger()
{
return value;
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.VM/Types/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Neo.VM.Types
{
Expand All @@ -37,6 +38,7 @@ public class Map : CompoundType, IReadOnlyDictionary<PrimitiveType, StackItem>
/// <returns>The element that has the specified key in the map.</returns>
public StackItem this[PrimitiveType key]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (key.Size > MaxKeySize)
Expand Down
3 changes: 3 additions & 0 deletions src/Neo.VM/Types/Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Neo.VM.Types
{
Expand All @@ -36,11 +37,13 @@ public override bool Equals(StackItem? other)
return other is Null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool GetBoolean()
{
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return 0;
Expand Down
41 changes: 15 additions & 26 deletions src/Plugins/LevelDBStore/IO/Data/LevelDB/DB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,15 @@ namespace Neo.IO.Data.LevelDB
/// A DB is a persistent ordered map from keys to values.
/// A DB is safe for concurrent access from multiple threads without any external synchronization.
/// </summary>
public class DB : IDisposable
public class DB : LevelDBHandle
{
private IntPtr handle;
private DB(IntPtr handle) : base(handle) { }

/// <summary>
/// Return true if haven't got valid handle
/// </summary>
public bool IsDisposed => handle == IntPtr.Zero;

private DB(IntPtr handle)
{
this.handle = handle;
}

public void Dispose()
protected override void FreeUnManagedObjects()
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
Native.leveldb_close(handle);
handle = IntPtr.Zero;
Native.leveldb_close(Handle);
}
}

Expand All @@ -48,7 +37,7 @@ public void Dispose()
/// </summary>
public void Delete(WriteOptions options, byte[] key)
{
Native.leveldb_delete(handle, options.handle, key, (UIntPtr)key.Length, out IntPtr error);
Native.leveldb_delete(Handle, options.Handle, key, (UIntPtr)key.Length, out var error);
NativeHelper.CheckError(error);
}

Expand All @@ -58,7 +47,7 @@ public void Delete(WriteOptions options, byte[] key)
/// </summary>
public byte[] Get(ReadOptions options, byte[] key)
{
IntPtr value = Native.leveldb_get(handle, options.handle, key, (UIntPtr)key.Length, out UIntPtr length, out IntPtr error);
var value = Native.leveldb_get(Handle, options.Handle, key, (UIntPtr)key.Length, out var length, out var error);
try
{
NativeHelper.CheckError(error);
Expand All @@ -72,7 +61,7 @@ public byte[] Get(ReadOptions options, byte[] key)

public bool Contains(ReadOptions options, byte[] key)
{
IntPtr value = Native.leveldb_get(handle, options.handle, key, (UIntPtr)key.Length, out _, out IntPtr error);
var value = Native.leveldb_get(Handle, options.Handle, key, (UIntPtr)key.Length, out _, out var error);
NativeHelper.CheckError(error);

if (value != IntPtr.Zero)
Expand All @@ -86,12 +75,12 @@ public bool Contains(ReadOptions options, byte[] key)

public Snapshot GetSnapshot()
{
return new Snapshot(handle);
return new Snapshot(Handle);
}

public Iterator NewIterator(ReadOptions options)
{
return new Iterator(Native.leveldb_create_iterator(handle, options.handle));
return new Iterator(Native.leveldb_create_iterator(Handle, options.Handle));
}

public static DB Open(string name)
Expand All @@ -101,9 +90,9 @@ public static DB Open(string name)

public static DB Open(string name, Options options)
{
IntPtr handle = Native.leveldb_open(options.handle, Path.GetFullPath(name), out IntPtr error);
var Handle = Native.leveldb_open(options.Handle, Path.GetFullPath(name), out var error);
NativeHelper.CheckError(error);
return new DB(handle);
return new DB(Handle);
}

/// <summary>
Expand All @@ -112,7 +101,7 @@ public static DB Open(string name, Options options)
/// </summary>
public void Put(WriteOptions options, byte[] key, byte[] value)
{
Native.leveldb_put(handle, options.handle, key, (UIntPtr)key.Length, value, (UIntPtr)value.Length, out IntPtr error);
Native.leveldb_put(Handle, options.Handle, key, (UIntPtr)key.Length, value, (UIntPtr)value.Length, out var error);
NativeHelper.CheckError(error);
}

Expand All @@ -124,13 +113,13 @@ public void Put(WriteOptions options, byte[] key, byte[] value)
/// </summary>
public static void Repair(string name, Options options)
{
Native.leveldb_repair_db(options.handle, Path.GetFullPath(name), out IntPtr error);
Native.leveldb_repair_db(options.Handle, Path.GetFullPath(name), out var error);
NativeHelper.CheckError(error);
}

public void Write(WriteOptions options, WriteBatch write_batch)
{
Native.leveldb_write(handle, options.handle, write_batch.handle, out IntPtr error);
Native.leveldb_write(Handle, options.Handle, write_batch.Handle, out var error);
NativeHelper.CheckError(error);
}
}
Expand Down
34 changes: 14 additions & 20 deletions src/Plugins/LevelDBStore/IO/Data/LevelDB/Iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@ namespace Neo.IO.Data.LevelDB
/// <summary>
/// An iterator yields a sequence of key/value pairs from a database.
/// </summary>
public class Iterator : IDisposable
public class Iterator : LevelDBHandle
{
private IntPtr handle;

internal Iterator(IntPtr handle)
{
this.handle = handle;
}
internal Iterator(IntPtr handle) : base(handle) { }

private void CheckError()
{
Native.leveldb_iter_get_error(handle, out IntPtr error);
Native.leveldb_iter_get_error(Handle, out var error);
NativeHelper.CheckError(error);
}

public void Dispose()
protected override void FreeUnManagedObjects()
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
Native.leveldb_iter_destroy(handle);
handle = IntPtr.Zero;
Native.leveldb_iter_destroy(Handle);
}
}

Expand All @@ -46,7 +40,7 @@ public void Dispose()
/// </summary>
public byte[] Key()
{
IntPtr key = Native.leveldb_iter_key(handle, out UIntPtr length);
var key = Native.leveldb_iter_key(Handle, out var length);
CheckError();
return key.ToByteArray(length);
}
Expand All @@ -58,13 +52,13 @@ public byte[] Key()
/// </summary>
public void Next()
{
Native.leveldb_iter_next(handle);
Native.leveldb_iter_next(Handle);
CheckError();
}

public void Prev()
{
Native.leveldb_iter_prev(handle);
Native.leveldb_iter_prev(Handle);
CheckError();
}

Expand All @@ -75,12 +69,12 @@ public void Prev()
/// </summary>
public void Seek(byte[] target)
{
Native.leveldb_iter_seek(handle, target, (UIntPtr)target.Length);
Native.leveldb_iter_seek(Handle, target, (UIntPtr)target.Length);
}

public void SeekToFirst()
{
Native.leveldb_iter_seek_to_first(handle);
Native.leveldb_iter_seek_to_first(Handle);
}

/// <summary>
Expand All @@ -89,17 +83,17 @@ public void SeekToFirst()
/// </summary>
public void SeekToLast()
{
Native.leveldb_iter_seek_to_last(handle);
Native.leveldb_iter_seek_to_last(Handle);
}

public bool Valid()
{
return Native.leveldb_iter_valid(handle);
return Native.leveldb_iter_valid(Handle);
}

public byte[] Value()
{
IntPtr value = Native.leveldb_iter_value(handle, out UIntPtr length);
var value = Native.leveldb_iter_value(Handle, out var length);
CheckError();
return value.ToByteArray(length);
}
Expand Down
Loading

0 comments on commit 74498e5

Please sign in to comment.