Skip to content

Commit

Permalink
4844 engine api (hyperledger#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 May 24, 2023
1 parent 6fef25a commit 36577b6
Show file tree
Hide file tree
Showing 21 changed files with 762 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,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 @@ -51,15 +51,19 @@ public enum RpcMethod {
DEBUG_GET_RAW_RECEIPTS("debug_getRawReceipts"),
ENGINE_GET_PAYLOAD_V1("engine_getPayloadV1"),
ENGINE_GET_PAYLOAD_V2("engine_getPayloadV2"),
ENGINE_GET_PAYLOAD_V3("engine_getPayloadV3"),
ENGINE_EXECUTE_PAYLOAD("engine_executePayloadV1"),
ENGINE_NEW_PAYLOAD_V1("engine_newPayloadV1"),
ENGINE_NEW_PAYLOAD_V2("engine_newPayloadV2"),
ENGINE_NEW_PAYLOAD_V3("engine_newPayloadV3"),
ENGINE_FORKCHOICE_UPDATED_V1("engine_forkchoiceUpdatedV1"),
ENGINE_FORKCHOICE_UPDATED_V2("engine_forkchoiceUpdatedV2"),
ENGINE_EXCHANGE_TRANSITION_CONFIGURATION("engine_exchangeTransitionConfigurationV1"),
ENGINE_GET_PAYLOAD_BODIES_BY_HASH_V1("engine_getPayloadBodiesByHashV1"),
ENGINE_GET_PAYLOAD_BODIES_BY_RANGE_V1("engine_getPayloadBodiesByRangeV1"),
ENGINE_EXCHANGE_CAPABILITIES("engine_exchangeCapabilities"),
ENGINE_PREPARE_PAYLOAD_DEBUG("engine_preparePayload_debug"),
ENGINE_GET_BLOBS_BUNDLE_V1("engine_getBlobsBundleV1"),
PRIV_CALL("priv_call"),
PRIV_GET_PRIVATE_TRANSACTION("priv_getPrivateTransaction"),
PRIV_GET_TRANSACTION_COUNT("priv_getTransactionCount"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +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.TimestampSchedule;

import java.util.Optional;

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

protected final Optional<TimestampSchedule> schedule;

public AbstractEngineGetPayload(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener) {
final EngineCallListener engineCallListener,
final TimestampSchedule schedule) {
super(vertx, protocolContext, engineCallListener);
this.mergeMiningCoordinator = mergeMiningCoordinator;
this.blockResultFactory = blockResultFactory;
this.schedule = Optional.ofNullable(schedule);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_PARAMS;

import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
import org.hyperledger.besu.datatypes.DataGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
Expand Down Expand Up @@ -160,9 +161,10 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
blockParam.getPrevRandao(),
0,
maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null),
null,
null,
headerFunctions);
blockParam.getExcessDataGas() == null
? null
: DataGas.fromHexString(blockParam.getExcessDataGas()),
null, headerFunctions);

// ensure the block hash matches the blockParam hash
// this must be done before any other check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.ethereum.api.jsonrpc.internal.methods.engine;

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.JsonRpcRequestContext;
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.BlobsBundleV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
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.TimestampSchedule;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import io.vertx.core.Vertx;
import org.apache.tuweni.bytes.Bytes;

public class EngineGetBlobsBundleV1 extends AbstractEngineGetPayload {

public EngineGetBlobsBundleV1(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
mergeMiningCoordinator,
blockResultFactory,
engineCallListener,
schedule);
}

@Override
protected JsonRpcResponse createResponse(
final JsonRpcRequestContext request, final BlockWithReceipts blockWithReceipts) {

return new JsonRpcSuccessResponse(
request.getRequest().getId(), createResponse(blockWithReceipts.getBlock()));
}

private BlobsBundleV1 createResponse(final Block block) {

List<Bytes> kzgs =
block.getBody().getTransactions().stream()
.map(Transaction::getBlobsWithCommitments)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(b -> b.getKzgCommitments().stream())
.collect(Collectors.toList());

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

return new BlobsBundleV1(block.getHash(), kzgs, blobs);
}

@Override
public String getName() {
return RpcMethod.ENGINE_GET_BLOBS_BUNDLE_V1.getMethodName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +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.TimestampSchedule;

import java.util.Optional;

Expand All @@ -35,8 +36,15 @@ public EngineGetPayloadV1(
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener) {
super(vertx, protocolContext, mergeMiningCoordinator, blockResultFactory, engineCallListener);
final EngineCallListener engineCallListener,
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
mergeMiningCoordinator,
blockResultFactory,
engineCallListener,
schedule);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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.TimestampSchedule;

import java.util.Optional;

Expand All @@ -36,8 +37,15 @@ public EngineGetPayloadV2(
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener) {
super(vertx, protocolContext, mergeMiningCoordinator, blockResultFactory, engineCallListener);
final EngineCallListener engineCallListener,
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
mergeMiningCoordinator,
blockResultFactory,
engineCallListener,
schedule);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.ethereum.api.jsonrpc.internal.methods.engine;

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.JsonRpcRequestContext;
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.DefaultTimestampSchedule;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import io.vertx.core.Vertx;

public class EngineGetPayloadV3 extends AbstractEngineGetPayload {

public EngineGetPayloadV3(
final Vertx vertx,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeMiningCoordinator,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener,
final TimestampSchedule schedule) {
super(
vertx,
protocolContext,
mergeMiningCoordinator,
blockResultFactory,
engineCallListener,
schedule);
}

@Override
public String getName() {
return RpcMethod.ENGINE_GET_PAYLOAD_V3.getMethodName();
}

@Override
protected JsonRpcResponse createResponse(
final JsonRpcRequestContext request, final BlockWithReceipts blockWithReceipts) {

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));
}

return new JsonRpcSuccessResponse(
request.getRequest().getId(),
blockResultFactory.payloadTransactionCompleteV3(blockWithReceipts));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.ethereum.api.jsonrpc.internal.methods.engine;

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.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.mainnet.TimestampSchedule;

import io.vertx.core.Vertx;

public class EngineNewPayloadV3 extends AbstractEngineNewPayload {

public EngineNewPayloadV3(
final Vertx vertx,
final TimestampSchedule timestampSchedule,
final ProtocolContext protocolContext,
final MergeMiningCoordinator mergeCoordinator,
final EthPeers ethPeers,
final EngineCallListener engineCallListener) {
super(
vertx, timestampSchedule, protocolContext, mergeCoordinator, ethPeers, engineCallListener);
}

@Override
public String getName() {
return RpcMethod.ENGINE_NEW_PAYLOAD_V3.getMethodName();
}
}
Loading

0 comments on commit 36577b6

Please sign in to comment.