Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
30d553f
neo as dotnet standard
shargon Jan 10, 2024
3cbd2ae
More changes
shargon Jan 10, 2024
f4ab9ac
Clean usings
shargon Jan 10, 2024
9be998a
More changes
shargon Jan 10, 2024
3343e7a
Awaits
shargon Jan 10, 2024
8be98a2
Fix ContractTask<T>
shargon Jan 10, 2024
eae2783
Fix UT
shargon Jan 10, 2024
09245cd
Remove extra method
shargon Jan 10, 2024
ef761da
Clean changes
shargon Jan 10, 2024
798b716
More clean changes
shargon Jan 10, 2024
48a25e8
Use cschuchardt88 GetBitLength version
shargon Jan 10, 2024
7e5798b
Merge branch 'master' into dotnet-standard
shargon Jan 10, 2024
e7c66a7
Add comments
shargon Jan 10, 2024
d43ebdf
Merge branch 'dotnet-standard' of https://github.com/neo-project/neo …
shargon Jan 10, 2024
ed969ba
Merge branch 'master' into dotnet-standard
Jan 11, 2024
b2d927a
Remove compiler directive
shargon Jan 11, 2024
e75bf25
Remove duplicate code
shargon Jan 11, 2024
575dc98
Add UT
shargon Jan 11, 2024
650203f
NotImplemented
shargon Jan 11, 2024
11f830b
Merge branch 'master' into dotnet-standard
shargon Jan 11, 2024
33b3647
Copy Sqrt exception
shargon Jan 11, 2024
9750ddb
var clean
shargon Jan 11, 2024
c18dea6
Rename
shargon Jan 11, 2024
ce6aedf
Try algorithm
shargon Jan 11, 2024
a3cf222
@cschuchardt88 solution
shargon Jan 11, 2024
c2e8e57
@cschuchardt88 Solution
shargon Jan 11, 2024
2ca7628
Merge branch 'master' into dotnet-standard
shargon Jan 11, 2024
a390944
Merge branch 'master' into dotnet-standard
shargon Jan 19, 2024
85e5f6b
Merge branch 'master' into dotnet-standard
Jan 24, 2024
a2665fe
Merge branch 'master' into dotnet-standard
shargon Feb 2, 2024
7c67352
Merge branch 'master' into dotnet-standard
Feb 4, 2024
7da0534
Max Size
shargon Feb 5, 2024
95835d3
Fix limits
shargon Feb 5, 2024
a17e078
clean code
shargon Feb 5, 2024
85f62af
Check match
shargon Feb 6, 2024
eb97a1e
Possible solution (ut not fixed)
shargon Feb 6, 2024
28ebae7
Limit to 256 bits
shargon Feb 6, 2024
dda715d
Compiler directive
shargon Feb 6, 2024
0e0e2be
Add comment
shargon Feb 6, 2024
aa727a8
Fix comment
shargon Feb 6, 2024
eab2466
Fix ut
shargon Feb 6, 2024
858e428
Merge branch 'master' into dotnet-standard
Feb 7, 2024
ae6075a
Revert change
shargon Feb 7, 2024
74c2c08
Merge branch 'dotnet-standard' of https://github.com/neo-project/neo …
shargon Feb 7, 2024
354e0c3
Revert indent
shargon Feb 7, 2024
1fd95d2
add -1
shargon Feb 7, 2024
690bd5e
fix
shargon Feb 7, 2024
9db4d39
Revert to same logic
shargon Feb 7, 2024
95f2ec6
Jimmy's feedback
shargon Feb 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Neo/BigDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BigDecimal(BigInteger value, byte decimals)
public unsafe BigDecimal(decimal value)
{
Span<int> span = stackalloc int[4];
decimal.GetBits(value, span);
span = decimal.GetBits(value);
fixed (int* p = span)
{
ReadOnlySpan<byte> buffer = new(p, 16);
Expand All @@ -73,7 +73,7 @@ public unsafe BigDecimal(decimal value)
public unsafe BigDecimal(decimal value, byte decimals)
{
Span<int> span = stackalloc int[4];
decimal.GetBits(value, span);
span = decimal.GetBits(value);
fixed (int* p = span)
{
ReadOnlySpan<byte> buffer = new(p, 16);
Expand Down
27 changes: 26 additions & 1 deletion src/Neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Buffers.Binary;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static Neo.Helper;
Expand Down Expand Up @@ -99,7 +100,7 @@ public static byte[] Murmur128(this byte[] value, uint seed)
/// <returns>The computed hash code.</returns>
public static byte[] Murmur128(this ReadOnlySpan<byte> value, uint seed)
{
byte[] buffer = GC.AllocateUninitializedArray<byte>(16);
byte[] buffer = new byte[16];
using Murmur128 murmur = new(seed);
murmur.TryComputeHash(value, buffer, out _);
return buffer;
Expand Down Expand Up @@ -239,5 +240,29 @@ internal static bool Test(this BloomFilter filter, Transaction tx)
return true;
return false;
}

/// <summary>
/// Rotates the specified value left by the specified number of bits.
/// Similar in behavior to the x86 instruction ROL.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..31] is treated as congruent mod 32.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint RotateLeft(uint value, int offset)
=> (value << offset) | (value >> (32 - offset));

/// <summary>
/// Rotates the specified value left by the specified number of bits.
/// Similar in behavior to the x86 instruction ROL.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..63] is treated as congruent mod 64.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong RotateLeft(ulong value, int offset)
=> (value << offset) | (value >> (64 - offset));
}
}
14 changes: 7 additions & 7 deletions src/Neo/Cryptography/Murmur128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ protected override void HashCore(ReadOnlySpan<byte> source)
{
ulong k1 = BinaryPrimitives.ReadUInt64LittleEndian(source[i..]);
k1 *= c1;
k1 = BitOperations.RotateLeft(k1, r1);
k1 = Helper.RotateLeft(k1, r1);
k1 *= c2;
H1 ^= k1;
H1 = BitOperations.RotateLeft(H1, 27);
H1 = Helper.RotateLeft(H1, 27);
H1 += H2;
H1 = H1 * m + n1;

ulong k2 = BinaryPrimitives.ReadUInt64LittleEndian(source[(i + 8)..]);
k2 *= c2;
k2 = BitOperations.RotateLeft(k2, r2);
k2 = Helper.RotateLeft(k2, r2);
k2 *= c1;
H2 ^= k2;
H2 = BitOperations.RotateLeft(H2, 31);
H2 = Helper.RotateLeft(H2, 31);
H2 += H1;
H2 = H2 * m + n2;
}
Expand All @@ -102,14 +102,14 @@ protected override void HashCore(ReadOnlySpan<byte> source)
case 1: remainingBytesL ^= (ulong)source[alignedLength] << 0; break;
}

H2 ^= BitOperations.RotateLeft(remainingBytesH * c2, r2) * c1;
H1 ^= BitOperations.RotateLeft(remainingBytesL * c1, r1) * c2;
H2 ^= Helper.RotateLeft(remainingBytesH * c2, r2) * c1;
H1 ^= Helper.RotateLeft(remainingBytesL * c1, r1) * c2;
}
}

