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

Remove Peer json RPC added #1129

Merged
merged 17 commits into from
Mar 20, 2019
Prev Previous commit
Next Next commit
Removed excess code
  • Loading branch information
tmohay committed Mar 18, 2019
commit c1b6ae245aa370f26199d81d1acb4b819b31e247
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
import tech.pegasys.pantheon.metrics.MetricsSystem;
Expand Down Expand Up @@ -86,7 +85,6 @@ public Map<String, JsonRpcMethod> methods() {
final Optional<AccountWhitelistController> accountWhitelistController =
Optional.of(mock(AccountWhitelistController.class));
final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);
final PeerCache peerCache = mock(PeerCache.class);

return new JsonRpcMethodsFactory()
.methods(
Expand All @@ -104,7 +102,6 @@ public Map<String, JsonRpcMethod> methods() {
new HashSet<>(),
accountWhitelistController,
RpcApis.DEFAULT_JSON_RPC_APIS,
privacyParameters,
peerCache);
privacyParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec;
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability;
import tech.pegasys.pantheon.ethereum.util.RawBlockIterator;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
Expand Down Expand Up @@ -188,8 +187,7 @@ public void setupTest() throws Exception {
supportedCapabilities,
Optional.empty(),
JSON_RPC_APIS,
privacyParameters,
mock(PeerCache.class));
privacyParameters);
final JsonRpcConfiguration config = JsonRpcConfiguration.createDefault();
config.setPort(0);
service =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -102,8 +101,7 @@ public void initServerAndClient() throws Exception {
supportedCapabilities,
Optional.of(mock(AccountWhitelistController.class)),
JSON_RPC_APIS,
mock(PrivacyParameters.class),
mock(PeerCache.class)));
mock(PrivacyParameters.class)));
service = createJsonRpcHttpService();
service.start().join();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

Expand Down Expand Up @@ -133,8 +132,7 @@ public static void initServerAndClient() throws Exception {
supportedCapabilities,
Optional.empty(),
JSON_RPC_APIS,
mock(PrivacyParameters.class),
mock(PeerCache.class)));
mock(PrivacyParameters.class)));
service = createJsonRpcHttpService();
jwtAuth = service.authenticationService.get().getJwtAuthProvider();
service.start().join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -185,8 +184,7 @@ private JsonRpcHttpService createJsonRpcHttpServiceWithRpcApis(final JsonRpcConf
supportedCapabilities,
Optional.of(mock(AccountWhitelistController.class)),
config.getRpcApis(),
mock(PrivacyParameters.class),
mock(PeerCache.class)));
mock(PrivacyParameters.class)));
final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService(
vertx, folder.newFolder().toPath(), config, new NoOpMetricsSystem(), rpcMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.ethereum.p2p.wire.Capability;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -130,8 +129,7 @@ public static void initServerAndClient() throws Exception {
supportedCapabilities,
Optional.of(mock(AccountWhitelistController.class)),
JSON_RPC_APIS,
mock(PrivacyParameters.class),
mock(PeerCache.class)));
mock(PrivacyParameters.class)));
service = createJsonRpcHttpService();
service.start().join();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.crypto.SECP256K1.PublicKey;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
Expand All @@ -28,10 +25,6 @@
import tech.pegasys.pantheon.ethereum.p2p.P2pDisabledException;
import tech.pegasys.pantheon.ethereum.p2p.PeerNotWhitelistedException;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.cache.PeerCache;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.math.BigInteger;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -43,23 +36,13 @@
public class AdminAddPeerTest {

@Mock private P2PNetwork p2pNetwork;
@Mock private PeerCache peerCache;
private final JsonRpcParameter parameter = new JsonRpcParameter();

private AdminAddPeer method;

private final EnodeURL validEnodeURL =
new EnodeURL(
PublicKey.create(BigInteger.valueOf(0)).toString().substring(2), "127.0.0.1", 30303);

private final JsonRpcRequest validRequest =
new JsonRpcRequest("2.0", "admin_addPeer", new String[] {validEnodeURL.toString()});

@Before
public void setup() {

method = new AdminAddPeer(p2pNetwork, parameter, peerCache);
when(peerCache.add(any())).thenReturn(true);
method = new AdminAddPeer(p2pNetwork, parameter);
}

@Test
Expand Down Expand Up @@ -111,38 +94,46 @@ public void requestHasInvalidEnode() {
public void requestAddsValidEnode() {
when(p2pNetwork.addMaintainConnectionPeer(any())).thenReturn(true);

final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(validRequest.getId(), true);

final JsonRpcResponse actualResponse = method.response(validRequest);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
verify(peerCache).add(eq(validEnodeURL));
}

@Test
public void ifPeerIsNotAddedToCacheFalseIsReturned() {
when(p2pNetwork.addMaintainConnectionPeer(any())).thenReturn(true);
when(peerCache.add(any())).thenReturn(false);

final JsonRpcRequest request =
new JsonRpcRequest("2.0", "admin_addPeer", new String[] {validEnodeURL.toString()});
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303"
});

final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId(), false);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId(), true);

