|
| 1 | +package org.unichain.program; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.LoggerContext; |
| 4 | +import ch.qos.logback.classic.joran.JoranConfigurator; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 8 | +import org.unichain.common.application.Application; |
| 9 | +import org.unichain.common.application.ApplicationFactory; |
| 10 | +import org.unichain.common.application.UnichainApplicationContext; |
| 11 | +import org.unichain.core.Constant; |
| 12 | +import org.unichain.core.config.DefaultConfig; |
| 13 | +import org.unichain.core.config.args.Args; |
| 14 | +import org.unichain.core.services.RpcApiService; |
| 15 | +import org.unichain.core.services.WitnessService; |
| 16 | +import org.unichain.core.services.http.fullnode.FullNodeHttpApiService; |
| 17 | +import org.unichain.core.services.interfaceOnSolidity.HttpApiOnSolidityService; |
| 18 | +import org.unichain.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | + |
| 22 | +@Slf4j(topic = "app") |
| 23 | +public class ExplorerNode { |
| 24 | + public static void load(String path) { |
| 25 | + try { |
| 26 | + File file = new File(path); |
| 27 | + if (!file.exists() || !file.isFile() || !file.canRead()) { |
| 28 | + return; |
| 29 | + } |
| 30 | + LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 31 | + JoranConfigurator configurator = new JoranConfigurator(); |
| 32 | + configurator.setContext(lc); |
| 33 | + lc.reset(); |
| 34 | + configurator.doConfigure(file); |
| 35 | + } catch (Exception e) { |
| 36 | + logger.error(e.getMessage()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Start the Explorer Node |
| 42 | + */ |
| 43 | + public static void main(String[] args) { |
| 44 | + logger.info("Explorer node running."); |
| 45 | + Args.setParam(args, Constant.TESTNET_CONF); |
| 46 | + Args cfgArgs = Args.getInstance(); |
| 47 | + |
| 48 | + //for explorer node |
| 49 | + cfgArgs.setExplorerNode(true); |
| 50 | + |
| 51 | + load(cfgArgs.getLogbackPath()); |
| 52 | + |
| 53 | + if (cfgArgs.isHelp()) { |
| 54 | + logger.info("Here is the help message."); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (Args.getInstance().isDebug()) { |
| 59 | + logger.info("In debug mode, it won't check energy time"); |
| 60 | + } else { |
| 61 | + logger.info("Not in debug mode, it will check energy time"); |
| 62 | + } |
| 63 | + |
| 64 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 65 | + beanFactory.setAllowCircularReferences(false); |
| 66 | + UnichainApplicationContext context = new UnichainApplicationContext(beanFactory); |
| 67 | + context.register(DefaultConfig.class); |
| 68 | + |
| 69 | + context.refresh(); |
| 70 | + Application appT = ApplicationFactory.create(context); |
| 71 | + shutdown(appT); |
| 72 | + |
| 73 | + // snapshot http api |
| 74 | + FullNodeHttpApiService httpApiService = context.getBean(FullNodeHttpApiService.class); |
| 75 | + appT.addService(httpApiService); |
| 76 | + |
| 77 | + // solidity http api |
| 78 | + if (Args.getInstance().getStorage().getDbVersion() == 2) { |
| 79 | + HttpApiOnSolidityService httpApiOnSolidityService = context.getBean(HttpApiOnSolidityService.class); |
| 80 | + appT.addService(httpApiOnSolidityService); |
| 81 | + } |
| 82 | + |
| 83 | + appT.initServices(cfgArgs); |
| 84 | + appT.startServices(); |
| 85 | + appT.startup(); |
| 86 | + |
| 87 | + System.out.println(" _ _ _ __ (_) ___| |__ __ _(_)_ __ "); |
| 88 | + System.out.println(" | | | | \'_ \\| |/ __| \'_ \\ / _` | | \'_ \\ "); |
| 89 | + System.out.println(" | |_| | | | | | (__| | | | (_| | | | | |"); |
| 90 | + System.out.println(" \\__,_|_| |_|_|\\___|_| |_|\\__,_|_|_| |_|"); |
| 91 | + } |
| 92 | + |
| 93 | + public static void shutdown(final Application app) { |
| 94 | + logger.info("********register application shutdown hook********"); |
| 95 | + Runtime.getRuntime().addShutdownHook(new Thread(app::shutdown)); |
| 96 | + } |
| 97 | +} |
0 commit comments