Ejemplo n.º 1
0
        public bool Start()
        {
            if (string.IsNullOrWhiteSpace(ChainConfig.Instance.ChainId))
            {
                _logger?.Error("No chain id.");
                return(false);
            }

            _logger?.Info($"Chain Id = {ChainConfig.Instance.ChainId}");

            #region setup

            try
            {
                LogGenesisContractInfo();

                var curHash = BlockChain.GetCurrentBlockHashAsync().Result;

                var chainExists = curHash != null && !curHash.Equals(Hash.Genesis);

                if (!chainExists)
                {
                    // Creation of the chain if it doesn't already exist
                    CreateNewChain(TokenGenesisContractCode, ConsensusGenesisContractCode,
                                   BasicContractZero, SideChainGenesisContractZero);
                }
            }
            catch (Exception e)
            {
                _logger?.Error(e, $"Could not create the chain : {ChainConfig.Instance.ChainId}.");
            }

            #endregion setup

            #region start

            _txHub.Start();

            if (NodeConfig.Instance.IsMiner)
            {
                _miner.Init();
                _logger?.Debug($"Coinbase = {_miner.Coinbase.DumpHex()}");
            }

            Thread.Sleep(1000);

            if (NodeConfig.Instance.ConsensusInfoGenerator)
            {
                StartMining();
                // Start directly.
                _consensus?.Start();
            }

            MessageHub.Instance.Subscribe <BlockReceived>(async inBlock =>
            {
                await _blockSynchronizer.ReceiveBlock(inBlock.Block);
            });

            #endregion start

            MessageHub.Instance.Publish(new ChainInitialized(null));

            return(true);
        }