Skip to content

Commit

Permalink
Merge branch 'master' into feature/external-signer
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Feb 12, 2024
2 parents c535c1b + f262cb2 commit b15fe43
Show file tree
Hide file tree
Showing 56 changed files with 279 additions and 252 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to Nethermind

The Nethermind team maintains guidelines for contributing to the Nethermind repos. Check out our [docs page](https://docs.nethermind.io/nethermind/) for more info about us.
The Nethermind team maintains guidelines for contributing to the Nethermind repos. Check out our [docs page](https://docs.nethermind.io/) for more info about us.

### Code of Conduct

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ protected TestContractBlockchain()
ChainSpecLoader loader = new(new EthereumJsonSerializer());
string name = string.IsNullOrEmpty(testSuffix) ? $"{typeof(TTestClass).FullName}.json" : $"{typeof(TTestClass).FullName}.{testSuffix}.json";
using Stream? stream = typeof(TTestClass).Assembly.GetManifestResourceStream(name);
using StreamReader reader = new(stream ?? new MemoryStream());
ChainSpec chainSpec = loader.Load(reader.ReadToEnd());
ChainSpec chainSpec = loader.Load(stream);
ChainSpecBasedSpecProvider chainSpecBasedSpecProvider = new(chainSpec);
return (chainSpec, chainSpecBasedSpecProvider);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.IO;
Expand Down Expand Up @@ -71,9 +71,8 @@ private Block GetGenesisBlock(string chainspecPath)

private static ChainSpec LoadChainSpec(string path)
{
string data = File.ReadAllText(path);
ChainSpecLoader chainSpecLoader = new(new EthereumJsonSerializer());
ChainSpec chainSpec = chainSpecLoader.Load(data);
ChainSpec chainSpec = chainSpecLoader.LoadFromFile(path);
return chainSpec;
}
}
7 changes: 2 additions & 5 deletions src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BlockStore : IBlockStore

private readonly LruCache<ValueHash256, Block>
_blockCache = new(CacheSize, CacheSize, "blocks");
private long? _maxSize;
private readonly long? _maxSize;

public BlockStore(IDb blockDb, long? maxSize = null)
{
Expand Down Expand Up @@ -97,10 +97,7 @@ public void Delete(long blockNumber, Hash256 blockHash)
GetBlockNumPrefixedKey(blockNumber, blockHash, keyWithBlockNumber);

MemoryManager<byte>? memoryOwner = _blockDb.GetOwnedMemory(keyWithBlockNumber);
if (memoryOwner is null)
{
memoryOwner = _blockDb.GetOwnedMemory(blockHash.Bytes);
}
memoryOwner ??= _blockDb.GetOwnedMemory(blockHash.Bytes);

return BlockDecoder.DecodeToReceiptRecoveryBlock(memoryOwner, memoryOwner?.Memory ?? Memory<byte>.Empty, RlpBehaviors.None);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CopyTreeVisitor : ITreeVisitor, IDisposable
private readonly WriteFlags _writeFlags;
private readonly CancellationToken _cancellationToken;
private const int Million = 1_000_000;
private ConcurrentWriteBatcher _concurrentWriteBatcher;
private readonly ConcurrentWriteBatcher _concurrentWriteBatcher;

public CopyTreeVisitor(
IPruningContext pruningContext,
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Core/Bloom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public BloomStructRef(ReadOnlySpan<byte> bytes)

public ReadOnlySpan<byte> Bytes { get; }

public bool Matches(ReadOnlySpan<byte> sequence)
public readonly bool Matches(ReadOnlySpan<byte> sequence)
{
Bloom.BloomExtract indexes = GetExtract(sequence);
return Matches(in indexes);
Expand Down Expand Up @@ -281,7 +281,7 @@ public override readonly int GetHashCode()
return Core.Extensions.Bytes.GetSimplifiedHashCode(Bytes);
}

public bool Matches(LogEntry logEntry)
public readonly bool Matches(LogEntry logEntry)
{
if (Matches(logEntry.LoggersAddress))
{
Expand All @@ -306,9 +306,9 @@ private readonly bool Get(int index)
return Bytes[bytePosition].GetBit(shift);
}

public bool Matches(Address address) => Matches(address.Bytes);
public readonly bool Matches(Address address) => Matches(address.Bytes);

public bool Matches(Hash256 topic) => Matches(topic.Bytes);
public readonly bool Matches(Hash256 topic) => Matches(topic.Bytes);

public readonly bool Matches(in Bloom.BloomExtract extract) => Get(extract.Index1) && Get(extract.Index2) && Get(extract.Index3);

Expand Down
23 changes: 0 additions & 23 deletions src/Nethermind/Nethermind.Core/Collections/IListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,6 @@ public static int BinarySearch<TItem, TSearch>(this IList<TItem> list, TSearch v
return ~lower;
}

/// <summary>
/// Performs a binary search on the specified collection.
/// </summary>
/// <typeparam name="TItem">The type of the item.</typeparam>
/// <param name="list">The list to be searched.</param>
/// <param name="value">The value to search for.</param>
/// <returns></returns>
public static int BinarySearch<TItem>(this IList<TItem> list, TItem value) => BinarySearch(list, value, Comparer<TItem>.Default);

/// <summary>
/// Performs a binary search on the specified collection.
/// </summary>
/// <typeparam name="TItem">The type of the item.</typeparam>
/// <param name="list">The list to be searched.</param>
/// <param name="value">The value to search for.</param>
/// <param name="comparer">The comparer that is used to compare the value
/// with the list items.</param>
/// <returns></returns>
public static int BinarySearch<TItem>(this IList<TItem> list, TItem value, IComparer<TItem> comparer) => list.BinarySearch(value, comparer.Compare);

public static bool TryGetSearchedItem<TComparable>(this IList<TComparable> list, in TComparable activation, out TComparable? item) where TComparable : IComparable<TComparable> =>
list.TryGetSearchedItem(activation, (b, c) => b.CompareTo(c), out item);

public static bool TryGetForBlock(this IList<long> list, in long blockNumber, out long item) =>
list.TryGetSearchedItem(blockNumber, (b, c) => b.CompareTo(c), out item);

Expand Down
37 changes: 34 additions & 3 deletions src/Nethermind/Nethermind.Core/Collections/StackList.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Nethermind.Core.Collections
{
public class StackList<T> : List<T>
public sealed class StackList<T> : List<T>
where T : struct, IComparable<T>
{
public T Peek() => this[^1];

public bool TryPeek(out T? item)
public bool TryPeek(out T item)
{
if (Count > 0)
{
Expand All @@ -30,7 +33,7 @@ public T Pop()
return value;
}

public bool TryPop(out T? item)
public bool TryPop(out T item)
{
if (Count > 0)
{
Expand All @@ -48,5 +51,33 @@ public void Push(T item)
{
Add(item);
}

public bool TryGetSearchedItem(T activation, out T item)
{
Span<T> span = CollectionsMarshal.AsSpan(this);
int index = span.BinarySearch(activation);
bool result;
if ((uint)index < (uint)span.Length)
{
item = span[index];
result = true;
}
else
{
index = ~index - 1;
if ((uint)index < (uint)span.Length)
{
item = span[index];
result = true;
}
else
{
item = default;
result = false;
}
}

return result;
}
}
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Crypto/Keccak512.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Keccak512(byte[] bytes)

public byte[]? Bytes { get; }

public override string ToString()
public override readonly string ToString()
{
return ToString(true);
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public readonly bool Equals(Keccak512 other)
return Core.Extensions.Bytes.AreEqual(other.Bytes, Bytes);
}

public override bool Equals(object obj)
public override readonly bool Equals(object obj)
{
return obj?.GetType() == typeof(Keccak512) && Equals((Keccak512)obj);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Nethermind/Nethermind.Db.Rocks/DbOnTheRocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -985,9 +985,7 @@ protected internal bool KeyExistsWithColumn(ReadOnlySpan<byte> key, ColumnFamily

try
{
// seems it has no performance impact
return _db.Get(key, cf, _defaultReadOptions) is not null;
// return _db.Get(key, 32, _keyExistsBuffer, 0, 0, null, null) != -1;
return _db.HasKey(key, cf, _defaultReadOptions);
}
catch (RocksDbSharpException e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Db/IDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Flush() { }
void Clear() { }
void Compact() { }

struct DbMetric
readonly struct DbMetric
{
public long Size { get; init; }
public long CacheSize { get; init; }
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm.Test/EvmPooledMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public void GetTrace_memory_should_not_overflow()
run(input);
}

private static PrivateKey PrivateKeyD = new("0000000000000000000000000000000000000000000000000000001000000000");
private static Address sender = new Address("0x59ede65f910076f60e07b2aeb189c72348525e72");
private static readonly PrivateKey PrivateKeyD = new("0000000000000000000000000000000000000000000000000000001000000000");
private static readonly Address sender = new Address("0x59ede65f910076f60e07b2aeb189c72348525e72");

private static Address to = new Address("0x000000000000000000000000636f6e7472616374");
private static Address coinbase = new Address("0x4444588443C3a91288c5002483449Aba1054192b");
private static readonly Address to = new Address("0x000000000000000000000000636f6e7472616374");
private static readonly Address coinbase = new Address("0x4444588443C3a91288c5002483449Aba1054192b");
private static readonly EthereumEcdsa ethereumEcdsa = new(BlockchainIds.Goerli, LimboLogs.Instance);
private static string run(byte[] input)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm/EvmState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public StackPool(int maxCallStackDepth = VirtualMachine.MaxCallDepth * 2)
_maxCallStackDepth = maxCallStackDepth;
}

private readonly ConcurrentStack<byte[]> _dataStackPool = new();
private readonly ConcurrentStack<int[]> _returnStackPool = new();
private readonly Stack<byte[]> _dataStackPool = new(32);
private readonly Stack<int[]> _returnStackPool = new(32);

private int _dataStackPoolDepth;
private int _returnStackPoolDepth;
Expand All @@ -55,7 +55,7 @@ private byte[] RentDataStack()
return result;
}

Interlocked.Increment(ref _dataStackPoolDepth);
_dataStackPoolDepth++;
if (_dataStackPoolDepth > _maxCallStackDepth)
{
EvmStack.ThrowEvmStackOverflowException();
Expand All @@ -71,7 +71,7 @@ private int[] RentReturnStack()
return result;
}

Interlocked.Increment(ref _returnStackPoolDepth);
_returnStackPoolDepth++;
if (_returnStackPoolDepth > _maxCallStackDepth)
{
EvmStack.ThrowEvmStackOverflowException();
Expand Down
47 changes: 29 additions & 18 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1649,24 +1649,8 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
}
case Instruction.SLOAD:
{
Metrics.SloadOpcode++;
gasAvailable -= spec.GetSLoadCost();

if (!stack.PopUInt256(out result)) goto StackUnderflow;
storageCell = new(env.ExecutingAccount, result);
if (!ChargeStorageAccessGas(
ref gasAvailable,
vmState,
in storageCell,
StorageAccessType.SLOAD,
spec)) goto OutOfGas;

ReadOnlySpan<byte> value = _state.Get(in storageCell);
stack.PushBytes(value);
if (typeof(TTracingStorage) == typeof(IsTracing))
{
_txTracer.LoadOperationStorage(storageCell.Address, result, value);
}
exceptionType = InstructionSLoad<TTracingInstructions, TTracingStorage>(vmState, ref stack, ref gasAvailable, spec);
if (exceptionType != EvmExceptionType.None) goto ReturnFailure;

break;
}
Expand Down Expand Up @@ -2610,6 +2594,33 @@ private static EvmExceptionType InstructionLog<TTracing>(EvmState vmState, ref E
return EvmExceptionType.None;
}

[SkipLocalsInit]
private EvmExceptionType InstructionSLoad<TTracingInstructions, TTracingStorage>(EvmState vmState, ref EvmStack<TTracingInstructions> stack, ref long gasAvailable, IReleaseSpec spec)
where TTracingInstructions : struct, IIsTracing
where TTracingStorage : struct, IIsTracing
{
Metrics.SloadOpcode++;
gasAvailable -= spec.GetSLoadCost();

if (!stack.PopUInt256(out UInt256 result)) return EvmExceptionType.StackUnderflow;
StorageCell storageCell = new(vmState.Env.ExecutingAccount, result);
if (!ChargeStorageAccessGas(
ref gasAvailable,
vmState,
in storageCell,
StorageAccessType.SLOAD,
spec)) return EvmExceptionType.OutOfGas;

ReadOnlySpan<byte> value = _state.Get(in storageCell);
stack.PushBytes(value);
if (typeof(TTracingStorage) == typeof(IsTracing))
{
_txTracer.LoadOperationStorage(storageCell.Address, result, value);
}

return EvmExceptionType.None;
}

[SkipLocalsInit]
private EvmExceptionType InstructionSStore<TTracingInstructions, TTracingRefunds, TTracingStorage>(EvmState vmState, ref EvmStack<TTracingInstructions> stack, ref long gasAvailable, IReleaseSpec spec)
where TTracingInstructions : struct, IIsTracing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DebugModuleTests
private readonly IJsonRpcConfig jsonRpcConfig = new JsonRpcConfig();
private readonly ISpecProvider specProvider = Substitute.For<ISpecProvider>();
private readonly IDebugBridge debugBridge = Substitute.For<IDebugBridge>();
private MemDb _blocksDb = new();
private readonly MemDb _blocksDb = new();

[Test]
public async Task Get_from_db()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static AccessList ToAccessList(IEnumerable<AccessListItemForRpc> accessLi
}

public readonly bool Equals(AccessListItemForRpc other) => Equals(Address, other.Address) && StorageKeys.NullableSequenceEqual(other.StorageKeys);
public override bool Equals(object? obj) => obj is AccessListItemForRpc other && Equals(other);
public override readonly bool Equals(object? obj) => obj is AccessListItemForRpc other && Equals(other);
public override readonly int GetHashCode() => HashCode.Combine(Address, StorageKeys);
}
}
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.JsonRpc/Data/AccountForRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Nethermind.JsonRpc.Data;

public struct AccountForRpc
public readonly struct AccountForRpc
{
private readonly Account? _account;
private readonly AccountStruct? _accountStruct;
Expand All @@ -21,9 +21,9 @@ public AccountForRpc(in AccountStruct? account)
_accountStruct = account;
}

public ValueHash256 CodeHash => (_accountStruct?.CodeHash ?? _account?.CodeHash.ValueHash256)!.Value;
public ValueHash256 StorageRoot => (_accountStruct?.StorageRoot ?? _account?.StorageRoot.ValueHash256)!.Value;
public UInt256 Balance => (_accountStruct?.Balance ?? _account?.Balance)!.Value;
public UInt256 Nonce => (_accountStruct?.Nonce ?? _account?.Nonce)!.Value;
public readonly ValueHash256 CodeHash => (_accountStruct?.CodeHash ?? _account?.CodeHash.ValueHash256)!.Value;
public readonly ValueHash256 StorageRoot => (_accountStruct?.StorageRoot ?? _account?.StorageRoot.ValueHash256)!.Value;
public readonly UInt256 Balance => (_accountStruct?.Balance ?? _account?.Balance)!.Value;
public readonly UInt256 Nonce => (_accountStruct?.Nonce ?? _account?.Nonce)!.Value;

}
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
using Nethermind.Core.Collections;
using Nethermind.Core.Extensions;
Expand Down Expand Up @@ -157,6 +158,12 @@ public async IAsyncEnumerable<JsonRpcResult> ProcessAsync(PipeReader reader, Jso
if (_logger.IsDebug) _logger.Debug($"Couldn't read request.{Environment.NewLine}{e}");
yield break;
}
catch (ConnectionResetException e)
{
// Logs exception, then stop processing.
if (_logger.IsTrace) _logger.Trace($"Connection reset.{Environment.NewLine}{e}");
yield break;
}
catch (Exception ex)
{
// Handles general exceptions during parsing and validation.
Expand Down
Loading

0 comments on commit b15fe43

Please sign in to comment.