Skip to content

Commit

Permalink
clean up the ProcessableBlockHeader (#6117)
Browse files Browse the repository at this point in the history
* clean up

Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
  • Loading branch information
pinges authored Nov 6, 2023
1 parent cea3d8a commit 646c5a3
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -193,7 +189,6 @@ protected BlockCreationResult createBlock(
disposableWorldState,
maybeTransactions,
miningBeneficiary,
blobGasPrice,
newProtocolSpec);

transactionResults.logSelectionStats();
Expand Down Expand Up @@ -321,14 +316,18 @@ private TransactionSelectionResults selectTransactions(
final MutableWorldState disposableWorldState,
final Optional<List<Transaction>> transactions,
final Address miningBeneficiary,
final Wei blobGasPrice,
final ProtocolSpec protocolSpec)
throws RuntimeException {
final MainnetTransactionProcessor transactionProcessor = protocolSpec.getTransactionProcessor();

final AbstractBlockProcessor.TransactionReceiptFactory transactionReceiptFactory =
protocolSpec.getTransactionReceiptFactory();

Wei blobGasPrice =
protocolSpec
.getFeeMarket()
.blobGasPricePerGas(calculateExcessBlobGasForParent(protocolSpec, parentHeader));

final BlockTransactionSelector selector =
new BlockTransactionSelector(
miningParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ public ProcessableBlockHeader buildProcessableBlockHeader() {
timestamp,
baseFee,
mixHashOrPrevRandao,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot);
}

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<Long> blobGasUsed;
// excessBlogGas is included for Cancun
protected final Optional<BlobGas> excessBlobGas;
// parentBeaconBlockRoot is included for Cancun
protected final Bytes32 parentBeaconBlockRoot;

Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -81,6 +73,7 @@ protected ProcessableBlockHeader(
*
* @return the block parent block hash
*/
@Override
public Hash getParentHash() {
return parentHash;
}
Expand All @@ -90,6 +83,7 @@ public Hash getParentHash() {
*
* @return the block coinbase address
*/
@Override
public Address getCoinbase() {
return coinbase;
}
Expand All @@ -99,6 +93,7 @@ public Address getCoinbase() {
*
* @return the block difficulty
*/
@Override
public Difficulty getDifficulty() {
return difficulty;
}
Expand Down Expand Up @@ -168,18 +163,17 @@ public Bytes32 getMixHashOrPrevRandao() {
*
* @return the raw bytes of the prevRandao field
*/
@Override
public Optional<Bytes32> getPrevRandao() {
return Optional.ofNullable(mixHashOrPrevRandao);
}

public Optional<BlobGas> getExcessBlobGas() {
return excessBlobGas;
}

public Optional<Long> getBlobGasUsed() {
return blobGasUsed;
}

/**
* Returns the parent beacon block root.
*
* @return the parent beacon block root.
*/
@Override
public Optional<Bytes32> getParentBeaconBlockRoot() {
return Optional.ofNullable(parentBeaconBlockRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,8 +79,6 @@ protected SealableBlockHeader(
timestamp,
baseFee,
mixHashOrPrevRandao,
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot);
this.ommersHash = ommersHash;
this.stateRoot = stateRoot;
Expand All @@ -87,6 +89,8 @@ protected SealableBlockHeader(
this.logsBloom = logsBloom;
this.gasUsed = gasUsed;
this.extraData = extraData;
this.blobGasUsed = blobGasUsed;
this.excessBlobGas = excessBlobGas;
}

/**
Expand Down Expand Up @@ -169,4 +173,22 @@ public Optional<Hash> getWithdrawalsRoot() {
public Optional<Hash> getDepositsRoot() {
return Optional.ofNullable(depositsRoot);
}

/**
* Returns the blob gas used if available.
*
* @return the blob gas used if available.
*/
public Optional<Long> getBlobGasUsed() {
return Optional.ofNullable(blobGasUsed);
}

/**
* Returns the excess blob gas used if available.
*
* @return the excess blob gas used if available.
*/
public Optional<BlobGas> getExcessBlobGas() {
return Optional.ofNullable(excessBlobGas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,19 @@
*/
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.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/**
* The minimum set of data for a BlockHeader, as defined in the <a href=
* "https://ethereum.github.io/yellowpaper/paper.pdf">Ethereum Yellow Paper</a>.
*/
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.
Expand All @@ -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.
*
* <p>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.
Expand Down Expand Up @@ -91,45 +71,13 @@ 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.
*
* @return A scalar value equal to the total gas used in transactions in this block.
*/
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.
*
Expand Down Expand Up @@ -163,25 +111,6 @@ public interface BlockHeader {
*/
Hash getBlockHash();

/**
* The BASEFEE of this header.
*
* @return The BASEFEE of this header.
*/
@Unstable
default Optional<? extends Quantity> getBaseFee() {
return Optional.empty();
}

/**
* Optional 32 bytes of prevRandao data.
*
* @return Optional prevRandao bytes from this header.
*/
default Optional<Bytes32> 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.
Expand Down Expand Up @@ -216,12 +145,4 @@ default Optional<Bytes32> getPrevRandao() {
*/
@Unstable
Optional<? extends Long> getBlobGasUsed();

/**
* The parent beacon block root of this header.
*
* @return The parent_beacon_block_root of this header.
*/
@Unstable
Optional<? extends Bytes32> getParentBeaconBlockRoot();
}
Loading

0 comments on commit 646c5a3

Please sign in to comment.