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

[4844] Fix Data Gas Price Calculation #5680

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
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.services;

import static com.google.common.base.Preconditions.checkArgument;
import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -112,7 +113,9 @@ private void trace(final Block block, final OperationTracer tracer) {
.getFeeMarket()
.dataPricePerGas(
maybeParentHeader
.flatMap(BlockHeader::getExcessDataGas)
.map(
parent ->
calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));
final TransactionProcessingResult result =
transactionProcessor.processTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
Expand Down Expand Up @@ -90,7 +92,9 @@ public TransactionTrace apply(final TransactionTrace transactionTrace) {
protocolSpec
.getFeeMarket()
.dataPricePerGas(
maybeParentHeader.flatMap(BlockHeader::getExcessDataGas).orElse(DataGas.ZERO));
maybeParentHeader
.map(parent -> calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));
final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(header, blockchain);
result =
transactionProcessor.processTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -55,7 +57,7 @@ public Optional<BlockTrace> block(
.dataPricePerGas(
blockchain
.getBlockHeader(header.getParentHash())
.flatMap(BlockHeader::getExcessDataGas)
.map(parent -> calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));

final List<TransactionTrace> transactionTraces =
Expand Down Expand Up @@ -89,7 +91,7 @@ public <T> Optional<T> beforeTransactionInBlock(
.dataPricePerGas(
blockchain
.getBlockHeader(header.getParentHash())
.flatMap(BlockHeader::getExcessDataGas)
.map(parent -> calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));

for (final Transaction transaction : body.getTransactions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor;

import static java.util.function.Predicate.isEqual;
import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -121,7 +122,7 @@ public List<String> traceTransactionToFile(
.dataPricePerGas(
blockchain
.getBlockHeader(header.getParentHash())
.flatMap(BlockHeader::getExcessDataGas)
.map(parent -> calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));
for (int i = 0; i < body.getTransactions().size(); i++) {
((StackedUpdater<?, ?>) stackedUpdater).markTransactionBoundary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import static com.google.common.base.Preconditions.checkArgument;
import static org.hyperledger.besu.ethereum.api.query.cache.TransactionLogBloomCacher.BLOCKS_PER_BLOOM_CACHE;
import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

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.api.ApiConfiguration;
Expand Down Expand Up @@ -695,7 +695,8 @@ private Optional<Wei> getDataGasPrice(
parentHeader ->
protocolSpec
.getFeeMarket()
.dataPricePerGas(parentHeader.getExcessDataGas().orElse(DataGas.ZERO)));
.dataPricePerGas(
calculateExcessDataGasForParent(protocolSpec, parentHeader)));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -35,6 +36,7 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.tracing.StandardJsonTracer;
import org.hyperledger.besu.evm.worldstate.StackedUpdater;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class TransactionTracerTest {
@Mock private DebugOperationTracer tracer;

@Mock private ProtocolSpec protocolSpec;
@Mock private GasCalculator gasCalculator;

@Mock private Tracer.TraceableState mutableWorldState;

Expand Down Expand Up @@ -112,6 +115,8 @@ public void setUp() throws Exception {
when(protocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
when(protocolSpec.getBadBlocksManager()).thenReturn(new BadBlockManager());
when(protocolSpec.getGasCalculator()).thenReturn(gasCalculator);
when(gasCalculator.computeExcessDataGas(anyLong(), anyLong())).thenReturn(0L);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.blockcreation;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
Expand Down Expand Up @@ -169,10 +171,10 @@ protected BlockCreationResult createBlock(
createPendingBlockHeader(timestamp, maybePrevRandao, newProtocolSpec);
final Address miningBeneficiary =
miningBeneficiaryCalculator.getMiningBeneficiary(processableBlockHeader.getNumber());
final Wei dataGasPrice =
Wei dataGasPrice =
newProtocolSpec
.getFeeMarket()
.dataPricePerGas(parentHeader.getExcessDataGas().orElse(DataGas.ZERO));
.dataPricePerGas(calculateExcessDataGasForParent(newProtocolSpec, parentHeader));

throwIfStopped();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,23 @@ private static long parseUnsignedLong(final String value) {
private static boolean isShanghaiAtGenesis(final GenesisConfigFile genesis) {
final OptionalLong shanghaiTimestamp = genesis.getConfigOptions().getShanghaiTime();
if (shanghaiTimestamp.isPresent()) {
return shanghaiTimestamp.getAsLong() == genesis.getTimestamp();
return genesis.getTimestamp() >= shanghaiTimestamp.getAsLong();
}
return false;
}

private static boolean isCancunAtGenesis(final GenesisConfigFile genesis) {
final OptionalLong cancunTimestamp = genesis.getConfigOptions().getCancunTime();
if (cancunTimestamp.isPresent()) {
return cancunTimestamp.getAsLong() == genesis.getTimestamp();
return genesis.getTimestamp() >= cancunTimestamp.getAsLong();
}
return false;
}

private static boolean isExperimentalEipsTimeAtGenesis(final GenesisConfigFile genesis) {
final OptionalLong experimentalEipsTime = genesis.getConfigOptions().getExperimentalEipsTime();
if (experimentalEipsTime.isPresent()) {
return experimentalEipsTime.getAsLong() == genesis.getTimestamp();
return genesis.getTimestamp() >= experimentalEipsTime.getAsLong();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public String toString() {
if (withdrawalsRoot != null) {
sb.append("withdrawalsRoot=").append(withdrawalsRoot).append(", ");
}
if (dataGasUsed != null) {
sb.append("dataGasUsed=").append(dataGasUsed).append(", ");
}
if (excessDataGas != null) {
sb.append("excessDataGas=").append(excessDataGas).append(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public class BlockHeaderBuilder {
// instead of an invalid identifier such as -1.
private OptionalLong nonce = OptionalLong.empty();

private Long dataGasUsed;

private Long dataGasUsed = null;
private DataGas excessDataGas = null;

public static BlockHeaderBuilder create() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/
package org.hyperledger.besu.ethereum.mainnet;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockProcessingOutputs;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
Expand Down Expand Up @@ -112,14 +113,19 @@ public BlockProcessingResult processBlock(
final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(blockHeader, blockchain);
final Address miningBeneficiary =
miningBeneficiaryCalculator.calculateBeneficiary(blockHeader);
final Wei dataGasPrice =
protocolSpec
.getFeeMarket()
.dataPricePerGas(
blockchain
.getBlockHeader(blockHeader.getParentHash())
.flatMap(BlockHeader::getExcessDataGas)
.orElse(DataGas.ZERO));

Optional<BlockHeader> maybeParentHeader =
blockchain.getBlockHeader(blockHeader.getParentHash());

Wei dataGasPrice =
maybeParentHeader
.map(
(parentHeader) ->
protocolSpec
.getFeeMarket()
.dataPricePerGas(
calculateExcessDataGasForParent(protocolSpec, parentHeader)))
.orElse(Wei.ZERO);

final TransactionProcessingResult result =
transactionProcessor.processTransaction(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet.feemarket;

import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;

/** Calculates the excess data gas for a parent block header. */
public class ExcessDataGasCalculator {
/**
* public class ExcessDataGasCalculator { /** Calculates the excess data gas for a parent block
* header.
*
* @param protocolSpec The protocol specification.
* @param parentHeader The parent block header.
* @return The excess data gas.
*/
public static DataGas calculateExcessDataGasForParent(
final ProtocolSpec protocolSpec, final BlockHeader parentHeader) {
// Blob Data Excess
long headerExcess =
protocolSpec
.getGasCalculator()
.computeExcessDataGas(
parentHeader.getExcessDataGas().map(DataGas::toLong).orElse(0L),
parentHeader.getDataGasUsed().orElse(0L));
return DataGas.of(headerExcess);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.transaction;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessDataGasCalculator.calculateExcessDataGasForParent;

import org.hyperledger.besu.crypto.SECPSignature;
import org.hyperledger.besu.crypto.SignatureAlgorithm;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
Expand Down Expand Up @@ -231,7 +233,9 @@ public Optional<TransactionSimulatorResult> processWithWorldUpdater(
protocolSpec
.getFeeMarket()
.dataPricePerGas(
maybeParentHeader.flatMap(BlockHeader::getExcessDataGas).orElse(DataGas.ZERO));
maybeParentHeader
.map(parent -> calculateExcessDataGasForParent(protocolSpec, parent))
.orElse(DataGas.ZERO));

final Transaction transaction = maybeTransaction.get();
final TransactionProcessingResult result =
Expand Down
Loading