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 95a1eb2d0ae..9c5d3351e05 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 2cd9ea95f15..93bc5dca917 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -118,15 +118,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..cb19fcae8c2 --- /dev/null +++ b/framework/src/main/java/org/tron/common/application/AbstractService.java @@ -0,0 +1,71 @@ +package org.tron.common.application; + +import com.google.common.base.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j(topic = "service") +public abstract class AbstractService implements Service { + + protected int port; + @Getter + protected boolean enable; + protected final String name = this.getClass().getSimpleName(); + protected final AtomicBoolean started = new AtomicBoolean(false); + protected final AtomicBoolean stopped = new AtomicBoolean(false); + + + @Override + public void start() { + if (started.compareAndSet(false, true)) { + try { + innerStart(); + logger.info("{} started, listening on {}", name, port); + } catch (Exception e) { + logger.error("{}", name, e); + } + } else { + logger.error("Attempted to start already running {}.", name); + } + } + + @Override + public void stop() { + if (started.get() && stopped.compareAndSet(false, true)) { + logger.info("{} shutdown...", name); + try { + innerStop(); + } catch (Exception e) { + logger.warn("{}", name, e); + } + logger.info("{} shutdown complete", name); + } else { + logger.error("Attempted to stop already stopped {}.", name); + } + + } + + @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; + +} 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..b4e248b2c02 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,6 +74,7 @@ public void shutdown() { tronNetService.close(); } dbManager.close(); + shutdown.countDown(); } @Override @@ -80,7 +84,12 @@ public void startServices() { @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..f9eef50461d 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,25 @@ package org.tron.common.application; -import com.google.common.base.Objects; import lombok.extern.slf4j.Slf4j; import org.eclipse.jetty.server.Server; @Slf4j(topic = "rpc") -public abstract class HttpService implements Service { +public abstract class HttpService extends AbstractService { protected Server apiServer; - protected int port; @Override - public void blockUntilShutdown() { + public void innerStart() throws Exception { if (apiServer != null) { - try { - apiServer.join(); - } catch (InterruptedException e) { - logger.warn("{}", e.getMessage()); - Thread.currentThread().interrupt(); - } + apiServer.start(); } } @Override - public void start() { + public void innerStop() throws Exception { if (apiServer != null) { - try { - apiServer.start(); - logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); - } catch (Exception e) { - logger.error("{}", this.getClass().getSimpleName(), e); - } + 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()); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - 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); - } } 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..129348f12e7 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,27 @@ package org.tron.common.application; -import com.google.common.base.Objects; import io.grpc.Server; -import java.io.IOException; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; @Slf4j(topic = "rpc") -public abstract class RpcService implements Service { +public abstract class RpcService extends AbstractService { protected Server apiServer; - protected int port; @Override - public void blockUntilShutdown() { + public void innerStart() throws Exception { if (apiServer != null) { - try { - apiServer.awaitTermination(); - } catch (InterruptedException e) { - logger.warn("{}", e.getMessage()); - Thread.currentThread().interrupt(); - } + apiServer.start(); } } @Override - public void start() { + public void innerStop() throws Exception { if (apiServer != null) { - try { - apiServer.start(); - logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); - } catch (IOException e) { - logger.error("{}", this.getClass().getSimpleName(), e); - } + 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()); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - RpcService that = (RpcService) o; - return port == that.port; - } - - @Override - public int hashCode() { - return Objects.hashCode(getClass().getSimpleName(), port); - } - } 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..7255391108c 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,11 @@ package org.tron.common.application; -import org.tron.common.parameter.CommonParameter; - public interface Service { - void init(); - - void init(CommonParameter parameter); - - /** - * Start the service. - * {@link Service#init(CommonParameter parameter) init(CommonParameter parameter)} must be called - * before this method. - */ void start(); void stop(); - void blockUntilShutdown(); + boolean isEnable(); } 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..bd00a2584af 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,41 @@ package org.tron.common.application; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Set; +import java.util.List; +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(Service::start); 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(Service::stop); logger.info("All api services stopped."); } - - public void blockUntilShutdown() { - this.services.stream().findFirst().ifPresent(Service::blockUntilShutdown); - } } 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 a8547b73948..e3ec84640be 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; @@ -447,6 +451,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); } @@ -455,6 +471,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..dabe50e1032 100755 --- a/framework/src/main/java/org/tron/core/services/RpcApiService.java +++ b/framework/src/main/java/org/tron/core/services/RpcApiService.java @@ -12,6 +12,7 @@ import io.grpc.stub.StreamObserver; import java.util.Objects; import java.util.concurrent.TimeUnit; +import javax.annotation.PostConstruct; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -199,14 +200,9 @@ public class RpcApiService extends RpcService { 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(); } @Override 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..37939c7b834 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 @@ -13,7 +13,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,13 +293,9 @@ 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 = Args.getInstance().isFullNodeHttpEnable(); } @Override 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..286ead4322d 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 @@ -11,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.http.EstimateEnergyServlet; @@ -166,13 +165,9 @@ 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 = Args.getInstance().isSolidityNodeHttpEnable(); } @Override 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..a26ffa5e4ff 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 @@ -17,13 +17,9 @@ public class JsonRpcServiceOnPBFT extends HttpService { @Autowired private JsonRpcOnPBFTServlet jsonRpcOnPBFTServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public JsonRpcServiceOnPBFT() { port = CommonParameter.getInstance().getJsonRpcHttpPBFTPort(); + enable = CommonParameter.getInstance().isJsonRpcHttpPBFTNodeEnable(); } @Override 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..46c633cfd63 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 @@ -17,13 +17,9 @@ public class JsonRpcServiceOnSolidity extends HttpService { @Autowired private JsonRpcOnSolidityServlet jsonRpcOnSolidityServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public JsonRpcServiceOnSolidity() { port = CommonParameter.getInstance().getJsonRpcHttpSolidityPort(); + enable = CommonParameter.getInstance().isJsonRpcHttpSolidityNodeEnable(); } @Override 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..45e5fde9440 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 @@ -81,13 +81,9 @@ public class RpcApiServiceOnPBFT extends RpcService { private final String executorName = "rpc-pbft-executor"; - @Override - public void init() { - } - - @Override - public void init(CommonParameter parameter) { + public RpcApiServiceOnPBFT() { port = Args.getInstance().getRpcOnPBFTPort(); + enable = Args.getInstance().isRpcPBFTEnable(); } @Override 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..38566482769 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 @@ -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; @@ -172,14 +171,9 @@ 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 = Args.getInstance().isPBFTHttpEnable(); } @Override 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..037b47e740c 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 @@ -83,13 +83,9 @@ public class RpcApiServiceOnSolidity extends RpcService { private final String executorName = "rpc-solidity-executor"; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public RpcApiServiceOnSolidity() { port = Args.getInstance().getRpcOnSolidityPort(); + enable = Args.getInstance().isRpcSolidityEnable(); } @Override 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..e5347fb7874 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,14 +177,9 @@ 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 = Args.getInstance().isSolidityNodeHttpEnable(); } @Override 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..d6e7d94e647 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 @@ -22,13 +22,9 @@ public class FullNodeJsonRpcHttpService extends HttpService { @Autowired private JsonRpcServlet jsonRpcServlet; - @Override - public void init() { - } - - @Override - public void init(CommonParameter args) { + public FullNodeJsonRpcHttpService() { port = CommonParameter.getInstance().getJsonRpcHttpFullNodePort(); + enable = CommonParameter.getInstance().isJsonRpcHttpFullNodeEnable(); } @Override 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..fd5f7d56ef5 100644 --- a/framework/src/main/java/org/tron/program/SolidityNode.java +++ b/framework/src/main/java/org/tron/program/SolidityNode.java @@ -6,9 +6,7 @@ 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.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 +19,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") @@ -82,14 +78,6 @@ public static void main(String[] args) { Metrics.init(); 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); - } - SolidityNode node = new SolidityNode(appT.getDbManager()); node.start(); appT.startup(); 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/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/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);