Skip to content

Commit

Permalink
Added comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Jan 16, 2024
1 parent d4dcd1c commit c08dccc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
17 changes: 8 additions & 9 deletions go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func NewBatchExecutor(
}
}

// estimateL1Fees - this function modifies the state db according to the transactions contained within the batch context
// in order to subtract gas fees from the balance. It returns a list of the transactions that have prepaid for their L1
// publishing costs.
func (executor *batchExecutor) estimateL1Fees(stateDB *state.StateDB, context *BatchExecutionContext) (common.L2PricedTransactions, common.L2PricedTransactions) {
// filterTransactionsWithSufficientFunds - this function estimates hte l1 fees for the transaction in a given batch execution context. It does so by taking the price of the
// pinned L1 block and using it as the cost per gas for the estimated gas of the calldata encoding of a transaction. It filters out any transactions that cannot afford to pay for their L1
// publishing cost.
func (executor *batchExecutor) filterTransactionsWithSufficientFunds(stateDB *state.StateDB, context *BatchExecutionContext) (common.L2PricedTransactions, common.L2PricedTransactions) {
transactions := make(common.L2PricedTransactions, 0)
freeTransactions := make(common.L2PricedTransactions, 0)
block, _ := executor.storage.FetchBlock(context.BlockPtr)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
crossChainTransactions := executor.crossChainProcessors.Local.CreateSyntheticTransactions(messages, stateDB)
executor.crossChainProcessors.Local.ExecuteValueTransfers(transfers, stateDB)

transactionsToProcess, freeTransactions := executor.estimateL1Fees(stateDB, context)
transactionsToProcess, freeTransactions := executor.filterTransactionsWithSufficientFunds(stateDB, context)

xchainTxs := make(common.L2PricedTransactions, 0)
for _, xTx := range crossChainTransactions {
Expand All @@ -175,19 +175,19 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
})
}

xchainTxs = append(xchainTxs, freeTransactions...)
syntheticTransactions := append(xchainTxs, freeTransactions...)

successfulTxs, excludedTxs, txReceipts, err := executor.processTransactions(batch, 0, transactionsToProcess, stateDB, context.ChainConfig, false)
if err != nil {
return nil, fmt.Errorf("could not process transactions. Cause: %w", err)
}

ccSuccessfulTxs, _, ccReceipts, err := executor.processTransactions(batch, len(successfulTxs), xchainTxs, stateDB, context.ChainConfig, true)
ccSuccessfulTxs, _, ccReceipts, err := executor.processTransactions(batch, len(successfulTxs), syntheticTransactions, stateDB, context.ChainConfig, true)
if err != nil {
return nil, err
}

if err = executor.verifyInboundCrossChainTransactions(xchainTxs, ccSuccessfulTxs, ccReceipts); err != nil {
if err = executor.verifyInboundCrossChainTransactions(syntheticTransactions, ccSuccessfulTxs, ccReceipts); err != nil {
return nil, fmt.Errorf("batch computation failed due to cross chain messages. Cause: %w", err)
}

Expand Down Expand Up @@ -218,7 +218,6 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail

// the logs and receipts produced by the EVM have the wrong hash which must be adjusted
for _, receipt := range allReceipts {
executor.logger.Info("New receipt! ", log.TxKey, receipt.TxHash)
receipt.BlockHash = copyBatch.Hash()
for _, l := range receipt.Logs {
l.BlockHash = copyBatch.Hash()
Expand Down
13 changes: 11 additions & 2 deletions go/enclave/evm/evm_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,21 @@ func executeTransaction(
hasL1Cost := l1cost.Cmp(big.NewInt(0)) != 0

if hasL1Cost {
l1Gas.Div(l1cost, header.BaseFee)
l1Gas.Add(l1Gas, big.NewInt(1))
l1Gas.Div(l1cost, header.BaseFee) // TotalCost/CostPerGas = Gas
l1Gas.Add(l1Gas, big.NewInt(1)) // Cover from leftover from the division

// The gas limit of the transaction (evm message) should always be higher than the gas overhead
// used to cover the l1 cost
if msg.GasLimit < l1Gas.Uint64() {
return nil, fmt.Errorf("gas limit set by user is too low to pay for execution and l1 fees. Want at least: %d have: %d", l1Gas, msg.GasLimit)
}

// Remove the gas overhead for l1 publishing from the gas limit in order to define
// the actual gas limit for execution
msg.GasLimit -= l1Gas.Uint64()

// Remove the l1 cost from the sender
// and pay it to the coinbase of the batch
statedb.SubBalance(msg.From, l1cost)
statedb.AddBalance(header.Coinbase, l1cost)

Check failure on line 152 in go/enclave/evm/evm_facade.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
Expand All @@ -150,6 +157,8 @@ func executeTransaction(
vmenv := vm.NewEVM(blockContext, vm.TxContext{BlobHashes: tx.Tx.BlobHashes()}, statedb, config, cfg)
receipt, err := applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx.Tx, usedGas, vmenv)
if err != nil {
// If the transaction has l1 cost, then revert the funds exchange
// as it will not be published on error (no receipt condition)
if hasL1Cost {
statedb.SubBalance(header.Coinbase, l1cost)
statedb.AddBalance(msg.From, l1cost)
Expand Down
4 changes: 3 additions & 1 deletion go/host/rpc/clientapi/client_api_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ func (api *EthereumAPI) FeeHistory(context.Context, string, rpc.BlockNumber, []f
}

for _, header := range batches {
feeHist.GasUsedRatio = append(feeHist.GasUsedRatio, 0.9) // todo @siliev - fix when baseFee is dynamic
// 0.9 - This number represents how full the block is. As we dont have a dynamic base fee, we tell whomever is requesting that
// we expect the baseFee to increase, rather than decrease in order to avoid underpriced transactions.
feeHist.GasUsedRatio = append(feeHist.GasUsedRatio, 0.9)
feeHist.BaseFee = append(feeHist.BaseFee, (*hexutil.Big)(header.BaseFee))
}
return feeHist, nil
Expand Down

0 comments on commit c08dccc

Please sign in to comment.