diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 22159063333..7dfe4468559 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -453,12 +453,30 @@ public class CommonParameter { @Getter @Setter public String cryptoEngine = Constant.ECKey_ENGINE; + + @Getter + @Setter + public boolean rpcEnable = true; + + @Getter + @Setter + public boolean rpcSolidityEnable = true; + + @Getter + @Setter + public boolean rpcPBFTEnable = true; + @Getter @Setter public boolean fullNodeHttpEnable = true; @Getter @Setter public boolean solidityNodeHttpEnable = true; + + @Getter + @Setter + public boolean pBFTHttpEnable = true; + @Getter @Setter public boolean jsonRpcHttpFullNodeEnable = false; diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 0e634d3ef7d..71eb87e2800 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -124,15 +124,21 @@ public class Constant { public static final String NODE_DNS_AWS_REGION = "node.dns.awsRegion"; public static final String NODE_DNS_AWS_HOST_ZONE_ID = "node.dns.awsHostZoneId"; + // config for rpc public static final String NODE_RPC_PORT = "node.rpc.port"; public static final String NODE_RPC_SOLIDITY_PORT = "node.rpc.solidityPort"; public static final String NODE_RPC_PBFT_PORT = "node.rpc.PBFTPort"; + public static final String NODE_RPC_ENABLE = "node.rpc.enable"; + public static final String NODE_RPC_SOLIDITY_ENABLE = "node.rpc.solidityEnable"; + public static final String NODE_RPC_PBFT_ENABLE = "node.rpc.PBFTEnable"; + // config for http public static final String NODE_HTTP_FULLNODE_PORT = "node.http.fullNodePort"; public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort"; public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable"; public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable"; + public static final String NODE_HTTP_PBFT_ENABLE = "node.http.PBFTEnable"; public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort"; - + // config for jsonrpc public static final String NODE_JSONRPC_HTTP_FULLNODE_ENABLE = "node.jsonrpc.httpFullNodeEnable"; public static final String NODE_JSONRPC_HTTP_FULLNODE_PORT = "node.jsonrpc.httpFullNodePort"; public static final String NODE_JSONRPC_HTTP_SOLIDITY_ENABLE = "node.jsonrpc.httpSolidityEnable"; diff --git a/framework/src/main/java/org/tron/common/application/AbstractService.java b/framework/src/main/java/org/tron/common/application/AbstractService.java new file mode 100644 index 00000000000..79c25dc4944 --- /dev/null +++ b/framework/src/main/java/org/tron/common/application/AbstractService.java @@ -0,0 +1,73 @@ +package org.tron.common.application; + +import com.google.common.base.Objects; +import java.util.concurrent.CompletableFuture; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.tron.core.config.args.Args; + + +@Slf4j(topic = "service") +public abstract class AbstractService implements Service { + + protected int port; + @Getter + protected boolean enable; + @Getter + protected final String name = this.getClass().getSimpleName(); + + + @Override + public CompletableFuture start() { + logger.info("{} starting on {}", name, port); + final CompletableFuture resultFuture = new CompletableFuture<>(); + try { + innerStart(); + resultFuture.complete(true); + logger.info("{} started, listening on {}", name, port); + } catch (Exception e) { + resultFuture.completeExceptionally(e); + } + return resultFuture; + } + + @Override + public CompletableFuture stop() { + logger.info("{} shutdown...", name); + final CompletableFuture resultFuture = new CompletableFuture<>(); + try { + innerStop(); + resultFuture.complete(true); + logger.info("{} shutdown complete", name); + } catch (Exception e) { + resultFuture.completeExceptionally(e); + } + return resultFuture; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AbstractService that = (AbstractService) o; + return port == that.port; + } + + @Override + public int hashCode() { + return Objects.hashCode(name, port); + } + + public abstract void innerStart() throws Exception; + + public abstract void innerStop() throws Exception; + + protected boolean isFullNode() { + return !Args.getInstance().isSolidityNode(); + } + +} diff --git a/framework/src/main/java/org/tron/common/application/ApplicationImpl.java b/framework/src/main/java/org/tron/common/application/ApplicationImpl.java index 3cb75cb1e24..28108adfa88 100644 --- a/framework/src/main/java/org/tron/common/application/ApplicationImpl.java +++ b/framework/src/main/java/org/tron/common/application/ApplicationImpl.java @@ -1,5 +1,6 @@ package org.tron.common.application; +import java.util.concurrent.CountDownLatch; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -15,6 +16,7 @@ @Component public class ApplicationImpl implements Application { + @Autowired private ServiceContainer services; @Autowired @@ -29,6 +31,8 @@ public class ApplicationImpl implements Application { @Autowired private ConsensusService consensusService; + private final CountDownLatch shutdown = new CountDownLatch(1); + @Override public void setOptions(Args args) { // not used @@ -37,24 +41,23 @@ public void setOptions(Args args) { @Override @Autowired public void init(CommonParameter parameter) { - services = new ServiceContainer(); + // not used } @Override public void addService(Service service) { - services.add(service); + // used by test } @Override public void initServices(CommonParameter parameter) { - services.init(parameter); + // not used } /** * start up the app. */ public void startup() { - this.initServices(Args.getInstance()); this.startServices(); if ((!Args.getInstance().isSolidityNode()) && (!Args.getInstance().isP2pDisable())) { tronNetService.start(); @@ -71,16 +74,27 @@ public void shutdown() { tronNetService.close(); } dbManager.close(); + shutdown.countDown(); } @Override public void startServices() { - services.start(); + try { + services.start(); + } catch (Exception e) { + logger.error("Failed to start services", e); + System.exit(1); + } } @Override public void blockUntilShutdown() { - services.blockUntilShutdown(); + try { + shutdown.await(); + } catch (final InterruptedException e) { + logger.debug("Interrupted, exiting", e); + Thread.currentThread().interrupt(); + } } @Override diff --git a/framework/src/main/java/org/tron/common/application/HttpService.java b/framework/src/main/java/org/tron/common/application/HttpService.java index 76f8e74d65c..e9a902002ba 100644 --- a/framework/src/main/java/org/tron/common/application/HttpService.java +++ b/framework/src/main/java/org/tron/common/application/HttpService.java @@ -15,67 +15,61 @@ package org.tron.common.application; -import com.google.common.base.Objects; +import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.server.ConnectionLimit; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.tron.core.config.args.Args; @Slf4j(topic = "rpc") -public abstract class HttpService implements Service { +public abstract class HttpService extends AbstractService { protected Server apiServer; - protected int port; + + protected String contextPath; @Override - public void blockUntilShutdown() { - if (apiServer != null) { - try { - apiServer.join(); - } catch (InterruptedException e) { - logger.warn("{}", e.getMessage()); - Thread.currentThread().interrupt(); - } + public void innerStart() throws Exception { + if (this.apiServer != null) { + this.apiServer.start(); } } @Override - public void start() { - if (apiServer != null) { - try { - apiServer.start(); - logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); - } catch (Exception e) { - logger.error("{}", this.getClass().getSimpleName(), e); - } + public void innerStop() throws Exception { + if (this.apiServer != null) { + this.apiServer.stop(); } } @Override - public void stop() { - if (apiServer != null) { - logger.info("{} shutdown...", this.getClass().getSimpleName()); - try { - apiServer.stop(); - } catch (Exception e) { - logger.warn("{}", this.getClass().getSimpleName(), e); - } - logger.info("{} shutdown complete", this.getClass().getSimpleName()); - } + public CompletableFuture start() { + initServer(); + ServletContextHandler context = initContextHandler(); + addServlet(context); + addFilter(context); + return super.start(); } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; + protected void initServer() { + this.apiServer = new Server(this.port); + int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber(); + if (maxHttpConnectNumber > 0) { + this.apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, this.apiServer)); } - if (o == null || getClass() != o.getClass()) { - return false; - } - HttpService that = (HttpService) o; - return port == that.port; } - @Override - public int hashCode() { - return Objects.hashCode(getClass().getSimpleName(), port); + protected ServletContextHandler initContextHandler() { + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath(this.contextPath); + this.apiServer.setHandler(context); + return context; + } + + protected abstract void addServlet(ServletContextHandler context); + + protected void addFilter(ServletContextHandler context) { + } } diff --git a/framework/src/main/java/org/tron/common/application/RpcService.java b/framework/src/main/java/org/tron/common/application/RpcService.java index cb89441174a..e016e27d396 100644 --- a/framework/src/main/java/org/tron/common/application/RpcService.java +++ b/framework/src/main/java/org/tron/common/application/RpcService.java @@ -15,71 +15,97 @@ package org.tron.common.application; -import com.google.common.base.Objects; import io.grpc.Server; -import java.io.IOException; +import io.grpc.netty.NettyServerBuilder; +import io.grpc.protobuf.services.ProtoReflectionService; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.tron.common.es.ExecutorServiceManager; +import org.tron.common.parameter.CommonParameter; +import org.tron.core.config.args.Args; +import org.tron.core.services.filter.LiteFnQueryGrpcInterceptor; +import org.tron.core.services.ratelimiter.RateLimiterInterceptor; +import org.tron.core.services.ratelimiter.RpcApiAccessInterceptor; @Slf4j(topic = "rpc") -public abstract class RpcService implements Service { +public abstract class RpcService extends AbstractService { - protected Server apiServer; - protected int port; + private Server apiServer; + protected String executorName; + + @Autowired + private RateLimiterInterceptor rateLimiterInterceptor; + + @Autowired + private LiteFnQueryGrpcInterceptor liteFnQueryGrpcInterceptor; + + @Autowired + private RpcApiAccessInterceptor apiAccessInterceptor; @Override - public void blockUntilShutdown() { - if (apiServer != null) { - try { - apiServer.awaitTermination(); - } catch (InterruptedException e) { - logger.warn("{}", e.getMessage()); - Thread.currentThread().interrupt(); - } + public void innerStart() throws Exception { + if (this.apiServer != null) { + this.apiServer.start(); } } @Override - public void start() { - if (apiServer != null) { - try { - apiServer.start(); - logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); - } catch (IOException e) { - logger.error("{}", this.getClass().getSimpleName(), e); - } + public void innerStop() throws Exception { + if (this.apiServer != null) { + this.apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS); } } @Override - public void stop() { - if (apiServer != null) { - logger.info("{} shutdown...", this.getClass().getSimpleName()); - try { - apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - logger.warn("{}", this.getClass().getSimpleName(), e); - } - logger.info("{} shutdown complete", this.getClass().getSimpleName()); - } + public CompletableFuture start() { + NettyServerBuilder serverBuilder = initServerBuilder(); + addService(serverBuilder); + addInterceptor(serverBuilder); + initServer(serverBuilder); + this.rateLimiterInterceptor.init(this.apiServer); + return super.start(); } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; + protected NettyServerBuilder initServerBuilder() { + NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(this.port); + CommonParameter parameter = Args.getInstance(); + if (parameter.getRpcThreadNum() > 0) { + serverBuilder = serverBuilder + .executor(ExecutorServiceManager.newFixedThreadPool( + this.executorName, parameter.getRpcThreadNum())); } - if (o == null || getClass() != o.getClass()) { - return false; + // Set configs from config.conf or default value + serverBuilder + .maxConcurrentCallsPerConnection(parameter.getMaxConcurrentCallsPerConnection()) + .flowControlWindow(parameter.getFlowControlWindow()) + .maxConnectionIdle(parameter.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS) + .maxConnectionAge(parameter.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS) + .maxInboundMessageSize(parameter.getMaxMessageSize()) + .maxHeaderListSize(parameter.getMaxHeaderListSize()); + + if (parameter.isRpcReflectionServiceEnable()) { + serverBuilder.addService(ProtoReflectionService.newInstance()); } - RpcService that = (RpcService) o; - return port == that.port; + return serverBuilder; } - @Override - public int hashCode() { - return Objects.hashCode(getClass().getSimpleName(), port); + protected abstract void addService(NettyServerBuilder serverBuilder); + + protected void addInterceptor(NettyServerBuilder serverBuilder) { + // add a ratelimiter interceptor + serverBuilder.intercept(this.rateLimiterInterceptor); + + // add api access interceptor + serverBuilder.intercept(this.apiAccessInterceptor); + + // add lite fullnode query interceptor + serverBuilder.intercept(this.liteFnQueryGrpcInterceptor); + } + + protected void initServer(NettyServerBuilder serverBuilder) { + this.apiServer = serverBuilder.build(); } } diff --git a/framework/src/main/java/org/tron/common/application/Service.java b/framework/src/main/java/org/tron/common/application/Service.java index 67b4e3ce9ae..629e7e61a4c 100644 --- a/framework/src/main/java/org/tron/common/application/Service.java +++ b/framework/src/main/java/org/tron/common/application/Service.java @@ -15,22 +15,25 @@ package org.tron.common.application; -import org.tron.common.parameter.CommonParameter; +import java.util.concurrent.CompletableFuture; public interface Service { - void init(); - - void init(CommonParameter parameter); + /** + * Starts the service and all needed backend systems. + * + * @return completion state + */ + CompletableFuture start(); /** - * Start the service. - * {@link Service#init(CommonParameter parameter) init(CommonParameter parameter)} must be called - * before this method. + * Stops the service and performs needed cleanup. + * + * @return completion state */ - void start(); + CompletableFuture stop(); - void stop(); + boolean isEnable(); - void blockUntilShutdown(); + String getName(); } diff --git a/framework/src/main/java/org/tron/common/application/ServiceContainer.java b/framework/src/main/java/org/tron/common/application/ServiceContainer.java index 2951596add7..943f4617c1e 100644 --- a/framework/src/main/java/org/tron/common/application/ServiceContainer.java +++ b/framework/src/main/java/org/tron/common/application/ServiceContainer.java @@ -15,59 +15,77 @@ package org.tron.common.application; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Set; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; -import org.tron.common.parameter.CommonParameter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; @Slf4j(topic = "app") +@Component public class ServiceContainer { - private final Set services; + @Autowired + private List services; - public ServiceContainer() { - this.services = Collections.synchronizedSet(new LinkedHashSet<>()); - } - - public void add(Service service) { - this.services.add(service); - } + private List enabledServices; - - public void init() { - this.services.forEach(service -> { - logger.debug("Initing {}.", service.getClass().getSimpleName()); - service.init(); - }); + public ServiceContainer() { } - public void init(CommonParameter parameter) { - this.services.forEach(service -> { - logger.debug("Initing {}.", service.getClass().getSimpleName()); - service.init(parameter); - }); + @PostConstruct + private void initEnabledServices() { + this.enabledServices = this.services.stream() + .filter(Service::isEnable) + .collect(Collectors.toList()); } - public void start() { + void start() { logger.info("Starting api services."); - this.services.forEach(service -> { - logger.debug("Starting {}.", service.getClass().getSimpleName()); - service.start(); - }); + this.enabledServices.forEach(this::waitForServiceToStart); logger.info("All api services started."); } - public void stop() { + void stop() { logger.info("Stopping api services."); - this.services.forEach(service -> { - logger.debug("Stopping {}.", service.getClass().getSimpleName()); - service.stop(); - }); + this.enabledServices.forEach(this::waitForServiceToStop); logger.info("All api services stopped."); } - public void blockUntilShutdown() { - this.services.stream().findFirst().ifPresent(Service::blockUntilShutdown); + private void waitForServiceToStart(Service service) { + final String serviceName = service.getName(); + final CompletableFuture startFuture = service.start(); + do { + try { + startFuture.get(60, TimeUnit.SECONDS); + } catch (final InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while waiting for service to start", e); + } catch (final ExecutionException e) { + throw new IllegalStateException("Service " + serviceName + " failed to start", e); + } catch (final TimeoutException e) { + logger.warn("Service {} is taking an unusually long time to start", serviceName); + } + } while (!startFuture.isDone()); + } + + private void waitForServiceToStop(Service service) { + final String serviceName = service.getName(); + final CompletableFuture stopFuture = service.stop(); + try { + stopFuture.get(30, TimeUnit.SECONDS); + } catch (final InterruptedException e) { + logger.debug("Interrupted while waiting for service {} to complete", serviceName, e); + Thread.currentThread().interrupt(); + } catch (final ExecutionException e) { + logger.error("Service {} failed to shutdown", serviceName, e); + } catch (final TimeoutException e) { + logger.error("Service {} did not shut down cleanly", serviceName); + } } } diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 422efefaed8..4e6c8e2203f 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -189,8 +189,12 @@ public static void clearParam() { PARAMETER.validContractProtoThreadNum = 1; PARAMETER.shieldedTransInPendingMaxCounts = 10; PARAMETER.changedDelegation = 0; + PARAMETER.rpcEnable = true; + PARAMETER.rpcSolidityEnable = true; + PARAMETER.rpcPBFTEnable = true; PARAMETER.fullNodeHttpEnable = true; PARAMETER.solidityNodeHttpEnable = true; + PARAMETER.pBFTHttpEnable = true; PARAMETER.jsonRpcHttpFullNodeEnable = false; PARAMETER.jsonRpcHttpSolidityNodeEnable = false; PARAMETER.jsonRpcHttpPBFTNodeEnable = false; @@ -354,14 +358,21 @@ private static Map getOptionGroup() { * set parameters. */ public static void setParam(final String[] args, final String confFileName) { + Config config = Configuration.getByFileName(PARAMETER.shellConfFileName, confFileName); + setParam(args, config); + } + + /** + * set parameters. + */ + public static void setParam(final String[] args, final Config config) { + JCommander.newBuilder().addObject(PARAMETER).build().parse(args); if (PARAMETER.version) { printVersion(); exit(0); } - Config config = Configuration.getByFileName(PARAMETER.shellConfFileName, confFileName); - if (config.hasPath(Constant.NET_TYPE) && Constant.TESTNET.equalsIgnoreCase(config.getString(Constant.NET_TYPE))) { Wallet.setAddressPreFixByte(Constant.ADD_PRE_FIX_BYTE_TESTNET); @@ -448,6 +459,18 @@ public static void setParam(final String[] args, final String confFileName) { PARAMETER.lruCacheSize = config.getInt(Constant.VM_LRU_CACHE_SIZE); } + if (config.hasPath(Constant.NODE_RPC_ENABLE)) { + PARAMETER.rpcEnable = config.getBoolean(Constant.NODE_RPC_ENABLE); + } + + if (config.hasPath(Constant.NODE_RPC_SOLIDITY_ENABLE)) { + PARAMETER.rpcSolidityEnable = config.getBoolean(Constant.NODE_RPC_SOLIDITY_ENABLE); + } + + if (config.hasPath(Constant.NODE_RPC_PBFT_ENABLE)) { + PARAMETER.rpcPBFTEnable = config.getBoolean(Constant.NODE_RPC_PBFT_ENABLE); + } + if (config.hasPath(Constant.NODE_HTTP_FULLNODE_ENABLE)) { PARAMETER.fullNodeHttpEnable = config.getBoolean(Constant.NODE_HTTP_FULLNODE_ENABLE); } @@ -456,6 +479,10 @@ public static void setParam(final String[] args, final String confFileName) { PARAMETER.solidityNodeHttpEnable = config.getBoolean(Constant.NODE_HTTP_SOLIDITY_ENABLE); } + if (config.hasPath(Constant.NODE_HTTP_PBFT_ENABLE)) { + PARAMETER.pBFTHttpEnable = config.getBoolean(Constant.NODE_HTTP_PBFT_ENABLE); + } + if (config.hasPath(Constant.NODE_JSONRPC_HTTP_FULLNODE_ENABLE)) { PARAMETER.jsonRpcHttpFullNodeEnable = config.getBoolean(Constant.NODE_JSONRPC_HTTP_FULLNODE_ENABLE); diff --git a/framework/src/main/java/org/tron/core/services/RpcApiService.java b/framework/src/main/java/org/tron/core/services/RpcApiService.java index 85ad2dd18dd..8f9c6b15bb7 100755 --- a/framework/src/main/java/org/tron/core/services/RpcApiService.java +++ b/framework/src/main/java/org/tron/core/services/RpcApiService.java @@ -8,10 +8,8 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.netty.NettyServerBuilder; -import io.grpc.protobuf.services.ProtoReflectionService; import io.grpc.stub.StreamObserver; import java.util.Objects; -import java.util.concurrent.TimeUnit; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -75,7 +73,6 @@ import org.tron.api.WalletGrpc.WalletImplBase; import org.tron.api.WalletSolidityGrpc.WalletSolidityImplBase; import org.tron.common.application.RpcService; -import org.tron.common.es.ExecutorServiceManager; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.ByteArray; import org.tron.common.utils.Sha256Hash; @@ -97,9 +94,6 @@ import org.tron.core.exception.VMIllegalException; import org.tron.core.exception.ZksnarkException; import org.tron.core.metrics.MetricsApiService; -import org.tron.core.services.filter.LiteFnQueryGrpcInterceptor; -import org.tron.core.services.ratelimiter.RateLimiterInterceptor; -import org.tron.core.services.ratelimiter.RpcApiAccessInterceptor; import org.tron.core.utils.TransactionUtil; import org.tron.core.zen.address.DiversifierT; import org.tron.core.zen.address.IncomingViewingKey; @@ -182,12 +176,6 @@ public class RpcApiService extends RpcService { @Autowired private NodeInfoService nodeInfoService; @Autowired - private RateLimiterInterceptor rateLimiterInterceptor; - @Autowired - private LiteFnQueryGrpcInterceptor liteFnQueryGrpcInterceptor; - @Autowired - private RpcApiAccessInterceptor apiAccessInterceptor; - @Autowired private MetricsApiService metricsApiService; @Getter private DatabaseApi databaseApi = new DatabaseApi(); @@ -197,70 +185,27 @@ public class RpcApiService extends RpcService { @Getter private MonitorApi monitorApi = new MonitorApi(); - private final String executorName = "rpc-full-executor"; - - @Override - public void init() { - - } - - @Override - public void init(CommonParameter args) { + public RpcApiService() { port = Args.getInstance().getRpcPort(); + enable = Args.getInstance().isRpcEnable(); + executorName = "rpc-full-executor"; } @Override - public void start() { - try { - NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port).addService(databaseApi); - CommonParameter parameter = Args.getInstance(); - - if (parameter.getRpcThreadNum() > 0) { - serverBuilder = serverBuilder - .executor(ExecutorServiceManager.newFixedThreadPool( - executorName, parameter.getRpcThreadNum())); - } - - if (parameter.isSolidityNode()) { - serverBuilder = serverBuilder.addService(walletSolidityApi); - if (parameter.isWalletExtensionApi()) { - serverBuilder = serverBuilder.addService(new WalletExtensionApi()); - } - } else { - serverBuilder = serverBuilder.addService(walletApi); - } - - if (parameter.isNodeMetricsEnable()) { - serverBuilder = serverBuilder.addService(monitorApi); - } - - // Set configs from config.conf or default value - serverBuilder - .maxConcurrentCallsPerConnection(parameter.getMaxConcurrentCallsPerConnection()) - .flowControlWindow(parameter.getFlowControlWindow()) - .maxConnectionIdle(parameter.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS) - .maxConnectionAge(parameter.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS) - .maxInboundMessageSize(parameter.getMaxMessageSize()) - .maxHeaderListSize(parameter.getMaxHeaderListSize()); - - // add a rate limiter interceptor - serverBuilder.intercept(rateLimiterInterceptor); - - // add api access interceptor - serverBuilder.intercept(apiAccessInterceptor); - - // add lite fullnode query interceptor - serverBuilder.intercept(liteFnQueryGrpcInterceptor); - - if (parameter.isRpcReflectionServiceEnable()) { - serverBuilder.addService(ProtoReflectionService.newInstance()); + protected void addService(NettyServerBuilder serverBuilder) { + serverBuilder.addService(databaseApi); + CommonParameter parameter = Args.getInstance(); + if (parameter.isSolidityNode()) { + serverBuilder.addService(walletSolidityApi); + if (parameter.isWalletExtensionApi()) { + serverBuilder.addService(new WalletExtensionApi()); } + } else { + serverBuilder.addService(walletApi); + } - apiServer = serverBuilder.build(); - rateLimiterInterceptor.init(apiServer); - super.start(); - } catch (Exception e) { - logger.debug(e.getMessage(), e); + if (parameter.isNodeMetricsEnable()) { + serverBuilder.addService(monitorApi); } } diff --git a/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java b/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java index 55e6e07b5ec..76785218096 100644 --- a/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java +++ b/framework/src/main/java/org/tron/core/services/http/FullNodeHttpApiService.java @@ -4,8 +4,6 @@ import javax.servlet.DispatcherType; import javax.servlet.Filter; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHandler; @@ -13,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; import org.tron.core.config.args.Args; import org.tron.core.services.filter.HttpApiAccessFilter; import org.tron.core.services.filter.HttpInterceptor; @@ -294,261 +291,247 @@ public class FullNodeHttpApiService extends HttpService { @Autowired private CancelAllUnfreezeV2Servlet cancelAllUnfreezeV2Servlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public FullNodeHttpApiService() { port = Args.getInstance().getFullNodeHttpPort(); + enable = isFullNode() && Args.getInstance().isFullNodeHttpEnable(); + contextPath = "/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); + protected void addServlet(ServletContextHandler context) { + context.addServlet(new ServletHolder(getAccountServlet), "/wallet/getaccount"); + context.addServlet(new ServletHolder(transferServlet), "/wallet/createtransaction"); + context.addServlet(new ServletHolder(broadcastServlet), "/wallet/broadcasttransaction"); + context.addServlet(new ServletHolder(updateAccountServlet), "/wallet/updateaccount"); + context.addServlet(new ServletHolder(voteWitnessAccountServlet), + "/wallet/votewitnessaccount"); + context.addServlet(new ServletHolder(createAssetIssueServlet), "/wallet/createassetissue"); + context.addServlet(new ServletHolder(updateWitnessServlet), "/wallet/updatewitness"); + context.addServlet(new ServletHolder(createAccountServlet), "/wallet/createaccount"); + context.addServlet(new ServletHolder(createWitnessServlet), "/wallet/createwitness"); + context.addServlet(new ServletHolder(transferAssetServlet), "/wallet/transferasset"); + context.addServlet(new ServletHolder(participateAssetIssueServlet), + "/wallet/participateassetissue"); + context.addServlet(new ServletHolder(freezeBalanceServlet), "/wallet/freezebalance"); + context.addServlet(new ServletHolder(unFreezeBalanceServlet), "/wallet/unfreezebalance"); + context.addServlet(new ServletHolder(unFreezeAssetServlet), "/wallet/unfreezeasset"); + context.addServlet(new ServletHolder(withdrawBalanceServlet), "/wallet/withdrawbalance"); + context.addServlet(new ServletHolder(updateAssetServlet), "/wallet/updateasset"); + context.addServlet(new ServletHolder(listNodesServlet), "/wallet/listnodes"); + context.addServlet( + new ServletHolder(getAssetIssueByAccountServlet), "/wallet/getassetissuebyaccount"); + context.addServlet(new ServletHolder(getAccountNetServlet), "/wallet/getaccountnet"); + context.addServlet(new ServletHolder(getAssetIssueByNameServlet), + "/wallet/getassetissuebyname"); + context.addServlet(new ServletHolder(getAssetIssueListByNameServlet), + "/wallet/getassetissuelistbyname"); + context.addServlet(new ServletHolder(getAssetIssueByIdServlet), "/wallet/getassetissuebyid"); + context.addServlet(new ServletHolder(getNowBlockServlet), "/wallet/getnowblock"); + context.addServlet(new ServletHolder(getBlockByNumServlet), "/wallet/getblockbynum"); + context.addServlet(new ServletHolder(getBlockByIdServlet), "/wallet/getblockbyid"); + context.addServlet(new ServletHolder(getBlockByLimitNextServlet), + "/wallet/getblockbylimitnext"); + context.addServlet(new ServletHolder(getBlockByLatestNumServlet), + "/wallet/getblockbylatestnum"); + context.addServlet(new ServletHolder(getTransactionByIdServlet), + "/wallet/gettransactionbyid"); + context.addServlet( + new ServletHolder(getTransactionInfoByIdServlet), "/wallet/gettransactioninfobyid"); + context.addServlet( + new ServletHolder(getTransactionReceiptByIdServlet), "/wallet/gettransactionreceiptbyid"); + context.addServlet( + new ServletHolder(getTransactionCountByBlockNumServlet), + "/wallet/gettransactioncountbyblocknum"); + context.addServlet(new ServletHolder(listWitnessesServlet), "/wallet/listwitnesses"); + context.addServlet(new ServletHolder(getAssetIssueListServlet), "/wallet/getassetissuelist"); + context.addServlet( + new ServletHolder(getPaginatedAssetIssueListServlet), + "/wallet/getpaginatedassetissuelist"); + context.addServlet( + new ServletHolder(getPaginatedProposalListServlet), "/wallet/getpaginatedproposallist"); + context.addServlet( + new ServletHolder(getPaginatedExchangeListServlet), "/wallet/getpaginatedexchangelist"); + context.addServlet(new ServletHolder(totalTransactionServlet), "/wallet/totaltransaction"); + context.addServlet( + new ServletHolder(getNextMaintenanceTimeServlet), "/wallet/getnextmaintenancetime"); + context.addServlet(new ServletHolder(validateAddressServlet), "/wallet/validateaddress"); + context.addServlet(new ServletHolder(deployContractServlet), "/wallet/deploycontract"); + context.addServlet(new ServletHolder(triggerSmartContractServlet), + "/wallet/triggersmartcontract"); + context.addServlet(new ServletHolder(triggerConstantContractServlet), + "/wallet/triggerconstantcontract"); + context.addServlet(new ServletHolder(estimateEnergyServlet), "/wallet/estimateenergy"); + context.addServlet(new ServletHolder(getContractServlet), "/wallet/getcontract"); + context.addServlet(new ServletHolder(getContractInfoServlet), "/wallet/getcontractinfo"); + context.addServlet(new ServletHolder(clearABIServlet), "/wallet/clearabi"); + context.addServlet(new ServletHolder(proposalCreateServlet), "/wallet/proposalcreate"); + context.addServlet(new ServletHolder(proposalApproveServlet), "/wallet/proposalapprove"); + context.addServlet(new ServletHolder(proposalDeleteServlet), "/wallet/proposaldelete"); + context.addServlet(new ServletHolder(listProposalsServlet), "/wallet/listproposals"); + context.addServlet(new ServletHolder(getProposalByIdServlet), "/wallet/getproposalbyid"); + context.addServlet(new ServletHolder(exchangeCreateServlet), "/wallet/exchangecreate"); + context.addServlet(new ServletHolder(exchangeInjectServlet), "/wallet/exchangeinject"); + context.addServlet(new ServletHolder(exchangeTransactionServlet), + "/wallet/exchangetransaction"); + context.addServlet(new ServletHolder(exchangeWithdrawServlet), "/wallet/exchangewithdraw"); + context.addServlet(new ServletHolder(getExchangeByIdServlet), "/wallet/getexchangebyid"); + context.addServlet(new ServletHolder(listExchangesServlet), "/wallet/listexchanges"); + context.addServlet(new ServletHolder(getChainParametersServlet), + "/wallet/getchainparameters"); + context.addServlet(new ServletHolder(getAccountResourceServlet), + "/wallet/getaccountresource"); + context.addServlet(new ServletHolder(getTransactionSignWeightServlet), + "/wallet/getsignweight"); + context.addServlet(new ServletHolder(getTransactionApprovedListServlet), + "/wallet/getapprovedlist"); + context.addServlet(new ServletHolder(accountPermissionUpdateServlet), + "/wallet/accountpermissionupdate"); + context.addServlet(new ServletHolder(getNodeInfoServlet), "/wallet/getnodeinfo"); + context.addServlet(new ServletHolder(updateSettingServlet), "/wallet/updatesetting"); + context.addServlet(new ServletHolder(updateEnergyLimitServlet), "/wallet/updateenergylimit"); + context.addServlet(new ServletHolder(getDelegatedResourceServlet), + "/wallet/getdelegatedresource"); + context.addServlet(new ServletHolder(getDelegatedResourceV2Servlet), + "/wallet/getdelegatedresourcev2"); + context.addServlet(new ServletHolder(getCanDelegatedMaxSizeServlet), + "/wallet/getcandelegatedmaxsize"); + context.addServlet(new ServletHolder(getAvailableUnfreezeCountServlet), + "/wallet/getavailableunfreezecount"); + context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountServlet), + "/wallet/getcanwithdrawunfreezeamount"); + context.addServlet( + new ServletHolder(getDelegatedResourceAccountIndexServlet), + "/wallet/getdelegatedresourceaccountindex"); + context.addServlet( + new ServletHolder(getDelegatedResourceAccountIndexV2Servlet), + "/wallet/getdelegatedresourceaccountindexv2"); + context.addServlet(new ServletHolder(setAccountServlet), "/wallet/setaccountid"); + context.addServlet(new ServletHolder(getAccountByIdServlet), "/wallet/getaccountbyid"); + context + .addServlet(new ServletHolder(getExpandedSpendingKeyServlet), + "/wallet/getexpandedspendingkey"); + context.addServlet(new ServletHolder(getAkFromAskServlet), "/wallet/getakfromask"); + context.addServlet(new ServletHolder(getNkFromNskServlet), "/wallet/getnkfromnsk"); + context.addServlet(new ServletHolder(getSpendingKeyServlet), "/wallet/getspendingkey"); + context + .addServlet(new ServletHolder(getNewShieldedAddressServlet), + "/wallet/getnewshieldedaddress"); + context.addServlet(new ServletHolder(getDiversifierServlet), "/wallet/getdiversifier"); + context.addServlet(new ServletHolder(getIncomingViewingKeyServlet), + "/wallet/getincomingviewingkey"); + context.addServlet(new ServletHolder(getZenPaymentAddressServlet), + "/wallet/getzenpaymentaddress"); + // context.addServlet(new ServletHolder(createShieldedTransactionServlet), + // "/wallet/createshieldedtransaction"); + // context.addServlet(new ServletHolder(createShieldedTransactionWithoutSpendAuthSigServlet), + // "/wallet/createshieldedtransactionwithoutspendauthsig"); + // context.addServlet(new ServletHolder(scanNoteByIvkServlet), "/wallet/scannotebyivk"); + // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkServlet), + // "/wallet/scanandmarknotebyivk"); + // context.addServlet(new ServletHolder(scanNoteByOvkServlet), "/wallet/scannotebyovk"); + context.addServlet(new ServletHolder(getRcmServlet), "/wallet/getrcm"); + // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoServlet), + // "/wallet/getmerkletreevoucherinfo"); + // context.addServlet(new ServletHolder(isSpendServlet), "/wallet/isspend"); + context.addServlet(new ServletHolder(createSpendAuthSigServlet), + "/wallet/createspendauthsig"); + // context.addServlet(new ServletHolder(createShieldNullifierServlet), + // "/wallet/createshieldnullifier"); + // context.addServlet(new ServletHolder(getShieldTransactionHashServlet), + // "/wallet/getshieldtransactionhash"); - context.addServlet(new ServletHolder(getAccountServlet), "/wallet/getaccount"); - context.addServlet(new ServletHolder(transferServlet), "/wallet/createtransaction"); - context.addServlet(new ServletHolder(broadcastServlet), "/wallet/broadcasttransaction"); - context.addServlet(new ServletHolder(updateAccountServlet), "/wallet/updateaccount"); - context.addServlet(new ServletHolder(voteWitnessAccountServlet), - "/wallet/votewitnessaccount"); - context.addServlet(new ServletHolder(createAssetIssueServlet), "/wallet/createassetissue"); - context.addServlet(new ServletHolder(updateWitnessServlet), "/wallet/updatewitness"); - context.addServlet(new ServletHolder(createAccountServlet), "/wallet/createaccount"); - context.addServlet(new ServletHolder(createWitnessServlet), "/wallet/createwitness"); - context.addServlet(new ServletHolder(transferAssetServlet), "/wallet/transferasset"); - context.addServlet(new ServletHolder(participateAssetIssueServlet), - "/wallet/participateassetissue"); - context.addServlet(new ServletHolder(freezeBalanceServlet), "/wallet/freezebalance"); - context.addServlet(new ServletHolder(unFreezeBalanceServlet), "/wallet/unfreezebalance"); - context.addServlet(new ServletHolder(unFreezeAssetServlet), "/wallet/unfreezeasset"); - context.addServlet(new ServletHolder(withdrawBalanceServlet), "/wallet/withdrawbalance"); - context.addServlet(new ServletHolder(updateAssetServlet), "/wallet/updateasset"); - context.addServlet(new ServletHolder(listNodesServlet), "/wallet/listnodes"); - context.addServlet( - new ServletHolder(getAssetIssueByAccountServlet), "/wallet/getassetissuebyaccount"); - context.addServlet(new ServletHolder(getAccountNetServlet), "/wallet/getaccountnet"); - context.addServlet(new ServletHolder(getAssetIssueByNameServlet), - "/wallet/getassetissuebyname"); - context.addServlet(new ServletHolder(getAssetIssueListByNameServlet), - "/wallet/getassetissuelistbyname"); - context.addServlet(new ServletHolder(getAssetIssueByIdServlet), "/wallet/getassetissuebyid"); - context.addServlet(new ServletHolder(getNowBlockServlet), "/wallet/getnowblock"); - context.addServlet(new ServletHolder(getBlockByNumServlet), "/wallet/getblockbynum"); - context.addServlet(new ServletHolder(getBlockByIdServlet), "/wallet/getblockbyid"); - context.addServlet(new ServletHolder(getBlockByLimitNextServlet), - "/wallet/getblockbylimitnext"); - context.addServlet(new ServletHolder(getBlockByLatestNumServlet), - "/wallet/getblockbylatestnum"); - context.addServlet(new ServletHolder(getTransactionByIdServlet), - "/wallet/gettransactionbyid"); - context.addServlet( - new ServletHolder(getTransactionInfoByIdServlet), "/wallet/gettransactioninfobyid"); - context.addServlet( - new ServletHolder(getTransactionReceiptByIdServlet), "/wallet/gettransactionreceiptbyid"); - context.addServlet( - new ServletHolder(getTransactionCountByBlockNumServlet), - "/wallet/gettransactioncountbyblocknum"); - context.addServlet(new ServletHolder(listWitnessesServlet), "/wallet/listwitnesses"); - context.addServlet(new ServletHolder(getAssetIssueListServlet), "/wallet/getassetissuelist"); - context.addServlet( - new ServletHolder(getPaginatedAssetIssueListServlet), - "/wallet/getpaginatedassetissuelist"); - context.addServlet( - new ServletHolder(getPaginatedProposalListServlet), "/wallet/getpaginatedproposallist"); - context.addServlet( - new ServletHolder(getPaginatedExchangeListServlet), "/wallet/getpaginatedexchangelist"); - context.addServlet(new ServletHolder(totalTransactionServlet), "/wallet/totaltransaction"); - context.addServlet( - new ServletHolder(getNextMaintenanceTimeServlet), "/wallet/getnextmaintenancetime"); - context.addServlet(new ServletHolder(validateAddressServlet), "/wallet/validateaddress"); - context.addServlet(new ServletHolder(deployContractServlet), "/wallet/deploycontract"); - context.addServlet(new ServletHolder(triggerSmartContractServlet), - "/wallet/triggersmartcontract"); - context.addServlet(new ServletHolder(triggerConstantContractServlet), - "/wallet/triggerconstantcontract"); - context.addServlet(new ServletHolder(estimateEnergyServlet), "/wallet/estimateenergy"); - context.addServlet(new ServletHolder(getContractServlet), "/wallet/getcontract"); - context.addServlet(new ServletHolder(getContractInfoServlet), "/wallet/getcontractinfo"); - context.addServlet(new ServletHolder(clearABIServlet), "/wallet/clearabi"); - context.addServlet(new ServletHolder(proposalCreateServlet), "/wallet/proposalcreate"); - context.addServlet(new ServletHolder(proposalApproveServlet), "/wallet/proposalapprove"); - context.addServlet(new ServletHolder(proposalDeleteServlet), "/wallet/proposaldelete"); - context.addServlet(new ServletHolder(listProposalsServlet), "/wallet/listproposals"); - context.addServlet(new ServletHolder(getProposalByIdServlet), "/wallet/getproposalbyid"); - context.addServlet(new ServletHolder(exchangeCreateServlet), "/wallet/exchangecreate"); - context.addServlet(new ServletHolder(exchangeInjectServlet), "/wallet/exchangeinject"); - context.addServlet(new ServletHolder(exchangeTransactionServlet), - "/wallet/exchangetransaction"); - context.addServlet(new ServletHolder(exchangeWithdrawServlet), "/wallet/exchangewithdraw"); - context.addServlet(new ServletHolder(getExchangeByIdServlet), "/wallet/getexchangebyid"); - context.addServlet(new ServletHolder(listExchangesServlet), "/wallet/listexchanges"); - context.addServlet(new ServletHolder(getChainParametersServlet), - "/wallet/getchainparameters"); - context.addServlet(new ServletHolder(getAccountResourceServlet), - "/wallet/getaccountresource"); - context.addServlet(new ServletHolder(getTransactionSignWeightServlet), - "/wallet/getsignweight"); - context.addServlet(new ServletHolder(getTransactionApprovedListServlet), - "/wallet/getapprovedlist"); - context.addServlet(new ServletHolder(accountPermissionUpdateServlet), - "/wallet/accountpermissionupdate"); - context.addServlet(new ServletHolder(getNodeInfoServlet), "/wallet/getnodeinfo"); - context.addServlet(new ServletHolder(updateSettingServlet), "/wallet/updatesetting"); - context.addServlet(new ServletHolder(updateEnergyLimitServlet), "/wallet/updateenergylimit"); - context.addServlet(new ServletHolder(getDelegatedResourceServlet), - "/wallet/getdelegatedresource"); - context.addServlet(new ServletHolder(getDelegatedResourceV2Servlet), - "/wallet/getdelegatedresourcev2"); - context.addServlet(new ServletHolder(getCanDelegatedMaxSizeServlet), - "/wallet/getcandelegatedmaxsize"); - context.addServlet(new ServletHolder(getAvailableUnfreezeCountServlet), - "/wallet/getavailableunfreezecount"); - context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountServlet), - "/wallet/getcanwithdrawunfreezeamount"); - context.addServlet( - new ServletHolder(getDelegatedResourceAccountIndexServlet), - "/wallet/getdelegatedresourceaccountindex"); - context.addServlet( - new ServletHolder(getDelegatedResourceAccountIndexV2Servlet), - "/wallet/getdelegatedresourceaccountindexv2"); - context.addServlet(new ServletHolder(setAccountServlet), "/wallet/setaccountid"); - context.addServlet(new ServletHolder(getAccountByIdServlet), "/wallet/getaccountbyid"); - context - .addServlet(new ServletHolder(getExpandedSpendingKeyServlet), - "/wallet/getexpandedspendingkey"); - context.addServlet(new ServletHolder(getAkFromAskServlet), "/wallet/getakfromask"); - context.addServlet(new ServletHolder(getNkFromNskServlet), "/wallet/getnkfromnsk"); - context.addServlet(new ServletHolder(getSpendingKeyServlet), "/wallet/getspendingkey"); - context - .addServlet(new ServletHolder(getNewShieldedAddressServlet), - "/wallet/getnewshieldedaddress"); - context.addServlet(new ServletHolder(getDiversifierServlet), "/wallet/getdiversifier"); - context.addServlet(new ServletHolder(getIncomingViewingKeyServlet), - "/wallet/getincomingviewingkey"); - context.addServlet(new ServletHolder(getZenPaymentAddressServlet), - "/wallet/getzenpaymentaddress"); - // context.addServlet(new ServletHolder(createShieldedTransactionServlet), - // "/wallet/createshieldedtransaction"); - // context.addServlet(new ServletHolder(createShieldedTransactionWithoutSpendAuthSigServlet), - // "/wallet/createshieldedtransactionwithoutspendauthsig"); - // context.addServlet(new ServletHolder(scanNoteByIvkServlet), "/wallet/scannotebyivk"); - // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkServlet), - // "/wallet/scanandmarknotebyivk"); - // context.addServlet(new ServletHolder(scanNoteByOvkServlet), "/wallet/scannotebyovk"); - context.addServlet(new ServletHolder(getRcmServlet), "/wallet/getrcm"); - // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoServlet), - // "/wallet/getmerkletreevoucherinfo"); - // context.addServlet(new ServletHolder(isSpendServlet), "/wallet/isspend"); - context.addServlet(new ServletHolder(createSpendAuthSigServlet), - "/wallet/createspendauthsig"); - // context.addServlet(new ServletHolder(createShieldNullifierServlet), - // "/wallet/createshieldnullifier"); - // context.addServlet(new ServletHolder(getShieldTransactionHashServlet), - // "/wallet/getshieldtransactionhash"); + context + .addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentServlet), + "/wallet/isshieldedtrc20contractnotespent"); + context.addServlet(new ServletHolder(createShieldedContractParametersServlet), + "/wallet/createshieldedcontractparameters"); + context.addServlet(new ServletHolder(createShieldedContractParametersWithoutAskServlet), + "/wallet/createshieldedcontractparameterswithoutask"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkServlet), + "/wallet/scanshieldedtrc20notesbyivk"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkServlet), + "/wallet/scanshieldedtrc20notesbyovk"); + context.addServlet(new ServletHolder(getTriggerInputForShieldedTRC20ContractServlet), + "/wallet/gettriggerinputforshieldedtrc20contract"); - context - .addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentServlet), - "/wallet/isshieldedtrc20contractnotespent"); - context.addServlet(new ServletHolder(createShieldedContractParametersServlet), - "/wallet/createshieldedcontractparameters"); - context.addServlet(new ServletHolder(createShieldedContractParametersWithoutAskServlet), - "/wallet/createshieldedcontractparameterswithoutask"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkServlet), - "/wallet/scanshieldedtrc20notesbyivk"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkServlet), - "/wallet/scanshieldedtrc20notesbyovk"); - context.addServlet(new ServletHolder(getTriggerInputForShieldedTRC20ContractServlet), - "/wallet/gettriggerinputforshieldedtrc20contract"); + context.addServlet(new ServletHolder(broadcastHexServlet), "/wallet/broadcasthex"); + context.addServlet(new ServletHolder(getBrokerageServlet), "/wallet/getBrokerage"); + context.addServlet(new ServletHolder(getRewardServlet), "/wallet/getReward"); + context.addServlet(new ServletHolder(updateBrokerageServlet), "/wallet/updateBrokerage"); + context.addServlet(new ServletHolder(createCommonTransactionServlet), + "/wallet/createCommonTransaction"); + context.addServlet(new ServletHolder(getTransactionInfoByBlockNumServlet), + "/wallet/gettransactioninfobyblocknum"); + context.addServlet(new ServletHolder(listNodesServlet), "/net/listnodes"); - context.addServlet(new ServletHolder(broadcastHexServlet), "/wallet/broadcasthex"); - context.addServlet(new ServletHolder(getBrokerageServlet), "/wallet/getBrokerage"); - context.addServlet(new ServletHolder(getRewardServlet), "/wallet/getReward"); - context.addServlet(new ServletHolder(updateBrokerageServlet), "/wallet/updateBrokerage"); - context.addServlet(new ServletHolder(createCommonTransactionServlet), - "/wallet/createCommonTransaction"); - context.addServlet(new ServletHolder(getTransactionInfoByBlockNumServlet), - "/wallet/gettransactioninfobyblocknum"); - context.addServlet(new ServletHolder(listNodesServlet), "/net/listnodes"); + context.addServlet(new ServletHolder(metricsServlet), "/monitor/getstatsinfo"); + context.addServlet(new ServletHolder(getNodeInfoServlet), "/monitor/getnodeinfo"); + context.addServlet(new ServletHolder(marketSellAssetServlet), "/wallet/marketsellasset"); + context.addServlet(new ServletHolder(marketCancelOrderServlet), "/wallet/marketcancelorder"); + context.addServlet(new ServletHolder(getMarketOrderByAccountServlet), + "/wallet/getmarketorderbyaccount"); + context.addServlet(new ServletHolder(getMarketOrderByIdServlet), + "/wallet/getmarketorderbyid"); + context.addServlet(new ServletHolder(getMarketPriceByPairServlet), + "/wallet/getmarketpricebypair"); + context.addServlet(new ServletHolder(getMarketOrderListByPairServlet), + "/wallet/getmarketorderlistbypair"); + context.addServlet(new ServletHolder(getMarketPairListServlet), + "/wallet/getmarketpairlist"); - context.addServlet(new ServletHolder(metricsServlet), "/monitor/getstatsinfo"); - context.addServlet(new ServletHolder(getNodeInfoServlet), "/monitor/getnodeinfo"); - context.addServlet(new ServletHolder(marketSellAssetServlet), "/wallet/marketsellasset"); - context.addServlet(new ServletHolder(marketCancelOrderServlet), "/wallet/marketcancelorder"); - context.addServlet(new ServletHolder(getMarketOrderByAccountServlet), - "/wallet/getmarketorderbyaccount"); - context.addServlet(new ServletHolder(getMarketOrderByIdServlet), - "/wallet/getmarketorderbyid"); - context.addServlet(new ServletHolder(getMarketPriceByPairServlet), - "/wallet/getmarketpricebypair"); - context.addServlet(new ServletHolder(getMarketOrderListByPairServlet), - "/wallet/getmarketorderlistbypair"); - context.addServlet(new ServletHolder(getMarketPairListServlet), - "/wallet/getmarketpairlist"); + context.addServlet(new ServletHolder(getAccountBalanceServlet), + "/wallet/getaccountbalance"); + context.addServlet(new ServletHolder(getBlockBalanceServlet), + "/wallet/getblockbalance"); + context.addServlet(new ServletHolder(getBurnTrxServlet), "/wallet/getburntrx"); + context.addServlet(new ServletHolder(getTransactionFromPendingServlet), + "/wallet/gettransactionfrompending"); + context.addServlet(new ServletHolder(getTransactionListFromPendingServlet), + "/wallet/gettransactionlistfrompending"); + context.addServlet(new ServletHolder(getPendingSizeServlet), "/wallet/getpendingsize"); + context.addServlet(new ServletHolder(getEnergyPricesServlet), "/wallet/getenergyprices"); + context.addServlet(new ServletHolder(getBandwidthPricesServlet), + "/wallet/getbandwidthprices"); + context.addServlet(new ServletHolder(getBlockServlet), "/wallet/getblock"); + context.addServlet(new ServletHolder(getMemoFeePricesServlet), "/wallet/getmemofee"); - context.addServlet(new ServletHolder(getAccountBalanceServlet), - "/wallet/getaccountbalance"); - context.addServlet(new ServletHolder(getBlockBalanceServlet), - "/wallet/getblockbalance"); - context.addServlet(new ServletHolder(getBurnTrxServlet), "/wallet/getburntrx"); - context.addServlet(new ServletHolder(getTransactionFromPendingServlet), - "/wallet/gettransactionfrompending"); - context.addServlet(new ServletHolder(getTransactionListFromPendingServlet), - "/wallet/gettransactionlistfrompending"); - context.addServlet(new ServletHolder(getPendingSizeServlet), "/wallet/getpendingsize"); - context.addServlet(new ServletHolder(getEnergyPricesServlet), "/wallet/getenergyprices"); - context.addServlet(new ServletHolder(getBandwidthPricesServlet), - "/wallet/getbandwidthprices"); - context.addServlet(new ServletHolder(getBlockServlet), "/wallet/getblock"); - context.addServlet(new ServletHolder(getMemoFeePricesServlet), "/wallet/getmemofee"); + context.addServlet(new ServletHolder(freezeBalanceV2Servlet), + "/wallet/freezebalancev2"); + context.addServlet(new ServletHolder(unFreezeBalanceV2Servlet), + "/wallet/unfreezebalancev2"); + context.addServlet(new ServletHolder(withdrawExpireUnfreezeServlet), + "/wallet/withdrawexpireunfreeze"); + context.addServlet(new ServletHolder(delegateResourceServlet), + "/wallet/delegateresource"); + context.addServlet(new ServletHolder(unDelegateResourceServlet), + "/wallet/undelegateresource"); + context.addServlet(new ServletHolder(cancelAllUnfreezeV2Servlet), + "/wallet/cancelallunfreezev2"); - context.addServlet(new ServletHolder(freezeBalanceV2Servlet), - "/wallet/freezebalancev2"); - context.addServlet(new ServletHolder(unFreezeBalanceV2Servlet), - "/wallet/unfreezebalancev2"); - context.addServlet(new ServletHolder(withdrawExpireUnfreezeServlet), - "/wallet/withdrawexpireunfreeze"); - context.addServlet(new ServletHolder(delegateResourceServlet), - "/wallet/delegateresource"); - context.addServlet(new ServletHolder(unDelegateResourceServlet), - "/wallet/undelegateresource"); - context.addServlet(new ServletHolder(cancelAllUnfreezeV2Servlet), - "/wallet/cancelallunfreezev2"); - - int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } + } - // filters the specified APIs - // when node is lite fullnode and openHistoryQueryWhenLiteFN is false - context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", - EnumSet.allOf(DispatcherType.class)); + @Override + protected void addFilter(ServletContextHandler context) { + // filters the specified APIs + // when node is lite fullnode and openHistoryQueryWhenLiteFN is false + context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", + EnumSet.allOf(DispatcherType.class)); - // http access filter, it should have higher priority than HttpInterceptor - context.addFilter(new FilterHolder(httpApiAccessFilter), "/*", - EnumSet.allOf(DispatcherType.class)); - // note: if the pathSpec of servlet is not started with wallet, it should be included here - context.getServletHandler().getFilterMappings()[1] - .setPathSpecs(new String[] {"/wallet/*", - "/net/listnodes", - "/monitor/getstatsinfo", - "/monitor/getnodeinfo"}); + // http access filter, it should have higher priority than HttpInterceptor + context.addFilter(new FilterHolder(httpApiAccessFilter), "/*", + EnumSet.allOf(DispatcherType.class)); + // note: if the pathSpec of servlet is not started with wallet, it should be included here + context.getServletHandler().getFilterMappings()[1] + .setPathSpecs(new String[] {"/wallet/*", + "/net/listnodes", + "/monitor/getstatsinfo", + "/monitor/getnodeinfo"}); - // metrics filter - ServletHandler handler = new ServletHandler(); - FilterHolder fh = handler - .addFilterWithMapping((Class) HttpInterceptor.class, "/*", - EnumSet.of(DispatcherType.REQUEST)); - context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST)); - super.start(); - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + // metrics filter + ServletHandler handler = new ServletHandler(); + FilterHolder fh = handler + .addFilterWithMapping((Class) HttpInterceptor.class, "/*", + EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST)); } } diff --git a/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java b/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java index 0c66b220e09..ea08d2d42cf 100644 --- a/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java +++ b/framework/src/main/java/org/tron/core/services/http/solidity/SolidityNodeHttpApiService.java @@ -3,15 +3,12 @@ import java.util.EnumSet; import javax.servlet.DispatcherType; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; import org.tron.core.config.args.Args; import org.tron.core.services.filter.HttpApiAccessFilter; import org.tron.core.services.http.EstimateEnergyServlet; @@ -166,140 +163,123 @@ public class SolidityNodeHttpApiService extends HttpService { private GetEnergyPricesServlet getEnergyPricesServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public SolidityNodeHttpApiService() { port = Args.getInstance().getSolidityHttpPort(); + enable = !isFullNode() && Args.getInstance().isSolidityNodeHttpEnable(); + contextPath = "/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); - - // same as FullNode - context.addServlet(new ServletHolder(getAccountServlet), "/walletsolidity/getaccount"); - context.addServlet(new ServletHolder(listWitnessesServlet), "/walletsolidity/listwitnesses"); - context.addServlet(new ServletHolder(getAssetIssueListServlet), - "/walletsolidity/getassetissuelist"); - context.addServlet(new ServletHolder(getPaginatedAssetIssueListServlet), - "/walletsolidity/getpaginatedassetissuelist"); - context.addServlet(new ServletHolder(getAssetIssueByNameServlet), - "/walletsolidity/getassetissuebyname"); - context.addServlet(new ServletHolder(getAssetIssueByIdServlet), - "/walletsolidity/getassetissuebyid"); - context.addServlet(new ServletHolder(getAssetIssueListByNameServlet), - "/walletsolidity/getassetissuelistbyname"); - context.addServlet(new ServletHolder(getNowBlockServlet), "/walletsolidity/getnowblock"); - context.addServlet(new ServletHolder(getBlockByNumServlet), "/walletsolidity/getblockbynum"); - context.addServlet(new ServletHolder(getDelegatedResourceServlet), - "/walletsolidity/getdelegatedresource"); - context.addServlet(new ServletHolder(getDelegatedResourceV2Servlet), - "/walletsolidity/getdelegatedresourcev2"); - context.addServlet(new ServletHolder(getCanDelegatedMaxSizeServlet), - "/walletsolidity/getcandelegatedmaxsize"); - context.addServlet(new ServletHolder(getAvailableUnfreezeCountServlet), - "/walletsolidity/getavailableunfreezecount"); - context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountServlet), - "/walletsolidity/getcanwithdrawunfreezeamount"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexServlet), - "/walletsolidity/getdelegatedresourceaccountindex"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2Servlet), - "/walletsolidity/getdelegatedresourceaccountindexv2"); - context - .addServlet(new ServletHolder(getExchangeByIdServlet), - "/walletsolidity/getexchangebyid"); - context.addServlet(new ServletHolder(listExchangesServlet), - "/walletsolidity/listexchanges"); + protected void addServlet(ServletContextHandler context) { + // same as FullNode + context.addServlet(new ServletHolder(getAccountServlet), "/walletsolidity/getaccount"); + context.addServlet(new ServletHolder(listWitnessesServlet), "/walletsolidity/listwitnesses"); + context.addServlet(new ServletHolder(getAssetIssueListServlet), + "/walletsolidity/getassetissuelist"); + context.addServlet(new ServletHolder(getPaginatedAssetIssueListServlet), + "/walletsolidity/getpaginatedassetissuelist"); + context.addServlet(new ServletHolder(getAssetIssueByNameServlet), + "/walletsolidity/getassetissuebyname"); + context.addServlet(new ServletHolder(getAssetIssueByIdServlet), + "/walletsolidity/getassetissuebyid"); + context.addServlet(new ServletHolder(getAssetIssueListByNameServlet), + "/walletsolidity/getassetissuelistbyname"); + context.addServlet(new ServletHolder(getNowBlockServlet), "/walletsolidity/getnowblock"); + context.addServlet(new ServletHolder(getBlockByNumServlet), "/walletsolidity/getblockbynum"); + context.addServlet(new ServletHolder(getDelegatedResourceServlet), + "/walletsolidity/getdelegatedresource"); + context.addServlet(new ServletHolder(getDelegatedResourceV2Servlet), + "/walletsolidity/getdelegatedresourcev2"); + context.addServlet(new ServletHolder(getCanDelegatedMaxSizeServlet), + "/walletsolidity/getcandelegatedmaxsize"); + context.addServlet(new ServletHolder(getAvailableUnfreezeCountServlet), + "/walletsolidity/getavailableunfreezecount"); + context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountServlet), + "/walletsolidity/getcanwithdrawunfreezeamount"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexServlet), + "/walletsolidity/getdelegatedresourceaccountindex"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2Servlet), + "/walletsolidity/getdelegatedresourceaccountindexv2"); + context + .addServlet(new ServletHolder(getExchangeByIdServlet), + "/walletsolidity/getexchangebyid"); + context.addServlet(new ServletHolder(listExchangesServlet), + "/walletsolidity/listexchanges"); - context.addServlet(new ServletHolder(getAccountByIdServlet), - "/walletsolidity/getaccountbyid"); - context.addServlet(new ServletHolder(getBlockByIdServlet), - "/walletsolidity/getblockbyid"); - context.addServlet(new ServletHolder(getBlockByLimitNextServlet), - "/walletsolidity/getblockbylimitnext"); - context.addServlet(new ServletHolder(getBlockByLatestNumServlet), - "/walletsolidity/getblockbylatestnum"); + context.addServlet(new ServletHolder(getAccountByIdServlet), + "/walletsolidity/getaccountbyid"); + context.addServlet(new ServletHolder(getBlockByIdServlet), + "/walletsolidity/getblockbyid"); + context.addServlet(new ServletHolder(getBlockByLimitNextServlet), + "/walletsolidity/getblockbylimitnext"); + context.addServlet(new ServletHolder(getBlockByLatestNumServlet), + "/walletsolidity/getblockbylatestnum"); - // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoServlet), - // "/walletsolidity/getmerkletreevoucherinfo"); - // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkServlet), - // "/walletsolidity/scanandmarknotebyivk"); - // context.addServlet(new ServletHolder(scanNoteByIvkServlet), - // "/walletsolidity/scannotebyivk"); - // context.addServlet(new ServletHolder(scanNoteByOvkServlet), - // "/walletsolidity/scannotebyovk"); - // context.addServlet(new ServletHolder(isSpendServlet), - // "/walletsolidity/isspend"); + // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoServlet), + // "/walletsolidity/getmerkletreevoucherinfo"); + // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkServlet), + // "/walletsolidity/scanandmarknotebyivk"); + // context.addServlet(new ServletHolder(scanNoteByIvkServlet), + // "/walletsolidity/scannotebyivk"); + // context.addServlet(new ServletHolder(scanNoteByOvkServlet), + // "/walletsolidity/scannotebyovk"); + // context.addServlet(new ServletHolder(isSpendServlet), + // "/walletsolidity/isspend"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkServlet), + "/walletsolidity/scanshieldedtrc20notesbyivk"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkServlet), + "/walletsolidity/scanshieldedtrc20notesbyovk"); + context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentServlet), + "/walletsolidity/isshieldedtrc20contractnotespent"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkServlet), - "/walletsolidity/scanshieldedtrc20notesbyivk"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkServlet), - "/walletsolidity/scanshieldedtrc20notesbyovk"); - context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentServlet), - "/walletsolidity/isshieldedtrc20contractnotespent"); + context.addServlet(new ServletHolder(getTransactionInfoByBlockNumServlet), + "/walletsolidity/gettransactioninfobyblocknum"); - context.addServlet(new ServletHolder(getTransactionInfoByBlockNumServlet), - "/walletsolidity/gettransactioninfobyblocknum"); + context.addServlet(new ServletHolder(getMarketOrderByAccountServlet), + "/walletsolidity/getmarketorderbyaccount"); + context.addServlet(new ServletHolder(getMarketOrderByIdServlet), + "/walletsolidity/getmarketorderbyid"); + context.addServlet(new ServletHolder(getMarketPriceByPairServlet), + "/walletsolidity/getmarketpricebypair"); + context.addServlet(new ServletHolder(getMarketOrderListByPairServlet), + "/walletsolidity/getmarketorderlistbypair"); + context.addServlet(new ServletHolder(getMarketPairListServlet), + "/walletsolidity/getmarketpairlist"); - context.addServlet(new ServletHolder(getMarketOrderByAccountServlet), - "/walletsolidity/getmarketorderbyaccount"); - context.addServlet(new ServletHolder(getMarketOrderByIdServlet), - "/walletsolidity/getmarketorderbyid"); - context.addServlet(new ServletHolder(getMarketPriceByPairServlet), - "/walletsolidity/getmarketpricebypair"); - context.addServlet(new ServletHolder(getMarketOrderListByPairServlet), - "/walletsolidity/getmarketorderlistbypair"); - context.addServlet(new ServletHolder(getMarketPairListServlet), - "/walletsolidity/getmarketpairlist"); + // only for SolidityNode + context.addServlet(new ServletHolder(getTransactionByIdServlet), + "/walletsolidity/gettransactionbyid"); - // only for SolidityNode - context.addServlet(new ServletHolder(getTransactionByIdServlet), - "/walletsolidity/gettransactionbyid"); + context + .addServlet(new ServletHolder(getTransactionInfoByIdServlet), + "/walletsolidity/gettransactioninfobyid"); + context + .addServlet(new ServletHolder(getTransactionCountByBlockNumServlet), + "/walletsolidity/gettransactioncountbyblocknum"); + context.addServlet(new ServletHolder(triggerConstantContractServlet), + "/walletsolidity/triggerconstantcontract"); + context.addServlet(new ServletHolder(estimateEnergyServlet), + "/walletsolidity/estimateenergy"); - context - .addServlet(new ServletHolder(getTransactionInfoByIdServlet), - "/walletsolidity/gettransactioninfobyid"); - context - .addServlet(new ServletHolder(getTransactionCountByBlockNumServlet), - "/walletsolidity/gettransactioncountbyblocknum"); - context.addServlet(new ServletHolder(triggerConstantContractServlet), - "/walletsolidity/triggerconstantcontract"); - context.addServlet(new ServletHolder(estimateEnergyServlet), - "/walletsolidity/estimateenergy"); - - context.addServlet(new ServletHolder(getNodeInfoServlet), "/wallet/getnodeinfo"); - context.addServlet(new ServletHolder(getNodeInfoServlet), "/walletsolidity/getnodeinfo"); - context.addServlet(new ServletHolder(getBrokerageServlet), "/walletsolidity/getBrokerage"); - context.addServlet(new ServletHolder(getRewardServlet), "/walletsolidity/getReward"); - context.addServlet(new ServletHolder(getBurnTrxServlet), "/walletsolidity/getburntrx"); - context.addServlet(new ServletHolder(getBlockServlet), "/walletsolidity/getblock"); - context.addServlet(new ServletHolder(getBandwidthPricesServlet), - "/walletsolidity/getbandwidthprices"); - context.addServlet(new ServletHolder(getEnergyPricesServlet), - "/walletsolidity/getenergyprices"); - - // http access filter - context.addFilter(new FilterHolder(httpApiAccessFilter), "/walletsolidity/*", - EnumSet.allOf(DispatcherType.class)); - context.getServletHandler().getFilterMappings()[0] - .setPathSpecs(new String[] {"/walletsolidity/*", - "/wallet/getnodeinfo"}); - - int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } + context.addServlet(new ServletHolder(getNodeInfoServlet), "/wallet/getnodeinfo"); + context.addServlet(new ServletHolder(getNodeInfoServlet), "/walletsolidity/getnodeinfo"); + context.addServlet(new ServletHolder(getBrokerageServlet), "/walletsolidity/getBrokerage"); + context.addServlet(new ServletHolder(getRewardServlet), "/walletsolidity/getReward"); + context.addServlet(new ServletHolder(getBurnTrxServlet), "/walletsolidity/getburntrx"); + context.addServlet(new ServletHolder(getBlockServlet), "/walletsolidity/getblock"); + context.addServlet(new ServletHolder(getBandwidthPricesServlet), + "/walletsolidity/getbandwidthprices"); + context.addServlet(new ServletHolder(getEnergyPricesServlet), + "/walletsolidity/getenergyprices"); + } - super.start(); - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + @Override + protected void addFilter(ServletContextHandler context) { + // http access filter + context.addFilter(new FilterHolder(httpApiAccessFilter), "/walletsolidity/*", + EnumSet.allOf(DispatcherType.class)); + context.getServletHandler().getFilterMappings()[0] + .setPathSpecs(new String[] {"/walletsolidity/*", + "/wallet/getnodeinfo"}); } } diff --git a/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnPBFT/JsonRpcServiceOnPBFT.java b/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnPBFT/JsonRpcServiceOnPBFT.java index 1893a46045a..fffaf8d4e7b 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnPBFT/JsonRpcServiceOnPBFT.java +++ b/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnPBFT/JsonRpcServiceOnPBFT.java @@ -1,14 +1,12 @@ package org.tron.core.services.interfaceJsonRpcOnPBFT; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; +import org.tron.core.config.args.Args; @Component @Slf4j(topic = "API") @@ -17,34 +15,14 @@ public class JsonRpcServiceOnPBFT extends HttpService { @Autowired private JsonRpcOnPBFTServlet jsonRpcOnPBFTServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { - port = CommonParameter.getInstance().getJsonRpcHttpPBFTPort(); + public JsonRpcServiceOnPBFT() { + port = Args.getInstance().getJsonRpcHttpPBFTPort(); + enable = isFullNode() && Args.getInstance().isJsonRpcHttpPBFTNodeEnable(); + contextPath = "/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); - - context.addServlet(new ServletHolder(jsonRpcOnPBFTServlet), "/jsonrpc"); - - int maxHttpConnectNumber = CommonParameter.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } - - super.start(); - - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + public void addServlet(ServletContextHandler context) { + context.addServlet(new ServletHolder(jsonRpcOnPBFTServlet), "/jsonrpc"); } } diff --git a/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnSolidity/JsonRpcServiceOnSolidity.java b/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnSolidity/JsonRpcServiceOnSolidity.java index 52f5b761ae2..a6f7d5dd5e7 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnSolidity/JsonRpcServiceOnSolidity.java +++ b/framework/src/main/java/org/tron/core/services/interfaceJsonRpcOnSolidity/JsonRpcServiceOnSolidity.java @@ -1,14 +1,12 @@ package org.tron.core.services.interfaceJsonRpcOnSolidity; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; +import org.tron.core.config.args.Args; @Component @Slf4j(topic = "API") @@ -17,33 +15,14 @@ public class JsonRpcServiceOnSolidity extends HttpService { @Autowired private JsonRpcOnSolidityServlet jsonRpcOnSolidityServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { - port = CommonParameter.getInstance().getJsonRpcHttpSolidityPort(); + public JsonRpcServiceOnSolidity() { + port = Args.getInstance().getJsonRpcHttpSolidityPort(); + enable = isFullNode() && Args.getInstance().isJsonRpcHttpSolidityNodeEnable(); + contextPath = "/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); - - context.addServlet(new ServletHolder(jsonRpcOnSolidityServlet), "/jsonrpc"); - - int maxHttpConnectNumber = CommonParameter.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } - super.start(); - - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + public void addServlet(ServletContextHandler context) { + context.addServlet(new ServletHolder(jsonRpcOnSolidityServlet), "/jsonrpc"); } } diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/RpcApiServiceOnPBFT.java b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/RpcApiServiceOnPBFT.java index 4d801f20e5c..54e7b69f7fc 100755 --- a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/RpcApiServiceOnPBFT.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/RpcApiServiceOnPBFT.java @@ -1,9 +1,7 @@ package org.tron.core.services.interfaceOnPBFT; import io.grpc.netty.NettyServerBuilder; -import io.grpc.protobuf.services.ProtoReflectionService; import io.grpc.stub.StreamObserver; -import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.tron.api.DatabaseGrpc.DatabaseImplBase; @@ -36,13 +34,8 @@ import org.tron.api.GrpcAPI.WitnessList; import org.tron.api.WalletSolidityGrpc.WalletSolidityImplBase; import org.tron.common.application.RpcService; -import org.tron.common.es.ExecutorServiceManager; -import org.tron.common.parameter.CommonParameter; import org.tron.core.config.args.Args; import org.tron.core.services.RpcApiService; -import org.tron.core.services.filter.LiteFnQueryGrpcInterceptor; -import org.tron.core.services.ratelimiter.RateLimiterInterceptor; -import org.tron.core.services.ratelimiter.RpcApiAccessInterceptor; import org.tron.protos.Protocol.Account; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.DelegatedResourceAccountIndex; @@ -70,70 +63,16 @@ public class RpcApiServiceOnPBFT extends RpcService { @Autowired private RpcApiService rpcApiService; - @Autowired - private RateLimiterInterceptor rateLimiterInterceptor; - - @Autowired - private LiteFnQueryGrpcInterceptor liteFnQueryGrpcInterceptor; - - @Autowired - private RpcApiAccessInterceptor apiAccessInterceptor; - - private final String executorName = "rpc-pbft-executor"; - - @Override - public void init() { - } - - @Override - public void init(CommonParameter parameter) { + public RpcApiServiceOnPBFT() { port = Args.getInstance().getRpcOnPBFTPort(); + enable = isFullNode() && Args.getInstance().isRpcPBFTEnable(); + executorName = "rpc-pbft-executor"; } @Override - public void start() { - try { - NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port) - .addService(new DatabaseApi()); - - CommonParameter args = CommonParameter.getInstance(); - - if (args.getRpcThreadNum() > 0) { - serverBuilder = serverBuilder - .executor(ExecutorServiceManager.newFixedThreadPool( - executorName, args.getRpcThreadNum())); - } - - serverBuilder = serverBuilder.addService(new WalletPBFTApi()); - - // Set configs from config.conf or default value - serverBuilder - .maxConcurrentCallsPerConnection(args.getMaxConcurrentCallsPerConnection()) - .flowControlWindow(args.getFlowControlWindow()) - .maxConnectionIdle(args.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS) - .maxConnectionAge(args.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS) - .maxInboundMessageSize(args.getMaxMessageSize()) - .maxHeaderListSize(args.getMaxHeaderListSize()); - - // add a ratelimiter interceptor - serverBuilder.intercept(rateLimiterInterceptor); - - // add api access interceptor - serverBuilder.intercept(apiAccessInterceptor); - - // add lite fullnode query interceptor - serverBuilder.intercept(liteFnQueryGrpcInterceptor); - - if (args.isRpcReflectionServiceEnable()) { - serverBuilder.addService(ProtoReflectionService.newInstance()); - } - - apiServer = serverBuilder.build(); - rateLimiterInterceptor.init(apiServer); - super.start(); - } catch (Exception e) { - logger.debug(e.getMessage(), e); - } + protected void addService(NettyServerBuilder serverBuilder) { + serverBuilder.addService(new DatabaseApi()); + serverBuilder.addService(new WalletPBFTApi()); } /** diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java index 7a5fd0cbcde..828d36e664f 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnPBFT/http/PBFT/HttpApiOnPBFTService.java @@ -3,14 +3,11 @@ import java.util.EnumSet; import javax.servlet.DispatcherType; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.annotation.Autowired; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; import org.tron.core.config.args.Args; import org.tron.core.services.filter.HttpApiAccessFilter; import org.tron.core.services.filter.LiteFnQueryHttpFilter; @@ -172,126 +169,109 @@ public class HttpApiOnPBFTService extends HttpService { @Autowired private GetDelegatedResourceV2OnPBFTServlet getDelegatedResourceV2OnPBFTServlet; - @Override - public void init() { - - } - - @Override - public void init(CommonParameter parameter) { + public HttpApiOnPBFTService() { port = Args.getInstance().getPBFTHttpPort(); + enable = isFullNode() && Args.getInstance().isPBFTHttpEnable(); + contextPath = "/walletpbft/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/walletpbft/"); - apiServer.setHandler(context); + protected void addServlet(ServletContextHandler context) { + // same as FullNode + context.addServlet(new ServletHolder(accountOnPBFTServlet), "/getaccount"); + context.addServlet(new ServletHolder(listWitnessesOnPBFTServlet), "/listwitnesses"); + context.addServlet(new ServletHolder(getAssetIssueListOnPBFTServlet), "/getassetissuelist"); + context.addServlet(new ServletHolder(getPaginatedAssetIssueListOnPBFTServlet), + "/getpaginatedassetissuelist"); + context + .addServlet(new ServletHolder(getAssetIssueByNameOnPBFTServlet), "/getassetissuebyname"); + context.addServlet(new ServletHolder(getAssetIssueByIdOnPBFTServlet), "/getassetissuebyid"); + context.addServlet(new ServletHolder(getAssetIssueListByNameOnPBFTServlet), + "/getassetissuelistbyname"); + context.addServlet(new ServletHolder(getNowBlockOnPBFTServlet), "/getnowblock"); + context.addServlet(new ServletHolder(getBlockByNumOnPBFTServlet), "/getblockbynum"); + context.addServlet(new ServletHolder(getDelegatedResourceOnPBFTServlet), + "/getdelegatedresource"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexOnPBFTServlet), + "/getdelegatedresourceaccountindex"); + context.addServlet(new ServletHolder(getExchangeByIdOnPBFTServlet), "/getexchangebyid"); + context.addServlet(new ServletHolder(listExchangesOnPBFTServlet), "/listexchanges"); + context.addServlet(new ServletHolder(getAccountByIdOnPBFTServlet), "/getaccountbyid"); + context.addServlet(new ServletHolder(getBlockByIdOnPBFTServlet), "/getblockbyid"); + context + .addServlet(new ServletHolder(getBlockByLimitNextOnPBFTServlet), "/getblockbylimitnext"); + context + .addServlet(new ServletHolder(getBlockByLatestNumOnPBFTServlet), "/getblockbylatestnum"); + context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoOnPBFTServlet), + "/getmerkletreevoucherinfo"); + context.addServlet(new ServletHolder(scanAndMarkNoteByIvkOnPBFTServlet), + "/scanandmarknotebyivk"); + context.addServlet(new ServletHolder(scanNoteByIvkOnPBFTServlet), "/scannotebyivk"); + context.addServlet(new ServletHolder(scanNoteByOvkOnPBFTServlet), "/scannotebyovk"); + context.addServlet(new ServletHolder(isSpendOnPBFTServlet), "/isspend"); + context.addServlet(new ServletHolder(triggerConstantContractOnPBFTServlet), + "/triggerconstantcontract"); + context.addServlet(new ServletHolder(estimateEnergyOnPBFTServlet), "/estimateenergy"); - // same as FullNode - context.addServlet(new ServletHolder(accountOnPBFTServlet), "/getaccount"); - context.addServlet(new ServletHolder(listWitnessesOnPBFTServlet), "/listwitnesses"); - context.addServlet(new ServletHolder(getAssetIssueListOnPBFTServlet), "/getassetissuelist"); - context.addServlet(new ServletHolder(getPaginatedAssetIssueListOnPBFTServlet), - "/getpaginatedassetissuelist"); - context - .addServlet(new ServletHolder(getAssetIssueByNameOnPBFTServlet), "/getassetissuebyname"); - context.addServlet(new ServletHolder(getAssetIssueByIdOnPBFTServlet), "/getassetissuebyid"); - context.addServlet(new ServletHolder(getAssetIssueListByNameOnPBFTServlet), - "/getassetissuelistbyname"); - context.addServlet(new ServletHolder(getNowBlockOnPBFTServlet), "/getnowblock"); - context.addServlet(new ServletHolder(getBlockByNumOnPBFTServlet), "/getblockbynum"); - context.addServlet(new ServletHolder(getDelegatedResourceOnPBFTServlet), - "/getdelegatedresource"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexOnPBFTServlet), - "/getdelegatedresourceaccountindex"); - context.addServlet(new ServletHolder(getExchangeByIdOnPBFTServlet), "/getexchangebyid"); - context.addServlet(new ServletHolder(listExchangesOnPBFTServlet), "/listexchanges"); - context.addServlet(new ServletHolder(getAccountByIdOnPBFTServlet), "/getaccountbyid"); - context.addServlet(new ServletHolder(getBlockByIdOnPBFTServlet), "/getblockbyid"); - context - .addServlet(new ServletHolder(getBlockByLimitNextOnPBFTServlet), "/getblockbylimitnext"); - context - .addServlet(new ServletHolder(getBlockByLatestNumOnPBFTServlet), "/getblockbylatestnum"); - context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoOnPBFTServlet), - "/getmerkletreevoucherinfo"); - context.addServlet(new ServletHolder(scanAndMarkNoteByIvkOnPBFTServlet), - "/scanandmarknotebyivk"); - context.addServlet(new ServletHolder(scanNoteByIvkOnPBFTServlet), "/scannotebyivk"); - context.addServlet(new ServletHolder(scanNoteByOvkOnPBFTServlet), "/scannotebyovk"); - context.addServlet(new ServletHolder(isSpendOnPBFTServlet), "/isspend"); - context.addServlet(new ServletHolder(triggerConstantContractOnPBFTServlet), - "/triggerconstantcontract"); - context.addServlet(new ServletHolder(estimateEnergyOnPBFTServlet), "/estimateenergy"); + // only for PBFTNode + context.addServlet(new ServletHolder(getTransactionByIdOnPBFTServlet), "/gettransactionbyid"); + context.addServlet(new ServletHolder(getTransactionInfoByIdOnPBFTServlet), + "/gettransactioninfobyid"); - // only for PBFTNode - context.addServlet(new ServletHolder(getTransactionByIdOnPBFTServlet), "/gettransactionbyid"); - context.addServlet(new ServletHolder(getTransactionInfoByIdOnPBFTServlet), - "/gettransactioninfobyid"); + context.addServlet(new ServletHolder(getTransactionCountByBlockNumOnPBFTServlet), + "/gettransactioncountbyblocknum"); - context.addServlet(new ServletHolder(getTransactionCountByBlockNumOnPBFTServlet), - "/gettransactioncountbyblocknum"); + context.addServlet(new ServletHolder(getNodeInfoOnPBFTServlet), "/getnodeinfo"); + context.addServlet(new ServletHolder(getBrokerageServlet), "/getBrokerage"); + context.addServlet(new ServletHolder(getRewardServlet), "/getReward"); - context.addServlet(new ServletHolder(getNodeInfoOnPBFTServlet), "/getnodeinfo"); - context.addServlet(new ServletHolder(getBrokerageServlet), "/getBrokerage"); - context.addServlet(new ServletHolder(getRewardServlet), "/getReward"); + context.addServlet(new ServletHolder(getMarketOrderByAccountOnPBFTServlet), + "/getmarketorderbyaccount"); + context.addServlet(new ServletHolder(getMarketOrderByIdOnPBFTServlet), + "/getmarketorderbyid"); + context.addServlet(new ServletHolder(getMarketPriceByPairOnPBFTServlet), + "/getmarketpricebypair"); + context.addServlet(new ServletHolder(getMarketOrderListByPairOnPBFTServlet), + "/getmarketorderlistbypair"); + context.addServlet(new ServletHolder(getMarketPairListOnPBFTServlet), + "/getmarketpairlist"); - context.addServlet(new ServletHolder(getMarketOrderByAccountOnPBFTServlet), - "/getmarketorderbyaccount"); - context.addServlet(new ServletHolder(getMarketOrderByIdOnPBFTServlet), - "/getmarketorderbyid"); - context.addServlet(new ServletHolder(getMarketPriceByPairOnPBFTServlet), - "/getmarketpricebypair"); - context.addServlet(new ServletHolder(getMarketOrderListByPairOnPBFTServlet), - "/getmarketorderlistbypair"); - context.addServlet(new ServletHolder(getMarketPairListOnPBFTServlet), - "/getmarketpairlist"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkOnPBFTServlet), + "/scanshieldedtrc20notesbyivk"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkOnPBFTServlet), + "/scanshieldedtrc20notesbyovk"); + context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentOnPBFTServlet), + "/isshieldedtrc20contractnotespent"); + context.addServlet(new ServletHolder(getBurnTrxOnPBFTServlet), + "/getburntrx"); + context.addServlet(new ServletHolder(getBandwidthPricesOnPBFTServlet), + "/getbandwidthprices"); + context.addServlet(new ServletHolder(getEnergyPricesOnPBFTServlet), + "/getenergyprices"); + context.addServlet(new ServletHolder(getBlockOnPBFTServlet), + "/getblock"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkOnPBFTServlet), - "/scanshieldedtrc20notesbyivk"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkOnPBFTServlet), - "/scanshieldedtrc20notesbyovk"); - context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentOnPBFTServlet), - "/isshieldedtrc20contractnotespent"); - context.addServlet(new ServletHolder(getBurnTrxOnPBFTServlet), - "/getburntrx"); - context.addServlet(new ServletHolder(getBandwidthPricesOnPBFTServlet), - "/getbandwidthprices"); - context.addServlet(new ServletHolder(getEnergyPricesOnPBFTServlet), - "/getenergyprices"); - context.addServlet(new ServletHolder(getBlockOnPBFTServlet), - "/getblock"); - - context.addServlet(new ServletHolder(getAvailableUnfreezeCountOnPBFTServlet), - "/getavailableunfreezecount"); - context.addServlet(new ServletHolder(getCanDelegatedMaxSizeOnPBFTServlet), - "/getcandelegatedmaxsize"); - context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountOnPBFTServlet), - "/getcanwithdrawunfreezeamount"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2OnPBFTServlet), - "/getdelegatedresourceaccountindexv2"); - context.addServlet(new ServletHolder(getDelegatedResourceV2OnPBFTServlet), - "/getdelegatedresourcev2"); - - int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } - - // filters the specified APIs - // when node is lite fullnode and openHistoryQueryWhenLiteFN is false - context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", - EnumSet.allOf(DispatcherType.class)); + context.addServlet(new ServletHolder(getAvailableUnfreezeCountOnPBFTServlet), + "/getavailableunfreezecount"); + context.addServlet(new ServletHolder(getCanDelegatedMaxSizeOnPBFTServlet), + "/getcandelegatedmaxsize"); + context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountOnPBFTServlet), + "/getcanwithdrawunfreezeamount"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2OnPBFTServlet), + "/getdelegatedresourceaccountindexv2"); + context.addServlet(new ServletHolder(getDelegatedResourceV2OnPBFTServlet), + "/getdelegatedresourcev2"); + } - // api access filter - context.addFilter(new FilterHolder(httpApiAccessFilter), "/*", - EnumSet.allOf(DispatcherType.class)); + @Override + protected void addFilter(ServletContextHandler context) { + // filters the specified APIs + // when node is lite fullnode and openHistoryQueryWhenLiteFN is false + context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", + EnumSet.allOf(DispatcherType.class)); - super.start(); - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + // api access filter + context.addFilter(new FilterHolder(httpApiAccessFilter), "/*", + EnumSet.allOf(DispatcherType.class)); } } diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java index 6bdfc824163..aa566f56042 100755 --- a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/RpcApiServiceOnSolidity.java @@ -2,9 +2,7 @@ import com.google.protobuf.ByteString; import io.grpc.netty.NettyServerBuilder; -import io.grpc.protobuf.services.ProtoReflectionService; import io.grpc.stub.StreamObserver; -import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.tron.api.DatabaseGrpc.DatabaseImplBase; @@ -35,15 +33,11 @@ import org.tron.api.GrpcAPI.WitnessList; import org.tron.api.WalletSolidityGrpc.WalletSolidityImplBase; import org.tron.common.application.RpcService; -import org.tron.common.es.ExecutorServiceManager; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.Sha256Hash; import org.tron.core.capsule.BlockCapsule; import org.tron.core.config.args.Args; import org.tron.core.services.RpcApiService; -import org.tron.core.services.filter.LiteFnQueryGrpcInterceptor; -import org.tron.core.services.ratelimiter.RateLimiterInterceptor; -import org.tron.core.services.ratelimiter.RpcApiAccessInterceptor; import org.tron.protos.Protocol.Account; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.DelegatedResourceAccountIndex; @@ -72,69 +66,16 @@ public class RpcApiServiceOnSolidity extends RpcService { @Autowired private RpcApiService rpcApiService; - @Autowired - private RateLimiterInterceptor rateLimiterInterceptor; - - @Autowired - private LiteFnQueryGrpcInterceptor liteFnQueryGrpcInterceptor; - - @Autowired - private RpcApiAccessInterceptor apiAccessInterceptor; - - private final String executorName = "rpc-solidity-executor"; - - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public RpcApiServiceOnSolidity() { port = Args.getInstance().getRpcOnSolidityPort(); + enable = isFullNode() && Args.getInstance().isRpcSolidityEnable(); + executorName = "rpc-solidity-executor"; } @Override - public void start() { - try { - NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(port) - .addService(new DatabaseApi()); - - CommonParameter parameter = Args.getInstance(); - - if (parameter.getRpcThreadNum() > 0) { - serverBuilder = serverBuilder - .executor(ExecutorServiceManager.newFixedThreadPool( - executorName, parameter.getRpcThreadNum())); - } - - serverBuilder = serverBuilder.addService(new WalletSolidityApi()); - - // Set configs from config.conf or default value - serverBuilder.maxConcurrentCallsPerConnection(parameter.getMaxConcurrentCallsPerConnection()) - .flowControlWindow(parameter.getFlowControlWindow()) - .maxConnectionIdle(parameter.getMaxConnectionIdleInMillis(), TimeUnit.MILLISECONDS) - .maxConnectionAge(parameter.getMaxConnectionAgeInMillis(), TimeUnit.MILLISECONDS) - .maxInboundMessageSize(parameter.getMaxMessageSize()) - .maxHeaderListSize(parameter.getMaxHeaderListSize()); - - // add a ratelimiter interceptor - serverBuilder.intercept(rateLimiterInterceptor); - - // add api access interceptor - serverBuilder.intercept(apiAccessInterceptor); - - // add lite fullnode query interceptor - serverBuilder.intercept(liteFnQueryGrpcInterceptor); - - if (parameter.isRpcReflectionServiceEnable()) { - serverBuilder.addService(ProtoReflectionService.newInstance()); - } - - apiServer = serverBuilder.build(); - rateLimiterInterceptor.init(apiServer); - super.start(); - } catch (Exception e) { - logger.debug(e.getMessage(), e); - } + protected void addService(NettyServerBuilder serverBuilder) { + serverBuilder.addService(new DatabaseApi()); + serverBuilder.addService(new WalletSolidityApi()); } private TransactionExtention transaction2Extention(Transaction transaction) { diff --git a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java index f89be80c71b..b1d940ce2cd 100644 --- a/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java +++ b/framework/src/main/java/org/tron/core/services/interfaceOnSolidity/http/solidity/HttpApiOnSolidityService.java @@ -10,7 +10,6 @@ import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.annotation.Autowired; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; import org.tron.core.config.args.Args; import org.tron.core.services.filter.HttpApiAccessFilter; import org.tron.core.services.filter.LiteFnQueryHttpFilter; @@ -178,144 +177,129 @@ public class HttpApiOnSolidityService extends HttpService { @Autowired private GetBlockOnSolidityServlet getBlockOnSolidityServlet; - @Override - public void init() { - - } - - @Override - public void init(CommonParameter args) { + public HttpApiOnSolidityService() { port = Args.getInstance().getSolidityHttpPort(); + enable = isFullNode() && Args.getInstance().isSolidityNodeHttpEnable(); + contextPath = "/"; } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); - - // same as FullNode - context.addServlet(new ServletHolder(accountOnSolidityServlet), "/walletsolidity/getaccount"); - context.addServlet(new ServletHolder(listWitnessesOnSolidityServlet), - "/walletsolidity/listwitnesses"); - context.addServlet(new ServletHolder(getAssetIssueListOnSolidityServlet), - "/walletsolidity/getassetissuelist"); - context.addServlet(new ServletHolder(getPaginatedAssetIssueListOnSolidityServlet), - "/walletsolidity/getpaginatedassetissuelist"); - context.addServlet(new ServletHolder(getAssetIssueByNameOnSolidityServlet), - "/walletsolidity/getassetissuebyname"); - context.addServlet(new ServletHolder(getAssetIssueByIdOnSolidityServlet), - "/walletsolidity/getassetissuebyid"); - context.addServlet(new ServletHolder(getAssetIssueListByNameOnSolidityServlet), - "/walletsolidity/getassetissuelistbyname"); - context.addServlet(new ServletHolder(getNowBlockOnSolidityServlet), - "/walletsolidity/getnowblock"); - context.addServlet(new ServletHolder(getBlockByNumOnSolidityServlet), - "/walletsolidity/getblockbynum"); - context.addServlet(new ServletHolder(getDelegatedResourceOnSolidityServlet), - "/walletsolidity/getdelegatedresource"); - context.addServlet(new ServletHolder(getDelegatedResourceV2OnSolidityServlet), - "/walletsolidity/getdelegatedresourcev2"); - context.addServlet(new ServletHolder(getCanDelegatedMaxSizeOnSolidityServlet), - "/walletsolidity/getcandelegatedmaxsize"); - context.addServlet(new ServletHolder(getAvailableUnfreezeCountOnSolidityServlet), - "/walletsolidity/getavailableunfreezecount"); - context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountOnSolidityServlet), - "/walletsolidity/getcanwithdrawunfreezeamount"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexOnSolidityServlet), - "/walletsolidity/getdelegatedresourceaccountindex"); - context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2OnSolidityServlet), - "/walletsolidity/getdelegatedresourceaccountindexv2"); - context.addServlet(new ServletHolder(getExchangeByIdOnSolidityServlet), - "/walletsolidity/getexchangebyid"); - context.addServlet(new ServletHolder(listExchangesOnSolidityServlet), - "/walletsolidity/listexchanges"); - context.addServlet(new ServletHolder(getAccountByIdOnSolidityServlet), - "/walletsolidity/getaccountbyid"); - context.addServlet(new ServletHolder(getBlockByIdOnSolidityServlet), - "/walletsolidity/getblockbyid"); - context.addServlet(new ServletHolder(getBlockByLimitNextOnSolidityServlet), - "/walletsolidity/getblockbylimitnext"); - context.addServlet(new ServletHolder(getBlockByLatestNumOnSolidityServlet), - "/walletsolidity/getblockbylatestnum"); - // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoOnSolidityServlet), - // "/walletsolidity/getmerkletreevoucherinfo"); - // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkOnSolidityServlet), - // "/walletsolidity/scanandmarknotebyivk"); - // context.addServlet(new ServletHolder(scanNoteByIvkOnSolidityServlet), - // "/walletsolidity/scannotebyivk"); - // context.addServlet(new ServletHolder(scanNoteByOvkOnSolidityServlet), - // "/walletsolidity/scannotebyovk"); - // context.addServlet(new ServletHolder(isSpendOnSolidityServlet), - // "/walletsolidity/isspend"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkOnSolidityServlet), - "/walletsolidity/scanshieldedtrc20notesbyivk"); - context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkOnSolidityServlet), - "/walletsolidity/scanshieldedtrc20notesbyovk"); - context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentOnSolidityServlet), - "/walletsolidity/isshieldedtrc20contractnotespent"); - context.addServlet(new ServletHolder(triggerConstantContractOnSolidityServlet), - "/walletsolidity/triggerconstantcontract"); - context.addServlet(new ServletHolder(estimateEnergyOnSolidityServlet), - "/walletsolidity/estimateenergy"); - context.addServlet(new ServletHolder(getTransactionInfoByBlockNumOnSolidityServlet), - "/walletsolidity/gettransactioninfobyblocknum"); - context.addServlet(new ServletHolder(getMarketOrderByAccountOnSolidityServlet), - "/walletsolidity/getmarketorderbyaccount"); - context.addServlet(new ServletHolder(getMarketOrderByIdOnSolidityServlet), - "/walletsolidity/getmarketorderbyid"); - context.addServlet(new ServletHolder(getMarketPriceByPairOnSolidityServlet), - "/walletsolidity/getmarketpricebypair"); - context.addServlet(new ServletHolder(getMarketOrderListByPairOnSolidityServlet), - "/walletsolidity/getmarketorderlistbypair"); - context.addServlet(new ServletHolder(getMarketPairListOnSolidityServlet), - "/walletsolidity/getmarketpairlist"); + protected void addServlet(ServletContextHandler context) { + // same as FullNode + context.addServlet(new ServletHolder(accountOnSolidityServlet), "/walletsolidity/getaccount"); + context.addServlet(new ServletHolder(listWitnessesOnSolidityServlet), + "/walletsolidity/listwitnesses"); + context.addServlet(new ServletHolder(getAssetIssueListOnSolidityServlet), + "/walletsolidity/getassetissuelist"); + context.addServlet(new ServletHolder(getPaginatedAssetIssueListOnSolidityServlet), + "/walletsolidity/getpaginatedassetissuelist"); + context.addServlet(new ServletHolder(getAssetIssueByNameOnSolidityServlet), + "/walletsolidity/getassetissuebyname"); + context.addServlet(new ServletHolder(getAssetIssueByIdOnSolidityServlet), + "/walletsolidity/getassetissuebyid"); + context.addServlet(new ServletHolder(getAssetIssueListByNameOnSolidityServlet), + "/walletsolidity/getassetissuelistbyname"); + context.addServlet(new ServletHolder(getNowBlockOnSolidityServlet), + "/walletsolidity/getnowblock"); + context.addServlet(new ServletHolder(getBlockByNumOnSolidityServlet), + "/walletsolidity/getblockbynum"); + context.addServlet(new ServletHolder(getDelegatedResourceOnSolidityServlet), + "/walletsolidity/getdelegatedresource"); + context.addServlet(new ServletHolder(getDelegatedResourceV2OnSolidityServlet), + "/walletsolidity/getdelegatedresourcev2"); + context.addServlet(new ServletHolder(getCanDelegatedMaxSizeOnSolidityServlet), + "/walletsolidity/getcandelegatedmaxsize"); + context.addServlet(new ServletHolder(getAvailableUnfreezeCountOnSolidityServlet), + "/walletsolidity/getavailableunfreezecount"); + context.addServlet(new ServletHolder(getCanWithdrawUnfreezeAmountOnSolidityServlet), + "/walletsolidity/getcanwithdrawunfreezeamount"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexOnSolidityServlet), + "/walletsolidity/getdelegatedresourceaccountindex"); + context.addServlet(new ServletHolder(getDelegatedResourceAccountIndexV2OnSolidityServlet), + "/walletsolidity/getdelegatedresourceaccountindexv2"); + context.addServlet(new ServletHolder(getExchangeByIdOnSolidityServlet), + "/walletsolidity/getexchangebyid"); + context.addServlet(new ServletHolder(listExchangesOnSolidityServlet), + "/walletsolidity/listexchanges"); + context.addServlet(new ServletHolder(getAccountByIdOnSolidityServlet), + "/walletsolidity/getaccountbyid"); + context.addServlet(new ServletHolder(getBlockByIdOnSolidityServlet), + "/walletsolidity/getblockbyid"); + context.addServlet(new ServletHolder(getBlockByLimitNextOnSolidityServlet), + "/walletsolidity/getblockbylimitnext"); + context.addServlet(new ServletHolder(getBlockByLatestNumOnSolidityServlet), + "/walletsolidity/getblockbylatestnum"); + // context.addServlet(new ServletHolder(getMerkleTreeVoucherInfoOnSolidityServlet), + // "/walletsolidity/getmerkletreevoucherinfo"); + // context.addServlet(new ServletHolder(scanAndMarkNoteByIvkOnSolidityServlet), + // "/walletsolidity/scanandmarknotebyivk"); + // context.addServlet(new ServletHolder(scanNoteByIvkOnSolidityServlet), + // "/walletsolidity/scannotebyivk"); + // context.addServlet(new ServletHolder(scanNoteByOvkOnSolidityServlet), + // "/walletsolidity/scannotebyovk"); + // context.addServlet(new ServletHolder(isSpendOnSolidityServlet), + // "/walletsolidity/isspend"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByIvkOnSolidityServlet), + "/walletsolidity/scanshieldedtrc20notesbyivk"); + context.addServlet(new ServletHolder(scanShieldedTRC20NotesByOvkOnSolidityServlet), + "/walletsolidity/scanshieldedtrc20notesbyovk"); + context.addServlet(new ServletHolder(isShieldedTRC20ContractNoteSpentOnSolidityServlet), + "/walletsolidity/isshieldedtrc20contractnotespent"); + context.addServlet(new ServletHolder(triggerConstantContractOnSolidityServlet), + "/walletsolidity/triggerconstantcontract"); + context.addServlet(new ServletHolder(estimateEnergyOnSolidityServlet), + "/walletsolidity/estimateenergy"); + context.addServlet(new ServletHolder(getTransactionInfoByBlockNumOnSolidityServlet), + "/walletsolidity/gettransactioninfobyblocknum"); + context.addServlet(new ServletHolder(getMarketOrderByAccountOnSolidityServlet), + "/walletsolidity/getmarketorderbyaccount"); + context.addServlet(new ServletHolder(getMarketOrderByIdOnSolidityServlet), + "/walletsolidity/getmarketorderbyid"); + context.addServlet(new ServletHolder(getMarketPriceByPairOnSolidityServlet), + "/walletsolidity/getmarketpricebypair"); + context.addServlet(new ServletHolder(getMarketOrderListByPairOnSolidityServlet), + "/walletsolidity/getmarketorderlistbypair"); + context.addServlet(new ServletHolder(getMarketPairListOnSolidityServlet), + "/walletsolidity/getmarketpairlist"); - // only for SolidityNode - context.addServlet(new ServletHolder(getTransactionByIdOnSolidityServlet), - "/walletsolidity/gettransactionbyid"); - context.addServlet(new ServletHolder(getTransactionInfoByIdOnSolidityServlet), - "/walletsolidity/gettransactioninfobyid"); + // only for SolidityNode + context.addServlet(new ServletHolder(getTransactionByIdOnSolidityServlet), + "/walletsolidity/gettransactionbyid"); + context.addServlet(new ServletHolder(getTransactionInfoByIdOnSolidityServlet), + "/walletsolidity/gettransactioninfobyid"); - context.addServlet(new ServletHolder(getTransactionCountByBlockNumOnSolidityServlet), - "/walletsolidity/gettransactioncountbyblocknum"); + context.addServlet(new ServletHolder(getTransactionCountByBlockNumOnSolidityServlet), + "/walletsolidity/gettransactioncountbyblocknum"); - context.addServlet(new ServletHolder(getNodeInfoOnSolidityServlet), "/wallet/getnodeinfo"); - context.addServlet(new ServletHolder(getNodeInfoOnSolidityServlet), - "/walletsolidity/getnodeinfo"); - context.addServlet(new ServletHolder(getBrokerageServlet), "/walletsolidity/getBrokerage"); - context.addServlet(new ServletHolder(getRewardServlet), "/walletsolidity/getReward"); - context - .addServlet(new ServletHolder(getBurnTrxOnSolidityServlet), "/walletsolidity/getburntrx"); - context.addServlet(new ServletHolder(getBandwidthPricesOnSolidityServlet), - "/walletsolidity/getbandwidthprices"); - context.addServlet(new ServletHolder(getEnergyPricesOnSolidityServlet), - "/walletsolidity/getenergyprices"); + context.addServlet(new ServletHolder(getNodeInfoOnSolidityServlet), "/wallet/getnodeinfo"); + context.addServlet(new ServletHolder(getNodeInfoOnSolidityServlet), + "/walletsolidity/getnodeinfo"); + context.addServlet(new ServletHolder(getBrokerageServlet), "/walletsolidity/getBrokerage"); + context.addServlet(new ServletHolder(getRewardServlet), "/walletsolidity/getReward"); + context + .addServlet(new ServletHolder(getBurnTrxOnSolidityServlet), "/walletsolidity/getburntrx"); + context.addServlet(new ServletHolder(getBandwidthPricesOnSolidityServlet), + "/walletsolidity/getbandwidthprices"); + context.addServlet(new ServletHolder(getEnergyPricesOnSolidityServlet), + "/walletsolidity/getenergyprices"); - context.addServlet(new ServletHolder(getBlockOnSolidityServlet), - "/walletsolidity/getblock"); + context.addServlet(new ServletHolder(getBlockOnSolidityServlet), + "/walletsolidity/getblock"); - // filters the specified APIs - // when node is lite fullnode and openHistoryQueryWhenLiteFN is false - context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", - EnumSet.allOf(DispatcherType.class)); + } - // api access filter - context.addFilter(new FilterHolder(httpApiAccessFilter), "/walletsolidity/*", - EnumSet.allOf(DispatcherType.class)); - context.getServletHandler().getFilterMappings()[1] - .setPathSpecs(new String[] {"/walletsolidity/*", - "/wallet/getnodeinfo"}); + @Override + protected void addFilter(ServletContextHandler context) { + // filters the specified APIs + // when node is lite fullnode and openHistoryQueryWhenLiteFN is false + context.addFilter(new FilterHolder(liteFnQueryHttpFilter), "/*", + EnumSet.allOf(DispatcherType.class)); - int maxHttpConnectNumber = Args.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } - super.start(); - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + // api access filter + context.addFilter(new FilterHolder(httpApiAccessFilter), "/walletsolidity/*", + EnumSet.allOf(DispatcherType.class)); + context.getServletHandler().getFilterMappings()[1] + .setPathSpecs(new String[] {"/walletsolidity/*", + "/wallet/getnodeinfo"}); } } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/FullNodeJsonRpcHttpService.java b/framework/src/main/java/org/tron/core/services/jsonrpc/FullNodeJsonRpcHttpService.java index ff017f9562e..566ad33a722 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/FullNodeJsonRpcHttpService.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/FullNodeJsonRpcHttpService.java @@ -3,8 +3,6 @@ import java.util.EnumSet; import javax.servlet.DispatcherType; import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.ConnectionLimit; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHandler; @@ -12,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tron.common.application.HttpService; -import org.tron.common.parameter.CommonParameter; +import org.tron.core.config.args.Args; import org.tron.core.services.filter.HttpInterceptor; @Component @@ -22,41 +20,24 @@ public class FullNodeJsonRpcHttpService extends HttpService { @Autowired private JsonRpcServlet jsonRpcServlet; - @Override - public void init() { + public FullNodeJsonRpcHttpService() { + port = Args.getInstance().getJsonRpcHttpFullNodePort(); + enable = isFullNode() && Args.getInstance().isJsonRpcHttpFullNodeEnable(); + contextPath = "/"; } @Override - public void init(CommonParameter args) { - port = CommonParameter.getInstance().getJsonRpcHttpFullNodePort(); + protected void addServlet(ServletContextHandler context) { + context.addServlet(new ServletHolder(jsonRpcServlet), "/jsonrpc"); } @Override - public void start() { - try { - apiServer = new Server(port); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - apiServer.setHandler(context); - - context.addServlet(new ServletHolder(jsonRpcServlet), "/jsonrpc"); - - int maxHttpConnectNumber = CommonParameter.getInstance().getMaxHttpConnectNumber(); - if (maxHttpConnectNumber > 0) { - apiServer.addBean(new ConnectionLimit(maxHttpConnectNumber, apiServer)); - } - - // filter - ServletHandler handler = new ServletHandler(); - FilterHolder fh = handler - .addFilterWithMapping(HttpInterceptor.class, "/*", - EnumSet.of(DispatcherType.REQUEST)); - context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST)); - - super.start(); - - } catch (Exception e) { - logger.debug("IOException: {}", e.getMessage()); - } + protected void addFilter(ServletContextHandler context) { + // filter + ServletHandler handler = new ServletHandler(); + FilterHolder fh = handler + .addFilterWithMapping(HttpInterceptor.class, "/*", + EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST)); } } diff --git a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java index 8b9aafe4715..04405dfc758 100644 --- a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java +++ b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java @@ -6,7 +6,6 @@ import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; -import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import org.tron.common.zksnark.JLibrustzcash; import org.tron.common.zksnark.LibrustzcashParam; @@ -14,7 +13,6 @@ @Slf4j(topic = "API") @Component -@DependsOn("fullNodeHttpApiService") public class ZksnarkInitService { @PostConstruct diff --git a/framework/src/main/java/org/tron/program/FullNode.java b/framework/src/main/java/org/tron/program/FullNode.java index 0fd87eb5de0..b7adf0ffe1a 100644 --- a/framework/src/main/java/org/tron/program/FullNode.java +++ b/framework/src/main/java/org/tron/program/FullNode.java @@ -15,16 +15,6 @@ import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; -import org.tron.core.net.P2pEventHandlerImpl; -import org.tron.core.services.RpcApiService; -import org.tron.core.services.http.FullNodeHttpApiService; -import org.tron.core.services.interfaceJsonRpcOnPBFT.JsonRpcServiceOnPBFT; -import org.tron.core.services.interfaceJsonRpcOnSolidity.JsonRpcServiceOnSolidity; -import org.tron.core.services.interfaceOnPBFT.RpcApiServiceOnPBFT; -import org.tron.core.services.interfaceOnPBFT.http.PBFT.HttpApiOnPBFTService; -import org.tron.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity; -import org.tron.core.services.interfaceOnSolidity.http.solidity.HttpApiOnSolidityService; -import org.tron.core.services.jsonrpc.FullNodeJsonRpcHttpService; @Slf4j(topic = "app") public class FullNode { @@ -80,55 +70,6 @@ public static void main(String[] args) { context.refresh(); Application appT = ApplicationFactory.create(context); context.registerShutdownHook(); - - // grpc api server - RpcApiService rpcApiService = context.getBean(RpcApiService.class); - appT.addService(rpcApiService); - - // http api server - FullNodeHttpApiService httpApiService = context.getBean(FullNodeHttpApiService.class); - if (CommonParameter.getInstance().fullNodeHttpEnable) { - appT.addService(httpApiService); - } - - // JSON-RPC http server - if (CommonParameter.getInstance().jsonRpcHttpFullNodeEnable) { - FullNodeJsonRpcHttpService jsonRpcHttpService = - context.getBean(FullNodeJsonRpcHttpService.class); - appT.addService(jsonRpcHttpService); - } - - // full node and solidity node fuse together - // provide solidity rpc and http server on the full node. - RpcApiServiceOnSolidity rpcApiServiceOnSolidity = context - .getBean(RpcApiServiceOnSolidity.class); - appT.addService(rpcApiServiceOnSolidity); - HttpApiOnSolidityService httpApiOnSolidityService = context - .getBean(HttpApiOnSolidityService.class); - if (CommonParameter.getInstance().solidityNodeHttpEnable) { - appT.addService(httpApiOnSolidityService); - } - - // JSON-RPC on solidity - if (CommonParameter.getInstance().jsonRpcHttpSolidityNodeEnable) { - JsonRpcServiceOnSolidity jsonRpcServiceOnSolidity = context - .getBean(JsonRpcServiceOnSolidity.class); - appT.addService(jsonRpcServiceOnSolidity); - } - - // PBFT API (HTTP and GRPC) - RpcApiServiceOnPBFT rpcApiServiceOnPBFT = context - .getBean(RpcApiServiceOnPBFT.class); - appT.addService(rpcApiServiceOnPBFT); - HttpApiOnPBFTService httpApiOnPBFTService = context - .getBean(HttpApiOnPBFTService.class); - appT.addService(httpApiOnPBFTService); - - // JSON-RPC on PBFT - if (CommonParameter.getInstance().jsonRpcHttpPBFTNodeEnable) { - JsonRpcServiceOnPBFT jsonRpcServiceOnPBFT = context.getBean(JsonRpcServiceOnPBFT.class); - appT.addService(jsonRpcServiceOnPBFT); - } appT.startup(); appT.blockUntilShutdown(); } diff --git a/framework/src/main/java/org/tron/program/SolidityNode.java b/framework/src/main/java/org/tron/program/SolidityNode.java index 4cf71177803..8def8ed7ebc 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -6,9 +6,8 @@ import java.util.concurrent.atomic.AtomicLong; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; -import org.springframework.context.ApplicationContext; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.util.ObjectUtils; -import org.springframework.util.StringUtils; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; @@ -21,8 +20,6 @@ import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.db.Manager; -import org.tron.core.services.RpcApiService; -import org.tron.core.services.http.solidity.SolidityNodeHttpApiService; import org.tron.protos.Protocol.Block; @Slf4j(topic = "app") @@ -71,9 +68,6 @@ public static void main(String[] args) { } parameter.setSolidityNode(true); - TronApplicationContext context = new TronApplicationContext(DefaultConfig.class); - context.registerShutdownHook(); - if (parameter.isHelp()) { logger.info("Here is the help message."); return; @@ -81,18 +75,17 @@ public static void main(String[] args) { // init metrics first Metrics.init(); + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + beanFactory.setAllowCircularReferences(false); + TronApplicationContext context = + new TronApplicationContext(beanFactory); + context.register(DefaultConfig.class); + context.refresh(); Application appT = ApplicationFactory.create(context); - RpcApiService rpcApiService = context.getBean(RpcApiService.class); - appT.addService(rpcApiService); - //http - SolidityNodeHttpApiService httpApiService = context.getBean(SolidityNodeHttpApiService.class); - if (CommonParameter.getInstance().solidityNodeHttpEnable) { - appT.addService(httpApiService); - } - + context.registerShutdownHook(); + appT.startup(); SolidityNode node = new SolidityNode(appT.getDbManager()); node.start(); - appT.startup(); appT.blockUntilShutdown(); } diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index 78427c30f87..88020581db7 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -209,6 +209,8 @@ node { fullNodePort = 8090 solidityEnable = true solidityPort = 8091 + PBFTEnable = true + PBFTPort = 8092 } # use your ipv6 address for node discovery and tcp connection, default false @@ -225,8 +227,12 @@ node { } rpc { + enable = true port = 50051 - #solidityPort = 50061 + solidityEnable = true + solidityPort = 50061 + PBFTEnable = true + PBFTPort = 50071 # Number of gRPC thread, default availableProcessors / 2 # thread = 16 diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 5bbf08fd96e..87933a62e5a 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -17,11 +17,15 @@ import com.google.common.collect.Lists; import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; import io.grpc.internal.GrpcUtil; import io.grpc.netty.NettyServerBuilder; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Assert; @@ -150,5 +154,94 @@ public void testOldRewardOpt() { thrown.expect(IllegalArgumentException.class); Args.setParam(new String[] {"-w"}, "args-test.conf"); } + + @Test + public void testInitService() { + Map storage = new HashMap<>(); + // avoid the exception for the missing storage + storage.put("storage.db.directory", "database"); + Config config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage)); + // test default value + Args.setParam(new String[] {}, config); + Assert.assertTrue(Args.getInstance().isRpcEnable()); + Assert.assertTrue(Args.getInstance().isRpcSolidityEnable()); + Assert.assertTrue(Args.getInstance().isRpcPBFTEnable()); + Assert.assertTrue(Args.getInstance().isFullNodeHttpEnable()); + Assert.assertTrue(Args.getInstance().isSolidityNodeHttpEnable()); + Assert.assertTrue(Args.getInstance().isPBFTHttpEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpFullNodeEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpPBFTNodeEnable()); + Args.clearParam(); + // test set all true value + storage.put("node.rpc.enable", "true"); + storage.put("node.rpc.solidityEnable", "true"); + storage.put("node.rpc.PBFTEnable", "true"); + storage.put("node.http.fullNodeEnable", "true"); + storage.put("node.http.solidityEnable", "true"); + storage.put("node.http.PBFTEnable", "true"); + storage.put("node.jsonrpc.httpFullNodeEnable", "true"); + storage.put("node.jsonrpc.httpSolidityEnable", "true"); + storage.put("node.jsonrpc.httpPBFTEnable", "true"); + config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage)); + // test value + Args.setParam(new String[] {}, config); + Assert.assertTrue(Args.getInstance().isRpcEnable()); + Assert.assertTrue(Args.getInstance().isRpcSolidityEnable()); + Assert.assertTrue(Args.getInstance().isRpcPBFTEnable()); + Assert.assertTrue(Args.getInstance().isFullNodeHttpEnable()); + Assert.assertTrue(Args.getInstance().isSolidityNodeHttpEnable()); + Assert.assertTrue(Args.getInstance().isPBFTHttpEnable()); + Assert.assertTrue(Args.getInstance().isJsonRpcHttpFullNodeEnable()); + Assert.assertTrue(Args.getInstance().isJsonRpcHttpSolidityNodeEnable()); + Assert.assertTrue(Args.getInstance().isJsonRpcHttpPBFTNodeEnable()); + Args.clearParam(); + // test set all false value + storage.put("node.rpc.enable", "false"); + storage.put("node.rpc.solidityEnable", "false"); + storage.put("node.rpc.PBFTEnable", "false"); + storage.put("node.http.fullNodeEnable", "false"); + storage.put("node.http.solidityEnable", "false"); + storage.put("node.http.PBFTEnable", "false"); + storage.put("node.jsonrpc.httpFullNodeEnable", "false"); + storage.put("node.jsonrpc.httpSolidityEnable", "false"); + storage.put("node.jsonrpc.httpPBFTEnable", "false"); + config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage)); + // test value + Args.setParam(new String[] {}, config); + Assert.assertFalse(Args.getInstance().isRpcEnable()); + Assert.assertFalse(Args.getInstance().isRpcSolidityEnable()); + Assert.assertFalse(Args.getInstance().isRpcPBFTEnable()); + Assert.assertFalse(Args.getInstance().isFullNodeHttpEnable()); + Assert.assertFalse(Args.getInstance().isSolidityNodeHttpEnable()); + Assert.assertFalse(Args.getInstance().isPBFTHttpEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpFullNodeEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpPBFTNodeEnable()); + Args.clearParam(); + // test set random value + storage.put("node.rpc.enable", "false"); + storage.put("node.rpc.solidityEnable", "false"); + storage.put("node.rpc.PBFTEnable", "true"); + storage.put("node.http.fullNodeEnable", "false"); + storage.put("node.http.solidityEnable", "true"); + storage.put("node.http.PBFTEnable", "false"); + storage.put("node.jsonrpc.httpFullNodeEnable", "true"); + storage.put("node.jsonrpc.httpSolidityEnable", "false"); + storage.put("node.jsonrpc.httpPBFTEnable", "true"); + config = ConfigFactory.defaultOverrides().withFallback(ConfigFactory.parseMap(storage)); + // test value + Args.setParam(new String[] {}, config); + Assert.assertFalse(Args.getInstance().isRpcEnable()); + Assert.assertFalse(Args.getInstance().isRpcSolidityEnable()); + Assert.assertTrue(Args.getInstance().isRpcPBFTEnable()); + Assert.assertFalse(Args.getInstance().isFullNodeHttpEnable()); + Assert.assertTrue(Args.getInstance().isSolidityNodeHttpEnable()); + Assert.assertFalse(Args.getInstance().isPBFTHttpEnable()); + Assert.assertTrue(Args.getInstance().isJsonRpcHttpFullNodeEnable()); + Assert.assertFalse(Args.getInstance().isJsonRpcHttpSolidityNodeEnable()); + Assert.assertTrue(Args.getInstance().isJsonRpcHttpPBFTNodeEnable()); + Args.clearParam(); + } } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index b3ed26b591f..98631210374 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -265,7 +265,6 @@ public void testGetTransactionByHash() { @Test public void testGetBlockByNumber2() { - fullNodeJsonRpcHttpService.init(Args.getInstance()); fullNodeJsonRpcHttpService.start(); JsonArray params = new JsonArray(); params.add(ByteArray.toJsonHex(blockCapsule.getNum())); @@ -303,9 +302,7 @@ public void testGetBlockByNumber2() { @Test public void testServicesInit() { try { - jsonRpcServiceOnPBFT.init(Args.getInstance()); jsonRpcServiceOnPBFT.start(); - jsonRpcServiceOnSolidity.init(Args.getInstance()); jsonRpcServiceOnSolidity.start(); } finally { jsonRpcServiceOnPBFT.stop(); diff --git a/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java b/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java index 51a46b09388..e36029c6141 100644 --- a/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java +++ b/framework/src/test/java/org/tron/core/metrics/MetricsApiServiceTest.java @@ -44,7 +44,7 @@ public void init() throws IOException { "--storage-db-directory", dbDirectory, "--storage-index-directory", indexDirectory }, - "config.conf" + Constant.TEST_CONF ); CommonParameter parameter = Args.getInstance(); parameter.setNodeListenPort(port); @@ -52,9 +52,7 @@ public void init() throws IOException { parameter.setNodeExternalIp("127.0.0.1"); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); - rpcApiService = context.getBean(RpcApiService.class); metricsApiService = context.getBean(MetricsApiService.class); - appT.addService(rpcApiService); appT.startup(); } diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java index 7ae8b355168..65771bae952 100644 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ b/framework/src/test/java/org/tron/core/net/BaseNet.java @@ -30,12 +30,12 @@ import org.tron.common.application.TronApplicationContext; import org.tron.common.parameter.CommonParameter; import org.tron.common.utils.FileUtil; +import org.tron.common.utils.PublicMethod; import org.tron.common.utils.ReflectUtils; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.net.peer.PeerConnection; -import org.tron.core.services.RpcApiService; @Slf4j public class BaseNet { @@ -48,7 +48,6 @@ public class BaseNet { protected static TronApplicationContext context; - private static RpcApiService rpcApiService; private static Application appT; private static TronNetDelegate tronNetDelegate; @@ -98,10 +97,15 @@ public static void init() throws Exception { parameter.setNodeListenPort(port); parameter.getSeedNode().getAddressList().clear(); parameter.setNodeExternalIp(Constant.LOCAL_HOST); + parameter.setRpcEnable(true); + parameter.setRpcPort(PublicMethod.chooseRandomPort()); + parameter.setRpcSolidityEnable(false); + parameter.setRpcPBFTEnable(false); + parameter.setFullNodeHttpEnable(false); + parameter.setSolidityNodeHttpEnable(false); + parameter.setPBFTHttpEnable(false); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); - rpcApiService = context.getBean(RpcApiService.class); - appT.addService(rpcApiService); appT.startup(); try { Thread.sleep(2000); diff --git a/framework/src/test/java/org/tron/core/pbft/PbftApiTest.java b/framework/src/test/java/org/tron/core/pbft/PbftApiTest.java index 9bc942d6684..84fb632b93d 100755 --- a/framework/src/test/java/org/tron/core/pbft/PbftApiTest.java +++ b/framework/src/test/java/org/tron/core/pbft/PbftApiTest.java @@ -58,7 +58,6 @@ public void pbftapi() throws IOException { Assert.assertTrue(dynamicPropertiesStore.getLatestBlockHeaderNumber() >= 10); commonDataBase.saveLatestPbftBlockNum(6); - httpApiOnPBFTService.init(Args.getInstance()); httpApiOnPBFTService.start(); CloseableHttpResponse response; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { diff --git a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java index ebb9e0bb223..deea264800c 100644 --- a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java +++ b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java @@ -135,8 +135,11 @@ public static void init() throws IOException { Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); String OWNER_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; + getInstance().setRpcEnable(true); getInstance().setRpcPort(PublicMethod.chooseRandomPort()); + getInstance().setRpcSolidityEnable(true); getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort()); + getInstance().setRpcPBFTEnable(true); getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); String fullNode = String.format("%s:%d", getInstance().getNodeLanIp(), getInstance().getRpcPort()); diff --git a/framework/src/test/java/org/tron/core/services/WalletApiTest.java b/framework/src/test/java/org/tron/core/services/WalletApiTest.java index 0a87c348fdb..8890d4bfd9e 100644 --- a/framework/src/test/java/org/tron/core/services/WalletApiTest.java +++ b/framework/src/test/java/org/tron/core/services/WalletApiTest.java @@ -5,8 +5,8 @@ import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; +import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.tron.api.GrpcAPI.EmptyMessage; @@ -14,7 +14,7 @@ import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; -import org.tron.common.utils.client.Configuration; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; @@ -23,34 +23,34 @@ @Slf4j public class WalletApiTest { - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @ClassRule + public static TemporaryFolder temporaryFolder = new TemporaryFolder(); private static TronApplicationContext context; - private String fullnode = Configuration.getByPath("testng.conf") - .getStringList("fullnode.ip.list").get(0); - private RpcApiService rpcApiService; - private Application appT; + private static Application appT; - @Before - public void init() throws IOException { + + @BeforeClass + public static void init() throws IOException { Args.setParam(new String[]{ "-d", temporaryFolder.newFolder().toString(), "--p2p-disable", "true"}, Constant.TEST_CONF); + Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcEnable(true); context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); - rpcApiService = context.getBean(RpcApiService.class); - appT.addService(rpcApiService); appT.startup(); } @Test public void listNodesTest() { + String fullNode = String.format("%s:%d", "127.0.0.1", + Args.getInstance().getRpcPort()); WalletGrpc.WalletBlockingStub walletStub = WalletGrpc - .newBlockingStub(ManagedChannelBuilder.forTarget(fullnode) + .newBlockingStub(ManagedChannelBuilder.forTarget(fullNode) .usePlaintext() .build()); Assert.assertTrue(walletStub.listNodes(EmptyMessage.getDefaultInstance()) - .getNodesList().size() == 0); + .getNodesList().isEmpty()); } @After diff --git a/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java b/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java index 5f883fc8c07..9484155a0b7 100644 --- a/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java @@ -14,10 +14,10 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.args.Args; import org.tron.core.services.http.FullNodeHttpApiService; @@ -39,21 +39,17 @@ public class HttpApiAccessFilterTest extends BaseTest { static { Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false); - } - - /** - * init dependencies. - */ - @Before - public void init() { - appT.addService(httpApiService); - appT.addService(httpApiOnSolidityService); - appT.addService(httpApiOnPBFTService); - appT.startup(); + Args.getInstance().setFullNodeHttpEnable(true); + Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setPBFTHttpEnable(true); + Args.getInstance().setPBFTHttpPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setSolidityNodeHttpEnable(true); + Args.getInstance().setSolidityHttpPort(PublicMethod.chooseRandomPort()); } @Test public void testHttpFilter() { + appT.startup(); List disabledApiList = new ArrayList<>(); disabledApiList.add("getaccount"); disabledApiList.add("getnowblock"); diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java index 081b5839f84..0db3a6a99ce 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java @@ -55,8 +55,11 @@ public class LiteFnQueryGrpcInterceptorTest { @BeforeClass public static void init() throws IOException { Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + Args.getInstance().setRpcEnable(true); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcSolidityEnable(true); Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcPBFTEnable(true); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); String fullnode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), Args.getInstance().getRpcPort()); diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java index 5fd4711273e..a9068c228b6 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.Set; -import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -19,28 +18,32 @@ import org.junit.Before; import org.junit.Test; import org.tron.common.BaseTest; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.args.Args; -import org.tron.core.services.http.FullNodeHttpApiService; -import org.tron.core.services.interfaceOnPBFT.http.PBFT.HttpApiOnPBFTService; -import org.tron.core.services.interfaceOnSolidity.http.solidity.HttpApiOnSolidityService; @Slf4j public class LiteFnQueryHttpFilterTest extends BaseTest { private final String ip = "127.0.0.1"; private int fullHttpPort; - @Resource - private FullNodeHttpApiService httpApiService; - @Resource - private HttpApiOnSolidityService httpApiOnSolidityService; - @Resource - private HttpApiOnPBFTService httpApiOnPBFTService; private final CloseableHttpClient httpClient = HttpClients.createDefault(); static { Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false); + Args.getInstance().setRpcEnable(false); + Args.getInstance().setRpcSolidityEnable(false); + Args.getInstance().setRpcPBFTEnable(false); + Args.getInstance().setFullNodeHttpEnable(true); + Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setPBFTHttpEnable(true); + Args.getInstance().setPBFTHttpPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setSolidityNodeHttpEnable(true); + Args.getInstance().setSolidityHttpPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setJsonRpcHttpFullNodeEnable(false); + Args.getInstance().setJsonRpcHttpSolidityNodeEnable(false); + Args.getInstance().setJsonRpcHttpPBFTNodeEnable(false); } /** @@ -48,9 +51,6 @@ public class LiteFnQueryHttpFilterTest extends BaseTest { */ @Before public void init() { - appT.addService(httpApiService); - appT.addService(httpApiOnSolidityService); - appT.addService(httpApiOnPBFTService); appT.startup(); } diff --git a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java index 38af756f790..57fcc5a2202 100644 --- a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java @@ -56,8 +56,11 @@ public class RpcApiAccessInterceptorTest { @BeforeClass public static void init() throws IOException { Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + Args.getInstance().setRpcEnable(true); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcSolidityEnable(true); Args.getInstance().setRpcOnSolidityPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcPBFTEnable(true); Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort()); String fullNode = String.format("%s:%d", Args.getInstance().getNodeLanIp(), Args.getInstance().getRpcPort()); diff --git a/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java b/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java index f363b7fbefc..e9cd9cb1f26 100644 --- a/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/TriggerSmartContractServletTest.java @@ -1,7 +1,6 @@ package org.tron.core.services.http; import com.google.gson.JsonObject; -import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.bouncycastle.util.encoders.Hex; @@ -9,10 +8,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.tron.common.BaseTest; import org.tron.common.utils.ByteArray; -import org.tron.common.utils.client.Configuration; +import org.tron.common.utils.PublicMethod; import org.tron.common.utils.client.utils.HttpMethed; import org.tron.core.Constant; import org.tron.core.capsule.ContractCapsule; @@ -25,31 +23,24 @@ @Slf4j public class TriggerSmartContractServletTest extends BaseTest { - private static final String httpNode = Configuration.getByPath("testng.conf") - .getStringList("httpnode.ip.list") - .get(0); + private static String httpNode; private static final byte[] ownerAddr = Hex.decode("410000000000000000000000000000000000000000"); private static final byte[] contractAddr = Hex.decode( "41000000000000000000000000000000000000dEaD"); - @Resource - private FullNodeHttpApiService httpApiService; - @BeforeClass public static void init() throws Exception { Args.setParam( new String[]{"--output-directory", dbPath(), "--debug", "--witness"}, Constant.TEST_CONF); Args.getInstance().needSyncCheck = false; - - // build app context - DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - beanFactory.setAllowCircularReferences(false); + Args.getInstance().setFullNodeHttpEnable(true); + Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort()); + httpNode = String.format("%s:%d", "127.0.0.1", + Args.getInstance().getFullNodeHttpPort()); } @Before public void before() { - appT.addService(httpApiService); - // start services appT.startup(); diff --git a/framework/src/test/java/org/tron/program/SolidityNodeTest.java b/framework/src/test/java/org/tron/program/SolidityNodeTest.java index a77fa2fa8c6..a95d07c0c11 100755 --- a/framework/src/test/java/org/tron/program/SolidityNodeTest.java +++ b/framework/src/test/java/org/tron/program/SolidityNodeTest.java @@ -1,12 +1,8 @@ package org.tron.program; -import java.io.File; -import java.io.IOException; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; import org.tron.common.BaseTest; import org.tron.common.client.DatabaseGrpcClient; @@ -26,41 +22,10 @@ public class SolidityNodeTest extends BaseTest { SolidityNodeHttpApiService solidityNodeHttpApiService; static { - try { - Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); - } catch (IOException e) { - Assert.fail("create temp directory failed."); - } + Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); Args.getInstance().setSolidityNode(true); } - /** - * init db. - */ - @BeforeClass - public static void init() { - } - - /** - * remo db when after test. - */ - @AfterClass - public static void removeDb() { - Args.clearParam(); - } - - private static Boolean deleteFolder(File index) { - if (!index.isDirectory() || index.listFiles().length <= 0) { - return index.delete(); - } - for (File file : index.listFiles()) { - if (null != file && !deleteFolder(file)) { - return false; - } - } - return index.delete(); - } - @Test public void testSolidityArgs() { Assert.assertNotNull(Args.getInstance().getTrustNodeAddr()); @@ -69,7 +34,6 @@ public void testSolidityArgs() { @Test public void testSolidityGrpcCall() { - rpcApiService.init(Args.getInstance()); rpcApiService.start(); DatabaseGrpcClient databaseGrpcClient = null; String address = Args.getInstance().getTrustNodeAddr(); @@ -99,7 +63,8 @@ public void testSolidityGrpcCall() { @Test public void testSolidityNodeHttpApiService() { - solidityNodeHttpApiService.init(Args.getInstance()); + solidityNodeHttpApiService.start(); + // start again solidityNodeHttpApiService.start(); solidityNodeHttpApiService.stop(); Assert.assertTrue(true); diff --git a/framework/src/test/resources/config-localtest.conf b/framework/src/test/resources/config-localtest.conf index d7f573fe90e..4b5aa8af76e 100644 --- a/framework/src/test/resources/config-localtest.conf +++ b/framework/src/test/resources/config-localtest.conf @@ -116,14 +116,30 @@ node { ] http { + fullNodeEnable = false fullNodePort = 8090 + solidityEnable = false solidityPort = 8091 + PBFTEnable = false + PBFTPort = 8092 + } + + jsonrpc { + httpFullNodeEnable = false + httpFullNodePort = 8545 + httpSolidityEnable = false + httpSolidityPort = 8555 + httpPBFTEnable = false + httpPBFTPort = 8565 } rpc { + enable = false port = 50051 - # default value is 50061 - # solidityPort = 50061 + solidityEnable = false + solidityPort = 50061 + PBFTEnable = false + PBFTPort = 50071 # Number of gRPC thread, default availableProcessors / 2 # thread = 16 diff --git a/framework/src/test/resources/config-test-index.conf b/framework/src/test/resources/config-test-index.conf index 6eb3621e747..cff08fd3abb 100644 --- a/framework/src/test/resources/config-test-index.conf +++ b/framework/src/test/resources/config-test-index.conf @@ -76,9 +76,23 @@ node { version = 43 # 43: testnet; 101: debug } + http { + fullNodeEnable = false + solidityEnable = false + PBFTEnable = false + } + + jsonrpc { + httpFullNodeEnable = false + httpSolidityEnable = false + httpPBFTEnable = false + } + rpc { port = 50051 - + enable = false + solidityEnable = false + PBFTEnable = false # Number of gRPC thread, default availableProcessors / 2 # thread = 16 diff --git a/framework/src/test/resources/config-test-mainnet.conf b/framework/src/test/resources/config-test-mainnet.conf index ad72d2afaaa..de9170a0ec4 100644 --- a/framework/src/test/resources/config-test-mainnet.conf +++ b/framework/src/test/resources/config-test-mainnet.conf @@ -82,6 +82,24 @@ node { version = 43 # 43: testnet; 101: debug } + http { + fullNodeEnable = false + solidityEnable = false + PBFTEnable = false + } + + jsonrpc { + httpFullNodeEnable = false + httpSolidityEnable = false + httpPBFTEnable = false + } + + rpc { + enable = false + solidityEnable = false + PBFTEnable = false + } + } sync { diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index db24bb2a8a0..8aedcc2f2ec 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -106,10 +106,15 @@ node { } http { - fullNodeEnable = true - fullNodePort = 8090 - solidityEnable = true - solidityPort = 8091 + fullNodeEnable = false + solidityEnable = false + PBFTEnable = false + } + + jsonrpc { + httpFullNodeEnable = false + httpSolidityEnable = false + httpPBFTEnable = false } # use your ipv6 address for node discovery and tcp connection, default false @@ -171,6 +176,9 @@ node { } rpc { + enable = false + solidityEnable = false + PBFTEnable = false port = 50051 PBFTPort = 50072 diff --git a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java index 5cb8c26c4bc..7e403a95dda 100644 --- a/plugins/src/test/java/org/tron/plugins/DbLiteTest.java +++ b/plugins/src/test/java/org/tron/plugins/DbLiteTest.java @@ -76,6 +76,7 @@ public void init() throws IOException { // allow account root Args.getInstance().setAllowAccountStateRoot(1); Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort()); + Args.getInstance().setRpcEnable(true); databaseDir = Args.getInstance().getStorage().getDbDirectory(); // init dbBackupConfig to avoid NPE Args.getInstance().dbBackupConfig = DbBackupConfig.getInstance();