Search in sources :

Example 1 with IAionChain

use of org.aion.zero.impl.blockchain.IAionChain in project aion by aionnetwork.

the class Aion method main.

public static void main(String[] args) throws InterruptedException {
    /*
         * @ATTENTION: ECKey have two layer: tx layer is KeyFac optional,
         *             network layer is hardcode to secp256.
         */
    ECKeyFac.setType(ED25519);
    HashUtil.setType(BLAKE2B_256);
    ServiceLoader.load(EventMgrModule.class);
    CfgAion cfg = CfgAion.inst();
    if (args != null && args.length > 0) {
        int ret = new Cli().call(args, cfg);
        System.exit(ret);
    }
    /*
         * if in the config.xml id is set as default [NODE-ID-PLACEHOLDER]
         * return true which means should save back to xml config
         */
    if (cfg.fromXML())
        cfg.toXML(new String[] { "--id=" + cfg.getId() });
    try {
        ServiceLoader.load(AionLoggerFactory.class);
    } catch (Exception e) {
        System.out.println("load AionLoggerFactory service fail!" + e.toString());
        throw e;
    }
    // If commit this out, the config setting will be ignore. all log module been set to "INFO" Level
    AionLoggerFactory.init(cfg.getLog().getModules());
    Logger LOG = AionLoggerFactory.getLogger(LogEnum.GEN.toString());
    System.out.println("                     _____                  \n" + "      .'.       |  .~     ~.  |..          |\n" + "    .'   `.     | |         | |  ``..      |\n" + "  .''''''''`.   | |         | |      ``..  |\n" + ".'           `. |  `._____.'  |          ``|\n\n" + "                    NETWORK  v" + KERNEL_VERSION + "\n\n");
    IAionChain ac = AionFactory.create();
    IMineRunner nm = ac.getBlockMiner();
    if (nm != null) {
        nm.delayedStartMining(10);
    }
    /*
         * Start Threads.
         */
    Thread zmqThread = null;
    ProtocolProcessor processor = null;
    if (cfg.getApi().getZmq().getActive()) {
        IHdlr handler = new HdlrZmq(new ApiAion0(ac));
        processor = new ProtocolProcessor(handler, cfg.getApi().getZmq());
        ProtocolProcessor finalProcessor = processor;
        zmqThread = new Thread(() -> {
            finalProcessor.run();
        }, "zmq-api");
        zmqThread.start();
    }
    HttpServer.start();
    /*
         * This is a hack, but used to let us pass zmqThread into thread
         * Shutdown hook for Ctrl+C
         */
    class ShutdownThreadHolder {

        final Thread zmqThread;

        final IMineRunner miner;

        final ProtocolProcessor pp;

        private ShutdownThreadHolder(Thread zmqThread, IMineRunner nm, ProtocolProcessor pp) {
            this.zmqThread = zmqThread;
            this.miner = nm;
            this.pp = pp;
        }
    }
    ShutdownThreadHolder holder = new ShutdownThreadHolder(zmqThread, nm, processor);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        LOG.info("Starting shutdown process...");
        HttpServer.shutdown();
        if (holder.pp != null) {
            LOG.info("Shutting down zmq ProtocolProcessor");
            try {
                holder.pp.shutdown();
                LOG.info("Shutdown zmq ProtocolProcessor... Done!");
            } catch (InterruptedException e) {
                LOG.info("Shutdown zmq ProtocolProcessor failed! {}", e.getMessage());
            }
        }
        if (holder.miner != null) {
            LOG.info("Shutting down sealer");
            holder.miner.stopMining();
            holder.miner.shutdown();
            LOG.info("Shutdown sealer... Done!");
        }
        // TODO : HTTPServer shutdown
        LOG.info("Shutting down the AionHub...");
        ac.getAionHub().close();
    }, "Shutdown"));
}
Also used : Cli(org.aion.zero.impl.cli.Cli) IAionChain(org.aion.zero.impl.blockchain.IAionChain) IMineRunner(org.aion.mcf.mine.IMineRunner) Logger(org.slf4j.Logger) IHdlr(org.aion.api.server.pb.IHdlr) HdlrZmq(org.aion.api.server.zmq.HdlrZmq) CfgAion(org.aion.zero.impl.config.CfgAion) ApiAion0(org.aion.api.server.pb.ApiAion0) ProtocolProcessor(org.aion.api.server.zmq.ProtocolProcessor)

Aggregations

ApiAion0 (org.aion.api.server.pb.ApiAion0)1 IHdlr (org.aion.api.server.pb.IHdlr)1 HdlrZmq (org.aion.api.server.zmq.HdlrZmq)1 ProtocolProcessor (org.aion.api.server.zmq.ProtocolProcessor)1 IMineRunner (org.aion.mcf.mine.IMineRunner)1 IAionChain (org.aion.zero.impl.blockchain.IAionChain)1 Cli (org.aion.zero.impl.cli.Cli)1 CfgAion (org.aion.zero.impl.config.CfgAion)1 Logger (org.slf4j.Logger)1