Skip to content

Commit

Permalink
MERGE: engine_exchangeTransitionConfigurationV1 API (hyperledger#3473)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Lehrner <[email protected]>
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
daniellehrner authored Mar 8, 2022
1 parent 6efdd94 commit 424ed9c
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum RpcMethod {
ENGINE_EXECUTE_PAYLOAD("engine_executePayloadV1"),
ENGINE_NEW_PAYLOAD("engine_newPayloadV1"),
ENGINE_FORKCHOICE_UPDATED("engine_forkchoiceUpdatedV1"),
ENGINE_EXCHANGE_TRANSITION_CONFIGURATION("engine_exchangeTransitionConfigurationV1"),

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,117 @@
/*
* 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 static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_PARAMS;
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.parameters.EngineExchangeTransitionConfigurationParameter;
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.EngineExchangeTransitionConfigurationResult;
import org.hyperledger.besu.ethereum.core.BlockHeader;

import java.util.Optional;

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

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

public EngineExchangeTransitionConfiguration(
final Vertx vertx, final ProtocolContext protocolContext) {
super(vertx, protocolContext);
}

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

@Override
public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext) {
final EngineExchangeTransitionConfigurationParameter remoteTransitionConfiguration =
requestContext.getRequiredParameter(
0, EngineExchangeTransitionConfigurationParameter.class);
final Object reqId = requestContext.getRequest().getId();

traceLambda(
LOG,
"received transitionConfiguration: {}",
() -> Json.encodePrettily(remoteTransitionConfiguration));

if (remoteTransitionConfiguration.getTerminalBlockNumber() != 0L) {
return respondWithError(reqId, INVALID_PARAMS);
}

final Optional<BlockHeader> maybeTerminalPoWBlockHeader = mergeContext.getTerminalPoWBlock();

final EngineExchangeTransitionConfigurationResult localTransitionConfiguration =
new EngineExchangeTransitionConfigurationResult(
mergeContext.getTerminalTotalDifficulty(),
maybeTerminalPoWBlockHeader.map(BlockHeader::getHash).orElse(Hash.ZERO),
maybeTerminalPoWBlockHeader.map(BlockHeader::getNumber).orElse(0L));

if (!localTransitionConfiguration
.getTerminalTotalDifficulty()
.equals(remoteTransitionConfiguration.getTerminalTotalDifficulty())) {
LOG.warn(
"Configured terminal total difficulty {} does not match value of consensus client {}",
localTransitionConfiguration.getTerminalTotalDifficulty(),
remoteTransitionConfiguration.getTerminalTotalDifficulty());
}

if (!localTransitionConfiguration
.getTerminalBlockHash()
.equals(remoteTransitionConfiguration.getTerminalBlockHash())) {
LOG.warn(
"Configured terminal block hash {} does not match value of consensus client {}",
localTransitionConfiguration.getTerminalBlockHash(),
remoteTransitionConfiguration.getTerminalBlockHash());
}

if (localTransitionConfiguration.getTerminalBlockNumber()
!= remoteTransitionConfiguration.getTerminalBlockNumber()) {
LOG.debug(
"Configured terminal block number {} does not match value of consensus client {}",
localTransitionConfiguration.getTerminalBlockNumber(),
remoteTransitionConfiguration.getTerminalBlockNumber());
}

return respondWith(reqId, localTransitionConfiguration);
}

private JsonRpcResponse respondWith(
final Object requestId,
final EngineExchangeTransitionConfigurationResult transitionConfiguration) {
return new JsonRpcSuccessResponse(requestId, transitionConfiguration);
}

private JsonRpcResponse respondWithError(
final Object requestId, final JsonRpcError jsonRpcError) {
return new JsonRpcErrorResponse(requestId, jsonRpcError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.parameters;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.Difficulty;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class EngineExchangeTransitionConfigurationParameter {
private final Difficulty terminalTotalDifficulty;
private final Hash terminalBlockHash;
private final long terminalBlockNumber;

@JsonCreator
public EngineExchangeTransitionConfigurationParameter(
@JsonProperty("terminalTotalDifficulty") final String terminalTotalDifficulty,
@JsonProperty("terminalBlockHash") final String terminalBlockHash,
@JsonProperty("terminalBlockNumber") final UnsignedLongParameter terminalBlockNumber) {
this.terminalTotalDifficulty = Difficulty.fromHexString(terminalTotalDifficulty);
this.terminalBlockHash = Hash.fromHexString(terminalBlockHash);
this.terminalBlockNumber = terminalBlockNumber.getValue();
}

public Difficulty getTerminalTotalDifficulty() {
return terminalTotalDifficulty;
}

public Hash getTerminalBlockHash() {
return terminalBlockHash;
}

public long getTerminalBlockNumber() {
return terminalBlockNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.results;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.Difficulty;

import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"terminalTotalDifficulty", "terminalBlockHash", "terminalBlockNumber"})
public class EngineExchangeTransitionConfigurationResult {
private final Difficulty terminalTotalDifficulty;
private final Hash terminalBlockHash;
private final long terminalBlockNumber;

public EngineExchangeTransitionConfigurationResult(
final Difficulty terminalTotalDifficulty,
final Hash terminalBlockHash,
final long terminalBlockNumber) {
this.terminalTotalDifficulty = terminalTotalDifficulty;
this.terminalBlockHash = terminalBlockHash;
this.terminalBlockNumber = terminalBlockNumber;
}

@JsonGetter(value = "terminalTotalDifficulty")
public String getTerminalTotalDifficultyAsString() {
return terminalTotalDifficulty.toHexString();
}

public Difficulty getTerminalTotalDifficulty() {
return terminalTotalDifficulty;
}

@JsonGetter(value = "terminalBlockHash")
public String getTerminalBlockHashAsString() {
return terminalBlockHash.toHexString();
}

public Hash getTerminalBlockHash() {
return terminalBlockHash;
}

@JsonGetter(value = "terminalBlockNumber")
public String getTerminalBlockNumberAsString() {
return Optional.of(terminalBlockNumber)
.filter(val -> val != 0L)
.map(Long::toHexString)
.orElse("0x0");
}

public Long getTerminalBlockNumber() {
return terminalBlockNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis;
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.EngineForkchoiceUpdated;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineGetPayload;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.EngineNewPayload;
Expand Down Expand Up @@ -53,6 +54,7 @@ protected Map<String, JsonRpcMethod> create() {
return mapOf(
new EngineGetPayload(syncVertx, protocolContext, blockResultFactory),
new EngineNewPayload(syncVertx, protocolContext, mergeCoordinator),
new EngineForkchoiceUpdated(syncVertx, protocolContext, mergeCoordinator));
new EngineForkchoiceUpdated(syncVertx, protocolContext, mergeCoordinator),
new EngineExchangeTransitionConfiguration(syncVertx, protocolContext));
}
}
Loading

0 comments on commit 424ed9c

Please sign in to comment.