Skip to content

Commit

Permalink
Make the return of hardforkfor optional (#5547)
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia authored and jflo committed Jun 14, 2023
1 parent 2fee830 commit 500891d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
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

0 comments on commit 500891d

Please sign in to comment.