Skip to content

Commit

Permalink
Merge branch 'master' into pectra_eips
Browse files Browse the repository at this point in the history
  • Loading branch information
smartprogrammer93 committed Sep 14, 2024
2 parents 01deb2a + d0f32a7 commit 7d106ea
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 151 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build-solutions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ jobs:
submodules: ${{ matrix.solution == 'EthereumTests' }}
- name: Set up .NET
uses: actions/setup-dotnet@v4
- name: Cache dotnet packages
id: cache-dotnet
uses: actions/cache@v4
with:
# nuget cache files are stored in `~/.nuget/packages/` on Linux/macOS
path: ~/.nuget/packages/
key: ${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Packages.props') }}
restore-keys: |
${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Packages.props') }}
- name: Build ${{ matrix.solution }}.sln
run: dotnet build src/Nethermind/${{ matrix.solution }}.sln -c ${{ matrix.config }}
6 changes: 6 additions & 0 deletions .github/workflows/nethermind-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ jobs:
uses: actions/checkout@v4
- name: Set up .NET
uses: actions/setup-dotnet@v4
- name: Cache dotnet packages
id: cache-dotnet
uses: actions/cache/restore@v4
with:
path: ~/.nuget/packages/
key: ${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Packages.props') }}
- name: ${{ matrix.project }}
id: test
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Nethermind documentation is available at [docs.nethermind.io](https://docs.nethe

### Supported networks

**`Mainnet`** **`Goerli`** **`Sepolia`** **`Holesky`** **`Gnosis (xDai)`** **`Chiado`** **`Energy Web`** **`Volta`**
**`Mainnet`** **`Goerli`** **`Sepolia`** **`Holesky`** **`Gnosis (xDai)`** **`Chiado`** **`OP Mainnet`** **`OP Sepolia`** **`Base Mainnet`** **`Base Sepolia`** **`Energy Web`** **`Volta`**

## Download and run

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Copyright>Demerzel Solutions Limited</Copyright>
<Product>Nethermind</Product>
<SourceRevisionId Condition="'$(Commit)' != ''">$(Commit)</SourceRevisionId>
<VersionPrefix>1.28.0</VersionPrefix>
<VersionPrefix>1.29.0</VersionPrefix>
<VersionSuffix>unstable</VersionSuffix>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public long MigratedBlockNumber
set
{
_migratedBlockNumber = value;
_defaultColumn.Set(MigrationBlockNumberKey, MigratedBlockNumber.ToBigEndianByteArrayWithoutLeadingZeros());
_defaultColumn.PutSpan(MigrationBlockNumberKey.Bytes, value.ToBigEndianSpanWithoutLeadingZeros(out _));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void SetValidators(long finalizingBlockNumber, Address[] validators)
var validatorInfo = new ValidatorInfo(finalizingBlockNumber, _latestFinalizedValidatorsBlockNumber, validators);
var rlp = Rlp.Encode(validatorInfo);
_db.Set(GetKey(finalizingBlockNumber), rlp.Bytes);
_db.Set(LatestFinalizedValidatorsBlockNumberKey, finalizingBlockNumber.ToBigEndianByteArrayWithoutLeadingZeros());
_db.PutSpan(LatestFinalizedValidatorsBlockNumberKey.Bytes, finalizingBlockNumber.ToBigEndianSpanWithoutLeadingZeros(out _));
_latestFinalizedValidatorsBlockNumber = finalizingBlockNumber;
_latestValidatorInfo = validatorInfo;
Metrics.ValidatorsCount = validators.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private Task RunProcessing()

private void RunProcessingLoop()
{
const int BlocksBacklogTriggeringManualGC = 20;
const int BlocksBacklogTriggeringManualGC = 4;
const int MaxBlocksWithoutGC = 100;

if (_logger.IsDebug) _logger.Debug($"Starting block processor - {_blockQueue.Count} blocks waiting in the queue.");
Expand Down
11 changes: 10 additions & 1 deletion src/Nethermind/Nethermind.Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Text.Json.Serialization;

using Nethermind.Core.Crypto;
Expand Down Expand Up @@ -153,7 +154,15 @@ public bool Equals(Address? other)
return true;
}

return Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);
// Address must be 20 bytes long Vector128 + uint
ref byte bytes0 = ref MemoryMarshal.GetArrayDataReference(Bytes);
ref byte bytes1 = ref MemoryMarshal.GetArrayDataReference(other.Bytes);
// Compare first 16 bytes with Vector128 and last 4 bytes with uint
return
Unsafe.As<byte, Vector128<byte>>(ref bytes0) ==
Unsafe.As<byte, Vector128<byte>>(ref bytes1) &&
Unsafe.As<byte, uint>(ref Unsafe.Add(ref bytes0, Vector128<byte>.Count)) ==
Unsafe.As<byte, uint>(ref Unsafe.Add(ref bytes1, Vector128<byte>.Count));
}

public static Address FromNumber(in UInt256 number)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Buffers;

namespace Nethermind.Core.Buffers;

public readonly struct ArrayPoolDisposableReturn : IDisposable
{
private readonly byte[] _array;

private ArrayPoolDisposableReturn(byte[] array) => _array = array;

public static ArrayPoolDisposableReturn Rent(int size, out byte[] array)
{
array = ArrayPool<byte>.Shared.Rent(size);
return new(array);
}

public void Dispose() => ArrayPool<byte>.Shared.Return(_array);
}
115 changes: 12 additions & 103 deletions src/Nethermind/Nethermind.Core/Extensions/Int64Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,29 @@

using System;
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using Nethermind.Int256;

namespace Nethermind.Core.Extensions;

public static class Int64Extensions
{
public static byte[] ToBigEndianByteArrayWithoutLeadingZeros(this long value)
public static ReadOnlySpan<byte> ToBigEndianSpanWithoutLeadingZeros(this long value, out long buffer)
{
byte byte6 = (byte)(value >> 8);
byte byte5 = (byte)(value >> 16);
byte byte4 = (byte)(value >> 24);
byte byte3 = (byte)(value >> 32);
byte byte2 = (byte)(value >> 40);
byte byte1 = (byte)(value >> 48);
byte byte0 = (byte)(value >> 56);

if (byte0 == 0)
{
if (byte1 == 0)
{
if (byte2 == 0)
{
if (byte3 == 0)
{
if (byte4 == 0)
{
if (byte5 == 0)
{
if (byte6 == 0)
{
byte[] bytes = new byte[1];
bytes[0] = (byte)value;
return bytes;
}
else
{
byte[] bytes = new byte[2];
bytes[1] = (byte)value;
bytes[0] = byte6;
return bytes;
}
}
else
{
byte[] bytes = new byte[3];
bytes[2] = (byte)value;
bytes[1] = byte6;
bytes[0] = byte5;
return bytes;
}
}
else
{
byte[] bytes = new byte[4];
bytes[3] = (byte)value;
bytes[2] = byte6;
bytes[1] = byte5;
bytes[0] = byte4;
return bytes;
}
}
else
{
byte[] bytes = new byte[5];
bytes[4] = (byte)value;
bytes[3] = byte6;
bytes[2] = byte5;
bytes[1] = byte4;
bytes[0] = byte3;
return bytes;
}
}
else
{
byte[] bytes = new byte[6];
bytes[5] = (byte)value;
bytes[4] = byte6;
bytes[3] = byte5;
bytes[2] = byte4;
bytes[1] = byte3;
bytes[0] = byte2;
return bytes;
}
}
else
{
byte[] bytes = new byte[7];
bytes[6] = (byte)value;
bytes[5] = byte6;
bytes[4] = byte5;
bytes[3] = byte4;
bytes[2] = byte3;
bytes[1] = byte2;
bytes[0] = byte1;
return bytes;
}
}
else
{
byte[] bytes = new byte[8];
bytes[7] = (byte)value;
bytes[6] = byte6;
bytes[5] = byte5;
bytes[4] = byte4;
bytes[3] = byte3;
bytes[2] = byte2;
bytes[1] = byte1;
bytes[0] = byte0;
return bytes;
}
// Min 7 bytes as we still want a byte if the value is 0.
var start = Math.Min(BitOperations.LeadingZeroCount((ulong)value) / sizeof(long), sizeof(long) - 1);
// We create the span over the out value to ensure the span stack space remains valid.
buffer = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;
ReadOnlySpan<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref buffer, 1));
return span[start..];
}

public static byte[] ToBigEndianByteArrayWithoutLeadingZeros(this long value)
=> value.ToBigEndianSpanWithoutLeadingZeros(out _).ToArray();

public static byte[] ToBigEndianByteArray(this long value)
{
byte[] bytes = BitConverter.GetBytes(value);
Expand Down
2 changes: 0 additions & 2 deletions src/Nethermind/Nethermind.Core/IKeyValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Buffers;
using Nethermind.Core.Buffers;
using Nethermind.Core.Extensions;

namespace Nethermind.Core
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Core/KeyValueStoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ public static bool KeyExists(this IReadOnlyKeyValueStore db, Hash256 key)

public static bool KeyExists(this IReadOnlyKeyValueStore db, long key)
{
return db.KeyExists(key.ToBigEndianByteArrayWithoutLeadingZeros());
return db.KeyExists(key.ToBigEndianSpanWithoutLeadingZeros(out _));
}

public static byte[]? Get(this IReadOnlyKeyValueStore db, long key) => db[key.ToBigEndianByteArrayWithoutLeadingZeros()];
public static byte[]? Get(this IReadOnlyKeyValueStore db, long key) => db[key.ToBigEndianSpanWithoutLeadingZeros(out _)];

/// <summary>
///
/// </summary>
/// <param name="db"></param>
/// <param name="key"></param>
/// <returns>Can return null or empty Span on missing key</returns>
public static Span<byte> GetSpan(this IReadOnlyKeyValueStore db, long key) => db.GetSpan(key.ToBigEndianByteArrayWithoutLeadingZeros());
public static Span<byte> GetSpan(this IReadOnlyKeyValueStore db, long key) => db.GetSpan(key.ToBigEndianSpanWithoutLeadingZeros(out _));

public static MemoryManager<byte>? GetOwnedMemory(this IReadOnlyKeyValueStore db, ReadOnlySpan<byte> key)
{
Expand Down Expand Up @@ -140,7 +140,7 @@ public static void Delete(this IWriteOnlyKeyValueStore db, Hash256 key)

public static void Delete(this IWriteOnlyKeyValueStore db, long key)
{
db.Remove(key.ToBigEndianByteArrayWithoutLeadingZeros());
db.Remove(key.ToBigEndianSpanWithoutLeadingZeros(out _));
}

[SkipLocalsInit]
Expand All @@ -153,7 +153,7 @@ public static void Delete(this IWriteOnlyKeyValueStore db, long blockNumber, Has

public static void Set(this IWriteOnlyKeyValueStore db, long key, byte[] value)
{
db[key.ToBigEndianByteArrayWithoutLeadingZeros()] = value;
db[key.ToBigEndianSpanWithoutLeadingZeros(out _)] = value;
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Db.Rocks/DbOnTheRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using ConcurrentCollections;
using Nethermind.Config;
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Db/Blooms/BloomStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void Migrate(IEnumerable<BlockHeader> headers)

private void Set(Hash256 key, long value)
{
_bloomInfoDb.Set(key, value.ToBigEndianByteArrayWithoutLeadingZeros());
_bloomInfoDb.PutSpan(key.Bytes, value.ToBigEndianSpanWithoutLeadingZeros(out _));
}

public void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Db/SimpleFilePublicKeyDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Win32.SafeHandles;

using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.Core.Collections;
using Nethermind.Core.Extensions;
using Nethermind.Logging;
Expand Down Expand Up @@ -208,7 +209,7 @@ private void LoadData()

using SafeFileHandle fileHandle = File.OpenHandle(DbPath, FileMode.OpenOrCreate);

byte[] rentedBuffer = ArrayPool<byte>.Shared.Rent(maxLineLength);
using var handle = ArrayPoolDisposableReturn.Rent(maxLineLength, out byte[] rentedBuffer);
int read = RandomAccess.Read(fileHandle, rentedBuffer, 0);

long offset = 0L;
Expand Down Expand Up @@ -270,7 +271,6 @@ private void LoadData()
read = RandomAccess.Read(fileHandle, rentedBuffer.AsSpan(bytes.Length), offset);
}

ArrayPool<byte>.Shared.Return(rentedBuffer);
if (bytes.Length > 0)
{
if (_logger.IsWarn) _logger.Warn($"Malformed {Name}. Ignoring...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
using Nethermind.State;
Expand All @@ -30,21 +31,15 @@ public class Db

public ITypedArray<byte> getState(object address, object hash)
{
byte[] array = ArrayPool<byte>.Shared.Rent(32);
try
{
ReadOnlySpan<byte> bytes = WorldState.Get(new StorageCell(address.ToAddress(), hash.GetHash()));
if (bytes.Length < array.Length)
{
Array.Clear(array);
}
bytes.CopyTo(array.AsSpan(array.Length - bytes.Length));
return array.ToTypedScriptArray();
}
finally
using var handle = ArrayPoolDisposableReturn.Rent(32, out byte[] array);

ReadOnlySpan<byte> bytes = WorldState.Get(new StorageCell(address.ToAddress(), hash.GetHash()));
if (bytes.Length < array.Length)
{
ArrayPool<byte>.Shared.Return(array);
Array.Clear(array);
}
bytes.CopyTo(array.AsSpan(array.Length - bytes.Length));
return array.ToTypedScriptArray();
}

public bool exists(object address) => WorldState.TryGetAccount(address.ToAddress(), out AccountStruct account) && !account.IsTotallyEmpty;
Expand Down
Loading

0 comments on commit 7d106ea

Please sign in to comment.