Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the return of hardforkfor optional #5547

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void putTimestampMilestone(final long timestamp, final ProtocolSpec proto
}

@Override
public ScheduledProtocolSpec.Hardfork hardforkFor(
public Optional<ScheduledProtocolSpec.Hardfork> hardforkFor(
final Predicate<ScheduledProtocolSpec> predicate) {
return this.transitionUtils.dispatchFunctionAccordingToMergeState(
schedule -> schedule.hardforkFor(predicate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec;

import java.util.Optional;

import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EngineGetPayloadV3 extends AbstractEngineGetPayload {

private static final Logger LOG = LoggerFactory.getLogger(EngineGetPayloadV3.class);
private final ScheduledProtocolSpec.Hardfork shanghai;
private final ScheduledProtocolSpec.Hardfork cancun;
private final Optional<ScheduledProtocolSpec.Hardfork> shanghai;
private final Optional<ScheduledProtocolSpec.Hardfork> cancun;

public EngineGetPayloadV3(
final Vertx vertx,
Expand Down Expand Up @@ -70,20 +72,22 @@ protected JsonRpcResponse createResponse(
try {
long builtAt = blockWithReceipts.getHeader().getTimestamp();

if (builtAt < this.shanghai.milestone()) {
if (cancun.isPresent() && builtAt >= cancun.get().milestone()) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV1(blockWithReceipts.getBlock()));
} else if (builtAt >= this.shanghai.milestone() && builtAt < this.cancun.milestone()) {
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}

if (shanghai.isPresent() && builtAt >= this.shanghai.get().milestone()) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV2(blockWithReceipts));
} else {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}

return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV1(blockWithReceipts.getBlock()));

} catch (ClassCastException e) {
LOG.error("configuration error, can't call V3 endpoint with non-default protocol schedule");
return new JsonRpcErrorResponse(request.getRequest().getId(), JsonRpcError.INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void before() {
when(mergeContext.retrieveBlockById(mockPid)).thenReturn(Optional.of(mockBlockWithReceipts));
when(protocolContext.safeConsensusContext(Mockito.any())).thenReturn(Optional.of(mergeContext));
when(protocolSchedule.hardforkFor(any()))
.thenReturn(new ScheduledProtocolSpec.Hardfork("shanghai", SHANGHAI_AT));
.thenReturn(Optional.of(new ScheduledProtocolSpec.Hardfork("shanghai", SHANGHAI_AT)));
this.method =
methodFactory.create(
vertx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,12 @@ public boolean anyMatch(final Predicate<ScheduledProtocolSpec> predicate) {
}

@Override
public ScheduledProtocolSpec.Hardfork hardforkFor(
public Optional<ScheduledProtocolSpec.Hardfork> hardforkFor(
final Predicate<ScheduledProtocolSpec> predicate) {
ScheduledProtocolSpec spec =
this.protocolSpecs.stream()
.filter(predicate)
.findFirst()
.orElseThrow(
() -> new IllegalStateException("No hardfork found for predicate " + predicate));
return spec.fork();
return this.protocolSpecs.stream()
.filter(predicate)
.findFirst()
.map(ScheduledProtocolSpec::fork);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ default ProtocolSpec getForNextBlockHeader(

void putTimestampMilestone(final long timestamp, final ProtocolSpec protocolSpec);

default ScheduledProtocolSpec.Hardfork hardforkFor(
default Optional<ScheduledProtocolSpec.Hardfork> hardforkFor(
final Predicate<ScheduledProtocolSpec> predicate) {
throw new UnsupportedOperationException("Not implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void putTimestampMilestone(final long timestamp, final ProtocolSpec proto
}

@Override
public ScheduledProtocolSpec.Hardfork hardforkFor(
public Optional<ScheduledProtocolSpec.Hardfork> hardforkFor(
final Predicate<ScheduledProtocolSpec> predicate) {
return delegate.hardforkFor(predicate);
}
Expand Down