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

Commit

Permalink
lesgetit
Browse files Browse the repository at this point in the history
  • Loading branch information
smatthewenglish committed May 6, 2019
1 parent 97227aa commit f8e6392
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,139 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.methods;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryBlockchain;
import static tech.pegasys.pantheon.ethereum.core.InMemoryStorageProvider.createInMemoryWorldStateArchive;

import tech.pegasys.pantheon.config.StubGenesisConfigOptions;
import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Synchronizer;
import tech.pegasys.pantheon.ethereum.eth.transactions.TransactionPool;
import tech.pegasys.pantheon.ethereum.jsonrpc.BlockchainImporter;
import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcResponseUtils;
import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcTestMethodsFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcConfiguration;
import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcMethodsFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter.FilterIdGenerator;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter.FilterManager;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter.FilterRepository;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.WebSocketConfiguration;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.ethereum.permissioning.NodeLocalConfigPermissioningController;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
import tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration;

import java.net.URL;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.junit.Before;
import org.junit.Test;

public class NetServicesIntegrationTest {
//private final JsonRpcResponseUtils jsonRpcResponseUtils = new JsonRpcResponseUtils();
private JsonRpcTestMethodsFactory jsonRpcTestMethodsFactory;
private JsonRpcMethod jsonRpcMethod;

@Before
public void setUp() throws Exception {
final URL blocksUrl =
NetServicesIntegrationTest.class
.getClassLoader()
.getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestBlockchain.blocks");
final URL genesisJsonUrl =
NetServicesIntegrationTest.class
.getClassLoader()
.getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestGenesis.json");
final String genesisJson = Resources.toString(genesisJsonUrl, Charsets.UTF_8);
final BlockchainImporter blockchainImporter = new BlockchainImporter(blocksUrl, genesisJson);
jsonRpcTestMethodsFactory = new JsonRpcTestMethodsFactory(blockchainImporter);

jsonRpcMethod = jsonRpcTestMethodsFactory.methods().get("net_services");
}

@Test
public void test() {
final JsonRpcRequest jsonRpcRequest = new JsonRpcRequest("2.0", "net_services", new Object[]{});
final JsonRpcSuccessResponse jsonRpcSuccessResponse = (JsonRpcSuccessResponse) jsonRpcMethod.response(jsonRpcRequest);

System.out.println("getType: " + jsonRpcSuccessResponse.getType());
System.out.println("hashCode: " + jsonRpcSuccessResponse.hashCode());
System.out.println("getId: " + jsonRpcSuccessResponse.getId());

System.out.println("getResult: " + jsonRpcSuccessResponse.getResult());
}
private Map<String, JsonRpcMethod> JsonRpcMethodsMap;
private JsonRpcMethod jsonRpcMethod;

@Before
public void setUp() throws Exception {
final URL blocksUrl =
NetServicesIntegrationTest.class
.getClassLoader()
.getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestBlockchain.blocks");
final URL genesisJsonUrl =
NetServicesIntegrationTest.class
.getClassLoader()
.getResource("tech/pegasys/pantheon/ethereum/jsonrpc/jsonRpcTestGenesis.json");
final String genesisJson = Resources.toString(genesisJsonUrl, Charsets.UTF_8);

final String CLIENT_VERSION = "TestClientVersion/0.1.0";
final int NETWORK_ID = 123;

final BlockchainImporter blockchainImporter = new BlockchainImporter(blocksUrl, genesisJson);
final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
blockchainImporter.getGenesisState().writeStateTo(worldStateArchive.getMutable());

final MutableBlockchain mutableBlockchain =
createInMemoryBlockchain(blockchainImporter.getGenesisBlock());
final BlockchainQueries blockchainQueries =
new BlockchainQueries(mutableBlockchain, worldStateArchive);

final Synchronizer synchronizer = mock(Synchronizer.class);
final TransactionPool transactionPool = mock(TransactionPool.class);
final FilterManager filterManager =
new FilterManager(
blockchainQueries, transactionPool, new FilterIdGenerator(), new FilterRepository());
final EthHashMiningCoordinator miningCoordinator = mock(EthHashMiningCoordinator.class);
final MetricsSystem metricsSystem = new NoOpMetricsSystem();
final Optional<AccountWhitelistController> accountWhitelistController =
Optional.of(mock(AccountWhitelistController.class));
final Optional<NodeLocalConfigPermissioningController> nodeWhitelistController =
Optional.of(mock(NodeLocalConfigPermissioningController.class));
final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);

// JsonRpcConfiguration:
final JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault();
jsonRpcConfiguration.setEnabled(true);
// WebSocketConfiguration:
final WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault();
webSocketConfiguration.setEnabled(true);
// P2PNetwork:
final P2PNetwork p2PNetwork = mock(P2PNetwork.class);
when(p2PNetwork.isP2pEnabled()).thenReturn(true);
// MetricsConfiguration:
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault();
metricsConfiguration.setEnabled(true);

JsonRpcMethodsMap =
new JsonRpcMethodsFactory()
.methods(
CLIENT_VERSION,
NETWORK_ID,
new StubGenesisConfigOptions(),
p2PNetwork,
blockchainQueries,
synchronizer,
MainnetProtocolSchedule.create(),
filterManager,
transactionPool,
miningCoordinator,
metricsSystem,
new HashSet<>(),
accountWhitelistController,
nodeWhitelistController,
RpcApis.DEFAULT_JSON_RPC_APIS,
privacyParameters,
jsonRpcConfiguration,
webSocketConfiguration,
metricsConfiguration);

jsonRpcMethod = JsonRpcMethodsMap.get("net_services");
}

