Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/Neo.Configuration/BlockchainBackupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES

using Neo.Configuration.Json;
using System;
using System.IO;

namespace Neo.Configuration
{
public class BlockchainBackupOptions
public class BlockchainBackupOptions : JsonModel
{
public string BackupPath { get; init; } = Path.Combine(AppContext.BaseDirectory, "data", "backups");

Expand Down
6 changes: 3 additions & 3 deletions src/Neo.Core/Blockchain/InventoryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public enum InventoryType : byte
/// <summary>
/// Indicates that the inventory is a transaction.
/// </summary>
TX = MessageCommandType.Transaction,
TX = ProtocolMessageCommand.Transaction,

/// <summary>
/// Indicates that the inventory is a block.
/// </summary>
Block = MessageCommandType.Block,
Block = ProtocolMessageCommand.Block,

/// <summary>
/// Indicates that the inventory is an extensible payload.
/// </summary>
Extensible = MessageCommandType.Extensible
Extensible = ProtocolMessageCommand.Extensible
}
}
11 changes: 11 additions & 0 deletions src/Neo.Core/CoreUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ public static long ToUnixTimeMilliseconds(TimeSpan timeSpan, TimeSpan offset = d
new DateTimeOffset(timeSpan.Ticks, offset)
.ToUnixTimeMilliseconds();

public static uint ToUnixTimeSeconds(DateTime dateTime, TimeSpan offset = default) =>
(uint)new DateTimeOffset(dateTime, offset)
.ToUnixTimeSeconds();

public static uint ToUnixTimeSeconds(TimeSpan timeSpan, TimeSpan offset = default) =>
(uint)new DateTimeOffset(timeSpan.Ticks, offset)
.ToUnixTimeSeconds();

public static DateTime FromUnixTimeMilliseconds(ulong milliseconds, bool isLocalDateTime = false) =>
FromUnixTimeMilliseconds((long)milliseconds, isLocalDateTime);

public static DateTime FromUnixTimeSeconds(uint seconds, bool isLocalDateTime = false) =>
FromUnixTimeMilliseconds(seconds * TimeSpan.MillisecondsPerSecond, isLocalDateTime);

public static DateTime FromUnixTimeMilliseconds(long milliseconds, bool isLocalDateTime = false)
{
var dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Core/Cryptography/ECC/ECCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ECCurve(BigInteger p, BigInteger b, ECCurveName curve)
};
}

