Skip to content

Commit

Permalink
Taiko (#7326)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey <[email protected]>
Co-authored-by: Ben Adams <[email protected]>
Co-authored-by: Lukasz Rozmej <[email protected]>
Co-authored-by: Ruben Buniatyan <[email protected]>
  • Loading branch information
5 people authored Oct 24, 2024
1 parent ebc0def commit d3d0ec0
Show file tree
Hide file tree
Showing 78 changed files with 3,352 additions and 56 deletions.
292 changes: 292 additions & 0 deletions src/Nethermind/Chains/taiko-hekla.json

Large diffs are not rendered by default.

280 changes: 280 additions & 0 deletions src/Nethermind/Chains/taiko-mainnet.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Api/Extensions/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Nethermind.Api.Extensions;

public class PluginConfig : IPluginConfig
{
public string[] PluginOrder { get; set; } = { "Clique", "Aura", "Ethash", "Optimism", "Shutter", "AuRaMerge", "Merge", "MEV", "HealthChecks", "Hive" };
public string[] PluginOrder { get; set; } = { "Clique", "Aura", "Ethash", "Optimism", "Shutter", "Taiko", "AuRaMerge", "Merge", "MEV", "HealthChecks", "Hive" };
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@

namespace Nethermind.Consensus.Processing
{
internal static class BlockExtensions
public static class BlockExtensions
{
public static Block CreateCopy(this Block block, BlockHeader header) =>
block is BlockToProduce blockToProduce
? new BlockToProduce(header, blockToProduce.Transactions, blockToProduce.Uncles, blockToProduce.Withdrawals, blockToProduce.Requests)
: new Block(header, block.Transactions, block.Uncles, block.Withdrawals, block.Requests);

public static IEnumerable<Transaction> GetTransactions(this Block block) =>
block is BlockToProduce blockToProduce
? blockToProduce.Transactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private Block PrepareBlockForProcessing(Block suggestedBlock)
headerForProcessing.StateRoot = bh.StateRoot;
}

return suggestedBlock.CreateCopy(headerForProcessing);
return suggestedBlock.WithReplacedHeader(headerForProcessing);
}

// TODO: block processor pipeline
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Evm.TransactionProcessing;

namespace Nethermind.Consensus.Processing;

public interface IReadOnlyTxProcessingEnvFactory
{
public IReadOnlyTxProcessorSource Create();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Nethermind.Blockchain;
using Nethermind.Core.Specs;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;

Expand All @@ -13,7 +14,7 @@ public class ReadOnlyTxProcessingEnvFactory(
IReadOnlyBlockTree readOnlyBlockTree,
ISpecProvider? specProvider,
ILogManager? logManager,
IWorldState? worldStateToWarmUp = null)
IWorldState? worldStateToWarmUp = null) : IReadOnlyTxProcessingEnvFactory
{
public ReadOnlyTxProcessingEnvFactory(
IWorldStateManager worldStateManager,
Expand All @@ -25,5 +26,5 @@ public ReadOnlyTxProcessingEnvFactory(
{
}

public ReadOnlyTxProcessingEnv Create() => new(worldStateManager, readOnlyBlockTree, specProvider, logManager, worldStateToWarmUp);
public IReadOnlyTxProcessorSource Create() => new ReadOnlyTxProcessingEnv(worldStateManager, readOnlyBlockTree, specProvider, logManager, worldStateToWarmUp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public BlockProductionPolicy(
public bool ShouldStartBlockProduction() => _miningConfig.Enabled;
}

public class NeverStartBlockProductionPolicy : IBlockProductionPolicy
{
public bool ShouldStartBlockProduction() => false;

public static NeverStartBlockProductionPolicy Instance =>
LazyInitializer.EnsureInitialized(ref _instance, () => new());

private static NeverStartBlockProductionPolicy? _instance;
private NeverStartBlockProductionPolicy() { }
}

public class AlwaysStartBlockProductionPolicy : IBlockProductionPolicy
{
public bool ShouldStartBlockProduction() => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ public BlockToProduce(BlockHeader blockHeader,
{
Transactions = transactions;
}

public override Block WithReplacedHeader(BlockHeader newHeader) => new BlockToProduce(newHeader, Transactions, Uncles, Withdrawals, Requests);
}
}
26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.Consensus/Producers/FailBlockProducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Evm.Tracing;

namespace Nethermind.Consensus.Producers;

/// <summary>
/// A BlockProducer that always fails.
/// It is used in tests or when the node is not supposed to produce blocks, and we want to detect block production being triggered.
/// </summary>
public class FailBlockProducer : IBlockProducer
{
public Task<Block?> BuildBlock(
BlockHeader? parentHeader = null,
IBlockTracer? blockTracer = null,
PayloadAttributes? payloadAttributes = null,
CancellationToken? cancellationToken = null)
{
throw new InvalidOperationException("FailBlockProducer is not supposed to produce blocks.");
}
}
2 changes: 0 additions & 2 deletions src/Nethermind/Nethermind.Consensus/Transactions/ITxSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ public interface ITxSource
{
IEnumerable<Transaction> GetTransactions(BlockHeader parent, long gasLimit, PayloadAttributes? payloadAttributes = null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public static bool ValidateRequestsOrder(Block block, out string? error)
return true;
}

private bool ValidateTransactions(Block block, IReleaseSpec spec, out string? errorMessage)
protected virtual bool ValidateTransactions(Block block, IReleaseSpec spec, out string? errorMessage)
{
Transaction[] transactions = block.Transactions;

Expand All @@ -362,7 +362,7 @@ private bool ValidateTransactions(Block block, IReleaseSpec spec, out string? er
return true;
}

private bool ValidateEip4844Fields(Block block, IReleaseSpec spec, out string? error)
protected virtual bool ValidateEip4844Fields(Block block, IReleaseSpec spec, out string? error)
{
if (!spec.IsEip4844Enabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public virtual bool Validate(BlockHeader header, BlockHeader? parent, bool isUnc
&& ValidateBlobGasFields(header, parent, spec, ref error);
}

private bool Validate1559(BlockHeader header, BlockHeader parent, IReleaseSpec spec, ref string? error)
protected virtual bool Validate1559(BlockHeader header, BlockHeader parent, IReleaseSpec spec, ref string? error)
{
if (spec.IsEip1559Enabled)
{
Expand All @@ -111,7 +111,7 @@ private bool Validate1559(BlockHeader header, BlockHeader parent, IReleaseSpec s
return true;
}

private bool ValidateBlockNumber(BlockHeader header, BlockHeader parent, ref string? error)
protected virtual bool ValidateBlockNumber(BlockHeader header, BlockHeader parent, ref string? error)
{
if (header.Number != parent.Number + 1)
{
Expand All @@ -123,7 +123,7 @@ private bool ValidateBlockNumber(BlockHeader header, BlockHeader parent, ref str
return true;
}

private bool ValidateGasUsed(BlockHeader header, ref string? error)
protected virtual bool ValidateGasUsed(BlockHeader header, ref string? error)
{
if (header.GasUsed > header.GasLimit)
{
Expand All @@ -135,7 +135,7 @@ private bool ValidateGasUsed(BlockHeader header, ref string? error)
return true;
}

protected bool ValidateParent(BlockHeader header, BlockHeader? parent, ref string? error)
protected virtual bool ValidateParent(BlockHeader header, BlockHeader? parent, ref string? error)
{
if (parent is null)
{
Expand All @@ -158,7 +158,7 @@ protected bool ValidateParent(BlockHeader header, BlockHeader? parent, ref strin
return true;
}

private bool ValidateSeal(BlockHeader header, BlockHeader parent, bool isUncle, ref string? error)
protected virtual bool ValidateSeal(BlockHeader header, BlockHeader parent, bool isUncle, ref string? error)
{
bool result = _sealValidator.ValidateParams(parent, header, isUncle);

Expand All @@ -171,7 +171,7 @@ private bool ValidateSeal(BlockHeader header, BlockHeader parent, bool isUncle,
return result;
}

private bool ValidateFieldLimit(BlockHeader blockHeader, ref string? error)
protected virtual bool ValidateFieldLimit(BlockHeader blockHeader, ref string? error)
{
// Note, these are out of spec. Technically, there could be a block with field with very high value that is
// valid when using ulong, but wrapped to negative value when using long. However, switching to ulong
Expand Down Expand Up @@ -247,7 +247,7 @@ protected virtual bool ValidateGasLimitRange(BlockHeader header, BlockHeader par
return gasLimitNotTooHigh && gasLimitNotTooLow;
}

private bool ValidateTimestamp(BlockHeader header, BlockHeader parent, ref string? error)
protected virtual bool ValidateTimestamp(BlockHeader header, BlockHeader parent, ref string? error)
{
bool timestampMoreThanAtParent = header.Timestamp > parent.Timestamp;
if (!timestampMoreThanAtParent)
Expand Down Expand Up @@ -311,7 +311,7 @@ private bool ValidateGenesis(BlockHeader header) =>
header.Bloom is not null &&
header.ExtraData.Length <= _specProvider.GenesisSpec.MaximumExtraDataSize;

private bool ValidateBlobGasFields(BlockHeader header, BlockHeader parentHeader, IReleaseSpec spec, ref string? error)
protected virtual bool ValidateBlobGasFields(BlockHeader header, BlockHeader parentHeader, IReleaseSpec spec, ref string? error)
{
if (spec.IsEip4844Enabled)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/Json/Base64ConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Serialization.Json;
using NUnit.Framework;
using System.Linq;

namespace Nethermind.Core.Test.Json;

[TestFixture]
public class Base64ConverterTests : ConverterTestBase<byte[]?>
{
[TestCase(null)]
[TestCase(new byte[0])]
[TestCase(new byte[] { 1 })]
[TestCase(new byte[] { 0, 1 })]
[TestCase(new byte[] { 0, 0, 1 })]
[TestCase(new byte[] { 0, 0, 255 })]
[TestCase(new byte[] { 0, 0, 1, 0 })]
[TestCase(new byte[] { 0, 0, 1, 0, 0 })]
[TestCase(new byte[] { 0, 0, 1, 0, 127 })]
[TestCase(new byte[] { 0, 0, 1, 0, 255 })]
public void ValueWithAndWithoutLeadingZeros_are_equal(byte[]? value)
{
TestConverter(
value,
(before, after) => (before is null && after is null) || (before is not null && after is not null && before.SequenceEqual(after)),
new Base64Converter());
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Block(BlockHeader header) : this(
)
{ }

public Block WithReplacedHeader(BlockHeader newHeader) => new(newHeader, Body);
public virtual Block WithReplacedHeader(BlockHeader newHeader) => new(newHeader, Body);

public Block WithReplacedBody(BlockBody newBody) => new(Header, newBody);

Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Core/SealEngineType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public static class SealEngineType
public const string Ethash = nameof(Ethash);
public const string BeaconChain = nameof(BeaconChain);
public const string Optimism = nameof(Optimism);
public const string Taiko = nameof(Taiko);
}
}
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec
/// OP Granite
bool IsOpGraniteEnabled { get; }

/// Taiko Ontake
bool IsOntakeEnabled { get; }

/// <summary>
/// Should transactions be validated against chainId.
/// </summary>
Expand Down
46 changes: 46 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/ReleaseSpecDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,54 @@ public class ReleaseSpecDecorator(IReleaseSpec spec) : IReleaseSpec
public bool IsEip7702Enabled => spec.IsEip7702Enabled;
public virtual bool IsRip7212Enabled => spec.IsRip7212Enabled;
public virtual bool IsOpGraniteEnabled => spec.IsOpGraniteEnabled;
public virtual bool IsOntakeEnabled => spec.IsOntakeEnabled;
public virtual ulong WithdrawalTimestamp => spec.WithdrawalTimestamp;
public virtual ulong Eip4844TransitionTimestamp => spec.Eip4844TransitionTimestamp;
public virtual bool IsEip158IgnoredAccount(Address address) => spec.IsEip158IgnoredAccount(address);
public bool IsEip4844FeeCollectorEnabled => spec.IsEip4844FeeCollectorEnabled;

public virtual long MaxInitCodeSize => spec.MaxInitCodeSize;
public virtual bool ValidateChainId => spec.ValidateChainId;
public virtual bool ClearEmptyAccountWhenTouched => spec.ClearEmptyAccountWhenTouched;
// VM
public virtual bool LimitCodeSize => spec.LimitCodeSize;
public virtual bool UseHotAndColdStorage => spec.UseHotAndColdStorage;
public virtual bool UseTxAccessLists => spec.UseTxAccessLists;
public virtual bool AddCoinbaseToTxAccessList => spec.AddCoinbaseToTxAccessList;
public virtual bool ModExpEnabled => spec.ModExpEnabled;
public virtual bool Bn128Enabled => spec.Bn128Enabled;
public virtual bool BlakeEnabled => spec.BlakeEnabled;
public virtual bool Bls381Enabled => spec.Bls381Enabled;
public virtual bool ChargeForTopLevelCreate => spec.ChargeForTopLevelCreate;
public virtual bool FailOnOutOfGasCodeDeposit => spec.FailOnOutOfGasCodeDeposit;
public virtual bool UseShanghaiDDosProtection => spec.UseShanghaiDDosProtection;
public virtual bool UseExpDDosProtection => spec.UseExpDDosProtection;
public virtual bool UseLargeStateDDosProtection => spec.UseLargeStateDDosProtection;
public virtual bool ReturnDataOpcodesEnabled => spec.ReturnDataOpcodesEnabled;
public virtual bool ChainIdOpcodeEnabled => spec.ChainIdOpcodeEnabled;
public virtual bool Create2OpcodeEnabled => spec.Create2OpcodeEnabled;
public virtual bool DelegateCallEnabled => spec.DelegateCallEnabled;
public virtual bool StaticCallEnabled => spec.StaticCallEnabled;
public virtual bool ShiftOpcodesEnabled => spec.ShiftOpcodesEnabled;
public virtual bool RevertOpcodeEnabled => spec.RevertOpcodeEnabled;
public virtual bool ExtCodeHashOpcodeEnabled => spec.ExtCodeHashOpcodeEnabled;
public virtual bool SelfBalanceOpcodeEnabled => spec.SelfBalanceOpcodeEnabled;
public virtual bool UseConstantinopleNetGasMetering => spec.UseConstantinopleNetGasMetering;
public virtual bool UseIstanbulNetGasMetering => spec.UseIstanbulNetGasMetering;
public virtual bool UseNetGasMetering => spec.UseNetGasMetering;
public virtual bool UseNetGasMeteringWithAStipendFix => spec.UseNetGasMeteringWithAStipendFix;
public virtual bool Use63Over64Rule => spec.Use63Over64Rule;
public virtual bool BaseFeeEnabled => spec.BaseFeeEnabled;
// EVM Related
public virtual bool IncludePush0Instruction => spec.IncludePush0Instruction;
public virtual bool TransientStorageEnabled => spec.TransientStorageEnabled;
public virtual bool WithdrawalsEnabled => spec.WithdrawalsEnabled;
public virtual bool SelfdestructOnlyOnSameTransaction => spec.SelfdestructOnlyOnSameTransaction;
public virtual bool IsBeaconBlockRootAvailable => spec.IsBeaconBlockRootAvailable;
public virtual bool IsBlockHashInStateAvailable => spec.IsBlockHashInStateAvailable;
public virtual bool MCopyIncluded => spec.MCopyIncluded;
public virtual bool BlobBaseFeeEnabled => spec.BlobBaseFeeEnabled;
public virtual Address? FeeCollector => spec.FeeCollector;
public virtual UInt256? Eip1559BaseFeeMinValue => spec.Eip1559BaseFeeMinValue;
public virtual bool ValidateReceipts => spec.ValidateReceipts;
}
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class Transaction
/// </summary>
public TxType Type { get; set; }

// Taiko Anchor transaction
public bool IsAnchorTx { get; set; }

// Optimism deposit transaction fields
// SourceHash uniquely identifies the source of the deposit
public Hash256? SourceHash { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private static void UpdateMetrics(ExecutionOptions opts, UInt256 effectiveGasPri
/// <param name="opts">Options (Flags) to use for execution</param>
/// <param name="intrinsicGas">Calculated intrinsic gas</param>
/// <returns></returns>
protected TransactionResult ValidateStatic(
protected virtual TransactionResult ValidateStatic(
Transaction tx,
BlockHeader header,
IReleaseSpec spec,
Expand Down Expand Up @@ -369,7 +369,7 @@ protected virtual TransactionResult ValidateGas(Transaction tx, BlockHeader head
if (validate && tx.GasLimit > header.GasLimit - header.GasUsed)
{
TraceLogInvalidTx(tx, $"BLOCK_GAS_LIMIT_EXCEEDED {tx.GasLimit} > {header.GasLimit} - {header.GasUsed}");
return "block gas limit exceeded";
return TransactionResult.BlockGasLimitExceeded;
}

return TransactionResult.Ok;
Expand Down Expand Up @@ -794,5 +794,7 @@ public readonly struct TransactionResult(string? error)
public static implicit operator TransactionResult(string? error) => new(error);
public static implicit operator bool(TransactionResult result) => result.Success;
public override string ToString() => Error is not null ? $"Fail : {Error}" : "Success";

public const string BlockGasLimitExceeded = "Block gas limit exceeded";
}
}
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected virtual Task InitBlockchain()
{
(IApiWithStores getApi, IApiWithBlockchain setApi) = _api.ForBlockchain;
setApi.TransactionComparerProvider = new TransactionComparerProvider(getApi.SpecProvider!, getApi.BlockTree!.AsReadOnly());
setApi.TxValidator = CreateTxValidator(_api.SpecProvider!.ChainId);

IInitConfig initConfig = getApi.Config<IInitConfig>();
IBlocksConfig blocksConfig = getApi.Config<IBlocksConfig>();
Expand Down Expand Up @@ -145,6 +146,11 @@ protected virtual Task InitBlockchain()
return Task.CompletedTask;
}

protected virtual TxValidator? CreateTxValidator(ulong v)
{
return new TxValidator(_api.SpecProvider!.ChainId);
}

protected virtual IBlockValidator CreateBlockValidator()
{
return new BlockValidator(
Expand Down
Loading

0 comments on commit d3d0ec0

Please sign in to comment.