From 3baa4da99e11272db042f8afbcaf8287bad6896e Mon Sep 17 00:00:00 2001 From: Justin Florentine Date: Thu, 30 Jun 2022 19:38:45 -0400 Subject: [PATCH] allow upgrade whether websockets enabled or not (#4019) * allow upgrade whether websockets enabled or not Signed-off-by: Justin Florentine Co-authored-by: Sally MacFarlane --- .../org/hyperledger/besu/RunnerBuilder.java | 11 +++-- .../hyperledger/besu/RunnerBuilderTest.java | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 687e754759e..cae72b00225 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -627,6 +627,9 @@ public Runner build() { new HealthService(new ReadinessCheck(peerNetwork, synchronizer)))); } + final SubscriptionManager subscriptionManager = + createSubscriptionManager(vertx, transactionPool, blockchainQueries); + Optional engineJsonRpcService = Optional.empty(); if (engineJsonRpcConfiguration.isPresent() && engineJsonRpcConfiguration.get().isEnabled()) { final Map engineMethods = @@ -669,6 +672,9 @@ public Runner build() { ? webSocketConfiguration : WebSocketConfiguration.createEngineDefault(); + final WebSocketMethodsFactory websocketMethodsFactory = + new WebSocketMethodsFactory(subscriptionManager, engineMethods); + engineJsonRpcService = Optional.of( new JsonRpcService( @@ -677,7 +683,7 @@ public Runner build() { engineJsonRpcConfiguration.orElse(JsonRpcConfiguration.createEngineDefault()), metricsSystem, natService, - engineMethods, + websocketMethodsFactory.methods(), Optional.ofNullable(engineSocketConfig), besuController.getProtocolManager().ethContext().getScheduler(), authToUse, @@ -743,9 +749,6 @@ public Runner build() { dataDir, rpcEndpointServiceImpl); - final SubscriptionManager subscriptionManager = - createSubscriptionManager(vertx, transactionPool, blockchainQueries); - createLogsSubscriptionService( context.getBlockchain(), context.getWorldStateArchive(), diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java index d3bce6401a6..4db04398497 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java @@ -304,6 +304,49 @@ public void whenEngineApiAddedWebSocketReadyOnSamePort() { assertThat(runner.getEngineJsonRpcPort()).isPresent(); } + @Test + public void whenEngineApiAddedEthSubscribeAvailable() { + WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault(); + wsRpc.setEnabled(true); + EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class); + when(mockMainnet.getNetworkId()).thenReturn(BigInteger.ONE); + MergeConfigOptions.setMergeEnabled(true); + when(besuController.getMiningCoordinator()).thenReturn(mock(MergeMiningCoordinator.class)); + JsonRpcConfiguration engineConf = JsonRpcConfiguration.createEngineDefault(); + engineConf.setEnabled(true); + + final Runner runner = + new RunnerBuilder() + .discovery(true) + .p2pListenInterface("0.0.0.0") + .p2pListenPort(30303) + .p2pAdvertisedHost("127.0.0.1") + .p2pEnabled(true) + .natMethod(NatMethod.NONE) + .besuController(besuController) + .ethNetworkConfig(mockMainnet) + .metricsSystem(mock(ObservableMetricsSystem.class)) + .permissioningService(mock(PermissioningServiceImpl.class)) + .jsonRpcConfiguration(JsonRpcConfiguration.createDefault()) + .engineJsonRpcConfiguration(engineConf) + .webSocketConfiguration(wsRpc) + .jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)) + .graphQLConfiguration(mock(GraphQLConfiguration.class)) + .metricsConfiguration(mock(MetricsConfiguration.class)) + .vertx(Vertx.vertx()) + .dataDir(dataDir.getRoot().toPath()) + .storageProvider(mock(KeyValueStorageProvider.class)) + .forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY)) + .rpcEndpointService(new RpcEndpointServiceImpl()) + .besuPluginContext(mock(BesuPluginContextImpl.class)) + .build(); + + assertThat(runner.getEngineJsonRpcPort()).isPresent(); + runner.startExternalServices(); + // assert that rpc method collection has eth_subscribe in it. + runner.stop(); + } + @Test public void noEngineApiNoServiceForMethods() { JsonRpcConfiguration defaultRpcConfig = JsonRpcConfiguration.createDefault();