Skip to content

Commit

Permalink
add MaxBlobCount in PayloadAttributes, refactoring, resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 committed Nov 5, 2024
1 parent 2162240 commit 9d4ec74
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ private void OnHeadChanged(object? sender, BlockReplacementEventArgs e)
BlockGasLimit = e.Block!.GasLimit;
CurrentBaseFee = e.Block.Header.BaseFeePerGas;
CurrentFeePerBlobGas =
BlobGasCalculator.TryCalculateFeePerBlobGas(e.Block.Header, out UInt256 currentFeePerBlobGas, SpecProvider)
BlobGasCalculator.TryCalculateFeePerBlobGas(e.Block.Header, out UInt256 currentFeePerBlobGas,
SpecProvider.GetSpec(e.Block.Header))
? currentFeePerBlobGas
: UInt256.Zero;
HeadChanged?.Invoke(sender, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private bool TryCall(BlockHeader header, Transaction transaction, out byte[] res

try
{
_transactionProcessor.Execute(transaction, new BlockExecutionContext(header, SpecProvider), tracer);
_transactionProcessor.Execute(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), tracer);
result = tracer.ReturnValue;
return tracer.StatusCode == StatusCode.Success;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Blockchain/Contracts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ protected byte[] CallCore(ITransactionProcessor transactionProcessor, BlockHeade
{
if (callAndRestore)
{
transactionProcessor.CallAndRestore(transaction, new BlockExecutionContext(header, SpecProvider), tracer);
transactionProcessor.CallAndRestore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), tracer);
}
else
{
transactionProcessor.Execute(transaction, new BlockExecutionContext(header, SpecProvider), tracer);
transactionProcessor.Execute(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), tracer);
}

failure = tracer.StatusCode != StatusCode.Success;
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Blockchain/GenesisLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void Preallocate(Block genesis)
};

CallOutputTracer outputTracer = new();
_transactionProcessor.Execute(constructorTransaction, new BlockExecutionContext(genesis.Header, _specProvider), outputTracer);
_transactionProcessor.Execute(constructorTransaction, new BlockExecutionContext(genesis.Header, _specProvider.GetSpec(genesis.Header)), outputTracer);

if (outputTracer.StatusCode != StatusCode.Success)
{
Expand Down
18 changes: 16 additions & 2 deletions src/Nethermind/Nethermind.Consensus/Producers/PayloadAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class PayloadAttributes

public ulong? TargetBlobCount { get; set; }

public ulong? MaxBlobCount { get; set; }

public virtual long? GetGasLimit() => null;

public override string ToString() => ToString(string.Empty);
Expand All @@ -54,6 +56,11 @@ public string ToString(string indentation)
sb.Append($", {nameof(TargetBlobCount)} : {TargetBlobCount}");
}

if (MaxBlobCount is not null)
{
sb.Append($", {nameof(MaxBlobCount)} : {MaxBlobCount}");
}

sb.Append('}');

return sb.ToString();
Expand All @@ -79,7 +86,8 @@ protected virtual int ComputePayloadIdMembersSize() =>
+ Address.Size // suggested fee recipient
+ (Withdrawals is null ? 0 : Keccak.Size) // withdrawals root hash
+ (ParentBeaconBlockRoot is null ? 0 : Keccak.Size) // parent beacon block root
+ (TargetBlobCount is null ? 0 : sizeof(ulong)); // target blob count
+ (TargetBlobCount is null ? 0 : sizeof(ulong)) // target blob count
+ (MaxBlobCount is null ? 0 : sizeof(ulong)); // max blob count

protected static string ComputePayloadId(Span<byte> inputSpan)
{
Expand Down Expand Up @@ -124,6 +132,12 @@ protected virtual int WritePayloadIdMembers(BlockHeader parentHeader, Span<byte>
position += sizeof(ulong);
}

if (MaxBlobCount.HasValue)
{
BinaryPrimitives.WriteUInt64BigEndian(inputSpan.Slice(position, sizeof(ulong)), MaxBlobCount.Value);
position += sizeof(ulong);
}

return position;
}

