-
Notifications
You must be signed in to change notification settings - Fork 982
Allow Besu to host RPC endpoints via a plugin. #2754
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f25dadc
Initial PoC for RPC end points via the plugin mechanism.
antonydenyer 35d619a
Make acceptance more explicit around what is being tested
antonydenyer cb20cfa
Remove RpcApis and switch to String
antonydenyer 58bcf4d
Only enable plugin rpc api when enabled on --rpc-http-api or --rpc-ws…
antonydenyer d1ebe35
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer 01e1d5a
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer 7e1c7ac
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer 80396f3
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer 7553651
Update changelog
antonydenyer 05479e4
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer fc65ddf
Prevent rpc endpoints from being overridden
antonydenyer 13fa025
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer ef28dfc
fix issues based on feedback
antonydenyer e40f8cc
fix method name in test
antonydenyer aadc0a4
Fix copyright on new files
antonydenyer eac5a0f
tidy up javadocs
antonydenyer 9a2be51
Fix method/function rename
antonydenyer 39d3de4
Merge branch 'main' of github.com:hyperledger/besu into json-rpc-plugin
antonydenyer 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
...test-plugins/src/main/java/org/hyperledger/besu/plugins/TestRpcEndpointServicePlugin.java
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * Copyright ConsenSys AG. | ||
| * | ||
| * 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.plugins; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| import org.hyperledger.besu.plugin.BesuContext; | ||
| import org.hyperledger.besu.plugin.BesuPlugin; | ||
| import org.hyperledger.besu.plugin.services.RpcEndpointService; | ||
| import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
|
|
||
| @AutoService(BesuPlugin.class) | ||
| public class TestRpcEndpointServicePlugin implements BesuPlugin { | ||
|
|
||
| private final AtomicReference<String> stringStorage = new AtomicReference<>("InitialValue"); | ||
| private final AtomicReference<Object[]> arrayStorage = new AtomicReference<>(); | ||
|
|
||
| private String setValue(final PluginRpcRequest request) { | ||
| checkArgument(request.getParams().length == 1, "Only one parameter accepted"); | ||
| return stringStorage.updateAndGet(x -> request.getParams()[0].toString()); | ||
| } | ||
|
|
||
| private String getValue(final PluginRpcRequest request) { | ||
| return stringStorage.get(); | ||
| } | ||
|
|
||
| private Object[] replaceValueList(final PluginRpcRequest request) { | ||
| return arrayStorage.updateAndGet(x -> request.getParams()); | ||
| } | ||
|
|
||
| private String throwException(final PluginRpcRequest request) { | ||
| throw new RuntimeException("Kaboom"); | ||
| } | ||
|
|
||
| @Override | ||
| public void register(final BesuContext context) { | ||
| context | ||
| .getService(RpcEndpointService.class) | ||
| .ifPresent( | ||
| rpcEndpointService -> { | ||
| rpcEndpointService.registerRPCEndpoint("tests", "getValue", this::getValue); | ||
| rpcEndpointService.registerRPCEndpoint("tests", "setValue", this::setValue); | ||
| rpcEndpointService.registerRPCEndpoint( | ||
| "tests", "replaceValueList", this::replaceValueList); | ||
| rpcEndpointService.registerRPCEndpoint( | ||
| "tests", "throwException", this::throwException); | ||
| rpcEndpointService.registerRPCEndpoint("notEnabled", "getValue", this::getValue); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void start() {} | ||
|
|
||
| @Override | ||
| public void stop() {} | ||
| } | ||
This file contains hidden or 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
122 changes: 122 additions & 0 deletions
122
...test/java/org/hyperledger/besu/tests/acceptance/plugins/RpcEndpointServicePluginTest.java
This file contains hidden or 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,122 @@ | ||
| /* | ||
| * Copyright ConsenSys AG. | ||
| * | ||
| * 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.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import okhttp3.MediaType; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.RequestBody; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class RpcEndpointServicePluginTest extends AcceptanceTestBase { | ||
|
|
||
| private BesuNode node; | ||
|
|
||
| private OkHttpClient client; | ||
| protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| node = besu.createPluginsNode("node1", List.of("testPlugins"), List.of("--rpc-http-api=TESTS")); | ||
| cluster.start(node); | ||
| client = new OkHttpClient(); | ||
| } | ||
|
|
||
| @Test | ||
| public void canUseRpcToSetValue() throws IOException { | ||
| String setValue = "secondCall"; | ||
|
|
||
| ObjectNode resultJson = callTestMethod("tests_getValue", List.of()); | ||
| assertThat(resultJson.get("result").asText()).isEqualTo("InitialValue"); | ||
|
|
||
| resultJson = callTestMethod("tests_setValue", List.of(setValue)); | ||
| assertThat(resultJson.get("result").asText()).isEqualTo(setValue); | ||
|
|
||
| resultJson = callTestMethod("tests_getValue", List.of("ignored")); | ||
| assertThat(resultJson.get("result").asText()).isEqualTo(setValue); | ||
| } | ||
|
|
||
| @Test | ||
| public void canCheckArgumentInsideSetValue() throws IOException { | ||
| ObjectNode resultJson = callTestMethod("tests_setValue", List.of("one", "two")); | ||
| assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Internal error"); | ||
| } | ||
|
|
||
| @Test | ||
| public void canThrowExceptions() throws IOException { | ||
| ObjectNode resultJson = callTestMethod("tests_throwException", List.of()); | ||
| assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Internal error"); | ||
macfarla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Test | ||
| public void onlyEnabledMethodsReturn() throws IOException { | ||
| ObjectNode resultJson = callTestMethod("notEnabled_getValue", List.of()); | ||
| assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Method not found"); | ||
| } | ||
|
|
||
| @Test | ||
| public void mixedTypeArraysAreStringified() throws IOException { | ||
| ObjectNode resultJson = callTestMethod("tests_replaceValueList", List.of()); | ||
| assertThat(resultJson.get("result")).isEmpty(); | ||
|
|
||
| resultJson = callTestMethod("tests_replaceValueList", List.of("One", 2, true)); | ||
| JsonNode result = resultJson.get("result"); | ||
|
|
||
| assertThat(result.get(0).asText()).isEqualTo("One"); | ||
| assertThat(result.get(1).asText()).isEqualTo("2"); | ||
| assertThat(result.get(2).asText()).isEqualTo("true"); | ||
| } | ||
|
|
||
| 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://" | ||
| + node.getHostName() | ||
| + ":" | ||
| + node.getJsonRpcSocketPort().get() | ||
| + "/") | ||
| .build()) | ||
| .execute() | ||
| .body() | ||
| .string(); | ||
| return JsonUtil.objectNodeFromString(resultString); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.