Skip to content

Commit

Permalink
passing beacon block root hash from RPC calls, implement precompile a…
Browse files Browse the repository at this point in the history
…nd storage of root hash when executing a block

Signed-off-by: Stefan <[email protected]>
  • Loading branch information
pinges committed Aug 4, 2023
1 parent 7738a6a commit b6615df
Show file tree
Hide file tree
Showing 35 changed files with 345 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static BlockHeader createBlockHeader(
null,
null,
null,
null,
null, null,
blockHeaderFunctions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,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));
}

private void clearSyncTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,26 @@ public MergeBlockCreator(
/**
* Create block and return block creation result.
*
* @param maybeTransactions the maybe transactions
* @param random the random
* @param timestamp the timestamp
* @param withdrawals optional list of withdrawals
* @param maybeTransactions the maybe transactions
* @param random the random
* @param timestamp the timestamp
* @param withdrawals optional list of withdrawals
* @param parentBeaconBlockRoot optional root hash of the parent beacon block
* @return the block creation result
*/
public BlockCreationResult createBlock(
final Optional<List<Transaction>> maybeTransactions,
final Bytes32 random,
final long timestamp,
final Optional<List<Withdrawal>> withdrawals) {
final Optional<List<Transaction>> maybeTransactions,
final Bytes32 random,
final long timestamp,
final Optional<List<Withdrawal>> withdrawals,
final Optional<Bytes32> parentBeaconBlockRoot) {

return createBlock(
maybeTransactions,
Optional.of(Collections.emptyList()),
withdrawals,
Optional.of(random),
parentBeaconBlockRoot,
timestamp,
false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ public void changeTargetGasLimit(final Long newTargetGasLimit) {

@Override
public PayloadIdentifier preparePayload(
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals) {
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals, final Optional<Bytes32> parentBeaconBlockRoot) {

// we assume that preparePayload is always called sequentially, since the RPC Engine calls
// are sequential, if this assumption changes then more synchronization should be added to
Expand All @@ -273,7 +273,7 @@ public PayloadIdentifier preparePayload(
// put the empty block in first
final Block emptyBlock =
mergeBlockCreator
.createBlock(Optional.of(Collections.emptyList()), prevRandao, timestamp, withdrawals)
.createBlock(Optional.of(Collections.emptyList()), prevRandao, timestamp, withdrawals, parentBeaconBlockRoot)
.getBlock();

BlockProcessingResult result = validateProposedBlock(emptyBlock);
Expand All @@ -294,7 +294,7 @@ public PayloadIdentifier preparePayload(
}
}

tryToBuildBetterBlock(timestamp, prevRandao, payloadIdentifier, mergeBlockCreator, withdrawals);
tryToBuildBetterBlock(timestamp, prevRandao, payloadIdentifier, mergeBlockCreator, withdrawals, parentBeaconBlockRoot);

return payloadIdentifier;
}
Expand Down Expand Up @@ -330,14 +330,14 @@ public void finalizeProposalById(final PayloadIdentifier payloadId) {
}

private void tryToBuildBetterBlock(
final Long timestamp,
final Bytes32 random,
final PayloadIdentifier payloadIdentifier,
final MergeBlockCreator mergeBlockCreator,
final Optional<List<Withdrawal>> withdrawals) {
final Long timestamp,
final Bytes32 random,
final PayloadIdentifier payloadIdentifier,
final MergeBlockCreator mergeBlockCreator,
final Optional<List<Withdrawal>> withdrawals, final Optional<Bytes32> parentBeaconBlockRoot) {

final Supplier<BlockCreationResult> blockCreator =
() -> mergeBlockCreator.createBlock(Optional.empty(), random, timestamp, withdrawals);
() -> mergeBlockCreator.createBlock(Optional.empty(), random, timestamp, withdrawals, parentBeaconBlockRoot);

LOG.debug(
"Block creation started for payload id {}, remaining time is {}ms",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ public interface MergeMiningCoordinator extends MiningCoordinator {
/**
* Prepare payload identifier.
*
* @param parentHeader the parent header
* @param timestamp the timestamp
* @param prevRandao the prev randao
* @param feeRecipient the fee recipient
* @param withdrawals the optional list of withdrawals
* @param parentHeader the parent header
* @param timestamp the timestamp
* @param prevRandao the prev randao
* @param feeRecipient the fee recipient
* @param withdrawals the optional list of withdrawals
* @param parentBeaconBlockRoot optional root hash of the parent beacon block
* @return the payload identifier
*/
PayloadIdentifier preparePayload(
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals);
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals, final Optional<Bytes32> parentBeaconBlockRoot);

@Override
default boolean isCompatibleWithEngineApi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ public void changeTargetGasLimit(final Long targetGasLimit) {

@Override
public PayloadIdentifier preparePayload(
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals) {
final BlockHeader parentHeader,
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals, final Optional<Bytes32> parentBeaconBlockRoot) {
return mergeCoordinator.preparePayload(
parentHeader, timestamp, prevRandao, feeRecipient, withdrawals);
parentHeader, timestamp, prevRandao, feeRecipient, withdrawals, parentBeaconBlockRoot);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void coinbaseShouldMatchSuggestedFeeRecipient() {
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
EMPTY_WITHDRAWALS);
EMPTY_WITHDRAWALS, Optional.empty());

ArgumentCaptor<BlockWithReceipts> blockWithReceipts =
ArgumentCaptor.forClass(BlockWithReceipts.class);
Expand Down Expand Up @@ -294,7 +294,7 @@ public void exceptionDuringBuildingBlockShouldNotBeInvalid()
.doThrow(new MerkleTrieException("missing leaf"))
.doCallRealMethod()
.when(beingSpiedOn)
.createBlock(any(), any(Bytes32.class), anyLong(), eq(Optional.empty()));
.createBlock(any(), any(Bytes32.class), anyLong(), eq(Optional.empty()), Optional.empty());
return beingSpiedOn;
};

Expand Down Expand Up @@ -330,7 +330,7 @@ public void exceptionDuringBuildingBlockShouldNotBeInvalid()
System.currentTimeMillis() / 1000,
Bytes32.random(),
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

verify(willThrow, never()).addBadBlock(any(), any());
blockCreationTask.get();
Expand Down Expand Up @@ -362,7 +362,7 @@ public void shouldNotRecordProposedBadBlockToBadBlockManager()
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

verify(badBlockManager, never()).addBadBlock(any(), any());
assertThat(badBlockManager.getBadBlocks().size()).isEqualTo(0);
Expand Down Expand Up @@ -394,7 +394,7 @@ public void shouldContinueBuildingBlocksUntilFinalizeIsCalled()
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

blockCreationTask.get();

Expand Down Expand Up @@ -445,7 +445,7 @@ public void blockCreationRepetitionShouldTakeNotLessThanRepetitionMinDuration()
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

blockCreationTask.get();

Expand Down Expand Up @@ -491,7 +491,7 @@ public void shouldRetryBlockCreationOnRecoverableError()
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

blockCreationTask.get();

Expand Down Expand Up @@ -525,7 +525,7 @@ public void shouldStopRetryBlockCreationIfTimeExpired() throws InterruptedExcept
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

try {
blockCreationTask.get();
Expand Down Expand Up @@ -567,7 +567,7 @@ public void shouldStopInProgressBlockCreationIfFinalizedIsCalled()
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

waitForBlockCreationInProgress.await();
coordinator.finalizeProposalById(payloadId);
Expand Down Expand Up @@ -613,7 +613,7 @@ public void shouldNotStartAnotherBlockCreationJobIfCalledAgainWithTheSamePayload
timestamp,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

final CompletableFuture<Void> task1 = blockCreationTask;

Expand All @@ -623,7 +623,7 @@ public void shouldNotStartAnotherBlockCreationJobIfCalledAgainWithTheSamePayload
timestamp,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

assertThat(payloadId1).isEqualTo(payloadId2);

Expand Down Expand Up @@ -658,7 +658,7 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
timestamp,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

assertThat(coordinator.isBlockCreationCancelled(payloadId1)).isFalse();

Expand All @@ -668,7 +668,7 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
timestamp + 1,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

assertThat(payloadId1).isNotEqualTo(payloadId2);
assertThat(coordinator.isBlockCreationCancelled(payloadId1)).isTrue();
Expand Down Expand Up @@ -697,7 +697,7 @@ public void shouldUseExtraDataFromMiningParameters() {
1L,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty());
Optional.empty(), Optional.empty());

ArgumentCaptor<BlockWithReceipts> blockWithReceipts =
ArgumentCaptor.forClass(BlockWithReceipts.class);
Expand Down
24 changes: 13 additions & 11 deletions datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,28 @@ public class Address extends DelegatingBytes {
public static final Address ALTBN128_PAIRING = Address.precompiled(0x08);
/** The constant BLAKE2B_F_COMPRESSION. */
public static final Address BLAKE2B_F_COMPRESSION = Address.precompiled(0x09);
/** The constant KZG_POINT_EVAL aka POINT_EVALUATION_PRECOMPILE_ADDRESS. */
public static final Address KZG_POINT_EVAL = Address.precompiled(0xA);
/** The constant PARENT_BEACON_BLOCK_ROOT_REGISTRY aka HISTORY_STORAGE_ADDRESS. */
public static final Address PARENT_BEACON_BLOCK_ROOT_REGISTRY = Address.precompiled(0xB);
/** The constant BLS12_G1ADD. */
public static final Address BLS12_G1ADD = Address.precompiled(0xA);
public static final Address BLS12_G1ADD = Address.precompiled(0xC);
/** The constant BLS12_G1MUL. */
public static final Address BLS12_G1MUL = Address.precompiled(0xB);
public static final Address BLS12_G1MUL = Address.precompiled(0xD);
/** The constant BLS12_G1MULTIEXP. */
public static final Address BLS12_G1MULTIEXP = Address.precompiled(0xC);
public static final Address BLS12_G1MULTIEXP = Address.precompiled(0xE);
/** The constant BLS12_G2ADD. */
public static final Address BLS12_G2ADD = Address.precompiled(0xD);
public static final Address BLS12_G2ADD = Address.precompiled(0xF);
/** The constant BLS12_G2MUL. */
public static final Address BLS12_G2MUL = Address.precompiled(0xE);
public static final Address BLS12_G2MUL = Address.precompiled(0x10);
/** The constant BLS12_G2MULTIEXP. */
public static final Address BLS12_G2MULTIEXP = Address.precompiled(0xF);
public static final Address BLS12_G2MULTIEXP = Address.precompiled(0x11);
/** The constant BLS12_PAIRING. */
public static final Address BLS12_PAIRING = Address.precompiled(0x10);
public static final Address BLS12_PAIRING = Address.precompiled(0x12);
/** The constant BLS12_MAP_FP_TO_G1. */
public static final Address BLS12_MAP_FP_TO_G1 = Address.precompiled(0x11);
public static final Address BLS12_MAP_FP_TO_G1 = Address.precompiled(0x13);
/** The constant BLS12_MAP_FP2_TO_G2. */
public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x12);
/** The constant KZG_POINT_EVAL. */
public static final Address KZG_POINT_EVAL = Address.precompiled(0x14);
public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x14);
/** The constant ZERO. */
public static final Address ZERO = Address.fromHexString("0x0");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public JsonRpcResponse response(
withdrawalsRoot,
null, // ToDo 4844: set with the value of data_gas_used field
null, // ToDo 4844: set with the value of excess_data_gas field
depositsRoot,
null, depositsRoot,
blockHeaderFunctions);

return new JsonRpcSuccessResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
payloadAttributes.getTimestamp(),
payloadAttributes.getPrevRandao(),
payloadAttributes.getSuggestedFeeRecipient(),
withdrawals));
withdrawals,
Optional.ofNullable(payloadAttributes.getParentBeaconBlockRoot())));