Expand Down Expand Up @@ -180,7 +194,7 @@ public static class PayloadAttributesExtensions
public static int GetVersion(this PayloadAttributes executionPayload) =>
executionPayload switch
{
{ TargetBlobCount: not null } => EngineApiVersions.Prague,
{ MaxBlobCount: not null, TargetBlobCount: not null } => EngineApiVersions.Prague,
{ ParentBeaconBlockRoot: not null, Withdrawals: not null } => EngineApiVersions.Cancun,
{ Withdrawals: not null } => EngineApiVersions.Shanghai,
_ => EngineApiVersions.Paris
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public IEnumerable<Transaction> GetTransactions(BlockHeader parent, long gasLimi

int checkedTransactions = 0;
int selectedTransactions = 0;
using ArrayPoolList<Transaction> selectedBlobTxs = new((int)(parent.TargetBlobCount * 2 ?? Eip4844Constants.GetMaxBlobsPerBlock()));
using ArrayPoolList<Transaction> selectedBlobTxs = new((int)(payloadAttributes?.MaxBlobCount ?? Eip4844Constants.GetMaxBlobsPerBlock()));

SelectBlobTransactions(blobTransactions, parent, spec, selectedBlobTxs);

Expand Down
5 changes: 0 additions & 5 deletions src/Nethermind/Nethermind.Evm/BlobGasCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public static bool TryCalculateFeePerBlobGas(BlockHeader header, out UInt256 fee
return TryCalculateFeePerBlobGas(header.ExcessBlobGas, out feePerBlobGas, header.TargetBlobCount, spec);
}

public static bool TryCalculateFeePerBlobGas(BlockHeader header, out UInt256 feePerBlobGas, ISpecProvider specProvider)
{
return TryCalculateFeePerBlobGas(header.ExcessBlobGas, out feePerBlobGas, header.TargetBlobCount, specProvider.GetSpec(header));
}

public static bool TryCalculateFeePerBlobGas(ulong? excessBlobGas, out UInt256 feePerBlobGas, UInt256? targetBlobCount, IReleaseSpec? spec)
{
static bool FakeExponentialOverflow(UInt256 factor, UInt256 num, UInt256 denominator, out UInt256 feePerBlobGas)
Expand Down
5 changes: 0 additions & 5 deletions src/Nethermind/Nethermind.Evm/BlockExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public BlockExecutionContext(BlockHeader blockHeader, IReleaseSpec spec)
}
}

public BlockExecutionContext(BlockHeader blockHeader, ISpecProvider specProvider) : this(blockHeader,
specProvider.GetSpec(blockHeader))
{
}

public BlockExecutionContext(BlockHeader blockHeader, UInt256 forceBlobBaseFee)
{
Header = blockHeader;
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/GasEstimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private bool TryExecutableTransaction(Transaction transaction, BlockHeader block

transaction.GasLimit = gasLimit;

BlockExecutionContext blCtx = new(block, _specProvider);
BlockExecutionContext blCtx = new(block, _specProvider.GetSpec(block));
_transactionProcessor.CallAndRestore(transaction, in blCtx, tracer.WithCancellation(token));
transaction.GasLimit = originalGasLimit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected TransactionProcessorBase(
}

public TransactionResult CallAndRestore(Transaction transaction, in BlockHeader header, ITxTracer txTracer) =>
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider), txTracer, ExecutionOptions.CommitAndRestore);
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), txTracer, ExecutionOptions.CommitAndRestore);

public TransactionResult CallAndRestore(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
ExecuteCore(transaction, in blCtx, txTracer, ExecutionOptions.CommitAndRestore);
Expand All @@ -112,7 +112,7 @@ public TransactionResult BuildUp(Transaction transaction, in BlockHeader header,
// we need to treat the result of previous transaction as the original value of next transaction
// when we do not commit
WorldState.TakeSnapshot(true);
return ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider), txTracer, ExecutionOptions.None);
return ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), txTracer, ExecutionOptions.None);
}

