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

Add engine get payload body #4909

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add engine get payload body
Signed-off-by: Zhenyang Shi <wcgcyx@gmail.com>
  • Loading branch information
wcgcyx committed Jan 12, 2023
commit 46017377499d7062ff93568e7e9c2f46d1cabf24
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public enum RpcMethod {
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"),

GOQUORUM_ETH_GET_QUORUM_PAYLOAD("eth_getQuorumPayload"),
GOQUORUM_STORE_RAW("goquorum_storeRaw"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine;

import static org.hyperledger.besu.util.Slf4jLambdaHelper.traceLambda;

import org.hyperledger.besu.datatypes.Hash;
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.methods.ExecutionEngineJsonRpcMethod;
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.chain.Blockchain;

import java.util.Arrays;

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

public class EngineGetPayloadBodiesByHashV1 extends ExecutionEngineJsonRpcMethod {
private static final Logger LOG = LoggerFactory.getLogger(EngineGetPayloadBodiesByHashV1.class);
private final BlockResultFactory blockResultFactory;

public EngineGetPayloadBodiesByHashV1(
final Vertx vertx,
final ProtocolContext protocolContext,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener) {
super(vertx, protocolContext, engineCallListener);
this.blockResultFactory = blockResultFactory;
}

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

@Override
public JsonRpcResponse syncResponse(final JsonRpcRequestContext request) {
engineCallListener.executionEngineCalled();

final Hash[] blockHashes = request.getRequiredParameter(0, Hash[].class);

traceLambda(
LOG, "EngineGetPayloadBodiesByHashV1 parameters: blockHashes {}", () -> blockHashes);

final Blockchain blockchain = protocolContext.getBlockchain();
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
Arrays.stream(blockHashes)
.map(
blockHash ->
blockchain
.getBlockBody(blockHash)
.map(blockResultFactory::payloadBodyCompleteV1)
.orElse(null))
.toArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine;

import static org.hyperledger.besu.util.Slf4jLambdaHelper.traceLambda;

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.methods.ExecutionEngineJsonRpcMethod;
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.chain.Blockchain;

import java.util.stream.LongStream;

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

public class EngineGetPayloadBodiesByRangeV1 extends ExecutionEngineJsonRpcMethod {
private static final Logger LOG = LoggerFactory.getLogger(EngineGetPayloadBodiesByRangeV1.class);
private final BlockResultFactory blockResultFactory;

public EngineGetPayloadBodiesByRangeV1(
final Vertx vertx,
final ProtocolContext protocolContext,
final BlockResultFactory blockResultFactory,
final EngineCallListener engineCallListener) {
super(vertx, protocolContext, engineCallListener);
this.blockResultFactory = blockResultFactory;
}

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

@Override
public JsonRpcResponse syncResponse(final JsonRpcRequestContext request) {
engineCallListener.executionEngineCalled();

final long startBlockNumber = request.getRequiredParameter(0, Long.class);
final long count = request.getRequiredParameter(1, Long.class);

traceLambda(
LOG,
"EngineGetPayloadBodiesByRangeV1 parameters: start block number {} count {}",
() -> startBlockNumber,
() -> count);

final Blockchain blockchain = protocolContext.getBlockchain();
return new JsonRpcSuccessResponse(
request.getRequest().getId(),
LongStream.range(startBlockNumber, startBlockNumber + count)
.mapToObj(
blockNumber ->
blockchain
.getBlockHashByNumber(blockNumber)
.map(
blockHash ->
blockchain
.getBlockBody(blockHash)
.map(blockResultFactory::payloadBodyCompleteV1))
.orElse(null))
.toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.ethereum.api.query.BlockWithMetadata;
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;

Expand Down Expand Up @@ -106,6 +107,15 @@ public EngineGetPayloadResultV2 payloadTransactionCompleteV2(final Block block)
return new EngineGetPayloadResultV2(block.getHeader(), txs, Quantity.create(blockValue));
}

public EngineGetPayloadBodyResultV1 payloadBodyCompleteV1(final BlockBody blockBody) {
final List<String> txs =
blockBody.getTransactions().stream()
.map(TransactionEncoder::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());
return new EngineGetPayloadBodyResultV1(txs);
}

private long calculateBlockValue(final List<String> ignored) {
return 0L;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonValue;

public class EngineGetPayloadBodyResultV1 {
private final List<String> transactions;

public EngineGetPayloadBodyResultV1(final List<String> transactions) {
this.transactions = transactions;
}

@JsonValue
public List<String> getTransactions() {
return transactions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineExchangeTransitionConfiguration;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineForkchoiceUpdatedV1;
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.EngineGetPayloadV1;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayloadV2;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayloadV1;
Expand Down Expand Up @@ -89,7 +91,11 @@ protected Map<String, JsonRpcMethod> create() {
new EngineForkchoiceUpdatedV1(
consensusEngineServer, protocolContext, mergeCoordinator.get(), engineQosTimer),
new EngineExchangeTransitionConfiguration(
consensusEngineServer, protocolContext, engineQosTimer));
consensusEngineServer, protocolContext, engineQosTimer),
new EngineGetPayloadBodiesByHashV1(
consensusEngineServer, protocolContext, blockResultFactory, engineQosTimer),
new EngineGetPayloadBodiesByRangeV1(
consensusEngineServer, protocolContext, blockResultFactory, engineQosTimer));
} else {
return mapOf(
new EngineExchangeTransitionConfiguration(
Expand Down