Skip to content

Commit

Permalink
Merge branch 'mining-options-refactor' into time-limited-block-creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fab-10 committed Oct 18, 2023
2 parents 28a32af + ae775ae commit ca5095d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 39 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3174/badge)](https://bestpractices.coreinfrastructure.org/projects/3174)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/hyperledger/besu/blob/main/LICENSE)
[![Discord](https://img.shields.io/discord/905194001349627914?logo=Hyperledger&style=plastic)](https://discord.gg/hyperledger)
[![Twitter Follow](https://img.shields.io/twitter/follow/HyperledgerBesu)](https://twitter.com/HyperledgerBesu)

[Download](https://hyperledger.jfrog.io/artifactory/besu-binaries/besu/)

Expand All @@ -15,7 +16,7 @@ Besu is an Apache 2.0 licensed, MainNet compatible, Ethereum client written in J
* [Besu Issues]
* [Besu Wiki](https://wiki.hyperledger.org/display/BESU/Hyperledger+Besu)
* [How to Contribute to Besu](https://wiki.hyperledger.org/display/BESU/How+to+Contribute)
* [Besu Roadmap](https://wiki.hyperledger.org/display/BESU/Roadmap)
* [Besu Roadmap & Planning](https://wiki.hyperledger.org/pages/viewpage.action?pageId=24781786)


## Issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,21 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
if (maybeNewHead.isEmpty()) {
return syncingResponse(requestId, forkChoice);
}
Optional<List<Withdrawal>> withdrawals = Optional.empty();
final BlockHeader newHead = maybeNewHead.get();

ForkchoiceResult forkchoiceResult = null;
if (!isValidForkchoiceState(
forkChoice.getSafeBlockHash(), forkChoice.getFinalizedBlockHash(), newHead)) {
forkChoice.getSafeBlockHash(), forkChoice.getFinalizedBlockHash(), maybeNewHead.get())) {
logForkchoiceUpdatedCall(INVALID, forkChoice);
return new JsonRpcErrorResponse(requestId, RpcErrorType.INVALID_FORKCHOICE_STATE);
} else {
forkchoiceResult =
mergeCoordinator.updateForkChoice(
maybeNewHead.get(),
forkChoice.getFinalizedBlockHash(),
forkChoice.getSafeBlockHash());
}
ForkchoiceResult result =
mergeCoordinator.updateForkChoice(
newHead, forkChoice.getFinalizedBlockHash(), forkChoice.getSafeBlockHash());

Optional<List<Withdrawal>> withdrawals = Optional.empty();
if (maybePayloadAttributes.isPresent()) {
final EnginePayloadAttributesParameter payloadAttributes = maybePayloadAttributes.get();
withdrawals =
Expand All @@ -136,7 +140,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
.map(WithdrawalParameter::toWithdrawal)
.collect(toList())));
Optional<JsonRpcErrorResponse> maybeError =
isPayloadAttributesValid(requestId, payloadAttributes, withdrawals, newHead);
isPayloadAttributesValid(requestId, payloadAttributes, withdrawals);
if (maybeError.isPresent()) {
LOG.atWarn()
.setMessage("RpcError {}: {}")
Expand All @@ -156,6 +160,20 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
}
}

final BlockHeader newHead = maybeNewHead.get();
if (maybePayloadAttributes.isPresent()) {
Optional<JsonRpcErrorResponse> maybeError =
isPayloadAttributeRelevantToNewHead(requestId, maybePayloadAttributes.get(), newHead);
if (maybeError.isPresent()) {
return maybeError.get();
}
if (!getWithdrawalsValidator(
protocolSchedule.get(), newHead, maybePayloadAttributes.get().getTimestamp())
.validateWithdrawals(withdrawals)) {
return new JsonRpcErrorResponse(requestId, getInvalidPayloadError());
}
}

ValidationResult<RpcErrorType> parameterValidationResult =
validateParameter(forkChoice, maybePayloadAttributes);
if (!parameterValidationResult.isValid()) {
Expand All @@ -169,13 +187,13 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
maybePayloadAttributes.ifPresentOrElse(
this::logPayload, () -> LOG.debug("Payload attributes are null"));

if (result.shouldNotProceedToPayloadBuildProcess()) {
if (ForkchoiceResult.Status.IGNORE_UPDATE_TO_OLD_HEAD.equals(result.getStatus())) {
if (forkchoiceResult.shouldNotProceedToPayloadBuildProcess()) {
if (ForkchoiceResult.Status.IGNORE_UPDATE_TO_OLD_HEAD.equals(forkchoiceResult.getStatus())) {
logForkchoiceUpdatedCall(VALID, forkChoice);
} else {
logForkchoiceUpdatedCall(INVALID, forkChoice);
}
return handleNonValidForkchoiceUpdate(requestId, result);
return handleNonValidForkchoiceUpdate(requestId, forkchoiceResult);
}

// begin preparing a block if we have a non-empty payload attributes param
Expand Down Expand Up @@ -205,27 +223,26 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
requestId,
new EngineUpdateForkchoiceResult(
VALID,
result.getNewHead().map(BlockHeader::getHash).orElse(null),
forkchoiceResult.getNewHead().map(BlockHeader::getHash).orElse(null),
payloadId.orElse(null),
Optional.empty()));
}

protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
protected abstract Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId,
final EnginePayloadAttributesParameter payloadAttributes,
final Optional<List<Withdrawal>> maybeWithdrawals);

protected Optional<JsonRpcErrorResponse> isPayloadAttributeRelevantToNewHead(
final Object requestId,
final EnginePayloadAttributesParameter payloadAttributes,
final Optional<List<Withdrawal>> maybeWithdrawals,
final BlockHeader headBlockHeader) {

if (payloadAttributes.getTimestamp() <= headBlockHeader.getTimestamp()) {
LOG.warn(
"Payload attributes timestamp is smaller than timestamp of header in fork choice update");
return Optional.of(new JsonRpcErrorResponse(requestId, getInvalidPayloadError()));
}
if (!getWithdrawalsValidator(
protocolSchedule.get(), headBlockHeader, payloadAttributes.getTimestamp())
.validateWithdrawals(maybeWithdrawals)) {
return Optional.of(new JsonRpcErrorResponse(requestId, getInvalidPayloadError()));
}

return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePayloadAttributesParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;

import java.util.List;
import java.util.Optional;

import io.vertx.core.Vertx;

public class EngineForkchoiceUpdatedV1 extends AbstractEngineForkchoiceUpdated {
Expand All @@ -33,6 +39,14 @@ public EngineForkchoiceUpdatedV1(
super(vertx, protocolSchedule, protocolContext, mergeCoordinator, engineCallListener);
}

@Override
protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId,
final EnginePayloadAttributesParameter payloadAttributes,
final Optional<List<Withdrawal>> maybeWithdrawals) {
return Optional.empty();
}

@Override
public String getName() {
return RpcMethod.ENGINE_FORKCHOICE_UPDATED_V1.getMethodName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePayloadAttributesParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;

Expand Down Expand Up @@ -55,22 +54,20 @@ public String getName() {
protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId,
final EnginePayloadAttributesParameter payloadAttributes,
final Optional<List<Withdrawal>> maybeWithdrawals,
final BlockHeader headBlockHeader) {
final Optional<List<Withdrawal>> maybeWithdrawals) {
if (payloadAttributes.getTimestamp() >= cancunTimestamp) {
if (payloadAttributes.getParentBeaconBlockRoot() == null
|| payloadAttributes.getParentBeaconBlockRoot().isEmpty()
|| payloadAttributes.getParentBeaconBlockRoot().isZero()) {
|| payloadAttributes.getParentBeaconBlockRoot().isEmpty()) {
return Optional.of(new JsonRpcErrorResponse(requestId, RpcErrorType.UNSUPPORTED_FORK));
} else {
return Optional.of(new JsonRpcErrorResponse(requestId, RpcErrorType.INVALID_PARAMS));
}
return Optional.of(new JsonRpcErrorResponse(requestId, RpcErrorType.UNSUPPORTED_FORK));
} else if (payloadAttributes.getParentBeaconBlockRoot() != null) {
LOG.error(
"Parent beacon block root hash present in payload attributes before cancun hardfork");
return Optional.of(new JsonRpcErrorResponse(requestId, RpcErrorType.INVALID_PARAMS));
} else {
return super.isPayloadAttributesValid(
requestId, payloadAttributes, maybeWithdrawals, headBlockHeader);
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.EnginePayloadAttributesParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec;
Expand Down Expand Up @@ -94,14 +93,8 @@ protected ValidationResult<RpcErrorType> validateForkSupported(final long blockT
protected Optional<JsonRpcErrorResponse> isPayloadAttributesValid(
final Object requestId,
final EnginePayloadAttributesParameter payloadAttributes,
final Optional<List<Withdrawal>> maybeWithdrawals,
final BlockHeader headBlockHeader) {
Optional<JsonRpcErrorResponse> maybeError =
super.isPayloadAttributesValid(
requestId, payloadAttributes, maybeWithdrawals, headBlockHeader);
if (maybeError.isPresent()) {
return maybeError;
} else if (payloadAttributes.getParentBeaconBlockRoot() == null) {
final Optional<List<Withdrawal>> maybeWithdrawals) {
if (payloadAttributes.getParentBeaconBlockRoot() == null) {
LOG.error(
"Parent beacon block root hash not present in payload attributes after cancun hardfork");
return Optional.of(new JsonRpcErrorResponse(requestId, RpcErrorType.INVALID_PARAMS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public PeerReputation() {
}

public PeerReputation(final int initialScore, final int maxScore) {
checkArgument(initialScore <= maxScore, "Inital score must be less than or equal to max score");
checkArgument(
initialScore <= maxScore, "Initial score must be less than or equal to max score");
this.maxScore = maxScore;
this.score = initialScore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public Difficulty totalDifficulty() {
}

/**
* Return The hash of the head of the associated node's local blockchian.
* Return The hash of the head of the associated node's local blockchain.
*
* @return The hash of the head of the associated node's local blockchian.
* @return The hash of the head of the associated node's local blockchain.
*/
public Hash bestHash() {
return status().bestHash;
Expand Down

0 comments on commit ca5095d

Please sign in to comment.