payloadId.ifPresent(
pid ->
Expand Down Expand Up @@ -210,6 +211,7 @@ private boolean isPayloadAttributesValid(
&& getWithdrawalsValidator(
protocolSchedule, headBlockHeader, payloadAttributes.getTimestamp())
.validateWithdrawals(maybeWithdrawals);
// TODO: stefan Add a parentBeaconBlockRoot validator to the protocolSchedule
}

private JsonRpcResponse handleNonValidForkchoiceUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
return respondWithInvalid(reqId, blockParam, null, INVALID, "Invalid versionedHash");
}

Optional<String> maybeParentBeaconBlockRootParam = requestContext.getOptionalParameter(2, String.class);

final Optional<BlockHeader> maybeParentHeader =
protocolContext.getBlockchain().getBlockHeader(blockParam.getParentHash());

Expand Down Expand Up @@ -202,6 +204,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
blockParam.getExcessDataGas() == null
? null
: DataGas.fromHexString(blockParam.getExcessDataGas()),
maybeParentBeaconBlockRootParam.map(Bytes32::fromHexString).orElse(null),
maybeDeposits.map(BodyValidation::depositsRoot).orElse(null),
headerFunctions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty()));

final var requestId = requestContext.getRequest().getId();
Expand Down Expand Up @@ -99,6 +100,7 @@ Optional<PayloadIdentifier> generatePayload(final EnginePreparePayloadParameter
param.getTimestamp().orElse(parentHeader.getTimestamp() + 1L),
param.getPrevRandao(),
param.getFeeRecipient(),
Optional.of(withdrawals)));
Optional.of(withdrawals),
param.getParentBeaconBlockRoot()));
}
}
Loading

0 comments on commit b6615df

Please sign in to comment.