internal byte[] CompressPoint(ReadOnlySpan<byte> span)
internal static byte[] CompressPoint(ReadOnlySpan<byte> span)
{
var prefix = span[^1] % 2 == 0 ?
(byte)0x02 : (byte)0x03;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Core/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void Deserialize(Stream reader)
public byte[] Encode(bool shouldCompress = true)
{
return shouldCompress ?
_curve.CompressPoint(_uncompressed.AsSpan()) :
ECCurve.CompressPoint(_uncompressed.AsSpan()) :
_uncompressed;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Neo.Core/Cryptography/ProtectedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public static string UnprotectData(ReadOnlySpan<byte> protectedData)

using var aes = new AesGcm(s_entropy, TagSize);

var nonceBytes = protectedData.Slice(0, NonceSize);
var tagBytes = protectedData.Slice(protectedData.Length - TagSize);
var nonceBytes = protectedData[..NonceSize];
var tagBytes = protectedData[^TagSize..];
var ciphertext = protectedData.Slice(NonceSize, protectedData.Length - NonceSize - TagSize);

Span<byte> plaintext = stackalloc byte[ciphertext.Length];
Expand Down
3 changes: 3 additions & 0 deletions src/Neo.Core/Cryptography/RipeMD160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ private RipeMD160()
public static byte[] HashData(byte[] data)
{
using var hasher = new RipeMD160();
// CA1850 wants HashData over ComputeHash, but this method *is* the static HashData entry point.
#pragma warning disable CA1850
return hasher.ComputeHash(data);
#pragma warning restore CA1850
}

public override void Initialize()
Expand Down
50 changes: 25 additions & 25 deletions src/Neo.Core/Cryptography/SCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public static byte[] Generate(byte[] password, byte[] salt, int n, int r, int p,
/// <summary>
/// Convenience overload for string password.
/// </summary>
public static byte[] Generate(string password, byte[] salt, int N = 16384, int r = 8, int p = 1, int keyLength = 32)
public static byte[] Generate(string password, byte[] salt, int n = 16384, int r = 8, int p = 1, int keyLength = 32)
{
var passBytes = CoreUtilities.StrictUtf8Encoding.GetBytes(password);
return Generate(passBytes, salt, N, r, p, keyLength);
return Generate(passBytes, salt, n, r, p, keyLength);
}

/// <summary>
Expand Down Expand Up @@ -77,12 +77,12 @@ public unsafe static byte[] CryptoScrypt(byte[] password, byte[] salt, int n, in
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * 128 * r) */
PBKDF2_SHA256(mac, password, salt, salt.Length, 1, ba, p * 128 * r);

fixed (byte* B = ba)
fixed (void* V = va)
fixed (void* XY = xYa)
fixed (byte* b = ba)
fixed (void* v = va)
fixed (void* xy = xYa)
{
for (var i = 0; i < p; i++)
SMix(&B[i * 128 * r], r, n, (uint*)V, (uint*)XY);
SMix(&b[i * 128 * r], r, n, (uint*)v, (uint*)xy);
}

/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
Expand All @@ -96,8 +96,8 @@ private static void PBKDF2_SHA256(HMACSHA256 mac, byte[] password, byte[] salt,
if (derivedKeyLength > (Math.Pow(2, 32) - 1) * 32)
throw new ArgumentException("Requested key length too long");

var U = new byte[32];
var T = new byte[32];
var u = new byte[32];
var t = new byte[32];
var saltBuffer = new byte[saltLength + 4];

var blockCount = (int)Math.Ceiling(derivedKeyLength / 32d);
Expand All @@ -114,20 +114,20 @@ private static void PBKDF2_SHA256(HMACSHA256 mac, byte[] password, byte[] salt,

mac.Initialize();
mac.TransformFinalBlock(saltBuffer, 0, saltBuffer.Length);
Buffer.BlockCopy(mac.Hash!, 0, U, 0, U.Length);
Buffer.BlockCopy(mac.Hash!, 0, u, 0, u.Length);

Buffer.BlockCopy(U, 0, T, 0, 32);
Buffer.BlockCopy(u, 0, t, 0, 32);

for (long j = 1; j < iterationCount; j++)
{
mac.TransformFinalBlock(U, 0, U.Length);
Buffer.BlockCopy(mac.Hash!, 0, U, 0, U.Length);
mac.TransformFinalBlock(u, 0, u.Length);
Buffer.BlockCopy(mac.Hash!, 0, u, 0, u.Length);

for (var k = 0; k < 32; k++)
T[k] ^= U[k];
t[k] ^= u[k];
}

Buffer.BlockCopy(T, 0, derivedKey, (i - 1) * 32, (i == blockCount ? r : 32));
Buffer.BlockCopy(t, 0, derivedKey, (i - 1) * 32, (i == blockCount ? r : 32));
}
}

Expand Down Expand Up @@ -164,12 +164,12 @@ private unsafe static uint Decode32(byte* p)
return p[0] | ((uint)p[1] << 8) | ((uint)p[2] << 16) | ((uint)p[3] << 24);
}

private unsafe static void Salsa208(uint* B)
private unsafe static void Salsa208(uint* b)
{
uint x0 = B[0], x1 = B[1], x2 = B[2], x3 = B[3],
x4 = B[4], x5 = B[5], x6 = B[6], x7 = B[7],
x8 = B[8], x9 = B[9], x10 = B[10], x11 = B[11],
x12 = B[12], x13 = B[13], x14 = B[14], x15 = B[15];
uint x0 = b[0], x1 = b[1], x2 = b[2], x3 = b[3],
x4 = b[4], x5 = b[5], x6 = b[6], x7 = b[7],
x8 = b[8], x9 = b[9], x10 = b[10], x11 = b[11],
x12 = b[12], x13 = b[13], x14 = b[14], x15 = b[15];

for (var i = 0; i < 8; i += 2)
{
Expand All @@ -184,10 +184,10 @@ private unsafe static void Salsa208(uint* B)
x12 ^= R(x15 + x14, 7); x13 ^= R(x12 + x15, 9); x14 ^= R(x13 + x12, 13); x15 ^= R(x14 + x13, 18);
}

B[0] += x0; B[1] += x1; B[2] += x2; B[3] += x3;
B[4] += x4; B[5] += x5; B[6] += x6; B[7] += x7;
B[8] += x8; B[9] += x9; B[10] += x10; B[11] += x11;
B[12] += x12; B[13] += x13; B[14] += x14; B[15] += x15;
b[0] += x0; b[1] += x1; b[2] += x2; b[3] += x3;
b[4] += x4; b[5] += x5; b[6] += x6; b[7] += x7;
b[8] += x8; b[9] += x9; b[10] += x10; b[11] += x11;
b[12] += x12; b[13] += x13; b[14] += x14; b[15] += x15;
}

private static uint R(uint a, int b) =>
Expand All @@ -209,9 +209,9 @@ private unsafe static void BlockMix(uint* bin, uint* bOut, uint* x, int r)
}
}