final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
verify(peerCache).add(eq(validEnodeURL));
}

@Test
public void requestRefusesListOfNodes() {

final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {validEnodeURL.toString(), validEnodeURL.toString()});
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303",
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000001"
+ "@127.0.0.2:30303"
});

final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
Expand All @@ -156,10 +147,22 @@ public void requestRefusesListOfNodes() {
public void requestReturnsFalseIfAddFails() {
when(p2pNetwork.addMaintainConnectionPeer(any())).thenReturn(false);

final JsonRpcResponse expectedResponse =
new JsonRpcSuccessResponse(validRequest.getId(), false);
final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303"
});

final JsonRpcResponse actualResponse = method.response(validRequest);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId(), false);

final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
Expand All @@ -170,10 +173,23 @@ public void requestReturnsErrorWhenP2pDisabled() {
.thenThrow(
new P2pDisabledException("P2P networking disabled. Unable to connect to add peer."));

final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303"
});

final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(validRequest.getId(), JsonRpcError.P2P_DISABLED);
new JsonRpcErrorResponse(request.getId(), JsonRpcError.P2P_DISABLED);

final JsonRpcResponse actualResponse = method.response(validRequest);
final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
Expand All @@ -183,11 +199,24 @@ public void requestReturnsErrorWhenPeerNotWhitelisted() {
when(p2pNetwork.addMaintainConnectionPeer(any()))
.thenThrow(new PeerNotWhitelistedException("Cannot add peer that is not whitelisted"));

final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"admin_addPeer",
new String[] {
"enode://"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "00000000000000000000000000000000"
+ "@127.0.0.1:30303"
});

final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(
validRequest.getId(), JsonRpcError.NON_WHITELISTED_NODE_CANNOT_BE_ADDED_AS_A_PEER);
request.getId(), JsonRpcError.NON_WHITELISTED_NODE_CANNOT_BE_ADDED_AS_A_PEER);

final JsonRpcResponse actualResponse = method.response(validRequest);
final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import tech.pegasys.pantheon.ethereum.rlp.RLPInput;
import tech.pegasys.pantheon.util.NetworkUtility;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.enode.EnodeURL;

import java.net.URI;
import java.util.Objects;
Expand All @@ -43,18 +42,6 @@ public class DefaultPeer extends DefaultPeerId implements Peer {

private final Endpoint endpoint;

public static DefaultPeer fromEnodeURL(final EnodeURL enodeURL) {
final int udpPort = enodeURL.getDiscoveryPort().orElse(enodeURL.getListeningPort());

final Endpoint endpoint =
new Endpoint(
enodeURL.getInetAddress().getHostAddress(),
udpPort,
OptionalInt.of(enodeURL.getListeningPort()));

return new DefaultPeer(BytesValue.fromHexString(enodeURL.getNodeId()), endpoint);
}

/**
* Creates a {@link DefaultPeer} instance from a String representation of an enode URL.
*
Expand Down
12 changes: 4 additions & 8 deletions pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ public Runner build() {
jsonRpcConfiguration.getRpcApis(),
filterManager,
accountWhitelistController,
privacyParameters,
peerCache);
privacyParameters);
jsonRpcHttpService =
Optional.of(
new JsonRpcHttpService(
Expand All @@ -322,8 +321,7 @@ public Runner build() {
webSocketConfiguration.getRpcApis(),
filterManager,
accountWhitelistController,
privacyParameters,
peerCache);
privacyParameters);

final SubscriptionManager subscriptionManager =
createSubscriptionManager(vertx, transactionPool);
Expand Down Expand Up @@ -382,8 +380,7 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(
final Collection<RpcApi> jsonRpcApis,
final FilterManager filterManager,
final Optional<AccountWhitelistController> accountWhitelistController,
final PrivacyParameters privacyParameters,
final PeerCache peerCache) {
final PrivacyParameters privacyParameters) {
final Map<String, JsonRpcMethod> methods =
new JsonRpcMethodsFactory()
.methods(
Expand All @@ -402,8 +399,7 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(
jsonRpcApis,
filterManager,
accountWhitelistController,
privacyParameters,
peerCache);
privacyParameters);
methods.putAll(pantheonController.getAdditionalJsonRpcMethods(jsonRpcApis));
return methods;
}
Expand Down