-
Notifications
You must be signed in to change notification settings - Fork 861
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
feat: Expose set finalized/safe block in plugin api BlockchainService #7382
Merged
usmansaleem
merged 20 commits into
hyperledger:main
from
usmansaleem:besu_plugin_blockchainservice_finalized_block
Jul 31, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8868adb
feat: Expose set finalized and safe block in plugin api BlockchainSer…
usmansaleem 364f848
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem 97f2fe4
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem b8c01a2
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem 0178dcd
check for poa network before setting finalized block
usmansaleem c65f755
fix init call
usmansaleem c8f13b8
Check for PoS for finalized and safe block
usmansaleem df461b2
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem a73a7bb
revert the init method
usmansaleem 44c0735
changelog
usmansaleem a91166e
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem 3e89e43
Add BlockchainService set finalized acceptance test
usmansaleem a52f185
Add BlockchainService set finalized acceptance test
usmansaleem 77e6d87
fix doc sentence
usmansaleem e3e4da0
AT for set finalized and set safe
usmansaleem 04a09f5
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem e05ccbc
Merge remote-tracking branch 'upstream/main' into besu_plugin_blockch…
usmansaleem cbffa16
trigger ci
usmansaleem bb7ac7d
Merge branch 'main' into besu_plugin_blockchainservice_finalized_block
fab-10 e2af439
Merge branch 'main' into besu_plugin_blockchainservice_finalized_block
fab-10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...a/org/hyperledger/besu/tests/acceptance/plugins/TestBlockchainServiceFinalizedPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* 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.tests.acceptance.plugins; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; | ||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.BesuPlugin; | ||
import org.hyperledger.besu.plugin.data.BlockContext; | ||
import org.hyperledger.besu.plugin.services.BlockchainService; | ||
import org.hyperledger.besu.plugin.services.RpcEndpointService; | ||
import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException; | ||
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@AutoService(BesuPlugin.class) | ||
public class TestBlockchainServiceFinalizedPlugin implements BesuPlugin { | ||
private static final Logger LOG = | ||
LoggerFactory.getLogger(TestBlockchainServiceFinalizedPlugin.class); | ||
private static final String RPC_NAMESPACE = "updater"; | ||
private static final String RPC_METHOD_FINALIZED_BLOCK = "updateFinalizedBlockV1"; | ||
private static final String RPC_METHOD_SAFE_BLOCK = "updateSafeBlockV1"; | ||
|
||
@Override | ||
public void register(final BesuContext besuContext) { | ||
LOG.trace("Registering plugin ..."); | ||
|
||
final RpcEndpointService rpcEndpointService = | ||
besuContext | ||
.getService(RpcEndpointService.class) | ||
.orElseThrow( | ||
() -> | ||
new RuntimeException( | ||
"Failed to obtain RpcEndpointService from the BesuContext.")); | ||
|
||
final BlockchainService blockchainService = | ||
besuContext | ||
.getService(BlockchainService.class) | ||
.orElseThrow( | ||
() -> | ||
new RuntimeException( | ||
"Failed to obtain BlockchainService from the BesuContext.")); | ||
|
||
final FinalizationUpdaterRpcMethod rpcMethod = | ||
new FinalizationUpdaterRpcMethod(blockchainService); | ||
rpcEndpointService.registerRPCEndpoint( | ||
RPC_NAMESPACE, RPC_METHOD_FINALIZED_BLOCK, rpcMethod::setFinalizedBlock); | ||
rpcEndpointService.registerRPCEndpoint( | ||
RPC_NAMESPACE, RPC_METHOD_SAFE_BLOCK, rpcMethod::setSafeBlock); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
LOG.trace("Starting plugin ..."); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
LOG.trace("Stopping plugin ..."); | ||
} | ||
|
||
static class FinalizationUpdaterRpcMethod { | ||
private final BlockchainService blockchainService; | ||
private final JsonRpcParameter parameterParser = new JsonRpcParameter(); | ||
|
||
FinalizationUpdaterRpcMethod(final BlockchainService blockchainService) { | ||
this.blockchainService = blockchainService; | ||
} | ||
|
||
Boolean setFinalizedBlock(final PluginRpcRequest request) { | ||
return setFinalizedOrSafeBlock(request, true); | ||
} | ||
|
||
Boolean setSafeBlock(final PluginRpcRequest request) { | ||
return setFinalizedOrSafeBlock(request, false); | ||
} | ||
|
||
private Boolean setFinalizedOrSafeBlock( | ||
final PluginRpcRequest request, final boolean isFinalized) { | ||
final Long blockNumberToSet = parseResult(request); | ||
|
||
// lookup finalized block by number in local chain | ||
final Optional<BlockContext> finalizedBlock = | ||
blockchainService.getBlockByNumber(blockNumberToSet); | ||
if (finalizedBlock.isEmpty()) { | ||
throw new PluginRpcEndpointException( | ||
RpcErrorType.BLOCK_NOT_FOUND, | ||
"Block not found in the local chain: " + blockNumberToSet); | ||
} | ||
|
||
try { | ||
final Hash blockHash = finalizedBlock.get().getBlockHeader().getBlockHash(); | ||
if (isFinalized) { | ||
blockchainService.setFinalizedBlock(blockHash); | ||
} else { | ||
blockchainService.setSafeBlock(blockHash); | ||
} | ||
} catch (final IllegalArgumentException e) { | ||
throw new PluginRpcEndpointException( | ||
RpcErrorType.BLOCK_NOT_FOUND, | ||
"Block not found in the local chain: " + blockNumberToSet); | ||
} catch (final UnsupportedOperationException e) { | ||
throw new PluginRpcEndpointException( | ||
RpcErrorType.METHOD_NOT_ENABLED, | ||
"Method not enabled for PoS network: setFinalizedBlock"); | ||
} catch (final Exception e) { | ||
throw new PluginRpcEndpointException( | ||
RpcErrorType.INTERNAL_ERROR, "Error setting finalized block: " + blockNumberToSet); | ||
} | ||
|
||
return Boolean.TRUE; | ||
} | ||
|
||
private Long parseResult(final PluginRpcRequest request) { | ||
Long blockNumber; | ||
try { | ||
final Object[] params = request.getParams(); | ||
blockNumber = parameterParser.required(params, 0, Long.class); | ||
} catch (final Exception e) { | ||
throw new PluginRpcEndpointException(RpcErrorType.INVALID_PARAMS, e.getMessage()); | ||
} | ||
|
||
if (blockNumber <= 0) { | ||
throw new PluginRpcEndpointException( | ||
RpcErrorType.INVALID_PARAMS, "Block number must be greater than 0"); | ||
} | ||
|
||
return blockNumber; | ||
} | ||
} | ||
} |
158 changes: 158 additions & 0 deletions
158
.../hyperledger/besu/tests/acceptance/plugins/BlockchainServiceFinalizedBlockPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu. | ||
* | ||
* 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.tests.acceptance.plugins; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.hyperledger.besu.config.JsonUtil; | ||
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; | ||
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
public class BlockchainServiceFinalizedBlockPluginTest extends AcceptanceTestBase { | ||
|
||
private BesuNode pluginNode; | ||
private BesuNode minerNode; | ||
private OkHttpClient client; | ||
protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
minerNode = besu.createMinerNode("minerNode"); | ||
pluginNode = | ||
besu.createPluginsNode("node1", List.of("testPlugins"), List.of("--rpc-http-api=UPDATER")); | ||
cluster.start(minerNode, pluginNode); | ||
client = new OkHttpClient(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calling update{Finalized/Safe}BlockV1 will set block") | ||
public void canUpdateFinalizedBlock() throws IOException { | ||
pluginNode.verify(blockchain.minimumHeight(5)); | ||
|
||
// RPC Call. Set the safe block number to 3 | ||
final ObjectNode resultJson = callTestMethod("updater_updateSafeBlockV1", List.of(3L)); | ||
assertThat(resultJson.get("result").asBoolean()).isTrue(); | ||
|
||
// RPC Call. Set the finalized block number to 4 | ||
final ObjectNode finalizedResultJson = | ||
callTestMethod("updater_updateFinalizedBlockV1", List.of(4L)); | ||
assertThat(finalizedResultJson.get("result").asBoolean()).isTrue(); | ||
|
||
final ObjectNode blockNumberSafeResult = | ||
callTestMethod("eth_getBlockByNumber", List.of("SAFE", true)); | ||
assertThat(blockNumberSafeResult.get("result").get("number").asText()).isEqualTo("0x3"); | ||
|
||
// Verify the value was set | ||
final ObjectNode blockNumberFinalizedResult = | ||
callTestMethod("eth_getBlockByNumber", List.of("FINALIZED", true)); | ||
assertThat(blockNumberFinalizedResult.get("result").get("number").asText()).isEqualTo("0x4"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calling update{Finalized/Safe}BlockV1 with non-existing block number returns error") | ||
public void nonExistingBlockNumberReturnsError() throws IOException { | ||
pluginNode.verify(blockchain.minimumHeight(5)); | ||
|
||
final ObjectNode[] resultsJson = new ObjectNode[2]; | ||
resultsJson[0] = callTestMethod("updater_updateFinalizedBlockV1", List.of(250L)); | ||
resultsJson[1] = callTestMethod("updater_updateSafeBlockV1", List.of(250L)); | ||
|
||
for (int i = 0; i < resultsJson.length; i++) { | ||
assertThat(resultsJson[i].get("error").get("code").asInt()).isEqualTo(-32000); | ||
assertThat(resultsJson[i].get("error").get("message").asText()).isEqualTo("Block not found"); | ||
assertThat(resultsJson[i].get("error").get("data").asText()) | ||
.isEqualTo("Block not found in the local chain: 250"); | ||
} | ||
} | ||
|
||
@ParameterizedTest(name = "{index} - blockNumber={0}") | ||
@ValueSource(longs = {-1, 0}) | ||
@DisplayName("Calling update{Finalized/Safe}BlockV1 with block number <= 0 returns error") | ||
public void invalidBlockNumberReturnsError(final long blockNumber) throws IOException { | ||
pluginNode.verify(blockchain.minimumHeight(5)); | ||
|
||
final ObjectNode[] resultsJson = new ObjectNode[2]; | ||
resultsJson[0] = callTestMethod("updater_updateFinalizedBlockV1", List.of(blockNumber)); | ||
resultsJson[1] = callTestMethod("updater_updateSafeBlockV1", List.of(blockNumber)); | ||
|
||
for (int i = 0; i < resultsJson.length; i++) { | ||
assertThat(resultsJson[i].get("error").get("code").asInt()).isEqualTo(-32602); | ||
assertThat(resultsJson[i].get("error").get("message").asText()).isEqualTo("Invalid params"); | ||
assertThat(resultsJson[i].get("error").get("data").asText()) | ||
.isEqualTo("Block number must be greater than 0"); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("Calling update{Finalized/Safe}BlockV1 with invalid block number type returns error") | ||
public void invalidBlockNumberTypeReturnsError() throws IOException { | ||
pluginNode.verify(blockchain.minimumHeight(5)); | ||
|
||
final ObjectNode[] resultsJson = new ObjectNode[2]; | ||
resultsJson[0] = callTestMethod("updater_updateFinalizedBlockV1", List.of("testblock")); | ||
resultsJson[1] = callTestMethod("updater_updateSafeBlockV1", List.of("testblock")); | ||
|
||
for (int i = 0; i < resultsJson.length; i++) { | ||
assertThat(resultsJson[i].get("error").get("code").asInt()).isEqualTo(-32602); | ||
assertThat(resultsJson[i].get("error").get("message").asText()).isEqualTo("Invalid params"); | ||
assertThat(resultsJson[i].get("error").get("data").asText()) | ||
.isEqualTo( | ||
"Invalid json rpc parameter at index 0. Supplied value was: 'testblock' of type: 'java.lang.String' - expected type: 'java.lang.Long'"); | ||
} | ||
} | ||
|
||
private ObjectNode callTestMethod(final String method, final List<Object> params) | ||
throws IOException { | ||
String format = | ||
String.format( | ||
"{\"jsonrpc\":\"2.0\",\"method\":\"%s\",\"params\":[%s],\"id\":42}", | ||
method, | ||
params.stream().map(value -> "\"" + value + "\"").collect(Collectors.joining(","))); | ||
|
||
RequestBody body = RequestBody.create(format, JSON); | ||
|
||
final String resultString = | ||
client | ||
.newCall( | ||
new Request.Builder() | ||
.post(body) | ||
.url( | ||
"http://" | ||
+ pluginNode.getHostName() | ||
+ ":" | ||
+ pluginNode.getJsonRpcPort().get() | ||
+ "/") | ||
.build()) | ||
.execute() | ||
.body() | ||
.string(); | ||
return JsonUtil.objectNodeFromString(resultString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also test the updateSafeBlock method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.