private unsafe static long Integerify(uint* B, int r)
private unsafe static long Integerify(uint* b, int r)
{
var x = (uint*)(((byte*)B) + (2 * r - 1) * 64);
var x = (uint*)(((byte*)b) + (2 * r - 1) * 64);
return (((long)x[1] << 32) + x[0]);
}

Expand Down
1 change: 0 additions & 1 deletion src/Neo.Core/Extensions/ByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,5 @@ public static int ToHashCode(this byte[] data, int seed = 397) =>
(hash, b) =>
unchecked((hash * 31) ^ b)
);

}
}
2 changes: 1 addition & 1 deletion src/Neo.Core/Extensions/INeoSerializableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public static byte[] ToArray(this INeoSerializable source)

public static int GetSerializedSize(this INeoSerializable[] source) =>
source.Length.GetCompactSize() +
(source.Length * source.Sum(s => s.Size));
source.Sum(static s => s.Size);
}
}
6 changes: 6 additions & 0 deletions src/Neo.Core/Extensions/MemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public static TAccumulate Aggregate<TSource, TAccumulate>(this ReadOnlyMemory<TS
return val;
}

public static int ToHashCode(this Memory<byte> data, int seed) =>
data.Aggregate(seed,
(hash, b) =>
unchecked((hash * 31) ^ b)
);

public static int ToHashCode(this Memory<byte> data) =>
data.Aggregate(data.Length,
(hash, b) =>
Expand Down
32 changes: 26 additions & 6 deletions src/Neo.Core/Extensions/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES

using K4os.Compression.LZ4;
using Neo.Core.Serialization;
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

namespace Neo.Core.Extensions
Expand Down Expand Up @@ -78,16 +80,34 @@ public static TAccumulate Aggregate<TSource, TAccumulate>(this ReadOnlySpan<TSou
return val;
}

public static int ToHashCode(this Memory<byte> data, int seed = 397) =>
data.Aggregate(seed,
(hash, b) =>
unchecked((hash * 31) ^ b)
);

public static int ToHashCode(this ReadOnlySpan<byte> data, int seed = 397) =>
data.Aggregate(seed,
(hash, b) =>
unchecked((hash * 31) ^ b)
);

public static Span<byte> ToLz4Compress(this ReadOnlySpan<byte> data)
{
var maxLength = LZ4Codec.MaximumOutputSize(data.Length);
Span<byte> buffer = new byte[sizeof(uint) + maxLength];

BinaryPrimitives.WriteInt32LittleEndian(buffer, data.Length);

var length = LZ4Codec.Encode(data, buffer[sizeof(uint)..]);

return buffer[..(sizeof(uint) + length)];
}

public static Span<byte> ToLz4Decompress(this ReadOnlySpan<byte> data)
{
var length = BinaryPrimitives.ReadInt32LittleEndian(data);
Span<byte> result = new byte[length];
var decodedBytes = LZ4Codec.Decode(data[4..], result);

if (decodedBytes != length)
throw new FormatException($"Length \'{length}\' does not match the decompressed data length \'{decodedBytes}\'.");

return result;
}
}
}
3 changes: 2 additions & 1 deletion src/Neo.Core/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public static void WriteObjects<T>(this Stream stream, T[] values)
t.Serialize(stream);
}

public static T ReadCompact<T>(this Stream stream) where T : unmanaged, IBinaryInteger<T>
public static T ReadCompact<T>(this Stream stream)
where T : unmanaged, IBinaryInteger<T>
{
if (stream.CanRead == false)
throw new NotSupportedException("Stream does not support reading.");
Expand Down
6 changes: 6 additions & 0 deletions src/Neo.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ public static UInt160 ToScriptHash(this string address, byte version)

return new(data.AsSpan(1));
}

public static int GetSerializedSize(this string source)
{
var byteCount = CoreUtilities.StrictUtf8Encoding.GetByteCount(source);
return byteCount.GetCompactSize() + byteCount;
}
}
}
12 changes: 5 additions & 7 deletions src/Neo.Core/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ public static class TypeExtensions
{
public static object CreateInitializedObject(this Type source)
{
// Try to get the private parameterless constructor
// Try to get the private/public parameterless constructor.
var ctor = source.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
null,
Type.EmptyTypes, // No parameters
null);

if (ctor is null)
// Fallback: if no parameterless ctor exists at all
throw new InvalidOperationException($"Type {source.FullName} does not have a parameterless constructor (public or private).");
Type.EmptyTypes,
null)
?? throw new InvalidOperationException(
$"Type {source.FullName} does not have a parameterless constructor (public or private).");

return ctor.Invoke(null);
}
Expand Down
Loading
Loading