Skip to content

Commit

Permalink
Add additional admin rpc calls (#1255)
Browse files Browse the repository at this point in the history
* add admin rpcs, add consensus to adminnodeinfo, add enode to adminpeers, update tests

* add datadir admin rpc, fix adminnodeinfo consensus

* spotlessapply fixes
  • Loading branch information
xaviarias authored Oct 30, 2020
2 parents 8cb1dce + dafe4a8 commit d60432a
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 2 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/org/web3j/protocol/core/Ethereum.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.math.BigInteger;

import org.web3j.protocol.core.methods.request.ShhFilter;
import org.web3j.protocol.core.methods.response.BooleanResponse;
import org.web3j.protocol.core.methods.response.DbGetHex;
import org.web3j.protocol.core.methods.response.DbGetString;
import org.web3j.protocol.core.methods.response.DbPutHex;
Expand Down Expand Up @@ -62,8 +63,10 @@
import org.web3j.protocol.core.methods.response.ShhNewIdentity;
import org.web3j.protocol.core.methods.response.ShhUninstallFilter;
import org.web3j.protocol.core.methods.response.ShhVersion;
import org.web3j.protocol.core.methods.response.TxPoolStatus;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.core.methods.response.Web3Sha3;
import org.web3j.protocol.core.methods.response.admin.AdminDataDir;
import org.web3j.protocol.core.methods.response.admin.AdminNodeInfo;
import org.web3j.protocol.core.methods.response.admin.AdminPeers;

Expand All @@ -83,6 +86,12 @@ public interface Ethereum {

Request<?, AdminPeers> adminPeers();

Request<?, BooleanResponse> adminAddPeer(String url);

Request<?, BooleanResponse> adminRemovePeer(String url);

Request<?, AdminDataDir> adminDataDir();

Request<?, EthProtocolVersion> ethProtocolVersion();

Request<?, EthChainId> ethChainId();
Expand Down Expand Up @@ -215,4 +224,6 @@ Request<?, org.web3j.protocol.core.methods.response.ShhPost> shhPost(
Request<?, ShhMessages> shhGetFilterChanges(BigInteger filterId);

Request<?, ShhMessages> shhGetMessages(BigInteger filterId);

Request<?, TxPoolStatus> txPoolStatus();
}
27 changes: 27 additions & 0 deletions core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.web3j.protocol.core.methods.request.ShhFilter;
import org.web3j.protocol.core.methods.request.ShhPost;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.BooleanResponse;
import org.web3j.protocol.core.methods.response.DbGetHex;
import org.web3j.protocol.core.methods.response.DbGetString;
import org.web3j.protocol.core.methods.response.DbPutHex;
Expand Down Expand Up @@ -77,8 +78,10 @@
import org.web3j.protocol.core.methods.response.ShhNewIdentity;
import org.web3j.protocol.core.methods.response.ShhUninstallFilter;
import org.web3j.protocol.core.methods.response.ShhVersion;
import org.web3j.protocol.core.methods.response.TxPoolStatus;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.core.methods.response.Web3Sha3;
import org.web3j.protocol.core.methods.response.admin.AdminDataDir;
import org.web3j.protocol.core.methods.response.admin.AdminNodeInfo;
import org.web3j.protocol.core.methods.response.admin.AdminPeers;
import org.web3j.protocol.rx.JsonRpc2_0Rx;
Expand Down Expand Up @@ -155,6 +158,24 @@ public Request<?, AdminPeers> adminPeers() {
"admin_peers", Collections.emptyList(), web3jService, AdminPeers.class);
}

@Override
public Request<?, BooleanResponse> adminAddPeer(String url) {
return new Request<>(
"admin_addPeer", Arrays.asList(url), web3jService, BooleanResponse.class);
}

@Override
public Request<?, BooleanResponse> adminRemovePeer(String url) {
return new Request<>(
"admin_removePeer", Arrays.asList(url), web3jService, BooleanResponse.class);
}

@Override
public Request<?, AdminDataDir> adminDataDir() {
return new Request<>(
"admin_datadir", Collections.emptyList(), web3jService, AdminDataDir.class);
}

@Override
public Request<?, EthProtocolVersion> ethProtocolVersion() {
return new Request<>(
Expand Down Expand Up @@ -652,6 +673,12 @@ public Request<?, ShhMessages> shhGetMessages(BigInteger filterId) {
ShhMessages.class);
}

@Override
public Request<?, TxPoolStatus> txPoolStatus() {
return new Request<>(
"txpool_status", Collections.<String>emptyList(), web3jService, TxPoolStatus.class);
}

@Override
public Flowable<NewHeadsNotification> newHeadsNotifications() {
return web3jService.subscribe(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* 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 org.web3j.protocol.core.methods.response;

import org.web3j.protocol.core.Response;

/** Boolean response type. */
public class BooleanResponse extends Response<Boolean> {
public boolean success() {
return getResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* 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 org.web3j.protocol.core.methods.response;

import java.util.Map;

import org.web3j.protocol.core.Response;
import org.web3j.utils.Numeric;

/** txpool_status. */
public class TxPoolStatus extends Response<Map<String, String>> {
public Integer getPending() {
return Numeric.decodeQuantity((String) getResult().get("pending")).intValue();
}

public Integer getQueued() {
return Numeric.decodeQuantity((String) getResult().get("queued")).intValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* 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 org.web3j.protocol.core.methods.response.admin;

import org.web3j.protocol.core.Response;

/** String response type. */
public class AdminDataDir extends Response<String> {
public String getDataDir() {
return getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package org.web3j.protocol.core.methods.response.admin;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -40,15 +42,38 @@ public static class NodeInfo {
private String ip;
private String listenAddr;
private String name;
private String consensus;

@JsonProperty("protocols")
@SuppressWarnings("unchecked")
private void consensusDeserializer(Map<String, Object> protocols) {
if (protocols.containsKey("istanbul")) {
consensus = "istanbul";
} else if (protocols.containsKey("clique")) {
consensus = "clique";
} else if (protocols.containsKey("eth")) {
Map<String, Object> eth = (Map<String, Object>) protocols.get("eth");
consensus = (String) eth.get("consensus");
} else {
consensus = "unknown";
}
}

public NodeInfo() {}

public NodeInfo(String enode, String id, String ip, String listenAddr, String name) {
public NodeInfo(
String enode,
String id,
String ip,
String listenAddr,
String name,
String consensus) {
this.enode = enode;
this.id = id;
this.ip = ip;
this.listenAddr = listenAddr;
this.name = name;
this.consensus = consensus;
}

public String getEnode() {
Expand All @@ -70,6 +95,10 @@ public String getListenAddr() {
public String getName() {
return name;
}

public String getConsensus() {
return consensus;
}
}

public static class ResponseDeserialiser extends JsonDeserializer<NodeInfo> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public void setResult(List<Peer> result) {
public static class Peer {
public Peer() {}

public Peer(String id, String name, PeerNetwork network) {
public Peer(String id, String name, String enode, PeerNetwork network) {
this.id = id;
this.name = name;
this.network = network;
this.enode = enode;
}

public String getId() {
Expand All @@ -53,9 +54,14 @@ public String getName() {
return name;
}

public String getEnode() {
return enode;
}

private String id;
private String name;
private PeerNetwork network;
private String enode;

public PeerNetwork getNetwork() {
return network;
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/org/web3j/protocol/core/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ public void testAdminNodeInfo() throws Exception {
verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"admin_nodeInfo\",\"params\":[],\"id\":1}");
}

@Test
public void testAdminAddPeer() throws Exception {
web3j.adminAddPeer("url").send();

verifyResult(
"{\"jsonrpc\":\"2.0\",\"method\":\"admin_addPeer\",\"params\":[\"url\"],\"id\":1}");
}

@Test
public void testAdminDataDir() throws Exception {
web3j.adminDataDir().send();

verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"admin_datadir\",\"params\":[],\"id\":1}");
}

@Test
public void testAdminRemovePeer() throws Exception {
web3j.adminRemovePeer("url").send();

verifyResult(
"{\"jsonrpc\":\"2.0\",\"method\":\"admin_removePeer\",\"params\":[\"url\"],\"id\":1}");
}

@Test
public void testEthProtocolVersion() throws Exception {
web3j.ethProtocolVersion().send();
Expand Down Expand Up @@ -716,4 +739,11 @@ public void testShhGetMessages() throws Exception {
"{\"jsonrpc\":\"2.0\",\"method\":\"shh_getMessages\","
+ "\"params\":[\"0x07\"],\"id\":1}");
}

@Test
public void testTxPoolStatus() throws Exception {
web3j.txPoolStatus().send();

verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"txpool_status\",\"params\":[],\"id\":1}");
}
}
45 changes: 45 additions & 0 deletions core/src/test/java/org/web3j/protocol/core/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.web3j.protocol.ResponseTester;
import org.web3j.protocol.core.methods.response.AbiDefinition;
import org.web3j.protocol.core.methods.response.BooleanResponse;
import org.web3j.protocol.core.methods.response.DbGetHex;
import org.web3j.protocol.core.methods.response.DbGetString;
import org.web3j.protocol.core.methods.response.DbPutHex;
Expand Down Expand Up @@ -77,8 +78,10 @@
import org.web3j.protocol.core.methods.response.ShhVersion;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.TxPoolStatus;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.core.methods.response.Web3Sha3;
import org.web3j.protocol.core.methods.response.admin.AdminDataDir;
import org.web3j.protocol.core.methods.response.admin.AdminNodeInfo;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -217,6 +220,7 @@ public void testAdminNodeInfo() throws Exception {
+ " \"network\": 4,\n"
+ " \"difficulty\": 1,\n"
+ " \"genesis\": \"0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177\",\n"
+ " \"consensus\": \"clique\",\n"
+ " \"config\": {\n"
+ " \"chainId\": 4,\n"
+ " \"homesteadBlock\": 1,\n"
Expand Down Expand Up @@ -1567,4 +1571,45 @@ public void testSshMessages() {
ShhMessages shhMessages = deserialiseResponse(ShhMessages.class);
assertEquals(shhMessages.getMessages(), (messages));
}

@Test
public void testBooleanResponse() {
buildResponse(
"{\n"
+ " \"jsonrpc\":\"2.0\",\n"
+ " \"id\":22,\n"
+ " \"result\":true\n"
+ "}");

BooleanResponse booleanResponse = deserialiseResponse(BooleanResponse.class);
assertTrue(booleanResponse.success());
}

@Test
public void testAdminDataDir() {
buildResponse(
"{\n"
+ " \"jsonrpc\":\"2.0\",\n"
+ " \"id\":22,\n"
+ " \"result\":\"sampleDir\"\n"
+ "}");

AdminDataDir dataDir = deserialiseResponse(AdminDataDir.class);
assertEquals(dataDir.getDataDir(), "sampleDir");
}

@Test
public void testTxPoolStatus() {
buildResponse(
"{\n"
+ " \"jsonrpc\":\"2.0\",\n"
+ " \"id\":22,\n"
+ " \"result\":{ \"pending\": \"10\",\n"
+ " \"queued\": \"7\"}\n"
+ "}");

TxPoolStatus status = deserialiseResponse(TxPoolStatus.class);
assertEquals(status.getPending(), 10);
assertEquals(status.getQueued(), 7);
}
}

0 comments on commit d60432a

Please sign in to comment.