Skip to content

Commit

Permalink
New excess_data_gas block header field (hyperledger#4958)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored and eum602 committed Nov 3, 2023
1 parent cf5c86c commit ddf6862
Show file tree
Hide file tree
Showing 25 changed files with 241 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
### Breaking Changes

### Additions and Improvements
- Add `excess_data_gas` field to block header [#4958](https://github.com/hyperledger/besu/pull/4958)

### 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 @@ -54,6 +54,7 @@ public static BlockHeader createBlockHeader(
mixHash,
new BigInteger(block.getNonceRaw().substring(2), 16).longValue(),
null,
null,
blockHeaderFunctions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,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));
}

private void clearSyncTarget() {
Expand Down
147 changes: 147 additions & 0 deletions datatypes/src/main/java/org/hyperledger/besu/datatypes/DataGas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.datatypes;

import org.hyperledger.besu.plugin.data.Quantity;

import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.BaseUInt256Value;
import org.apache.tuweni.units.bigints.UInt256;

/** A particular quantity of DataGas */
public final class DataGas extends BaseUInt256Value<DataGas> implements Quantity {

/** The constant ZERO. */
public static final DataGas ZERO = of(0);

/** The constant ONE. */
public static final DataGas ONE = of(1);

/** The constant MAX_DATA_GAS. */
public static final DataGas MAX_DATA_GAS = of(UInt256.MAX_VALUE);

/**
* Instantiates a new DataGas.
*
* @param value the value
*/
DataGas(final UInt256 value) {
super(value, DataGas::new);
}

private DataGas(final long v) {
this(UInt256.valueOf(v));
}

private DataGas(final BigInteger v) {
this(UInt256.valueOf(v));
}

private DataGas(final String hexString) {
this(UInt256.fromHexString(hexString));
}

/**
* data gas of value.
*
* @param value the value
* @return the data gas
*/
public static DataGas of(final long value) {
return new DataGas(value);
}

/**
* data gas of value.
*
* @param value the value
* @return the data gas
*/
public static DataGas of(final BigInteger value) {
return new DataGas(value);
}

/**
* data gas of value.
*
* @param value the value
* @return the data gas
*/
public static DataGas of(final UInt256 value) {
return new DataGas(value);
}

/**
* data gas of value.
*
* @param value the value
* @return the data gas
*/
public static DataGas ofNumber(final Number value) {
return new DataGas((BigInteger) value);
}

/**
* Wrap data gas.
*
* @param value the value
* @return the data gas
*/
public static DataGas wrap(final Bytes value) {
return new DataGas(UInt256.fromBytes(value));
}

/**
* From hex string to data gas.
*
* @param str the str
* @return the data gas
*/
public static DataGas fromHexString(final String str) {
return new DataGas(str);
}

@Override
public Number getValue() {
return getAsBigInteger();
}

@Override
public BigInteger getAsBigInteger() {
return toBigInteger();
}

@Override
public String toHexString() {
return super.toHexString();
}

@Override
public String toShortHexString() {
return super.isZero() ? "0x0" : super.toShortHexString();
}

/**
* From quantity to data gas.
*
* @param quantity the quantity
* @return the data gas
*/
public static DataGas fromQuantity(final Quantity quantity) {
return DataGas.wrap((Bytes) quantity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public JsonRpcResponse response(
mixHash,
nonce,
withdrawalsRoot,
null, // ToDo 4844: set with the value of excess_data_gas field
blockHeaderFunctions);

return new JsonRpcSuccessResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
blockParam.getPrevRandao(),
0,
null,
null,
headerFunctions);

// ensure the block hash matches the blockParam hash
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,
null,
null),
new BlockBody(
List.of(
Expand Down Expand Up @@ -199,6 +200,7 @@ private Object createEmptyBlock(final Long height) {
Hash.EMPTY,
0,
null,
null,
null),
new BlockBody(List.of(), List.of())));
}
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,
null,
new BlockHeaderFunctions() {
@Override
public Hash hash(final BlockHeader header) {
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,
null,
new MainnetBlockHeaderFunctions());
testHash = fakeHeader.getHash();
final BlockBody fakeBody = new BlockBody(Collections.emptyList(), Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void setup() {
Hash.EMPTY,
0,
null,
null,
new MainnetBlockHeaderFunctions(),
Optional.of(testLogsBloomFilter));
testHash = fakeHeader.getHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private BlockHeader createBlock(final long number) {
Hash.EMPTY,
0,
null,
null,
new MainnetBlockHeaderFunctions(),
Optional.of(testLogsBloomFilter));
return fakeHeader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void setup() throws IOException {
Hash.EMPTY,
0,
null,
null,
new MainnetBlockHeaderFunctions());
testHash = fakeHeader.getHash();
when(blockchain.getBlockHeader(anyLong())).thenReturn(Optional.of(fakeHeader));
Expand Down Expand Up @@ -266,6 +267,7 @@ private BlockHeader createBlock(final long number, final Optional<String> messag
Hash.EMPTY,
0,
null,
null,
new MainnetBlockHeaderFunctions());
testHash = fakeHeader.getHash();
when(blockchain.getBlockHeader(number)).thenReturn(Optional.of(fakeHeader));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.core;

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.rlp.RLPInput;
Expand Down Expand Up @@ -63,6 +64,7 @@ public BlockHeader(
final Bytes32 mixHashOrPrevRandao,
final long nonce,
final Hash withdrawalsRoot,
final DataGas excessDataGas,
final BlockHeaderFunctions blockHeaderFunctions,
final Optional<LogsBloomFilter> privateLogsBloom) {
super(
Expand All @@ -81,7 +83,8 @@ public BlockHeader(
extraData,
baseFee,
mixHashOrPrevRandao,
withdrawalsRoot);
withdrawalsRoot,
excessDataGas);
this.nonce = nonce;
this.hash = Suppliers.memoize(() -> blockHeaderFunctions.hash(this));
this.parsedExtraData = Suppliers.memoize(() -> blockHeaderFunctions.parseExtraData(this));
Expand All @@ -106,6 +109,7 @@ public BlockHeader(
final Bytes32 mixHashOrPrevRandao,
final long nonce,
final Hash withdrawalsRoot,
final DataGas excessDataGas,
final BlockHeaderFunctions blockHeaderFunctions) {
super(
parentHash,
Expand All @@ -123,7 +127,8 @@ public BlockHeader(
extraData,
baseFee,
mixHashOrPrevRandao,
withdrawalsRoot);
withdrawalsRoot,
excessDataGas);
this.nonce = nonce;
this.hash = Suppliers.memoize(() -> blockHeaderFunctions.hash(this));
this.parsedExtraData = Suppliers.memoize(() -> blockHeaderFunctions.parseExtraData(this));
Expand Down Expand Up @@ -232,6 +237,9 @@ public void writeTo(final RLPOutput out) {
if (withdrawalsRoot != null) {
out.writeBytes(withdrawalsRoot);
}
if (excessDataGas != null) {
out.writeUInt256Scalar(excessDataGas);
}
out.endList();
}

Expand All @@ -256,6 +264,8 @@ public static BlockHeader readFrom(
final Wei baseFee = !input.isEndOfCurrentList() ? Wei.of(input.readUInt256Scalar()) : null;
final Hash withdrawalHashRoot =
!input.isEndOfCurrentList() ? Hash.wrap(input.readBytes32()) : null;
final DataGas excessDataGas =
!input.isEndOfCurrentList() ? DataGas.of(input.readUInt256Scalar()) : null;
input.leaveList();
return new BlockHeader(
parentHash,
Expand All @@ -275,6 +285,7 @@ public static BlockHeader readFrom(
mixHashOrPrevRandao,
nonce,
withdrawalHashRoot,
excessDataGas,
blockHeaderFunctions);
}

Expand Down Expand Up @@ -315,10 +326,13 @@ public String toString() {
sb.append("extraData=").append(extraData).append(", ");
sb.append("baseFee=").append(baseFee).append(", ");
sb.append("mixHashOrPrevRandao=").append(mixHashOrPrevRandao).append(", ");
sb.append("nonce=").append(nonce);
sb.append("nonce=").append(nonce).append(", ");
if (withdrawalsRoot != null) {
sb.append("withdrawalsRoot=").append(withdrawalsRoot).append(", ");
}
if (excessDataGas != null) {
sb.append("excessDataGas=").append(excessDataGas);
}
return sb.append("}").toString();
}

Expand Down Expand Up @@ -346,6 +360,7 @@ public static org.hyperledger.besu.ethereum.core.BlockHeader convertPluginBlockH
.getWithdrawalsRoot()
.map(h -> Hash.fromHexString(h.toHexString()))
.orElse(null),
pluginBlockHeader.getExcessDataGas().map(DataGas::fromQuantity).orElse(null),
blockHeaderFunctions);
}

Expand Down
Loading

0 comments on commit ddf6862

Please sign in to comment.