Skip to content

Commit

Permalink
EIP-7742: Add target_blob_count to block header (hyperledger#7808)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Dudley <[email protected]>
Signed-off-by: Marlene Marz <[email protected]>
  • Loading branch information
siladu authored and JanetMo committed Nov 17, 2024
1 parent 4db0007 commit e39cf97
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static BlockHeader createBlockHeader(
null,
null,
null,
null,
blockHeaderFunctions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.provider.Arguments;

// TODO SLD
@Disabled("TODO SLD - Enable when Prague spec is finalized")
public class ExecutionEnginePragueAcceptanceTest extends AbstractJsonRpcTest {
private static final String GENESIS_FILE = "/jsonrpc/engine/prague/genesis.json";
private static final String TEST_CASE_PATH = "/jsonrpc/engine/prague/test-cases/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public void miningParametersBlockPeriodSecondsIsUpdatedOnTransition() {
null,
null,
null,
null,
getBlockHeaderFunctions());
final Block block1 = new Block(header1, BlockBody.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public void miningParametersBlockPeriodSecondsIsUpdatedOnTransition() {
null,
null,
null,
null,
new CliqueBlockHeaderFunctions());
final Block block1 = new Block(header1, BlockBody.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,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, null, null));
null, null, null, null, null, null));
}

private void clearSyncTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ public String getParentBeaconBlockRoot() {
"0x0000000000000000000000000000000000000000000000000000000000000000");
}

/**
* Gets target blob count.
*
* @return the target blob count
*/
public Optional<String> getTargetBlobCount() {
// TODO SLD EIP-7742 not sure if we should use a default value here or enforce any
// "pragueAtGenesis" genesis file (used in devnets) to have this value
return JsonUtil.getValueAsString(genesisRoot, "targetblobcount");
}

