From 646c5a3bc63427cbe098139b9a4fa73fac50153e Mon Sep 17 00:00:00 2001 From: Stefan Pingel <16143240+pinges@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:25:46 +1000 Subject: [PATCH] clean up the ProcessableBlockHeader (#6117) * clean up Signed-off-by: stefan.pingel@consensys.net --- .../blockcreation/AbstractBlockCreator.java | 11 +- .../besu/ethereum/core/BlockHeader.java | 12 +- .../ethereum/core/BlockHeaderBuilder.java | 4 - .../ethereum/core/ProcessableBlockHeader.java | 30 ++--- .../ethereum/core/SealableBlockHeader.java | 26 ++++- .../referencetests/ReferenceTestEnv.java | 2 +- plugin-api/build.gradle | 2 +- .../besu/plugin/data/BlockHeader.java | 81 +------------ .../plugin/data/ProcessableBlockHeader.java | 108 ++++++++++++++++++ 9 files changed, 159 insertions(+), 117 deletions(-) create mode 100644 plugin-api/src/main/java/org/hyperledger/besu/plugin/data/ProcessableBlockHeader.java diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java index a8bb17e93cd..1954e3e4008 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java @@ -172,10 +172,6 @@ protected BlockCreationResult createBlock( timestamp, maybePrevRandao, maybeParentBeaconBlockRoot, newProtocolSpec); final Address miningBeneficiary = miningBeneficiaryCalculator.getMiningBeneficiary(processableBlockHeader.getNumber()); - Wei blobGasPrice = - newProtocolSpec - .getFeeMarket() - .blobGasPricePerGas(calculateExcessBlobGasForParent(newProtocolSpec, parentHeader)); throwIfStopped(); @@ -193,7 +189,6 @@ protected BlockCreationResult createBlock( disposableWorldState, maybeTransactions, miningBeneficiary, - blobGasPrice, newProtocolSpec); transactionResults.logSelectionStats(); @@ -321,7 +316,6 @@ private TransactionSelectionResults selectTransactions( final MutableWorldState disposableWorldState, final Optional> transactions, final Address miningBeneficiary, - final Wei blobGasPrice, final ProtocolSpec protocolSpec) throws RuntimeException { final MainnetTransactionProcessor transactionProcessor = protocolSpec.getTransactionProcessor(); @@ -329,6 +323,11 @@ private TransactionSelectionResults selectTransactions( final AbstractBlockProcessor.TransactionReceiptFactory transactionReceiptFactory = protocolSpec.getTransactionReceiptFactory(); + Wei blobGasPrice = + protocolSpec + .getFeeMarket() + .blobGasPricePerGas(calculateExcessBlobGasForParent(protocolSpec, parentHeader)); + final BlockTransactionSelector selector = new BlockTransactionSelector( miningParameters, diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeader.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeader.java index 26a8a41d463..7aa5770f3fc 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeader.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeader.java @@ -169,9 +169,9 @@ public void writeTo(final RLPOutput out) { if (withdrawalsRoot != null) { out.writeBytes(withdrawalsRoot); } - if (excessBlobGas.isPresent() && blobGasUsed.isPresent()) { - out.writeLongScalar(blobGasUsed.get()); - out.writeUInt64Scalar(excessBlobGas.get()); + if (excessBlobGas != null && blobGasUsed != null) { + out.writeLongScalar(blobGasUsed); + out.writeUInt64Scalar(excessBlobGas); } if (parentBeaconBlockRoot != null) { out.writeBytes(parentBeaconBlockRoot); @@ -278,8 +278,10 @@ public String toString() { if (withdrawalsRoot != null) { sb.append("withdrawalsRoot=").append(withdrawalsRoot).append(", "); } - blobGasUsed.ifPresent(aLong -> sb.append("blobGasUsed=").append(aLong).append(", ")); - excessBlobGas.ifPresent(blobGas -> sb.append("excessBlobGas=").append(blobGas).append(", ")); + if (blobGasUsed != null && excessBlobGas != null) { + sb.append("blobGasUsed=").append(blobGasUsed).append(", "); + sb.append("excessBlobGas=").append(excessBlobGas).append(", "); + } if (parentBeaconBlockRoot != null) { sb.append("parentBeaconBlockRoot=").append(parentBeaconBlockRoot).append(", "); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeaderBuilder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeaderBuilder.java index fc38b663ea9..b23c7a46c78 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeaderBuilder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeaderBuilder.java @@ -194,8 +194,6 @@ public ProcessableBlockHeader buildProcessableBlockHeader() { timestamp, baseFee, mixHashOrPrevRandao, - blobGasUsed, - excessBlobGas, parentBeaconBlockRoot); } @@ -261,8 +259,6 @@ public BlockHeaderBuilder populateFrom(final ProcessableBlockHeader processableB timestamp(processableBlockHeader.getTimestamp()); baseFee(processableBlockHeader.getBaseFee().orElse(null)); processableBlockHeader.getPrevRandao().ifPresent(this::prevRandao); - processableBlockHeader.getBlobGasUsed().ifPresent(this::blobGasUsed); - processableBlockHeader.getExcessBlobGas().ifPresent(this::excessBlobGas); processableBlockHeader.getParentBeaconBlockRoot().ifPresent(this::parentBeaconBlockRoot); return this; } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/ProcessableBlockHeader.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/ProcessableBlockHeader.java index dc5073c1bcb..75393315428 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/ProcessableBlockHeader.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/ProcessableBlockHeader.java @@ -15,7 +15,6 @@ package org.hyperledger.besu.ethereum.core; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.datatypes.BlobGas; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.evm.frame.BlockValues; @@ -26,7 +25,8 @@ import org.apache.tuweni.bytes.Bytes32; /** A block header capable of being processed. */ -public class ProcessableBlockHeader implements BlockValues { +public class ProcessableBlockHeader + implements BlockValues, org.hyperledger.besu.plugin.data.ProcessableBlockHeader { protected final Hash parentHash; @@ -44,10 +44,6 @@ public class ProcessableBlockHeader implements BlockValues { protected final Wei baseFee; // prevRandao is included for post-merge blocks protected final Bytes32 mixHashOrPrevRandao; - // blobGasUsed is included for Cancun - protected final Optional blobGasUsed; - // excessBlogGas is included for Cancun - protected final Optional excessBlobGas; // parentBeaconBlockRoot is included for Cancun protected final Bytes32 parentBeaconBlockRoot; @@ -60,8 +56,6 @@ protected ProcessableBlockHeader( final long timestamp, final Wei baseFee, final Bytes32 mixHashOrPrevRandao, - final Long blobGasUsed, - final BlobGas excessBlobGas, final Bytes32 parentBeaconBlockRoot) { this.parentHash = parentHash; this.coinbase = coinbase; @@ -71,8 +65,6 @@ protected ProcessableBlockHeader( this.timestamp = timestamp; this.baseFee = baseFee; this.mixHashOrPrevRandao = mixHashOrPrevRandao; - this.blobGasUsed = Optional.ofNullable(blobGasUsed); - this.excessBlobGas = Optional.ofNullable(excessBlobGas); this.parentBeaconBlockRoot = parentBeaconBlockRoot; } @@ -81,6 +73,7 @@ protected ProcessableBlockHeader( * * @return the block parent block hash */ + @Override public Hash getParentHash() { return parentHash; } @@ -90,6 +83,7 @@ public Hash getParentHash() { * * @return the block coinbase address */ + @Override public Address getCoinbase() { return coinbase; } @@ -99,6 +93,7 @@ public Address getCoinbase() { * * @return the block difficulty */ + @Override public Difficulty getDifficulty() { return difficulty; } @@ -168,18 +163,17 @@ public Bytes32 getMixHashOrPrevRandao() { * * @return the raw bytes of the prevRandao field */ + @Override public Optional getPrevRandao() { return Optional.ofNullable(mixHashOrPrevRandao); } - public Optional getExcessBlobGas() { - return excessBlobGas; - } - - public Optional getBlobGasUsed() { - return blobGasUsed; - } - + /** + * Returns the parent beacon block root. + * + * @return the parent beacon block root. + */ + @Override public Optional getParentBeaconBlockRoot() { return Optional.ofNullable(parentBeaconBlockRoot); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SealableBlockHeader.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SealableBlockHeader.java index 01ac52a768d..68855cfafbd 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SealableBlockHeader.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/SealableBlockHeader.java @@ -45,6 +45,10 @@ public class SealableBlockHeader extends ProcessableBlockHeader { protected final Hash depositsRoot; + protected final Long blobGasUsed; + + protected final BlobGas excessBlobGas; + protected SealableBlockHeader( final Hash parentHash, final Hash ommersHash, @@ -75,8 +79,6 @@ protected SealableBlockHeader( timestamp, baseFee, mixHashOrPrevRandao, - blobGasUsed, - excessBlobGas, parentBeaconBlockRoot); this.ommersHash = ommersHash; this.stateRoot = stateRoot; @@ -87,6 +89,8 @@ protected SealableBlockHeader( this.logsBloom = logsBloom; this.gasUsed = gasUsed; this.extraData = extraData; + this.blobGasUsed = blobGasUsed; + this.excessBlobGas = excessBlobGas; } /** @@ -169,4 +173,22 @@ public Optional getWithdrawalsRoot() { public Optional getDepositsRoot() { return Optional.ofNullable(depositsRoot); } + + /** + * Returns the blob gas used if available. + * + * @return the blob gas used if available. + */ + public Optional getBlobGasUsed() { + return Optional.ofNullable(blobGasUsed); + } + + /** + * Returns the excess blob gas used if available. + * + * @return the excess blob gas used if available. + */ + public Optional getExcessBlobGas() { + return Optional.ofNullable(excessBlobGas); + } } diff --git a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java index ae12be1b50e..2daf1893ba4 100644 --- a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java +++ b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java @@ -227,7 +227,7 @@ public BlockHeader updateFromParentValues(final ProtocolSpec protocolSpec) { .buildBlockHeader(), null))); } - if (excessBlobGas.isEmpty() && parentExcessBlobGas != null && parentBlobGasUsed != null) { + if (parentExcessBlobGas != null && parentBlobGasUsed != null) { builder.excessBlobGas( BlobGas.of( protocolSpec diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index beacad8710f..eaf03d74e79 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -69,7 +69,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = 'kyCYfllc1IcisRZIYuLxhC+0+POCzcMQPhE8F8mx1Ns=' + knownHash = 'YYrWGQIwp1sgEmwx2DcfjiskFO8euGGKeWh7Lq1F+24=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockHeader.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockHeader.java index b11c493b933..edd552e118d 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockHeader.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockHeader.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.plugin.data; -import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Quantity; import org.hyperledger.besu.plugin.Unstable; @@ -22,20 +21,12 @@ import java.util.Optional; import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.bytes.Bytes32; /** * The minimum set of data for a BlockHeader, as defined in the Ethereum Yellow Paper. */ -public interface BlockHeader { - - /** - * The Keccak 256-bit hash of the parent block’s header, in its entirety. - * - * @return The Keccak 256-bit hash of the parent block’s header, in its entirety. - */ - Hash getParentHash(); +public interface BlockHeader extends ProcessableBlockHeader { /** * The Keccak 256-bit hash of the ommers list portion of this block. @@ -44,17 +35,6 @@ public interface BlockHeader { */ Hash getOmmersHash(); - /** - * The 160-bit address to which all fees collected from the successful mining of this block be - * transferred. - * - *

The name in the yellow paper is beneficiary. - * - * @return The 160-bit address to which all fees collected from the successful mining of this - * block be transferred. - */ - Address getCoinbase(); - /** * The Keccak 256-bit hash of the root node of the state trie, after all transactions are executed * and finalizations applied. @@ -91,30 +71,6 @@ public interface BlockHeader { */ Bytes getLogsBloom(); - /** - * A scalar value corresponding to the difficulty level of this block. This can be calculated from - * the previous block’s difficulty level and the timestamp. - * - * @return A UInt256 value corresponding to the difficulty level of this block. This can be - * calculated from the previous block’s difficulty level and the timestamp. - */ - Quantity getDifficulty(); - - /** - * A scalar value equal to the number of ancestor blocks. The genesis block has a number of zero. - * - * @return A scalar value equal to the number of ancestor blocks. The genesis block has a number - * of zero. - */ - long getNumber(); - - /** - * A scalar value equal to the current limit of gas expenditure per block. - * - * @return A scalar value equal to the current limit of gas expenditure per block. - */ - long getGasLimit(); - /** * A scalar value equal to the total gas used in transactions in this block. * @@ -122,14 +78,6 @@ public interface BlockHeader { */ long getGasUsed(); - /** - * A scalar value equal to the reasonable output of Unix’s time() at this block’s inception. - * - * @return A scalar value equal to the reasonable output of Unix’s time() at this block’s - * inception. - */ - long getTimestamp(); - /** * An arbitrary byte array containing data relevant to this block. This must be 32 bytes or fewer. * @@ -163,25 +111,6 @@ public interface BlockHeader { */ Hash getBlockHash(); - /** - * The BASEFEE of this header. - * - * @return The BASEFEE of this header. - */ - @Unstable - default Optional getBaseFee() { - return Optional.empty(); - } - - /** - * Optional 32 bytes of prevRandao data. - * - * @return Optional prevRandao bytes from this header. - */ - default Optional getPrevRandao() { - return Optional.empty(); - } - /** * The Keccak 256-bit hash of the root node of the trie structure populated with each withdrawal * in the withdrawals list portion of the block. @@ -216,12 +145,4 @@ default Optional getPrevRandao() { */ @Unstable Optional getBlobGasUsed(); - - /** - * The parent beacon block root of this header. - * - * @return The parent_beacon_block_root of this header. - */ - @Unstable - Optional getParentBeaconBlockRoot(); } diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/ProcessableBlockHeader.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/ProcessableBlockHeader.java new file mode 100644 index 00000000000..db9bfa942e6 --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/ProcessableBlockHeader.java @@ -0,0 +1,108 @@ +/* + * Copyright ConsenSys AG. + * + * 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.plugin.data; + +import org.hyperledger.besu.datatypes.Address; +import org.hyperledger.besu.datatypes.Hash; +import org.hyperledger.besu.datatypes.Quantity; +import org.hyperledger.besu.plugin.Unstable; + +import java.util.Optional; + +import org.apache.tuweni.bytes.Bytes32; + +/** + * The minimum set of data for a BlockHeader, as defined in the Ethereum Yellow Paper. + */ +public interface ProcessableBlockHeader { + + /** + * The Keccak 256-bit hash of the parent block’s header, in its entirety. + * + * @return The Keccak 256-bit hash of the parent block’s header, in its entirety. + */ + Hash getParentHash(); + + /** + * The 160-bit address to which all fees collected from the successful mining of this block be + * transferred. + * + *

The name in the yellow paper is beneficiary. + * + * @return The 160-bit address to which all fees collected from the successful mining of this + * block be transferred. + */ + Address getCoinbase(); + + /** + * A scalar value corresponding to the difficulty level of this block. This can be calculated from + * the previous block’s difficulty level and the timestamp. + * + * @return A scalar value corresponding to the difficulty level of this block. This can be + * calculated from the previous block’s difficulty level and the timestamp. + */ + Quantity getDifficulty(); + + /** + * A scalar value equal to the number of ancestor blocks. The genesis block has a number of zero. + * + * @return A scalar value equal to the number of ancestor blocks. The genesis block has a number + * of zero. + */ + long getNumber(); + + /** + * A scalar value equal to the current limit of gas expenditure per block. + * + * @return A scalar value equal to the current limit of gas expenditure per block. + */ + long getGasLimit(); + + /** + * A scalar value equal to the reasonable output of Unix’s time() at this block’s inception. + * + * @return A scalar value equal to the reasonable output of Unix’s time() at this block’s + * inception. + */ + long getTimestamp(); + + /** + * Optional 32 bytes of prevRandao data. + * + * @return Optional prevRandao bytes from this header. + */ + default Optional getPrevRandao() { + return Optional.empty(); + } + + /** + * The base fee of this block. + * + * @return The base fee of this block. + */ + @Unstable + default Optional getBaseFee() { + return Optional.empty(); + } + + /** + * The parent beacon block root of this header. + * + * @return The parent_beacon_block_root of this header. + */ + @Unstable + Optional getParentBeaconBlockRoot(); +}