From f8e6392f2e22c0679bdd74ff67fc534204755171 Mon Sep 17 00:00:00 2001 From: "S. Matthew English" Date: Mon, 6 May 2019 06:11:27 -0400 Subject: [PATCH] lesgetit --- .../methods/NetServicesIntegrationTest.java | 164 ++++++++++++++---- .../ethereum/jsonrpc/JsonRpcHttpService.java | 3 +- .../jsonrpc/internal/methods/NetServices.java | 11 +- 3 files changed, 138 insertions(+), 40 deletions(-) diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/NetServicesIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/NetServicesIntegrationTest.java index 6418677a4e..ad6f161d58 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/NetServicesIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/NetServicesIntegrationTest.java @@ -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 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 = + Optional.of(mock(AccountWhitelistController.class)); + final Optional 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()); + } } diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java index e9c71362cc..29528c4c35 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java @@ -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) { + private void handleJsonSingleRequest( + final RoutingContext routingContext, final JsonObject request, final Optional user) { final HttpServerResponse response = routingContext.response(); vertx.executeBlocking( future -> { diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetServices.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetServices.java index bff737189c..d48d31d1c0 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetServices.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetServices.java @@ -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> servicesMapBuilder = ImmutableMap.builder(); @@ -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 createServiceDetailsMap(final String host, final int port) {