Inject synchronous Vertx as dependency for engine api (#4289)

Inject vertx dependency into EngineJsonRpcMethods instead of creating a new one. 

Improves testability, before some tests failed with too many files open because vertx inside EngineJsonRpcMethods was not being closed on test's afterEach hook

Signed-off-by: Pedro Novais <jpvnovais@gmail.com>
pull/4390/head
Pedro Novais 2 years ago committed by GitHub
parent e9d261d47c
commit 6dc07494bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
  2. 6
      ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java
  3. 21
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/ExecutionEngineJsonRpcMethods.java
  4. 8
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethodsFactory.java
  5. 6
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java
  6. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostAllowlistTest.java
  7. 4
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java
  8. 7
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java
  9. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTestBase.java
  10. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTlsClientAuthTest.java
  11. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTlsMisconfigurationTest.java
  12. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTlsTest.java
  13. 3
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/websocket/WebSocketServiceLoginTest.java

@ -142,6 +142,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import graphql.GraphQL; import graphql.GraphQL;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -986,6 +987,9 @@ public class RunnerBuilder {
final Map<String, BesuPlugin> namedPlugins, final Map<String, BesuPlugin> namedPlugins,
final Path dataDir, final Path dataDir,
final RpcEndpointServiceImpl rpcEndpointServiceImpl) { final RpcEndpointServiceImpl rpcEndpointServiceImpl) {
// sync vertx for engine consensus API, to process requests in FIFO order;
final Vertx consensusEngineServer = Vertx.vertx(new VertxOptions().setWorkerPoolSize(1));
final Map<String, JsonRpcMethod> methods = final Map<String, JsonRpcMethod> methods =
new JsonRpcMethodsFactory() new JsonRpcMethodsFactory()
.methods( .methods(
@ -1012,7 +1016,8 @@ public class RunnerBuilder {
natService, natService,
namedPlugins, namedPlugins,
dataDir, dataDir,
besuController.getProtocolManager().ethContext().getEthPeers()); besuController.getProtocolManager().ethContext().getEthPeers(),
consensusEngineServer);
methods.putAll(besuController.getAdditionalJsonRpcMethods(jsonRpcApis)); methods.putAll(besuController.getAdditionalJsonRpcMethods(jsonRpcApis));
var pluginMethods = rpcEndpointServiceImpl.getPluginMethods(jsonRpcConfiguration.getRpcApis()); var pluginMethods = rpcEndpointServiceImpl.getPluginMethods(jsonRpcConfiguration.getRpcApis());

@ -55,6 +55,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
/** Provides a facade to construct the JSON-RPC component. */ /** Provides a facade to construct the JSON-RPC component. */
public class JsonRpcTestMethodsFactory { public class JsonRpcTestMethodsFactory {
@ -180,6 +183,7 @@ public class JsonRpcTestMethodsFactory {
natService, natService,
new HashMap<>(), new HashMap<>(),
dataDir, dataDir,
ethPeers); ethPeers,
Vertx.vertx(new VertxOptions().setWorkerPoolSize(1)));
} }
} }