/**
* Gets coinbase.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ public enum JsonRpcResponseKey {
TRANSACTION_ROOT,
BASEFEE,
WITHDRAWALS_ROOT,
REQUESTS_HASH
REQUESTS_HASH,
TARGET_BLOB_COUNT
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.units.bigints.UInt64;

public class JsonRpcResponseUtils {

Expand Down Expand Up @@ -106,6 +107,10 @@ public JsonRpcResponse response(
values.containsKey(WITHDRAWALS_ROOT) ? hash(values.get(WITHDRAWALS_ROOT)) : null;
final Hash requestsHash =
values.containsKey(REQUESTS_HASH) ? hash(values.get(REQUESTS_HASH)) : null;
final UInt64 targetBlobCount =
values.containsKey(JsonRpcResponseKey.TARGET_BLOB_COUNT)
? UInt64.fromHexString(values.get(JsonRpcResponseKey.TARGET_BLOB_COUNT))
: null;
final List<JsonNode> ommers = new ArrayList<>();

final BlockHeader header =
Expand All @@ -131,6 +136,7 @@ public JsonRpcResponse response(
null, // ToDo 4844: set with the value of excess_blob_gas field
null, // TODO 4788: set with the value of the parent beacon block root field
requestsHash,
targetBlobCount,
blockHeaderFunctions);

return new JsonRpcSuccessResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
: BlobGas.fromHexString(blockParam.getExcessBlobGas()),
maybeParentBeaconBlockRoot.orElse(null),
maybeRequests.map(BodyValidation::requestsHash).orElse(null),
null, // TODO SLD EIP-7742 wiring in future PR
headerFunctions);

// ensure the block hash matches the blockParam hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class BlockResult implements JsonRpcResult {
private final String blobGasUsed;
private final String excessBlobGas;
private final String parentBeaconBlockRoot;
private final String targetBlobCount;

public BlockResult(
final BlockHeader header,
Expand Down Expand Up @@ -138,6 +139,7 @@ public BlockResult(
this.excessBlobGas = header.getExcessBlobGas().map(Quantity::create).orElse(null);
this.parentBeaconBlockRoot =
header.getParentBeaconBlockRoot().map(Bytes32::toHexString).orElse(null);
this.targetBlobCount = header.getTargetBlobCount().map(Quantity::create).orElse(null);
}

@JsonGetter(value = "number")
Expand Down Expand Up @@ -275,4 +277,9 @@ public String getExcessBlobGas() {
public String getParentBeaconBlockRoot() {
return parentBeaconBlockRoot;
}

@JsonGetter(value = "targetBlobCount")
public String getTargetBlobCount() {
return targetBlobCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ private Block createFakeBlock(
null,
null,
null,
null,
null),
new BlockBody(
IntStream.range(0, txsNum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private Block createFakeBlock(
null,
null,
null,
null,
null),
new BlockBody(
IntStream.range(0, txsNum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private BlockHeader createBlockHeader(final Hash blockHash, final long blockNumb
null,
null,
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 @@ -120,6 +120,7 @@ public void setup() {
null,
null,
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 @@ -108,6 +108,7 @@ public void setup() throws IOException {
null,
null,
null,
null,
new MainnetBlockHeaderFunctions());
testHash = fakeHeader.getHash();
when(blockchain.getBlockHeader(anyLong())).thenReturn(Optional.of(fakeHeader));
Expand Down Expand Up @@ -283,6 +284,7 @@ private BlockHeader createBlock(final long number, final Optional<String> messag
null,
null,
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 @@ -48,6 +48,7 @@
import com.google.common.base.MoreObjects;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt64;

public final class GenesisState {

Expand Down Expand Up @@ -218,6 +219,12 @@ private static BlockHeader buildHeader(
.parentBeaconBlockRoot(
(isCancunAtGenesis(genesis) ? parseParentBeaconBlockRoot(genesis) : null))
.requestsHash(isPragueAtGenesis(genesis) ? Hash.EMPTY_REQUESTS_HASH : null)
.targetBlobCount(
isPragueAtGenesis(genesis)
// TODO SLD EIP-7742 Currently defaulting to null due to dependency on web3j
// BlockHeader in CodeDelegationTransactionAcceptanceTest
? genesis.getTargetBlobCount().map(UInt64::fromHexString).orElse(null)
: null)
.buildBlockHeader();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt64;

/** A mined Ethereum block header. */
public class BlockHeader extends SealableBlockHeader
Expand Down Expand Up @@ -65,6 +66,7 @@ public BlockHeader(
final BlobGas excessBlobGas,
final Bytes32 parentBeaconBlockRoot,
final Hash requestsHash,
final UInt64 targetBlobCount,
final BlockHeaderFunctions blockHeaderFunctions) {
super(
parentHash,
Expand All @@ -86,7 +88,8 @@ public BlockHeader(
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
requestsHash);
requestsHash,
targetBlobCount);
this.nonce = nonce;
this.hash = Suppliers.memoize(() -> blockHeaderFunctions.hash(this));
this.parsedExtraData = Suppliers.memoize(() -> blockHeaderFunctions.parseExtraData(this));
Expand Down Expand Up @@ -187,6 +190,9 @@ public void writeTo(final RLPOutput out) {

if (requestsHash == null) break;
out.writeBytes(requestsHash);

if (targetBlobCount == null) break;
out.writeUInt64Scalar(targetBlobCount);
} while (false);
out.endList();
}
Expand Down Expand Up @@ -219,6 +225,7 @@ public static BlockHeader readFrom(
!input.isEndOfCurrentList() ? BlobGas.of(input.readUInt64Scalar()) : null;
final Bytes32 parentBeaconBlockRoot = !input.isEndOfCurrentList() ? input.readBytes32() : null;
final Hash requestsHash = !input.isEndOfCurrentList() ? Hash.wrap(input.readBytes32()) : null;
final UInt64 targetBlobCount = !input.isEndOfCurrentList() ? input.readUInt64Scalar() : null;
input.leaveList();
return new BlockHeader(
parentHash,
Expand All @@ -242,6 +249,7 @@ public static BlockHeader readFrom(
excessBlobGas,
parentBeaconBlockRoot,
requestsHash,
targetBlobCount,
blockHeaderFunctions);
}

Expand Down Expand Up @@ -295,6 +303,9 @@ public String toString() {
if (requestsHash != null) {
sb.append("requestsHash=").append(requestsHash);
}
if (targetBlobCount != null) {
sb.append("targetBlobCount=").append(targetBlobCount);
}
return sb.append("}").toString();
}

Expand Down Expand Up @@ -329,6 +340,7 @@ public static org.hyperledger.besu.ethereum.core.BlockHeader convertPluginBlockH
.getRequestsHash()
.map(h -> Hash.fromHexString(h.toHexString()))
.orElse(null),
pluginBlockHeader.getTargetBlobCount().orElse(null),
blockHeaderFunctions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt64;

/** A utility class for building block headers. */
public class BlockHeaderBuilder {
Expand Down Expand Up @@ -76,6 +77,7 @@ public class BlockHeaderBuilder {
private Long blobGasUsed = null;
private BlobGas excessBlobGas = null;
private Bytes32 parentBeaconBlockRoot = null;
private UInt64 targetBlobCount = null;

public static BlockHeaderBuilder create() {
return new BlockHeaderBuilder();
Expand Down Expand Up @@ -124,7 +126,8 @@ public static BlockHeaderBuilder fromHeader(final BlockHeader header) {
.blobGasUsed(header.getBlobGasUsed().orElse(null))
.excessBlobGas(header.getExcessBlobGas().orElse(null))
.parentBeaconBlockRoot(header.getParentBeaconBlockRoot().orElse(null))
.requestsHash(header.getRequestsHash().orElse(null));
.requestsHash(header.getRequestsHash().orElse(null))
.targetBlobCount(header.getTargetBlobCount().orElse(null));
}

public static BlockHeaderBuilder fromBuilder(final BlockHeaderBuilder fromBuilder) {
Expand All @@ -149,6 +152,7 @@ public static BlockHeaderBuilder fromBuilder(final BlockHeaderBuilder fromBuilde
.excessBlobGas(fromBuilder.excessBlobGas)
.parentBeaconBlockRoot(fromBuilder.parentBeaconBlockRoot)
.requestsHash(fromBuilder.requestsHash)
.targetBlobCount(fromBuilder.targetBlobCount)
.blockHeaderFunctions(fromBuilder.blockHeaderFunctions);
toBuilder.nonce = fromBuilder.nonce;
return toBuilder;
Expand Down Expand Up @@ -179,6 +183,7 @@ public BlockHeader buildBlockHeader() {
excessBlobGas,
parentBeaconBlockRoot,
requestsHash,
targetBlobCount,
blockHeaderFunctions);
}

Expand All @@ -194,7 +199,8 @@ public ProcessableBlockHeader buildProcessableBlockHeader() {
timestamp,
baseFee,
mixHashOrPrevRandao,
parentBeaconBlockRoot);
parentBeaconBlockRoot,
targetBlobCount);
}

public SealableBlockHeader buildSealableBlockHeader() {
Expand All @@ -220,7 +226,8 @@ public SealableBlockHeader buildSealableBlockHeader() {
blobGasUsed,
excessBlobGas,
parentBeaconBlockRoot,
requestsHash);
requestsHash,
targetBlobCount);
}

private void validateBlockHeader() {
Expand Down Expand Up @@ -260,6 +267,7 @@ public BlockHeaderBuilder populateFrom(final ProcessableBlockHeader processableB
baseFee(processableBlockHeader.getBaseFee().orElse(null));
processableBlockHeader.getPrevRandao().ifPresent(this::prevRandao);
processableBlockHeader.getParentBeaconBlockRoot().ifPresent(this::parentBeaconBlockRoot);
processableBlockHeader.getTargetBlobCount().ifPresent(this::targetBlobCount);
return this;
}

Expand All @@ -285,6 +293,7 @@ public BlockHeaderBuilder populateFrom(final SealableBlockHeader sealableBlockHe
sealableBlockHeader.getExcessBlobGas().ifPresent(this::excessBlobGas);
sealableBlockHeader.getParentBeaconBlockRoot().ifPresent(this::parentBeaconBlockRoot);
requestsHash(sealableBlockHeader.getRequestsHash().orElse(null));
sealableBlockHeader.getTargetBlobCount().ifPresent(this::targetBlobCount);
return this;
}

Expand Down Expand Up @@ -418,4 +427,9 @@ public BlockHeaderBuilder parentBeaconBlockRoot(final Bytes32 parentBeaconBlockR
this.parentBeaconBlockRoot = parentBeaconBlockRoot;
return this;
}

public BlockHeaderBuilder targetBlobCount(final UInt64 targetBlobCount) {
this.targetBlobCount = targetBlobCount;
return this;
}
}
Loading

0 comments on commit e39cf97

Please sign in to comment.