Skip to content

Commit

Permalink
add dataGasUsed
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan <stefan.pingel@consensys.net>
  • Loading branch information
pinges authored and jflo committed Jun 21, 2023
1 parent 3bcecbb commit be4d4dd
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static BlockHeader createBlockHeader(
mixHash,
new BigInteger(block.getNonceRaw().substring(2), 16).longValue(),
null,
0,
null,
null,
blockHeaderFunctions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void setSyncTarget() {
mock(EthPeer.class),
new org.hyperledger.besu.ethereum.core.BlockHeader(
null, null, null, null, null, null, null, null, 1, 1, 1, 1, null, null, null, 1, null,
null, null, null));
0, null, null, null));
}

private void clearSyncTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public JsonRpcResponse response(
mixHash,
nonce,
withdrawalsRoot,
0, // ToDo 4844: set with the value of data_gas_used field
null, // ToDo 4844: set with the value of excess_data_gas field
depositsRoot,
blockHeaderFunctions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
blockParam.getPrevRandao(),
0,
maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null),
blockParam.getDataGasUsed(),
blockParam.getExcessDataGas() == null
? null
: DataGas.fromHexString(blockParam.getExcessDataGas()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class EnginePayloadParameter {
private final List<String> transactions;
private final List<WithdrawalParameter> withdrawals;
private final List<DepositParameter> deposits;

private final long dataGasUsed;
private final String excessDataGas;

@JsonCreator
Expand All @@ -68,6 +68,7 @@ public EnginePayloadParameter(
@JsonProperty("prevRandao") final String prevRandao,
@JsonProperty("transactions") final List<String> transactions,
@JsonProperty("withdrawals") final List<WithdrawalParameter> withdrawals,
@JsonProperty("dataGasUsed") final UnsignedLongParameter dataGasUsed,
@JsonProperty("deposits") final List<DepositParameter> deposits,
@JsonProperty("excessDataGas") final String excessDataGas) {
this.blockHash = blockHash;
Expand All @@ -86,6 +87,7 @@ public EnginePayloadParameter(
this.transactions = transactions;
this.withdrawals = withdrawals;
this.deposits = deposits;
this.dataGasUsed = dataGasUsed.getValue();
this.excessDataGas = excessDataGas;
}

Expand Down Expand Up @@ -149,6 +151,10 @@ public List<WithdrawalParameter> getWithdrawals() {
return withdrawals;
}

public long getDataGasUsed() {
return dataGasUsed;
}

public String getExcessDataGas() {
return excessDataGas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private Object createFakeBlock(final Long height) {
Hash.EMPTY,
0,
null,
0,
null,
null,
null),
Expand Down Expand Up @@ -202,6 +203,7 @@ private Object createEmptyBlock(final Long height) {
Hash.EMPTY,
0,
null,
0,
null,
null,
null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ private EnginePayloadParameter mockPayload(
header.getPrevRandao().map(Bytes32::toHexString).orElse("0x0"),
txs,
withdrawals,
new UnsignedLongParameter(header.getDataGasUsed()),
deposits,
header.getExcessDataGas().map(DataGas::toHexString).orElse(null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ private BlockHeader createBlockHeader(final Hash blockHash, final long blockNumb
Bytes32.ZERO,
0,
null,
0,
null,
null,
new BlockHeaderFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void setup() {
Hash.EMPTY,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void setup() throws IOException {
Hash.EMPTY,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down Expand Up @@ -275,6 +276,7 @@ private BlockHeader createBlock(final long number, final Optional<String> messag
Hash.EMPTY,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,26 @@ protected BlockCreationResult createBlock(

throwIfStopped();

final DataGas newExcessDataGas = computeExcessDataGas(transactionResults, newProtocolSpec);
DataGas newExcessDataGas = null;
long newDataGasUsed = 0;
if (newProtocolSpec.getFeeMarket().implementsDataFee()) {
final var gasCalculator = newProtocolSpec.getGasCalculator();
newExcessDataGas =
DataGas.of(
gasCalculator.computeExcessDataGas(
// casting parent excess data gas to long since for the moment it should be well
// below that limit
parentHeader.getExcessDataGas().map(DataGas::toLong).orElse(0L),
parentHeader.getDataGasUsed()));

final int newBlobsCount =
transactionResults.getTransactionsByType(TransactionType.BLOB).stream()
.map(tx -> tx.getVersionedHashes().orElseThrow())
.mapToInt(List::size)
.sum();

newDataGasUsed = gasCalculator.dataGasCost(newBlobsCount);
}

throwIfStopped();

Expand All @@ -248,6 +267,7 @@ protected BlockCreationResult createBlock(
? BodyValidation.withdrawalsRoot(maybeWithdrawals.get())
: null)
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.dataGasUsed(newDataGasUsed)
.excessDataGas(newExcessDataGas)
.buildSealableBlockHeader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public BlockHeader(
final Bytes32 mixHashOrPrevRandao,
final long nonce,
final Hash withdrawalsRoot,
final long dataGasUsed,
final DataGas excessDataGas,
final Hash depositsRoot,
final BlockHeaderFunctions blockHeaderFunctions,
Expand All @@ -83,6 +84,7 @@ public BlockHeader(
baseFee,
mixHashOrPrevRandao,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
depositsRoot);
this.nonce = nonce;
Expand All @@ -108,6 +110,7 @@ public BlockHeader(
final Bytes32 mixHashOrPrevRandao,
final long nonce,
final Hash withdrawalsRoot,
final long dataGasUsed,
final DataGas excessDataGas,
final Hash depositsRoot,
final BlockHeaderFunctions blockHeaderFunctions) {
Expand All @@ -128,6 +131,7 @@ public BlockHeader(
baseFee,
mixHashOrPrevRandao,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
depositsRoot);
this.nonce = nonce;
Expand Down Expand Up @@ -213,6 +217,7 @@ public void writeTo(final RLPOutput out) {
out.writeBytes(withdrawalsRoot);
}
if (excessDataGas != null) {
out.writeLongScalar(dataGasUsed);
out.writeUInt256Scalar(excessDataGas);
}
if (depositsRoot != null) {
Expand Down Expand Up @@ -244,6 +249,7 @@ public static BlockHeader readFrom(
!(input.isEndOfCurrentList() || input.isZeroLengthString())
? Hash.wrap(input.readBytes32())
: null;
final long dataGasUsed = !input.isEndOfCurrentList() ? input.readUInt256Scalar().toLong() : 0;
final DataGas excessDataGas =
!input.isEndOfCurrentList() ? DataGas.of(input.readUInt256Scalar()) : null;
final Hash depositHashRoot =
Expand All @@ -267,6 +273,7 @@ public static BlockHeader readFrom(
mixHashOrPrevRandao,
nonce,
withdrawalHashRoot,
dataGasUsed,
excessDataGas,
depositHashRoot,
blockHeaderFunctions);
Expand Down Expand Up @@ -346,6 +353,7 @@ public static org.hyperledger.besu.ethereum.core.BlockHeader convertPluginBlockH
.getWithdrawalsRoot()
.map(h -> Hash.fromHexString(h.toHexString()))
.orElse(null),
pluginBlockHeader.getDataGasUsed(),
pluginBlockHeader.getExcessDataGas().map(DataGas::fromQuantity).orElse(null),
pluginBlockHeader
.getDepositsRoot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class BlockHeaderBuilder {
// instead of an invalid identifier such as -1.
private OptionalLong nonce = OptionalLong.empty();

private long dataGasUsed;

private DataGas excessDataGas = null;

public static BlockHeaderBuilder create() {
Expand Down Expand Up @@ -119,6 +121,7 @@ public static BlockHeaderBuilder fromHeader(final BlockHeader header) {
.nonce(header.getNonce())
.prevRandao(header.getPrevRandao().orElse(null))
.withdrawalsRoot(header.getWithdrawalsRoot().orElse(null))
.dataGasUsed(header.getDataGasUsed())
.excessDataGas(header.getExcessDataGas().orElse(null))
.depositsRoot(header.getDepositsRoot().orElse(null));
}
Expand Down Expand Up @@ -170,6 +173,7 @@ public BlockHeader buildBlockHeader() {
mixHashOrPrevRandao,
nonce.getAsLong(),
withdrawalsRoot,
dataGasUsed,
excessDataGas,
depositsRoot,
blockHeaderFunctions);
Expand Down Expand Up @@ -210,6 +214,7 @@ public SealableBlockHeader buildSealableBlockHeader() {
baseFee,
mixHashOrPrevRandao,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
depositsRoot);
}
Expand Down Expand Up @@ -399,4 +404,9 @@ public BlockHeaderBuilder excessDataGas(final DataGas excessDataGas) {
this.excessDataGas = excessDataGas;
return this;
}

public BlockHeaderBuilder dataGasUsed(final long dataGasUsed) {
this.dataGasUsed = dataGasUsed;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SealableBlockHeader extends ProcessableBlockHeader {
protected final Hash withdrawalsRoot;

protected final Hash depositsRoot;
protected final long dataGasUsed;

protected SealableBlockHeader(
final Hash parentHash,
Expand All @@ -62,6 +63,7 @@ protected SealableBlockHeader(
final Wei baseFee,
final Bytes32 mixHashOrPrevRandao,
final Hash withdrawalsRoot,
final long dataGasUsed,
final DataGas excessDataGas,
final Hash depositsRoot) {
super(
Expand All @@ -83,6 +85,7 @@ protected SealableBlockHeader(
this.logsBloom = logsBloom;
this.gasUsed = gasUsed;
this.extraData = extraData;
this.dataGasUsed = dataGasUsed;
}

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

/**
* Returns the data gas used
*
* @return the data gas used
*/
public long getDataGasUsed() {
return dataGasUsed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public Hash getBlockHash() {
public Optional<? extends Quantity> getExcessDataGas() {
return Optional.empty();
}

@Override
public long getDataGasUsed() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class LogRollingTests {
Hash.ZERO,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand All @@ -118,6 +119,7 @@ public class LogRollingTests {
Hash.ZERO,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public TestBlockHeader(
Hash.fromHexString(mixHash),
Bytes.fromHexStringLenient(nonce).toLong(),
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static BlockHeader prepareHeader(final long number, final Optional<String
Hash.EMPTY,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand All @@ -83,6 +84,7 @@ public static BlockHeader prepareWrongParentHash(final BlockHeader blockHeader)
blockHeader.getMixHash(),
blockHeader.getNonce(),
blockHeader.getWithdrawalsRoot().orElse(null),
blockHeader.getDataGasUsed(),
blockHeader.getExcessDataGas().orElse(null),
blockHeader.getDepositsRoot().orElse(null),
new MainnetBlockHeaderFunctions());
Expand Down Expand Up @@ -131,6 +133,7 @@ private static BlockHeader prepareEmptyHeader(final BlockHeader parent) {
Hash.EMPTY,
0,
null,
0,
null,
null,
new MainnetBlockHeaderFunctions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public ReferenceTestBlockHeader(
@JsonProperty("nonce") final String nonce,
@JsonProperty("withdrawalsRoot") final String withdrawalsRoot,
@JsonProperty("depositsRoot") final String depositsRoot,
@JsonProperty("dataGasUsed") final String dataGasUsed,
@JsonProperty("excessDataGas") final String excessDataGas,
@JsonProperty("hash") final String hash) {
super(
Expand All @@ -188,6 +189,7 @@ public ReferenceTestBlockHeader(
Hash.fromHexString(mixHash), // mixHash
Bytes.fromHexStringLenient(nonce).toLong(),
withdrawalsRoot != null ? Hash.fromHexString(withdrawalsRoot) : null,
dataGasUsed != null ? Long.parseLong(dataGasUsed) : 0,
excessDataGas != null ? DataGas.fromHexString(excessDataGas) : null,
depositsRoot != null ? Hash.fromHexString(depositsRoot) : null,
new BlockHeaderFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public ReferenceTestEnv(
Optional.ofNullable(random).map(Difficulty::fromHexString).orElse(Difficulty.ZERO),
0L,
null, // withdrawalsRoot
0,
null, // depositsRoot
null,
new MainnetBlockHeaderFunctions());
Expand Down
Loading

0 comments on commit be4d4dd

Please sign in to comment.