Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into taiko
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Oct 14, 2024
2 parents efb52ca + 6b4b485 commit 33f10db
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void WarmupTransactions(ParallelOptions parallelOptions, IReleaseSpec sp
{
scope.WorldState.WarmUp(tx.AccessList); // eip-2930
}
TransactionResult result = scope.TransactionProcessor.Trace(systemTransaction, new BlockExecutionContext(block.Header.Clone()), NullTxTracer.Instance);
TransactionResult result = scope.TransactionProcessor.Warmup(systemTransaction, new BlockExecutionContext(block.Header.Clone()), NullTxTracer.Instance);
if (_logger.IsTrace) _logger.Trace($"Finished pre-warming cache for tx[{i}] {tx.Hash} with {result}");
}
catch (Exception ex) when (ex is EvmException or OverflowException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public interface ITransactionProcessor
/// Will NOT charge gas from sender account, so stateDiff will miss gas fee
/// </summary>
TransactionResult Trace(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);

/// <summary>
/// Call transaction, no validations, don't commit state
/// Will NOT charge gas from sender account
/// </summary>
TransactionResult Warmup(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override TransactionResult Execute(Transaction tx, in BlockExecutionCo
WorldState.CreateAccountIfNotExists(Address.SystemUser, UInt256.Zero, UInt256.Zero);
}

return base.Execute(tx, in blCtx, tracer, !opts.HasFlag(ExecutionOptions.NoValidation)
return base.Execute(tx, in blCtx, tracer, (opts != ExecutionOptions.Warmup && !opts.HasFlag(ExecutionOptions.NoValidation))
? opts | (ExecutionOptions)OriginalValidate | ExecutionOptions.NoValidation
: opts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ protected enum ExecutionOptions
/// <summary>
/// Skip potential fail checks
/// </summary>
NoValidation = Commit | 4,
Warmup = 4,

/// <summary>
/// Skip potential fail checks and commit state after execution
/// </summary>
NoValidation = Commit | Warmup,

/// <summary>
/// Commit and later restore state also skip validation, use for CallAndRestore
Expand Down Expand Up @@ -112,6 +117,9 @@ public TransactionResult Execute(Transaction transaction, in BlockExecutionConte
public TransactionResult Trace(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
ExecuteCore(transaction, in blCtx, txTracer, ExecutionOptions.NoValidation);

public TransactionResult Warmup(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
ExecuteCore(transaction, in blCtx, txTracer, ExecutionOptions.Warmup);

private TransactionResult ExecuteCore(Transaction tx, in BlockExecutionContext blCtx, ITxTracer tracer, ExecutionOptions opts)
{
if (tx.IsSystem())
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ protected virtual IHealthHintService CreateHealthHintService() =>
protected virtual IBlockProductionPolicy CreateBlockProductionPolicy() =>
new BlockProductionPolicy(_api.Config<IMiningConfig>());

protected virtual TxPool.TxPool CreateTxPool(CodeInfoRepository codeInfoRepository) =>
new(_api.EthereumEcdsa!,
protected virtual ITxPool CreateTxPool(CodeInfoRepository codeInfoRepository) =>
new TxPool.TxPool(_api.EthereumEcdsa!,
_api.BlobTxStorage ?? NullBlobTxStorage.Instance,
new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.StateReader!, codeInfoRepository),
_api.Config<ITxPoolConfig>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Init.Steps;
using Nethermind.Merge.Plugin.InvalidChainTracker;
using Nethermind.TxPool;

namespace Nethermind.Optimism;

Expand Down Expand Up @@ -103,4 +104,7 @@ protected override IHealthHintService CreateHealthHintService() =>
new ManualHealthHintService(_blocksConfig.SecondsPerSlot * 6, HealthHintConstants.InfinityHint);

protected override IBlockProductionPolicy CreateBlockProductionPolicy() => AlwaysStartBlockProductionPolicy.Instance;

protected override ITxPool CreateTxPool(CodeInfoRepository codeInfoRepository) =>
api.Config<IOptimismConfig>().SequencerUrl is not null ? NullTxPool.Instance : base.CreateTxPool(codeInfoRepository);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ protected override void RegisterEthRpcModule(IRpcModuleProvider rpcModuleProvide

if (_config.SequencerUrl is null && _logger.IsWarn)
{
_logger.Warn($"SequencerUrl is not set. Nethermind will behave as a Sequencer");
_logger.Warn("SequencerUrl is not set. Nethermind will behave as a Sequencer");
}

BasicJsonRpcClient? sequencerJsonRpcClient = _config.SequencerUrl is null
? null
: new(new Uri(_config.SequencerUrl), _api.EthereumJsonSerializer, _api.LogManager);
ModuleFactoryBase<IEthRpcModule> ethModuleFactory = CreateEthModuleFactory();
BasicJsonRpcClient? sequencerJsonRpcClient = _config.SequencerUrl is not null
? new(new Uri(_config.SequencerUrl), _api.EthereumJsonSerializer, _api.LogManager)
: null;

ITxSigner txSigner = new WalletTxSigner(_api.Wallet, _api.SpecProvider.ChainId);
TxSealer sealer = new(txSigner, _api.Timestamper);
Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Runner/Nethermind.Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<InvariantGlobalization>true</InvariantGlobalization>
<ServerGarbageCollection>true</ServerGarbageCollection>
<TieredPGO>true</TieredPGO>
<PublishReadyToRun>true</PublishReadyToRun>
<GarbageCollectionAdaptationMode>0</GarbageCollectionAdaptationMode>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Taiko/TaikoTxProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected override TransactionResult BuyGas(Transaction tx, BlockHeader header,
=> base.BuyGas(tx, header, spec, tracer, tx.IsAnchorTx ? opts | ExecutionOptions.NoValidation : opts, in effectiveGasPrice, out premiumPerGas, out senderReservedGasPayment, out blobBaseFee);

protected override long Refund(Transaction tx, BlockHeader header, IReleaseSpec spec, ExecutionOptions opts,
in TransactionSubstate substate, in long unspentGas, in UInt256 gasPrice)
=> base.Refund(tx, header, spec, tx.IsAnchorTx ? opts | ExecutionOptions.NoValidation : opts, substate, unspentGas, gasPrice);
in TransactionSubstate substate, in long unspentGas, in UInt256 gasPrice, int codeInsertRefunds)
=> base.Refund(tx, header, spec, tx.IsAnchorTx ? opts | ExecutionOptions.NoValidation : opts, substate, unspentGas, gasPrice, codeInsertRefunds);

protected override void PayFees(Transaction tx, BlockHeader header, IReleaseSpec spec, ITxTracer tracer, in TransactionSubstate substate, in long spentGas, in UInt256 premiumPerGas, in UInt256 blobBaseFee, in byte statusCode)
{
Expand Down
45 changes: 35 additions & 10 deletions src/Nethermind/Nethermind.Trie/HexPrefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Nethermind.Trie
{
Expand Down Expand Up @@ -39,23 +42,45 @@ public static byte[] ToBytes(byte[] path, bool isLeaf)

public static (byte[] key, bool isLeaf) FromBytes(ReadOnlySpan<byte> bytes)
{
bool isLeaf = bytes[0] >= 32;
bool isEven = (bytes[0] & 16) == 0;
int nibblesCount = bytes.Length * 2 - (isEven ? 2 : 1);
byte[] path = new byte[nibblesCount];
for (int i = 0; i < nibblesCount; i++)
Span<byte> span = new(path);
if (!isEven)
{
span[0] = (byte)(bytes[0] & 0xF);
span = span.Slice(1);
}
bool isLeaf = bytes[0] >= 32;
bytes = bytes.Slice(1);

Span<ushort> nibbles = MemoryMarshal.CreateSpan(
ref Unsafe.As<byte, ushort>(ref MemoryMarshal.GetReference(span)),
span.Length / 2);

Debug.Assert(nibbles.Length == bytes.Length);

ref byte byteRef = ref MemoryMarshal.GetReference(bytes);
ref ushort lookup16 = ref MemoryMarshal.GetArrayDataReference(Lookup16);
for (int i = 0; i < nibbles.Length; i++)
{
path[i] =
isEven
? i % 2 == 0
? (byte)((bytes[1 + i / 2] & 240) / 16)
: (byte)(bytes[1 + i / 2] & 15)
: i % 2 == 0
? (byte)(bytes[i / 2] & 15)
: (byte)((bytes[1 + i / 2] & 240) / 16);
nibbles[i] = Unsafe.Add(ref lookup16, Unsafe.Add(ref byteRef, i));
}

return (path, isLeaf);
}

private static readonly ushort[] Lookup16 = CreateLookup16("x2");

private static ushort[] CreateLookup16(string format)
{
ushort[] result = new ushort[256];
for (int i = 0; i < 256; i++)
{
result[i] = (ushort)(((i & 0xF) << 8) | ((i & 240) >> 4));
}

return result;
}
}
}

0 comments on commit 33f10db

Please sign in to comment.