Skip to content

Commit

Permalink
4844 engine api (#4991)
Browse files Browse the repository at this point in the history
* json container for getting blob bundles
* spdx headers, formatting, npe fix

Signed-off-by: Justin Florentine <justin+github@florentine.us>
Co-authored-by: Jason Frame <jason.frame@consensys.net>
Co-authored-by: garyschulte <garyschulte@gmail.com>
(cherry picked from commit 5fa9433)
(cherry picked from commit af8466f1561b4d5995bfa85782acee0644c3f879)
  • Loading branch information
jflo committed Jun 30, 2023
1 parent 8315c8d commit 9e44c6c
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,13 @@ public void setPublicWorldStateArchiveForPrivacyBlockProcessor(
public void setProtocolContext(final ProtocolContext protocolContext) {
this.protocolContext = protocolContext;
}

/**
* Exposes timestamp based schedules for reference.
*
* @return timestampSchedule the timestamp schedule
*/
public TimestampSchedule getTimestampSchedule() {
return timestampSchedule;
}
}
18 changes: 18 additions & 0 deletions datatypes/src/main/java/org/hyperledger/besu/datatypes/Blob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.datatypes;

public class Blob {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.datatypes;

public class KZGCommitment {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import java.util.Optional;

Expand All @@ -40,15 +40,15 @@ public abstract class AbstractEngineGetPayload extends ExecutionEngineJsonRpcMet
protected final BlockResultFactory blockResultFactory;
private static final Logger LOG = LoggerFactory.getLogger(AbstractEngineGetPayload.class);

protected final Optional<ProtocolSchedule> schedule;
protected final Optional<TimestampSchedule> schedule;

public AbstractEngineGetPayload(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final ProtocolSchedule schedule) {
final TimestampSchedule schedule) {
super(vertx, protocolContext, engineCallListener);
this.mergeMiningCoordinator = mergeMiningCoordinator;
this.blockResultFactory = blockResultFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import java.util.List;
import java.util.Optional;
Expand All @@ -43,7 +43,7 @@ public EngineGetBlobsBundleV1(
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final ProtocolSchedule schedule) {
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
Expand All @@ -63,23 +63,20 @@ protected JsonRpcResponse createResponse(

private BlobsBundleV1 createResponse(final Block block) {

final List<Transaction.BlobsWithCommitments> blobsWithCommitments =
List<Bytes> kzgs =
block.getBody().getTransactions().stream()
.map(Transaction::getBlobsWithCommitments)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

final List<String> kzgs =
blobsWithCommitments.stream()
.flatMap(b -> b.getKzgCommitments().stream())
.map(Bytes::toString)
.collect(Collectors.toList());

final List<String> blobs =
blobsWithCommitments.stream()
List<Bytes> blobs =
block.getBody().getTransactions().stream()
.map(Transaction::getBlobsWithCommitments)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(b -> b.getBlobs().stream())
.map(Bytes::toString)
.collect(Collectors.toList());

return new BlobsBundleV1(block.getHash(), kzgs, blobs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import java.util.Optional;

Expand All @@ -37,7 +37,7 @@ public EngineGetPayloadV1(
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final ProtocolSchedule schedule) {
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import java.util.Optional;

Expand All @@ -38,7 +38,7 @@ public EngineGetPayloadV2(
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final ProtocolSchedule schedule) {
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,31 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec;

import java.util.Optional;
import org.hyperledger.besu.ethereum.mainnet.DefaultTimestampSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

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 Optional<ScheduledProtocolSpec.Hardfork> shanghai;
private final Optional<ScheduledProtocolSpec.Hardfork> cancun;

public EngineGetPayloadV3(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final ProtocolSchedule schedule) {
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
mergeMiningCoordinator,
blockResultFactory,
engineCallListener,
schedule);
this.shanghai = schedule.hardforkFor(s -> s.fork().name().equalsIgnoreCase("Shanghai"));
this.cancun = schedule.hardforkFor(s -> s.fork().name().equalsIgnoreCase("Cancun"));
}

@Override
Expand All @@ -66,37 +54,26 @@ public String getName() {
protected JsonRpcResponse createResponse(
final JsonRpcRequestContext request, final BlockWithReceipts blockWithReceipts) {

try {
long builtAt = blockWithReceipts.getHeader().getTimestamp();

if (beforeShanghai(builtAt)) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV1(blockWithReceipts.getBlock()));
} else if (duringShanghai(builtAt)) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV2(blockWithReceipts));
} else {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}

} 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);
DefaultTimestampSchedule tsched = (DefaultTimestampSchedule) this.schedule.get();
long shanghaiTimestamp = tsched.scheduledAt("Shanghai");
long cancunTimestamp = tsched.scheduledAt("Cancun");
long builtAt = blockWithReceipts.getHeader().getTimestamp();
if (builtAt < shanghaiTimestamp) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV1(blockWithReceipts.getBlock()));
} else if (builtAt >= shanghaiTimestamp && builtAt < cancunTimestamp) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV2(blockWithReceipts));
} else if (builtAt >= cancunTimestamp) {
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}
}

private boolean duringShanghai(final long builtAt) {
return (this.shanghai.isPresent() && builtAt >= this.shanghai.get().milestone())
&& //
(this.cancun.isEmpty()
|| (this.cancun.isPresent() && builtAt < this.cancun.get().milestone()));
}

private boolean beforeShanghai(final long builtAt) {
return this.shanghai.isEmpty() || builtAt < this.shanghai.get().milestone();
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import io.vertx.core.Vertx;

public class EngineNewPayloadV3 extends AbstractEngineNewPayload {

public EngineNewPayloadV3(
final Vertx vertx,
final ProtocolSchedule timestampSchedule,
final TimestampSchedule timestampSchedule,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeCoordinator,
final EthPeers ethPeers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,35 @@

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.tuweni.bytes.Bytes;

@JsonPropertyOrder({"blockHash", "kzgs", "blobs"})
public class BlobsBundleV1 {

private final String blockHash;
private final Hash blockHash;

private final List<String> kzgs;
private final List<Bytes> kzgs;

private final List<String> blobs;
private final List<Bytes> blobs;

public BlobsBundleV1(final Hash blockHash, final List<String> kzgs, final List<String> blobs) {
this.blockHash = blockHash.toString();
public BlobsBundleV1(final Hash blockHash, final List<Bytes> kzgs, final List<Bytes> blobs) {
this.blockHash = blockHash;
this.kzgs = kzgs;
this.blobs = blobs;
}

@JsonGetter("blockHash")
public String getBlockHash() {
public Hash getBlockHash() {
return blockHash;
}

@JsonGetter("kzgs")
public List<String> getKzgs() {
public List<Bytes> getKzgs() {
return kzgs;
}

@JsonGetter("blobs")
public List<String> getBlobs() {
public List<Bytes> getBlobs() {
return blobs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetBlobsBundleV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadBodiesByHashV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadBodiesByRangeV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetBlobsBundleV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadV2;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadV3;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayloadV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayloadV2;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayloadV3;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EnginePreparePayloadDebug;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayloadV3;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineQosTimer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
Expand Down Expand Up @@ -86,21 +88,21 @@ protected Map<String, JsonRpcMethod> create() {
mergeCoordinator.get(),
blockResultFactory,
engineQosTimer,
protocolSchedule),
timestampSchedule),
new EngineGetPayloadV2(
consensusEngineServer,
protocolContext,
mergeCoordinator.get(),
blockResultFactory,
engineQosTimer,
protocolSchedule),
timestampSchedule),
new EngineGetPayloadV3(
consensusEngineServer,
protocolContext,
mergeCoordinator.get(),
blockResultFactory,
engineQosTimer,
protocolSchedule),
timestampSchedule),
new EngineNewPayloadV1(
consensusEngineServer,
protocolSchedule,
Expand All @@ -117,7 +119,7 @@ protected Map<String, JsonRpcMethod> create() {
engineQosTimer),
new EngineNewPayloadV3(
consensusEngineServer,
protocolSchedule,
timestampSchedule,
protocolContext,
mergeCoordinator.get(),
ethPeers,
Expand All @@ -140,7 +142,7 @@ protected Map<String, JsonRpcMethod> create() {
mergeCoordinator.get(),
new BlockResultFactory(),
engineQosTimer,
protocolSchedule),
timestampSchedule),
new EngineExchangeTransitionConfiguration(
consensusEngineServer, protocolContext, engineQosTimer),
new EngineGetPayloadBodiesByHashV1(
Expand Down
Loading

0 comments on commit 9e44c6c

Please sign in to comment.