@Test
public void test() {
final JsonRpcRequest jsonRpcRequest =
new JsonRpcRequest("2.0", "net_services", new Object[] {});
final JsonRpcSuccessResponse jsonRpcSuccessResponse =
(JsonRpcSuccessResponse) jsonRpcMethod.response(jsonRpcRequest);

System.out.println("getType: " + jsonRpcSuccessResponse.getType());
System.out.println("hashCode: " + jsonRpcSuccessResponse.hashCode());
System.out.println("getId: " + jsonRpcSuccessResponse.getId());
/* * */
System.out.println("getResult: " + jsonRpcSuccessResponse.getResult());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ private void handleEmptyRequest(final RoutingContext routingContext) {
routingContext.response().setStatusCode(201).end();
}

private void handleJsonSingleRequest(final RoutingContext routingContext, final JsonObject request, final Optional<User> user) {
private void handleJsonSingleRequest(
final RoutingContext routingContext, final JsonObject request, final Optional<User> user) {
final HttpServerResponse response = routingContext.response();
vertx.executeBlocking(
future -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequest req) {

System.out.println("jsonRpcConfiguration.isEnabled(): " + jsonRpcConfiguration.isEnabled());
System.out.println("p2pNetwork.isP2pEnabled(): " + p2pNetwork.isP2pEnabled());

final ImmutableMap.Builder<String, ImmutableMap<String, String>> servicesMapBuilder =
ImmutableMap.builder();

Expand Down Expand Up @@ -76,7 +80,12 @@ public JsonRpcResponse response(final JsonRpcRequest req) {
createServiceDetailsMap(metricsConfiguration.getHost(), metricsConfiguration.getPort()));
}

return new JsonRpcSuccessResponse(req.getId(), servicesMapBuilder.build());
JsonRpcSuccessResponse jsonRpcSuccessResponse =
new JsonRpcSuccessResponse(req.getId(), servicesMapBuilder.build());

System.out.println("jsonRpcSuccessResponse: " + jsonRpcSuccessResponse.getResult());

return jsonRpcSuccessResponse;
}

private ImmutableMap<String, String> createServiceDetailsMap(final String host, final int port) {
Expand Down

0 comments on commit f8e6392

Please sign in to comment.