@ -30,7 +30,6 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods { public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods {
@ -38,13 +37,14 @@ public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods {
private final Optional<MergeMiningCoordinator> mergeCoordinator; private final Optional<MergeMiningCoordinator> mergeCoordinator;
private final ProtocolContext protocolContext; private final ProtocolContext protocolContext;
private final EthPeers ethPeers; private final EthPeers ethPeers;
private final Vertx consensusEngineServer;
ExecutionEngineJsonRpcMethods( ExecutionEngineJsonRpcMethods(
final MiningCoordinator miningCoordinator, final MiningCoordinator miningCoordinator,
final ProtocolContext protocolContext, final ProtocolContext protocolContext,
final EthPeers ethPeers) { final EthPeers ethPeers,
final Vertx consensusEngineServer) {
this.mergeCoordinator = this.mergeCoordinator =
Optional.ofNullable(miningCoordinator) Optional.ofNullable(miningCoordinator)
.filter(mc -> mc.isCompatibleWithEngineApi()) .filter(mc -> mc.isCompatibleWithEngineApi())
@ -52,6 +52,7 @@ public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods {
this.protocolContext = protocolContext; this.protocolContext = protocolContext;
this.ethPeers = ethPeers; this.ethPeers = ethPeers;
this.consensusEngineServer = consensusEngineServer;
} }
@Override @Override
@ -61,15 +62,17 @@ public class ExecutionEngineJsonRpcMethods extends ApiGroupJsonRpcMethods {
@Override @Override
protected Map<String, JsonRpcMethod> create() { protected Map<String, JsonRpcMethod> create() {
Vertx syncVertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(1));
if (mergeCoordinator.isPresent()) { if (mergeCoordinator.isPresent()) {
return mapOf( return mapOf(
new EngineGetPayload(syncVertx, protocolContext, blockResultFactory), new EngineGetPayload(consensusEngineServer, protocolContext, blockResultFactory),
new EngineNewPayload(syncVertx, protocolContext, mergeCoordinator.get(), ethPeers), new EngineNewPayload(
new EngineForkchoiceUpdated(syncVertx, protocolContext, mergeCoordinator.get()), consensusEngineServer, protocolContext, mergeCoordinator.get(), ethPeers),
new EngineExchangeTransitionConfiguration(syncVertx, protocolContext)); new EngineForkchoiceUpdated(
consensusEngineServer, protocolContext, mergeCoordinator.get()),
new EngineExchangeTransitionConfiguration(consensusEngineServer, protocolContext));
} else { } else {
return mapOf(new EngineExchangeTransitionConfiguration(syncVertx, protocolContext)); return mapOf(
new EngineExchangeTransitionConfiguration(consensusEngineServer, protocolContext));
} }
} }
} }

@ -46,6 +46,8 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import io.vertx.core.Vertx;
public class JsonRpcMethodsFactory { public class JsonRpcMethodsFactory {
public Map<String, JsonRpcMethod> methods( public Map<String, JsonRpcMethod> methods(
@ -72,7 +74,8 @@ public class JsonRpcMethodsFactory {
final NatService natService, final NatService natService,
final Map<String, BesuPlugin> namedPlugins, final Map<String, BesuPlugin> namedPlugins,
final Path dataDir, final Path dataDir,
final EthPeers ethPeers) { final EthPeers ethPeers,
final Vertx consensusEngineServer) {
final Map<String, JsonRpcMethod> enabled = new HashMap<>(); final Map<String, JsonRpcMethod> enabled = new HashMap<>();
if (!rpcApis.isEmpty()) { if (!rpcApis.isEmpty()) {
@ -94,7 +97,8 @@ public class JsonRpcMethodsFactory {
blockchainQueries, protocolSchedule, metricsSystem, transactionPool, dataDir), blockchainQueries, protocolSchedule, metricsSystem, transactionPool, dataDir),
new EeaJsonRpcMethods( new EeaJsonRpcMethods(
blockchainQueries, protocolSchedule, transactionPool, privacyParameters), blockchainQueries, protocolSchedule, transactionPool, privacyParameters),
new ExecutionEngineJsonRpcMethods(miningCoordinator, protocolContext, ethPeers), new ExecutionEngineJsonRpcMethods(
miningCoordinator, protocolContext, ethPeers, consensusEngineServer),
new GoQuorumJsonRpcPrivacyMethods( new GoQuorumJsonRpcPrivacyMethods(
blockchainQueries, protocolSchedule, transactionPool, privacyParameters), blockchainQueries, protocolSchedule, transactionPool, privacyParameters),
new EthJsonRpcMethods( new EthJsonRpcMethods(

@ -61,6 +61,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import org.junit.After; import org.junit.After;
@ -84,6 +85,7 @@ public abstract class AbstractJsonRpcHttpServiceTest {
RpcApis.TRACE.name()); RpcApis.TRACE.name());
protected final Vertx vertx = Vertx.vertx(); protected final Vertx vertx = Vertx.vertx();
protected final Vertx syncVertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(1));
protected JsonRpcHttpService service; protected JsonRpcHttpService service;
protected OkHttpClient client; protected OkHttpClient client;
protected String baseUrl; protected String baseUrl;
@ -188,7 +190,8 @@ public abstract class AbstractJsonRpcHttpServiceTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class)); mock(EthPeers.class),
syncVertx);
} }
protected void startService() throws Exception { protected void startService() throws Exception {
@ -224,5 +227,6 @@ public abstract class AbstractJsonRpcHttpServiceTest {
client.connectionPool().evictAll(); client.connectionPool().evictAll();
service.stop().join(); service.stop().join();
vertx.close(); vertx.close();
syncVertx.close();
} }
} }

@ -122,7 +122,8 @@ public class JsonRpcHttpServiceHostAllowlistTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
service = createJsonRpcHttpService(); service = createJsonRpcHttpService();
service.start().join(); service.start().join();

@ -87,6 +87,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.StrictStubs.class) @RunWith(MockitoJUnitRunner.StrictStubs.class)
public class JsonRpcHttpServiceLoginTest { public class JsonRpcHttpServiceLoginTest {
@ClassRule public static final TemporaryFolder folder = new TemporaryFolder(); @ClassRule public static final TemporaryFolder folder = new TemporaryFolder();
private static final Vertx vertx = Vertx.vertx(); private static final Vertx vertx = Vertx.vertx();
@ -151,7 +152,8 @@ public class JsonRpcHttpServiceLoginTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
service = createJsonRpcHttpService(); service = createJsonRpcHttpService();
jwtAuth = service.authenticationService.get().getJwtAuthProvider(); jwtAuth = service.authenticationService.get().getJwtAuthProvider();
service.start().join(); service.start().join();

@ -81,6 +81,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class JsonRpcHttpServiceRpcApisTest { public class JsonRpcHttpServiceRpcApisTest {
@Rule public final TemporaryFolder folder = new TemporaryFolder(); @Rule public final TemporaryFolder folder = new TemporaryFolder();
private final Vertx vertx = Vertx.vertx(); private final Vertx vertx = Vertx.vertx();
@ -220,7 +221,8 @@ public class JsonRpcHttpServiceRpcApisTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
final JsonRpcHttpService jsonRpcHttpService = final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService( new JsonRpcHttpService(
vertx, vertx,
@ -318,7 +320,8 @@ public class JsonRpcHttpServiceRpcApisTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
final JsonRpcHttpService jsonRpcHttpService = final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService( new JsonRpcHttpService(
vertx, vertx,

@ -130,7 +130,8 @@ public class JsonRpcHttpServiceTestBase {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
ethPeersMock)); ethPeersMock,
vertx));
service = createJsonRpcHttpService(createLimitedJsonRpcConfig()); service = createJsonRpcHttpService(createLimitedJsonRpcConfig());
service.start().join(); service.start().join();

@ -137,7 +137,8 @@ public class JsonRpcHttpServiceTlsClientAuthTest {
natService, natService,
Collections.emptyMap(), Collections.emptyMap(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
System.setProperty("javax.net.ssl.trustStore", CLIENT_AS_CA_CERT.getKeyStoreFile().toString()); System.setProperty("javax.net.ssl.trustStore", CLIENT_AS_CA_CERT.getKeyStoreFile().toString());
System.setProperty( System.setProperty(

@ -125,7 +125,8 @@ public class JsonRpcHttpServiceTlsMisconfigurationTest {
natService, natService,
Collections.emptyMap(), Collections.emptyMap(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
} }
@After @After

@ -127,7 +127,8 @@ public class JsonRpcHttpServiceTlsTest {
natService, natService,
Collections.emptyMap(), Collections.emptyMap(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
service = createJsonRpcHttpService(createJsonRpcConfig()); service = createJsonRpcHttpService(createJsonRpcConfig());
service.start().join(); service.start().join();
baseUrl = service.url(); baseUrl = service.url();

@ -184,7 +184,8 @@ public class WebSocketServiceLoginTest {
natService, natService,
new HashMap<>(), new HashMap<>(),
folder.getRoot().toPath(), folder.getRoot().toPath(),
mock(EthPeers.class))); mock(EthPeers.class),
vertx));
websocketMethods.putAll(rpcMethods); websocketMethods.putAll(rpcMethods);
webSocketMessageHandlerSpy = webSocketMessageHandlerSpy =

Loading…
Cancel
Save