-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial PoC for RPC end points via the plugin mechanism.
re-implementation of PegaSysEng/pantheon#1909 Signed-off-by: Antony Denyer <[email protected]> Co-authored-by: Danno Ferrin <[email protected]>
- Loading branch information
1 parent
a2fd214
commit 8eb98de
Showing
14 changed files
with
400 additions
and
8 deletions.
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
78 changes: 78 additions & 0 deletions
78
...test-plugins/src/main/java/org/hyperledger/besu/plugins/TestRpcEndpointServicePlugin.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,78 @@ | ||
/* | ||
* 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 { | ||
|
||
static class Bean { | ||
final String value; | ||
|
||
Bean(final String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
private final AtomicReference<String> storage = new AtomicReference<>("InitialValue"); | ||
|
||
private String replaceValue(final PluginRpcRequest request) { | ||
checkArgument(request.getParams().length == 1, "Only one parameter accepted"); | ||
return storage.getAndSet(request.getParams()[0].toString()); | ||
} | ||
|
||
private String[] replaceValueArray(final PluginRpcRequest request) { | ||
return new String[] {replaceValue(request)}; | ||
} | ||
|
||
private Bean replaceValueBean(final PluginRpcRequest request) { | ||
return new Bean(replaceValue(request)); | ||
} | ||
|
||
@Override | ||
public void register(final BesuContext context) { | ||
context | ||
.getService(RpcEndpointService.class) | ||
.ifPresent( | ||
rpcEndpointService -> { | ||
rpcEndpointService.registerRPCEndpoint( | ||
"unitTests", "replaceValue", this::replaceValue); | ||
rpcEndpointService.registerRPCEndpoint( | ||
"unitTests", "replaceValueArray", this::replaceValueArray); | ||
rpcEndpointService.registerRPCEndpoint( | ||
"unitTests", "replaceValueBean", this::replaceValueBean); | ||
}); | ||
} | ||
|
||
@Override | ||
public void start() {} | ||
|
||
@Override | ||
public void stop() {} | ||
} |
99 changes: 99 additions & 0 deletions
99
...test/java/org/hyperledger/besu/tests/acceptance/plugins/RpcEndpointServicePluginTest.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,99 @@ | ||
/* | ||
* 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.Collections; | ||
|
||
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", Collections.singletonList("testPlugins"), Collections.emptyList()); | ||
cluster.start(node); | ||
client = new OkHttpClient(); | ||
} | ||
|
||
@Test | ||
public void rpcWorking() throws IOException { | ||
final String firstCall = "FirstCall"; | ||
final String secondCall = "SecondCall"; | ||
final String thirdCall = "ThirdCall"; | ||
|
||
ObjectNode resultJson = callTestMethod("unitTests_replaceValue", firstCall); | ||
assertThat(resultJson.get("result").asText()).isEqualTo("InitialValue"); | ||
|
||
resultJson = callTestMethod("unitTests_replaceValueArray", secondCall); | ||
assertThat(resultJson.get("result").get(0).asText()).isEqualTo(firstCall); | ||
|
||
resultJson = callTestMethod("unitTests_replaceValueBean", thirdCall); | ||
assertThat(resultJson.get("result").get("value").asText()).isEqualTo(secondCall); | ||
} | ||
|
||
@Test | ||
public void throwsError() throws IOException { | ||
ObjectNode resultJson = callTestMethod("unitTests_replaceValue", null); | ||
assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Internal error"); | ||
} | ||
|
||
private ObjectNode callTestMethod(final String method, final String value) throws IOException { | ||
final String resultString = | ||
client | ||
.newCall( | ||
new Request.Builder() | ||
.post( | ||
RequestBody.create( | ||
"{\"jsonrpc\":\"2.0\",\"method\":\"" | ||
+ method | ||
+ "\",\"params\":[" | ||
+ "\"" | ||
+ value | ||
+ "\"" | ||
+ "],\"id\":33}", | ||
JSON)) | ||
.url( | ||
"http://" | ||
+ node.getHostName() | ||
+ ":" | ||
+ node.getJsonRpcSocketPort().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
50 changes: 50 additions & 0 deletions
50
besu/src/main/java/org/hyperledger/besu/services/RpcEndpointServiceImpl.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,50 @@ | ||
/* | ||
* 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.services; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.PluginJsonRpcMethod; | ||
import org.hyperledger.besu.plugin.services.RpcEndpointService; | ||
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class RpcEndpointServiceImpl implements RpcEndpointService { | ||
private final Map<String, Function<PluginRpcRequest, ?>> rpcMethods = new HashMap<>(); | ||
|
||
@Override | ||
public <T> void registerRPCEndpoint( | ||
final String namespace, | ||
final String functionName, | ||
final Function<PluginRpcRequest, T> function) { | ||
checkArgument(namespace.matches("\\p{Alnum}+"), "Namespace must be only alpha numeric"); | ||
checkArgument(functionName.matches("\\p{Alnum}+"), "Function Name must be only alpha numeric"); | ||
checkNotNull(function); | ||
|
||
rpcMethods.put(namespace + "_" + functionName, function); | ||
} | ||
|
||
public Map<String, ? extends JsonRpcMethod> getPluginMethods() { | ||
return rpcMethods.entrySet().stream() | ||
.map(entry -> new PluginJsonRpcMethod(entry.getKey(), entry.getValue())) | ||
.collect(Collectors.toMap(PluginJsonRpcMethod::getName, e -> e)); | ||
} | ||
} |
Oops, something went wrong.