public TransactionResult BuildUp(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer)
Expand All @@ -124,19 +124,19 @@ public TransactionResult BuildUp(Transaction transaction, in BlockExecutionConte
}

public TransactionResult Execute(Transaction transaction, in BlockHeader header, ITxTracer txTracer) =>
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider), txTracer, ExecutionOptions.Commit);
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), txTracer, ExecutionOptions.Commit);

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

public TransactionResult Trace(Transaction transaction, in BlockHeader header, ITxTracer txTracer) =>
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider), txTracer, ExecutionOptions.NoValidation);
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), txTracer, ExecutionOptions.NoValidation);

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

public TransactionResult Warmup(Transaction transaction, in BlockHeader header, ITxTracer txTracer) =>
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider), txTracer, ExecutionOptions.Warmup);
ExecuteCore(transaction, new BlockExecutionContext(header, SpecProvider.GetSpec(header)), txTracer, ExecutionOptions.Warmup);

public TransactionResult Warmup(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
ExecuteCore(transaction, in blCtx, txTracer, ExecutionOptions.Warmup);
Expand All @@ -146,7 +146,7 @@ private TransactionResult ExecuteCore(Transaction tx, in BlockExecutionContext b
if (tx.IsSystem())
{
_systemTransactionProcessor ??= new SystemTransactionProcessor(SpecProvider, WorldState, VirtualMachine, _codeInfoRepository, _logManager);
return _systemTransactionProcessor.Execute(tx, new BlockExecutionContext(blCtx.Header, SpecProvider), tracer, opts);
return _systemTransactionProcessor.Execute(tx, new BlockExecutionContext(blCtx.Header, SpecProvider.GetSpec(blCtx.Header)), tracer, opts);
}

return Execute(tx, in blCtx, tracer, opts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ public byte[][] Transactions
/// </summary>
public virtual ulong? TargetBlobCount { get; set; }

/// <summary>
/// Gets or sets <see cref="Block.MaxBlobCount"/> as defined in
/// <see href="https://eips.ethereum.org/EIPS/eip-7742">EIP-7742</see>.
/// </summary>
public virtual ulong? MaxBlobCount { get; set; }

public static ExecutionPayload Create(Block block) => Create<ExecutionPayload>(block);

protected static TExecutionPayload Create<TExecutionPayload>(Block block) where TExecutionPayload : ExecutionPayload, new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,4 @@ public override bool ValidateFork(ISpecProvider specProvider) =>
/// </summary>
[JsonRequired]
public sealed override ulong? TargetBlobCount { get; set; }

/// <summary>
/// Gets or sets <see cref="Block.MaxBlobCount"/> as defined in
/// <see href="https://eips.ethereum.org/EIPS/eip-7742">EIP-7742</see>.
/// </summary>
[JsonRequired]
public sealed override ulong? MaxBlobCount { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void CommitAndDisposeBatch(Batch batch)
batch.Dispose();
}

BlockExecutionContext blkCtx = new(blockHeader, _specProvider);
BlockExecutionContext blkCtx = new(blockHeader, _specProvider.GetSpec(blockHeader));
worldState.StateRoot = blockHeader.StateRoot;

Batch batch = new(maxBytesPerTxList, txSource.Length, txDecoder);
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.TxPool/TxPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void ReAddReorganisedTransactions(Block? previousBlock)
private void RemoveProcessedTransactions(Block block)
{
Transaction[] blockTransactions = block.Transactions;
using ArrayPoolList<Transaction> blobTxsToSave = new((int)(block.TargetBlobCount * 2 ?? Eip4844Constants.GetMaxBlobsPerBlock()));
using ArrayPoolList<Transaction> blobTxsToSave = new(blockTransactions.Length);
long discoveredForPendingTxs = 0;
long discoveredForHashCache = 0;
long eip1559Txs = 0;
Expand Down

0 comments on commit 9d4ec74

Please sign in to comment.