protected override byte[] HashFinal()
{
byte[] buffer = GC.AllocateUninitializedArray<byte>(sizeof(ulong) * 2);
byte[] buffer = new byte[sizeof(ulong) * 2];
TryHashFinal(buffer, out _);
return buffer;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Neo/Cryptography/Murmur32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ protected override void HashCore(ReadOnlySpan<byte> source)
{
uint k = BinaryPrimitives.ReadUInt32LittleEndian(source);
k *= c1;
k = BitOperations.RotateLeft(k, r1);
k = Helper.RotateLeft(k, r1);
k *= c2;
hash ^= k;
hash = BitOperations.RotateLeft(hash, r2);
hash = Helper.RotateLeft(hash, r2);
hash = hash * m + n;
}
if (source.Length > 0)
Expand All @@ -72,15 +72,15 @@ protected override void HashCore(ReadOnlySpan<byte> source)
case 1: remainingBytes ^= source[0]; break;
}
remainingBytes *= c1;
remainingBytes = BitOperations.RotateLeft(remainingBytes, r1);
remainingBytes = Helper.RotateLeft(remainingBytes, r1);
remainingBytes *= c2;
hash ^= remainingBytes;
}
}

protected override byte[] HashFinal()
{
byte[] buffer = GC.AllocateUninitializedArray<byte>(sizeof(uint));
byte[] buffer = new byte[sizeof(uint)];
TryHashFinal(buffer, out _);
return buffer;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Neo/Extensions/BigIntegerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// BigIntegerExtensions.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
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#if !NET5_0_OR_GREATER
using System.Runtime.CompilerServices;

namespace System.Numerics
{
public static class BigIntegerExtensions
{
public static int GetBitLength(this BigInteger i)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a copy from

static int GetBitLength(this BigInteger i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its wrong. Doesn't calculate right, like really big numbers or leading zeros. That's for calculating Int32.

This is how you do it,

public static long GetBitLength(BigInteger num)
{
    var bytes = num.ToByteArray();
    var size = bytes.Length;
    if (size == 0) return 0;
    int v = bytes[size - 1]; // 8-bit value to find the log2 of 
    if (v == 0) return (size - 1) * 8;
    int r; // result of log2(v) will go here
    int shift;
    r = (v > 0xF) ? 4 : 0; v >>= r;
    shift = (v > 0x3) ? 2 : 0; v >>= shift; r |= shift;
    r |= (v >> 1);
    return (size - 1) * 8 + r + 1;
}

{
byte[] b = i.ToByteArray();
return (b.Length - 1) * 8 + BitLen(i.Sign > 0 ? b[b.Length - 1] : 255 - b[b.Length - 1]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int BitLen(int w)
{
return (w < 1 << 15 ? (w < 1 << 7
? (w < 1 << 3 ? (w < 1 << 1
? (w < 1 << 0 ? (w < 0 ? 32 : 0) : 1)
: (w < 1 << 2 ? 2 : 3)) : (w < 1 << 5
? (w < 1 << 4 ? 4 : 5)
: (w < 1 << 6 ? 6 : 7)))
: (w < 1 << 11
? (w < 1 << 9 ? (w < 1 << 8 ? 8 : 9) : (w < 1 << 10 ? 10 : 11))
: (w < 1 << 13 ? (w < 1 << 12 ? 12 : 13) : (w < 1 << 14 ? 14 : 15)))) : (w < 1 << 23 ? (w < 1 << 19
? (w < 1 << 17 ? (w < 1 << 16 ? 16 : 17) : (w < 1 << 18 ? 18 : 19))
: (w < 1 << 21 ? (w < 1 << 20 ? 20 : 21) : (w < 1 << 22 ? 22 : 23))) : (w < 1 << 27
? (w < 1 << 25 ? (w < 1 << 24 ? 24 : 25) : (w < 1 << 26 ? 26 : 27))
: (w < 1 << 29 ? (w < 1 << 28 ? 28 : 29) : (w < 1 << 30 ? 30 : 31)))));
}
}
}

#endif
4 changes: 2 additions & 2 deletions src/Neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static ISerializable AsSerializable(this ReadOnlyMemory<byte> value, Type
public static ReadOnlyMemory<byte> CompressLz4(this ReadOnlySpan<byte> data)
{
int maxLength = LZ4Codec.MaximumOutputSize(data.Length);
byte[] buffer = GC.AllocateUninitializedArray<byte>(sizeof(uint) + maxLength);
byte[] buffer = new byte[sizeof(uint) + maxLength];
BinaryPrimitives.WriteInt32LittleEndian(buffer, data.Length);
int length = LZ4Codec.Encode(data, buffer.AsSpan(sizeof(uint)));
return buffer.AsMemory(0, sizeof(uint) + length);
Expand All @@ -118,7 +118,7 @@ public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
{
int length = BinaryPrimitives.ReadInt32LittleEndian(data);
if (length < 0 || length > maxOutput) throw new FormatException();
byte[] result = GC.AllocateUninitializedArray<byte>(length);
byte[] result = new byte[length];
if (LZ4Codec.Decode(data[4..], result) != length)
throw new FormatException();
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Neo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net7.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageTags>NEO;AntShares;Blockchain;Smart Contract</PackageTags>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Neo/Network/UPnP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ private static XmlDocument SOAPRequest(string url, string soap, string function)
request.Headers.Add("Content-Type", "text/xml; charset=\"utf-8\"");
request.Content = new StringContent(req);
using HttpClient http = new();
using HttpResponseMessage response = http.Send(request);
using Stream stream = response.EnsureSuccessStatusCode().Content.ReadAsStream();
using HttpResponseMessage response = http.SendAsync(request).GetAwaiter().GetResult();
using Stream stream = response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync().GetAwaiter().GetResult();
Comment on lines +186 to +187
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make async.

using HttpResponseMessage response = await http.SendAsync(request);
using Stream stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we should change it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of changing everywhere. You add sync in the P2P protocol function. But doesn't really matter, just looks better.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that then we need to set the method as async, and when we call it, do the same. I think that we have async code that it should be sync, like the smart contract execution

XmlDocument resp = new() { XmlResolver = null };
resp.Load(stream);
return resp;
Expand Down
23 changes: 19 additions & 4 deletions src/Neo/SmartContract/ContractTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,27 @@ public ContractTask()
}

[AsyncMethodBuilder(typeof(ContractTaskMethodBuilder<>))]
class ContractTask<T> : ContractTask
class ContractTask<T>
{
protected override ContractTaskAwaiter<T> CreateAwaiter() => new();
private readonly ContractTaskAwaiter<T> awaiter;

public override ContractTaskAwaiter<T> GetAwaiter() => (ContractTaskAwaiter<T>)base.GetAwaiter();
public static ContractTask CompletedTask { get; }

static ContractTask()
{
CompletedTask = new ContractTask();
CompletedTask.GetAwaiter().SetResult();
}

public override object GetResult() => GetAwaiter().GetResult();
public ContractTask()
{
awaiter = CreateAwaiter();
}

protected virtual ContractTaskAwaiter<T> CreateAwaiter() => new();

public virtual ContractTaskAwaiter<T> GetAwaiter() => awaiter;

public virtual object GetResult() => null;
}
}
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Manifest/ContractMethodDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override StackItem ToStackItem(ReferenceCounter referenceCounter)
};
if (string.IsNullOrEmpty(descriptor.Name)) throw new FormatException();
_ = descriptor.Parameters.ToDictionary(p => p.Name);
if (!Enum.IsDefined(descriptor.ReturnType)) throw new FormatException();
if (!Enum.IsDefined(typeof(ContractParameterType), descriptor.ReturnType)) throw new FormatException();
if (descriptor.Offset < 0) throw new FormatException();
return descriptor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static ContractParameterDefinition FromJson(JObject json)
};
if (string.IsNullOrEmpty(parameter.Name))
throw new FormatException();
if (!Enum.IsDefined(parameter.Type) || parameter.Type == ContractParameterType.Void)
if (!Enum.IsDefined(typeof(ContractParameterType), parameter.Type) || parameter.Type == ContractParameterType.Void)
throw new FormatException();
return parameter;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/StorageKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public byte[] ToArray()
{
if (cache is null)
{
cache = GC.AllocateUninitializedArray<byte>(sizeof(int) + Key.Length);
cache = new byte[sizeof(int) + Key.Length];
BinaryPrimitives.WriteInt32LittleEndian(cache, Id);
Key.CopyTo(cache.AsMemory(sizeof(int)));
}
Expand Down