Skip to content

Commit

Permalink
Pass readonly struct BlockExecutionContext via in (#6469)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 7, 2024
1 parent 74743cd commit 4460f7a
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public override TxReceipt[] ProcessTransactions(
{
if (IsAccountAbstractionTransaction(transaction))
{
BlockProcessor.TxAction action = ProcessAccountAbstractionTransaction(block, blkCtx, transaction, i++, receiptsTracer, processingOptions, transactionsInBlock);
BlockProcessor.TxAction action = ProcessAccountAbstractionTransaction(block, in blkCtx, transaction, i++, receiptsTracer, processingOptions, transactionsInBlock);
if (action == BlockProcessor.TxAction.Stop) break;
}
else
{
BlockProcessor.TxAction action = ProcessTransaction(block, blkCtx, transaction, i++, receiptsTracer, processingOptions, transactionsInBlock);
BlockProcessor.TxAction action = ProcessTransaction(block, in blkCtx, transaction, i++, receiptsTracer, processingOptions, transactionsInBlock);
if (action == BlockProcessor.TxAction.Stop) break;
}
}
Expand All @@ -81,7 +81,7 @@ private bool IsAccountAbstractionTransaction(Transaction transaction)

private BlockProcessor.TxAction ProcessAccountAbstractionTransaction(
Block block,
BlockExecutionContext blkCtx,
in BlockExecutionContext blkCtx,
Transaction currentTx,
int index,
BlockReceiptsTracer receiptsTracer,
Expand All @@ -90,7 +90,7 @@ private BlockProcessor.TxAction ProcessAccountAbstractionTransaction(
{
int snapshot = receiptsTracer.TakeSnapshot();

BlockProcessor.TxAction action = ProcessTransaction(block, blkCtx, currentTx, index, receiptsTracer, processingOptions, transactionsInBlock, false);
BlockProcessor.TxAction action = ProcessTransaction(block, in blkCtx, currentTx, index, receiptsTracer, processingOptions, transactionsInBlock, false);
if (action != BlockProcessor.TxAction.Add)
{
return action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public virtual TxReceipt[] ProcessTransactions(Block block, ProcessingOptions pr
BlockExecutionContext blkCtx = new(block.Header);
foreach (Transaction currentTx in transactions)
{
TxAction action = ProcessTransaction(block, blkCtx, currentTx, i++, receiptsTracer, processingOptions, transactionsInBlock);
TxAction action = ProcessTransaction(block, in blkCtx, currentTx, i++, receiptsTracer, processingOptions, transactionsInBlock);
if (action == TxAction.Stop) break;
}

Expand All @@ -91,7 +91,7 @@ public virtual TxReceipt[] ProcessTransactions(Block block, ProcessingOptions pr

protected TxAction ProcessTransaction(
Block block,
BlockExecutionContext blkCtx,
in BlockExecutionContext blkCtx,
Transaction currentTx,
int index,
BlockReceiptsTracer receiptsTracer,
Expand All @@ -109,7 +109,7 @@ protected TxAction ProcessTransaction(
}
else
{
_transactionProcessor.ProcessTransaction(blkCtx, currentTx, receiptsTracer, processingOptions, _stateProvider);
_transactionProcessor.ProcessTransaction(in blkCtx, currentTx, receiptsTracer, processingOptions, _stateProvider);

if (addToBlock)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public TxReceipt[] ProcessTransactions(Block block, ProcessingOptions processing
for (int i = 0; i < block.Transactions.Length; i++)
{
Transaction currentTx = block.Transactions[i];
ProcessTransaction(blkCtx, currentTx, i, receiptsTracer, processingOptions);
ProcessTransaction(in blkCtx, currentTx, i, receiptsTracer, processingOptions);
}
return receiptsTracer.TxReceipts.ToArray();
}

private void ProcessTransaction(BlockExecutionContext blkCtx, Transaction currentTx, int index, BlockReceiptsTracer receiptsTracer, ProcessingOptions processingOptions)
private void ProcessTransaction(in BlockExecutionContext blkCtx, Transaction currentTx, int index, BlockReceiptsTracer receiptsTracer, ProcessingOptions processingOptions)
{
_transactionProcessor.ProcessTransaction(blkCtx, currentTx, receiptsTracer, processingOptions, _stateProvider);
_transactionProcessor.ProcessTransaction(in blkCtx, currentTx, receiptsTracer, processingOptions, _stateProvider);
TransactionProcessed?.Invoke(this, new TxProcessedEventArgs(index, currentTx, receiptsTracer.TxReceipts[index]));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Nethermind.Consensus.Processing;
internal static class TransactionProcessorAdapterExtensions
{
public static void ProcessTransaction(this ITransactionProcessorAdapter transactionProcessor,
BlockExecutionContext blkCtx,
in BlockExecutionContext blkCtx,
Transaction currentTx,
BlockReceiptsTracer receiptsTracer,
ProcessingOptions processingOptions,
Expand All @@ -24,7 +24,7 @@ public static void ProcessTransaction(this ITransactionProcessorAdapter transact
}

using ITxTracer tracer = receiptsTracer.StartNewTxTrace(currentTx);
transactionProcessor.Execute(currentTx, blkCtx, receiptsTracer);
transactionProcessor.Execute(currentTx, in blkCtx, receiptsTracer);
receiptsTracer.EndTxTrace();
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/ExecutionEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ExecutionEnvironment
Address caller,
Address? codeSource,
ReadOnlyMemory<byte> inputData,
TxExecutionContext txExecutionContext,
in TxExecutionContext txExecutionContext,
UInt256 transferValue,
UInt256 value,
int callDepth = 0)
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 @@ -89,7 +89,7 @@ private bool TryExecutableTransaction(Transaction transaction, BlockHeader block
transaction.GasLimit = gasLimit;

BlockExecutionContext blCtx = new(block);
_transactionProcessor.CallAndRestore(transaction, blCtx, tracer.WithCancellation(token));
_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 @@ -15,7 +15,7 @@ public BuildUpTransactionProcessorAdapter(ITransactionProcessor transactionProce
_transactionProcessor = transactionProcessor;
}

public void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.BuildUp(transaction, blkCtx, txTracer);
public void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.BuildUp(transaction, in blkCtx, txTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CallAndRestoreTransactionProcessorAdapter(ITransactionProcessor transacti
_transactionProcessor = transactionProcessor;
}

public void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.CallAndRestore(transaction, blkCtx, txTracer);
public void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.CallAndRestore(transaction, in blkCtx, txTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public ChangeableTransactionProcessorAdapter(ITransactionProcessor transactionPr
TransactionProcessor = transactionProcessor;
}

public void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer)
public void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer)
{
CurrentAdapter.Execute(transaction, blkCtx, txTracer);
CurrentAdapter.Execute(transaction, in blkCtx, txTracer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ExecuteTransactionProcessorAdapter(ITransactionProcessor transactionProce
_transactionProcessor = transactionProcessor;
}

public void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.Execute(transaction, blkCtx, txTracer);
public void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.Execute(transaction, in blkCtx, txTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ public interface ITransactionProcessor
/// <summary>
/// Execute transaction, commit state
/// </summary>
void Execute(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer);
void Execute(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);

/// <summary>
/// Call transaction, rollback state
/// </summary>
void CallAndRestore(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer);
void CallAndRestore(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);

/// <summary>
/// Execute transaction, keep the state uncommitted
/// </summary>
void BuildUp(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer);
void BuildUp(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);

/// <summary>
/// Call transaction, no validations, commit state
/// Will NOT charge gas from sender account, so stateDiff will miss gas fee
/// </summary>
void Trace(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer);
void Trace(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Nethermind.Evm.TransactionProcessing
{
public interface ITransactionProcessorAdapter
{
void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer);
void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public ReadOnlyTransactionProcessor(ITransactionProcessor transactionProcessor,
_stateProvider.StateRoot = startState ?? throw new ArgumentNullException(nameof(startState));
}

public void Execute(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.Execute(transaction, blCtx, txTracer);
public void Execute(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.Execute(transaction, in blCtx, txTracer);

public void CallAndRestore(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.CallAndRestore(transaction, blCtx, txTracer);
public void CallAndRestore(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.CallAndRestore(transaction, in blCtx, txTracer);

public void BuildUp(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.BuildUp(transaction, blCtx, txTracer);
public void BuildUp(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.BuildUp(transaction, in blCtx, txTracer);

public void Trace(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.Trace(transaction, blCtx, txTracer);
public void Trace(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer) =>
_transactionProcessor.Trace(transaction, in blCtx, txTracer);


public bool IsContractDeployed(Address address) => _stateProvider.IsContract(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public TraceTransactionProcessorAdapter(ITransactionProcessor transactionProcess
_transactionProcessor = transactionProcessor;
}

public void Execute(Transaction transaction, BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.Trace(transaction, blkCtx, txTracer);
public void Execute(Transaction transaction, in BlockExecutionContext blkCtx, ITxTracer txTracer) =>
_transactionProcessor.Trace(transaction, in blkCtx, txTracer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,30 @@ public TransactionProcessor(
Ecdsa = new EthereumEcdsa(specProvider.ChainId, logManager);
}

public void CallAndRestore(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer)
public void CallAndRestore(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer)
{
Execute(transaction, blCtx, txTracer, ExecutionOptions.CommitAndRestore);
Execute(transaction, in blCtx, txTracer, ExecutionOptions.CommitAndRestore);
}

public void BuildUp(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer)
public void BuildUp(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer)
{
// we need to treat the result of previous transaction as the original value of next transaction
// when we do not commit
WorldState.TakeSnapshot(true);
Execute(transaction, blCtx, txTracer, ExecutionOptions.None);
Execute(transaction, in blCtx, txTracer, ExecutionOptions.None);
}

public void Execute(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer)
public void Execute(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer)
{
Execute(transaction, blCtx, txTracer, ExecutionOptions.Commit);
Execute(transaction, in blCtx, txTracer, ExecutionOptions.Commit);
}

public void Trace(Transaction transaction, BlockExecutionContext blCtx, ITxTracer txTracer)
public void Trace(Transaction transaction, in BlockExecutionContext blCtx, ITxTracer txTracer)
{
Execute(transaction, blCtx, txTracer, ExecutionOptions.NoValidation);
Execute(transaction, in blCtx, txTracer, ExecutionOptions.NoValidation);
}

protected virtual void Execute(Transaction tx, BlockExecutionContext blCtx, ITxTracer tracer, ExecutionOptions opts)
protected virtual void Execute(Transaction tx, in BlockExecutionContext blCtx, ITxTracer tracer, ExecutionOptions opts)
{
BlockHeader header = blCtx.Header;
IReleaseSpec spec = SpecProvider.GetSpec(header);
Expand Down Expand Up @@ -147,7 +147,7 @@ protected virtual void Execute(Transaction tx, BlockExecutionContext blCtx, ITxT
if (commit)
WorldState.Commit(spec, tracer.IsTracingState ? tracer : NullTxTracer.Instance);

ExecutionEnvironment env = BuildExecutionEnvironmnet(tx, blCtx, spec, tracer, opts, effectiveGasPrice);
ExecutionEnvironment env = BuildExecutionEnvironmnet(tx, in blCtx, spec, tracer, opts, effectiveGasPrice);

long gasAvailable = tx.GasLimit - intrinsicGas;
if (!ExecuteEVMCall(tx, header, spec, tracer, opts, gasAvailable, env, out TransactionSubstate? substate, out long spentGas, out byte statusCode))
Expand Down Expand Up @@ -433,15 +433,15 @@ protected virtual bool IncrementNonce(Transaction tx, BlockHeader header, IRelea
}

protected virtual ExecutionEnvironment BuildExecutionEnvironmnet(
Transaction tx, BlockExecutionContext blCtx, IReleaseSpec spec, ITxTracer tracer, ExecutionOptions opts,
Transaction tx, in BlockExecutionContext blCtx, IReleaseSpec spec, ITxTracer tracer, ExecutionOptions opts,
in UInt256 effectiveGasPrice)
{
Address recipient = tx.GetRecipient(tx.IsContractCreation ? WorldState.GetNonce(tx.SenderAddress) : 0) ??
// this transaction is not a contract creation so it should have the recipient known and not null
throw new InvalidDataException("Recipient has not been resolved properly before tx execution");

TxExecutionContext executionContext =
new(blCtx, tx.SenderAddress, effectiveGasPrice, tx.BlobVersionedHashes);
new(in blCtx, tx.SenderAddress, effectiveGasPrice, tx.BlobVersionedHashes);

CodeInfo codeInfo = tx.IsContractCreation ? new(tx.Data.AsArray())
: VirtualMachine.GetCachedCodeInfo(WorldState, recipient, spec);
Expand All @@ -450,7 +450,7 @@ protected virtual ExecutionEnvironment BuildExecutionEnvironmnet(

return new ExecutionEnvironment
(
txExecutionContext: executionContext,
txExecutionContext: in executionContext,
value: tx.Value,
transferValue: tx.Value,
caller: tx.SenderAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/TxExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public readonly struct TxExecutionContext
public UInt256 GasPrice { get; }
public byte[][]? BlobVersionedHashes { get; }

public TxExecutionContext(BlockExecutionContext blockExecutionContext, Address origin, in UInt256 gasPrice, byte[][] blobVersionedHashes)
public TxExecutionContext(in BlockExecutionContext blockExecutionContext, Address origin, in UInt256 gasPrice, byte[][] blobVersionedHashes)
{
BlockExecutionContext = blockExecutionContext;
Origin = origin;
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(

ExecutionEnvironment callEnv = new
(
txExecutionContext: env.TxExecutionContext,
txExecutionContext: in env.TxExecutionContext,
callDepth: env.CallDepth + 1,
caller: caller,
codeSource: codeSource,
Expand Down Expand Up @@ -2535,7 +2535,7 @@ private EvmExceptionType InstructionSelfDestruct<TTracing>(EvmState vmState, ref

ExecutionEnvironment callEnv = new
(
txExecutionContext: env.TxExecutionContext,
txExecutionContext: in env.TxExecutionContext,
callDepth: env.CallDepth + 1,
caller: env.ExecutingAccount,
executingAccount: contractAddress,
Expand Down
Loading

0 comments on commit 4460f7a

Please sign in to comment.