Skip to content

Commit

Permalink
[Interop-4844] Data gas accounting (hyperledger#4998)
Browse files Browse the repository at this point in the history
merge of hyperledger#4992 into interop feature branch

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 authored Jan 25, 2023
1 parent 5fa9433 commit 949e3fe
Show file tree
Hide file tree
Showing 27 changed files with 742 additions and 318 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
### Additions and Improvements
- Added option to evm CLI tool to allow code execution at specific forks [#4913](https://github.com/hyperledger/besu/pull/4913)
- Improve get account performance by using the world state updater cache [#4897](https://github.com/hyperledger/besu/pull/4897)
- Add new KZG precompile and option to override the trusted setup being used [#4822](https://github.com/hyperledger/besu/issues/4822)
- Add implementation for eth_createAccessList RPC method [#4942](https://github.com/hyperledger/besu/pull/4942)

### Bug Fixes
Expand All @@ -24,6 +23,7 @@
- Send only hash announcement for blob transaction type [#4940](https://github.com/hyperledger/besu/pull/4940)
- Add `excess_data_gas` field to block header [#4958](https://github.com/hyperledger/besu/pull/4958)
- Add `max_fee_per_data_gas` field to transaction [#4970](https://github.com/hyperledger/besu/pull/4970)
- Gas accounting for EIP-4844 [#4992](https://github.com/hyperledger/besu/pull/4992)

### Bug Fixes
- Mitigation fix for stale bonsai code storage leading to log rolling issues on contract recreates [#4906](https://github.com/hyperledger/besu/pull/4906)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package org.hyperledger.besu.ethereum.blockcreation;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
Expand All @@ -43,6 +45,7 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.evm.account.EvmAccount;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.data.TransactionType;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;

Expand Down Expand Up @@ -209,6 +212,10 @@ protected BlockCreationResult createBlock(

throwIfStopped();

final DataGas newExcessDataGas = computeExcessDataGas(transactionResults, newProtocolSpec);

throwIfStopped();

final SealableBlockHeader sealableBlockHeader =
BlockHeaderBuilder.create()
.populateFrom(processableBlockHeader)
Expand All @@ -224,6 +231,7 @@ protected BlockCreationResult createBlock(
withdrawalsCanBeProcessed
? BodyValidation.withdrawalsRoot(maybeWithdrawals.get())
: null)
.excessDataGas(newExcessDataGas)
.buildSealableBlockHeader();

final BlockHeader blockHeader = createFinalBlockHeader(sealableBlockHeader);
Expand All @@ -245,6 +253,26 @@ protected BlockCreationResult createBlock(
}
}

private DataGas computeExcessDataGas(
BlockTransactionSelector.TransactionSelectionResults transactionResults,
ProtocolSpec newProtocolSpec) {

if (newProtocolSpec.getFeeMarket().implementsDataFee()) {
final var gasCalculator = newProtocolSpec.getGasCalculator();
final int newBlobsCount =
transactionResults.getTransactionsByType(TransactionType.BLOB).stream()
.map(tx -> tx.getVersionedHashes().orElseThrow())
.mapToInt(List::size)
.sum();
// casting parent excess data gas to long since for the moment it should be well below that
// limit
return DataGas.of(
gasCalculator.computeExcessDataGas(
parentHeader.getExcessDataGas().map(DataGas::toLong).orElse(0L), newBlobsCount));
}
return null;
}

private BlockTransactionSelector.TransactionSelectionResults selectTransactions(
final ProcessableBlockHeader processableBlockHeader,
final MutableWorldState disposableWorldState,
Expand All @@ -269,7 +297,9 @@ private BlockTransactionSelector.TransactionSelectionResults selectTransactions(
minBlockOccupancyRatio,
isCancelled::get,
miningBeneficiary,
protocolSpec.getFeeMarket());
protocolSpec.getFeeMarket(),
protocolSpec.getGasCalculator(),
protocolSpec.getGasLimitCalculator());

if (transactions.isPresent()) {
return selector.evaluateTransactions(transactions.get());
Expand Down Expand Up @@ -312,13 +342,13 @@ private ProcessableBlockHeader createPendingBlockHeader(
final Optional<Bytes32> maybePrevRandao,
final ProtocolSpec protocolSpec) {
final long newBlockNumber = parentHeader.getNumber() + 1;
long gasLimit =
protocolSpec
.getGasLimitCalculator()
.nextGasLimit(
parentHeader.getGasLimit(),
targetGasLimitSupplier.get().orElse(parentHeader.getGasLimit()),
newBlockNumber);
final GasLimitCalculator gasLimitCalculator = protocolSpec.getGasLimitCalculator();

final long gasLimit =
gasLimitCalculator.nextGasLimit(
parentHeader.getGasLimit(),
targetGasLimitSupplier.get().orElse(parentHeader.getGasLimit()),
newBlockNumber);

final DifficultyCalculator difficultyCalculator = protocolSpec.getDifficultyCalculator();
final BigInteger difficulty =
Expand Down
Loading

0 comments on commit 949e3fe

Please sign in to comment.