Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[minor] add rpc_modules json-rpc #1036

Merged
merged 3 commits into from
Mar 4, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.NetListening;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.NetPeerCount;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.NetVersion;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.RpcModules;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.Web3ClientVersion;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.Web3Sha3;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.miner.MinerSetCoinbase;
Expand Down Expand Up @@ -160,7 +161,9 @@ public Map<String, JsonRpcMethod> methods(
final Collection<RpcApi> rpcApis,
final PrivacyParameters privacyParameters) {
final Map<String, JsonRpcMethod> enabledMethods = new HashMap<>();
// @formatter:off
if (!rpcApis.isEmpty()) {
addMethods(enabledMethods, new RpcModules(rpcApis));
}
if (rpcApis.contains(RpcApis.ETH)) {
addMethods(
enabledMethods,
Expand Down Expand Up @@ -277,7 +280,6 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
new EeaGetTransactionReceipt(
blockchainQueries, new Enclave(privacyParameters.getUrl()), parameter));
}
// @formatter:off
return enabledMethods;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2018 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.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApi;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class RpcModules implements JsonRpcMethod {

private final Map<String, String> moduleVersions;

public RpcModules(final Collection<RpcApi> modulesList) {
this.moduleVersions =
modulesList.stream()
.map(RpcApi::getCliValue)
.map(String::toLowerCase)
.collect(Collectors.toMap(Function.identity(), s -> "1.0"));
}

@Override
public String getName() {
return "rpc_modules";
}

@Override
public JsonRpcResponse response(final JsonRpcRequest req) {
// For now, just return an empty list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you can cut this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return new JsonRpcSuccessResponse(req.getId(), moduleVersions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 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.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;

public class RpcModulesTest {

private final String JSON_RPC_VERSION = "2.0";
private final String RPC_METHOD = "rpc_modules";

private RpcModules method;

@Before
public void setUp() {
method = new RpcModules(ImmutableList.of(RpcApis.DEBUG));
}

@Test
public void shouldReturnCorrectMethodName() {
assertThat(method.getName()).isEqualTo(RPC_METHOD);
}

@Test
public void shouldReturnCorrectResult() {
final JsonRpcRequest request =
new JsonRpcRequest(JSON_RPC_VERSION, RPC_METHOD, new Object[] {});

final JsonRpcResponse expected =
new JsonRpcSuccessResponse(request.getId(), ImmutableMap.of("debug", "1.0"));
final JsonRpcResponse actual = method.response(request);

assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
}