Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForkId calculation polish #5068

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -84,13 +84,13 @@ public ResultWrapper<Keccak> Simulate(UserOperation userOperation,
}
}

IEip1559Spec specfor1559 = _specProvider.GetSpecFor1559(parent.Number + 1);
IEip1559Spec specFor1559 = _specProvider.GetSpecFor1559(parent.Number + 1);
ReadOnlyTxProcessingEnv txProcessingEnv = _readOnlyTxProcessingEnvFactory.Create();
ITransactionProcessor transactionProcessor = txProcessingEnv.Build(_stateProvider.StateRoot);

// wrap userOp into a tx calling the simulateWallet function off-chain from zero-address (look at EntryPoint.sol for more context)
Transaction simulateValidationTransaction =
BuildSimulateValidationTransaction(userOperation, parent, specfor1559);
BuildSimulateValidationTransaction(userOperation, parent, specFor1559);

UserOperationSimulationResult simulationResult = SimulateValidation(simulateValidationTransaction, userOperation, parent, transactionProcessor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ private void IncrementOpsSeenForOpsSurpassingBaseFee()

private void UpdateCurrentBaseFee()
{
IEip1559Spec SpecFor1559 = _specProvider.GetSpecFor1559(_blockTree.Head!.Number + 1);
UInt256 baseFee = BaseFeeCalculator.Calculate(_blockTree.Head!.Header, SpecFor1559);
IEip1559Spec specFor1559 = _specProvider.GetSpecFor1559(_blockTree.Head!.Number + 1);
UInt256 baseFee = BaseFeeCalculator.Calculate(_blockTree.Head!.Header, specFor1559);
_currentBaseFee = baseFee;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using Nethermind.Consensus;
Expand Down Expand Up @@ -144,10 +144,10 @@ private void Assert1559Transactions(IComparer<Transaction> comparer, int feeCapX

private class TestingContext
{
private IBlockTree _blockTree;
private ITransactionComparerProvider _transactionComparerProvider;
private long blockNumber;
private UInt256 baseFee;
private readonly IBlockTree _blockTree;
private readonly ITransactionComparerProvider _transactionComparerProvider;
private long _blockNumber;
private UInt256 _baseFee;

public TestingContext(bool isEip1559Enabled = false, long eip1559TransitionBlock = 0)
{
Expand All @@ -173,23 +173,23 @@ public IComparer<Transaction> GetProducerComparer(BlockPreparationContext blockP

public TestingContext WithHeadBlockNumber(long headBlockNumber)
{
blockNumber = headBlockNumber;
_blockNumber = headBlockNumber;
UpdateBlockTreeHead();
return this;
}

public TestingContext WithHeadBaseFeeNumber(UInt256 headBaseFee)
{
baseFee = headBaseFee;
_baseFee = headBaseFee;
UpdateBlockTreeHead();
return this;
}

private void UpdateBlockTreeHead()
{
Block block = Build.A.Block
.WithNumber(blockNumber)
.WithBaseFeePerGas(baseFee).TestObject;
.WithNumber(_blockNumber)
.WithBaseFeePerGas(_baseFee).TestObject;
_blockTree.Head.Returns(block);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ResultWrapper<FeeHistoryResults> GetFeeHistory(long blockCount, BlockPara
long oldestBlockNumber = block!.Number;
Stack<UInt256> baseFeePerGas = new((int)(blockCount + 1));
baseFeePerGas.Push(BaseFeeCalculator.Calculate(block!.Header, _specProvider.GetSpecFor1559(block!.Number + 1)));
Stack<double> gasUsedRatio = new Stack<double>((int)blockCount);
Stack<double> gasUsedRatio = new((int)blockCount);

Stack<UInt256[]>? rewards = rewardPercentiles is null || rewardPercentiles.Any() is false ? null : new Stack<UInt256[]>((int)blockCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public void Timstamp_activation_equal_to_genesis_timestamp_loads_correctly()

[Test]
[NonParallelizable]

public void Logs_warning_when_timestampActivation_happens_before_blockActivation()
{
ChainSpecLoader loader = new(new EthereumJsonSerializer());
Expand Down
24 changes: 12 additions & 12 deletions src/Nethermind/Nethermind.Specs.Test/CustomSpecProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace Nethermind.Specs.Test
public class CustomSpecProvider : ISpecProvider
{
private ForkActivation? _theMergeBlock = null;
private (ForkActivation forkActivation, IReleaseSpec Release)[] _transitions;
private readonly (ForkActivation Activation, IReleaseSpec Spec)[] _transitions;

public ulong ChainId { get; }
public ForkActivation[] TransitionActivations { get; }

public CustomSpecProvider(params (ForkActivation forkActivation, IReleaseSpec Release)[] transitions) : this(0, transitions)
public CustomSpecProvider(params (ForkActivation Activation, IReleaseSpec Spec)[] transitions) : this(0, transitions)
{
}

public CustomSpecProvider(ulong chainId, params (ForkActivation forkActivation, IReleaseSpec Release)[] transitions)
public CustomSpecProvider(ulong chainId, params (ForkActivation Activation, IReleaseSpec Spec)[] transitions)
{
ChainId = chainId;

Expand All @@ -32,10 +32,10 @@ public CustomSpecProvider(ulong chainId, params (ForkActivation forkActivation,
throw new ArgumentException($"There must be at least one release specified when instantiating {nameof(CustomSpecProvider)}", $"{nameof(transitions)}");
}

_transitions = transitions.OrderBy(r => r.forkActivation).ToArray();
TransitionActivations = _transitions.Select(t => t.forkActivation).ToArray();
_transitions = transitions.OrderBy(r => r.Activation).ToArray();
TransitionActivations = _transitions.Select(t => t.Activation).ToArray();

if (transitions[0].forkActivation.BlockNumber != 0L)
if (transitions[0].Activation.BlockNumber != 0L)
{
throw new ArgumentException($"First release specified when instantiating {nameof(CustomSpecProvider)} should be at genesis block (0)", $"{nameof(transitions)}");
}
Expand All @@ -54,25 +54,25 @@ public void UpdateMergeTransitionInfo(long? blockNumber, UInt256? terminalTotalD

#pragma warning disable CS8602
#pragma warning disable CS8603
public IReleaseSpec GenesisSpec => _transitions?.Length == 0 ? null : _transitions[0].Release;
public IReleaseSpec GenesisSpec => _transitions.Length == 0 ? null : _transitions[0].Spec;
#pragma warning restore CS8603
#pragma warning restore CS8602

public IReleaseSpec GetSpec(ForkActivation forkActivation) =>
_transitions.TryGetSearchedItem(forkActivation,
CompareTransitionOnBlock,
out (ForkActivation, IReleaseSpec Release) transition)
? transition.Release
out (ForkActivation Activation, IReleaseSpec Spec) transition)
? transition.Spec
: GenesisSpec;

private static int CompareTransitionOnBlock(ForkActivation forkActivation, (ForkActivation activation, IReleaseSpec Release) transition) =>
forkActivation.CompareTo(transition.activation);
private static int CompareTransitionOnBlock(ForkActivation forkActivation, (ForkActivation Activation, IReleaseSpec Spec) transition) =>
forkActivation.CompareTo(transition.Activation);

public long? DaoBlockNumber
{
get
{
(ForkActivation forkActivation, IReleaseSpec daoRelease) = _transitions.SingleOrDefault(t => t.Release == Dao.Instance);
(ForkActivation forkActivation, IReleaseSpec daoRelease) = _transitions.SingleOrDefault(t => t.Spec == Dao.Instance);
return daoRelease is not null ? forkActivation.BlockNumber : null;
}
}
Expand Down
Loading