Skip to content

Commit

Permalink
Eip4788 parentBeaconBlockRoot (hyperledger#5758)
Browse files Browse the repository at this point in the history
Implement EIP 4788
Unit tests need to be added
Implementation is working for test net 8, address of the contract will probably change
EIP 4788 is still not finalized. Some changes might be necessary.
 
Signed-off-by: Stefan <stefan.pingel@consensys.net>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
pinges authored Aug 22, 2023
1 parent c3c7205 commit 47285a4
Show file tree
Hide file tree
Showing 55 changed files with 566 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static BlockHeader createBlockHeader(
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 @@ -265,6 +265,18 @@ public String getBlobGasUsed() {
return JsonUtil.getValueAsString(configRoot, "blobgasused", "0x0");
}

/**
* Gets parent beacon block root.
*
* @return the parent beacon block root
*/
public String getParentBeaconBlockRoot() {
return JsonUtil.getValueAsString(
configRoot,
"parentbeaconblockroot",
"0x0000000000000000000000000000000000000000000000000000000000000000");
}

/**
* Gets coinbase.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,22 @@ public MergeBlockCreator(
* @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<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 @@ -246,7 +246,8 @@ public PayloadIdentifier preparePayload(
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals) {
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 +274,12 @@ 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 +300,13 @@ public PayloadIdentifier preparePayload(
}
}

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

return payloadIdentifier;
}
Expand Down Expand Up @@ -334,10 +346,13 @@ private void tryToBuildBetterBlock(
final Bytes32 random,
final PayloadIdentifier payloadIdentifier,
final MergeBlockCreator mergeBlockCreator,
final Optional<List<Withdrawal>> withdrawals) {
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 @@ -41,14 +41,16 @@ public interface MergeMiningCoordinator extends MiningCoordinator {
* @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 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 @@ -147,9 +147,10 @@ public PayloadIdentifier preparePayload(
final Long timestamp,
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals) {
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 @@ -60,7 +60,6 @@
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.backwardsync.BackwardSyncContext;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
Expand Down Expand Up @@ -122,7 +121,6 @@ public class MergeCoordinatorTest implements MergeGenesisConfigHelper {
"ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f"));
private static final KeyPair KEYS1 =
new KeyPair(PRIVATE_KEY1, SIGNATURE_ALGORITHM.get().createPublicKey(PRIVATE_KEY1));
private static final Optional<List<Withdrawal>> EMPTY_WITHDRAWALS = Optional.empty();

private static final long REPETITION_MIN_DURATION = 100;
@Mock MergeContext mergeContext;
Expand Down Expand Up @@ -256,7 +254,8 @@ public void coinbaseShouldMatchSuggestedFeeRecipient() {
System.currentTimeMillis() / 1000,
Bytes32.ZERO,
suggestedFeeRecipient,
EMPTY_WITHDRAWALS);
Optional.empty(),
Optional.empty());

ArgumentCaptor<BlockWithReceipts> blockWithReceipts =
ArgumentCaptor.forClass(BlockWithReceipts.class);
Expand Down Expand Up @@ -294,7 +293,8 @@ 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()), eq(Optional.empty()));
return beingSpiedOn;
};

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

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

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

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

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

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

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

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

final CompletableFuture<Void> task1 = blockCreationTask;
Expand All @@ -623,6 +631,7 @@ public void shouldNotStartAnotherBlockCreationJobIfCalledAgainWithTheSamePayload
timestamp,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty());

assertThat(payloadId1).isEqualTo(payloadId2);
Expand Down Expand Up @@ -658,6 +667,7 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
timestamp,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty());

assertThat(coordinator.isBlockCreationCancelled(payloadId1)).isFalse();
Expand All @@ -668,6 +678,7 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
timestamp + 1,
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty());

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

ArgumentCaptor<BlockWithReceipts> blockWithReceipts =
Expand Down
26 changes: 15 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 @@ -57,26 +57,30 @@ 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);
// TODO: this is not a precompile anymore. The address is correct for testnet 8. Fix after testnet
// 8 when we know what the real address is
/** 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,6 +129,7 @@ public JsonRpcResponse response(
withdrawalsRoot,
null, // ToDo 4844: set with the value of blob_gas_used field
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
depositsRoot,
blockHeaderFunctions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum RpcMethod {
ENGINE_NEW_PAYLOAD_V3("engine_newPayloadV3"),
ENGINE_FORKCHOICE_UPDATED_V1("engine_forkchoiceUpdatedV1"),
ENGINE_FORKCHOICE_UPDATED_V2("engine_forkchoiceUpdatedV2"),
ENGINE_FORKCHOICE_UPDATED_V3("engine_forkchoiceUpdatedV3"),
ENGINE_EXCHANGE_TRANSITION_CONFIGURATION("engine_exchangeTransitionConfigurationV1"),
ENGINE_GET_PAYLOAD_BODIES_BY_HASH_V1("engine_getPayloadBodiesByHashV1"),
ENGINE_GET_PAYLOAD_BODIES_BY_RANGE_V1("engine_getPayloadBodiesByRangeV1"),
Expand Down
Loading

0 comments on commit 47285a4

Please sign in to comment.