Skip to content

Commit

Permalink
feat:add optimism field in payload attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Dec 5, 2023
1 parent bd665ed commit 142dbb3
Show file tree
Hide file tree
Showing 24 changed files with 327 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void setUp() {
when(mockProtocolSpec.getTransactionValidatorFactory())
.thenReturn(mockTransactionValidatorFactory);
when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
when(mockTransactionValidatorFactory.get().validate(any(), any(Optional.class), any()))
when(mockTransactionValidatorFactory.get().validate(any(), any(), any(Optional.class), any()))
.thenReturn(ValidationResult.valid());
when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any()))
.thenReturn(ValidationResult.valid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ default boolean isConsensusMigration() {
*/
OptionalLong getCanyonTime();



/**
* Gets base fee per gas.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
/*
* Copyright ConsenSys AG.
* Copyright OptimismJ.
*
* 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.config;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.OptionalLong;

/**
* the Optimism config options.
*/
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableMap;

/** the Optimism config options. */
public class OptimismConfigOptions {

/** The constant DEFAULT. */
Expand Down Expand Up @@ -64,5 +78,4 @@ Map<String, Object> asMap() {
getEIP1559DenominatorCanyon().ifPresent(l -> builder.put("eip1559DenominatorCanyon", l));
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public class StubGenesisConfigOptions implements GenesisConfigOptions, Cloneable
private OptionalLong cancunTime = OptionalLong.empty();
private OptionalLong futureEipsTime = OptionalLong.empty();
private OptionalLong experimentalEipsTime = OptionalLong.empty();
private OptionalLong bedrockBlock = OptionalLong.empty();
private OptionalLong regolithTime = OptionalLong.empty();
private OptionalLong canyonTime = OptionalLong.empty();

private final OptionalLong bedrockBlock = OptionalLong.empty();
private final OptionalLong regolithTime = OptionalLong.empty();
private final OptionalLong canyonTime = OptionalLong.empty();

private OptionalLong terminalBlockNumber = OptionalLong.empty();
private Optional<Hash> terminalBlockHash = Optional.empty();
Expand Down
1 change: 1 addition & 0 deletions consensus/merge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation project(':ethereum:eth')
implementation project(':ethereum:p2p')
implementation project(':ethereum:trie')
implementation project(':ethereum:rlp')
implementation project(':evm')
implementation project(':plugin-api')
implementation project(':util')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ public PayloadIdentifier preparePayload(
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals,
final Optional<Bytes32> parentBeaconBlockRoot) {
final Optional<Bytes32> parentBeaconBlockRoot,
final Optional<Boolean> noTxPool,
final Optional<List<Transaction>> transactions,
final Optional<Long> gasLimit) {

// 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 @@ -260,7 +263,10 @@ public PayloadIdentifier preparePayload(
prevRandao,
feeRecipient,
withdrawals,
parentBeaconBlockRoot);
parentBeaconBlockRoot,
noTxPool,
transactions,
gasLimit);

if (blockCreationTasks.containsKey(payloadIdentifier)) {
LOG.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.Withdrawal;

import java.util.List;
Expand All @@ -42,6 +43,9 @@ public interface MergeMiningCoordinator extends MiningCoordinator {
* @param feeRecipient the fee recipient
* @param withdrawals the optional list of withdrawals
* @param parentBeaconBlockRoot optional root hash of the parent beacon block
* @param noTxPool optional flag to indicate that the transaction pool should not be used
* @param transactions the optional list of transactions
* @param gasLimit the optional gas limit
* @return the payload identifier
*/
PayloadIdentifier preparePayload(
Expand All @@ -50,7 +54,10 @@ PayloadIdentifier preparePayload(
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals,
final Optional<Bytes32> parentBeaconBlockRoot);
final Optional<Bytes32> parentBeaconBlockRoot,
final Optional<Boolean> noTxPool,
final Optional<List<Transaction>> transactions,
final Optional<Long> gasLimit);

@Override
default boolean isCompatibleWithEngineApi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Quantity;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;

import java.math.BigInteger;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.primitives.Longs;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt64;
import org.bouncycastle.jcajce.provider.digest.SHA256;
import org.web3j.utils.Numeric;

/** The Payload identifier. */
public class PayloadIdentifier implements Quantity {
Expand Down Expand Up @@ -63,6 +69,9 @@ public PayloadIdentifier(final Long payloadId) {
* @param feeRecipient the fee recipient
* @param withdrawals the withdrawals
* @param parentBeaconBlockRoot the parent beacon block root
* @param noTxPool the no tx pool
* @param transactions the transactions
* @param gasLimit the gas limit
* @return the payload identifier
*/
public static PayloadIdentifier forPayloadParams(
Expand All @@ -71,7 +80,36 @@ public static PayloadIdentifier forPayloadParams(
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals,
final Optional<Bytes32> parentBeaconBlockRoot) {
final Optional<Bytes32> parentBeaconBlockRoot,
final Optional<Boolean> noTxPool,
final Optional<List<Transaction>> transactions,
final Optional<Long> gasLimit) {
// Optimism: gasLimit will exist
if (gasLimit.isPresent()) {
SHA256.Digest digest = new SHA256.Digest();
digest.update(parentHash.toArrayUnsafe());
digest.update(Longs.toByteArray(timestamp));
digest.update(prevRandao.toArrayUnsafe());
digest.update(feeRecipient.toArrayUnsafe());
withdrawals.ifPresent(
withdrawalList -> {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
out.writeList(withdrawalList, Withdrawal::writeTo);
digest.update(out.encoded().toArrayUnsafe());
});
parentBeaconBlockRoot.ifPresent(
parentBeaconBlockRootBytes -> digest.update(parentBeaconBlockRootBytes.toArrayUnsafe()));
boolean noTxPoolFlag = noTxPool.orElse(false);
List<Transaction> txList = transactions.orElse(Collections.emptyList());
if (noTxPoolFlag || !txList.isEmpty()) {
digest.update(new byte[] {(byte) (noTxPoolFlag ? 1 : 0)});
digest.update(Longs.toByteArray(txList.size()));
txList.forEach(tx -> digest.update(tx.getHash().toArrayUnsafe()));
}
digest.update(Longs.toByteArray(gasLimit.get()));

return new PayloadIdentifier(Numeric.toBigInt(digest.digest(), 0, 8).longValue());
}

return new PayloadIdentifier(
timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,20 @@ public PayloadIdentifier preparePayload(
final Bytes32 prevRandao,
final Address feeRecipient,
final Optional<List<Withdrawal>> withdrawals,
final Optional<Bytes32> parentBeaconBlockRoot) {
final Optional<Bytes32> parentBeaconBlockRoot,
final Optional<Boolean> noTxPool,
final Optional<List<Transaction>> transactions,
final Optional<Long> gasLimit) {
return mergeCoordinator.preparePayload(
parentHeader, timestamp, prevRandao, feeRecipient, withdrawals, parentBeaconBlockRoot);
parentHeader,
timestamp,
prevRandao,
feeRecipient,
withdrawals,
parentBeaconBlockRoot,
noTxPool,
transactions,
gasLimit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ public void coinbaseShouldMatchSuggestedFeeRecipient() {
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

ArgumentCaptor<PayloadWrapper> payloadWrapper = ArgumentCaptor.forClass(PayloadWrapper.class);
Expand Down Expand Up @@ -333,6 +336,9 @@ public void exceptionDuringBuildingBlockShouldNotBeInvalid()
Bytes32.random(),
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

verify(willThrow, never()).addBadBlock(any(), any());
Expand Down Expand Up @@ -373,6 +379,9 @@ public void shouldNotRecordProposedBadBlockToBadBlockManager()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

verify(badBlockManager, never()).addBadBlock(any(), any());
Expand Down Expand Up @@ -406,6 +415,9 @@ public void shouldContinueBuildingBlocksUntilFinalizeIsCalled()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

blockCreationTask.get();
Expand Down Expand Up @@ -464,6 +476,9 @@ public void blockCreationRepetitionShouldTakeNotLessThanRepetitionMinDuration()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

blockCreationTask.get();
Expand Down Expand Up @@ -515,6 +530,9 @@ public void shouldRetryBlockCreationOnRecoverableError()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

blockCreationTask.get();
Expand Down Expand Up @@ -564,6 +582,9 @@ public void shouldStopRetryBlockCreationIfTimeExpired() throws InterruptedExcept
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

try {
Expand Down Expand Up @@ -610,6 +631,9 @@ public void shouldStopInProgressBlockCreationIfFinalizedIsCalled()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

waitForBlockCreationInProgress.await();
Expand Down Expand Up @@ -664,6 +688,9 @@ public void shouldNotStartAnotherBlockCreationJobIfCalledAgainWithTheSamePayload
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

final CompletableFuture<Void> task1 = blockCreationTask;
Expand All @@ -675,6 +702,9 @@ public void shouldNotStartAnotherBlockCreationJobIfCalledAgainWithTheSamePayload
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

assertThat(payloadId1).isEqualTo(payloadId2);
Expand Down Expand Up @@ -716,6 +746,9 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

assertThat(coordinator.isBlockCreationCancelled(payloadId1)).isFalse();
Expand All @@ -727,6 +760,9 @@ public void shouldCancelPreviousBlockCreationJobIfCalledAgainWithNewPayloadId()
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

assertThat(payloadId1).isNotEqualTo(payloadId2);
Expand Down Expand Up @@ -757,6 +793,9 @@ public void shouldUseExtraDataFromMiningParameters() {
Bytes32.ZERO,
suggestedFeeRecipient,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());

ArgumentCaptor<PayloadWrapper> payloadWrapper = ArgumentCaptor.forClass(PayloadWrapper.class);
Expand Down
Loading

0 comments on commit 142dbb3

Please sign in to comment.