Esempio n. 1
0
        /// <summary>
        /// Creates a new chain with the provided genesis transactions and Smart Contract Zero.
        /// </summary>
        /// <returns>The new chain async.</returns>
        /// <param name="">The new chain id which will be derived from the creator address.</param>
        /// <param name="genesisTransactions">The transactions to be executed in the genesis block.</param>
        public async Task <Chain> CreateNewChainAsync(IEnumerable <Transaction> genesisTransactions)
        {
            try
            {
                var blockHeader = new BlockHeader
                {
                    Height            = AElfConstants.GenesisBlockHeight,
                    PreviousBlockHash = Hash.Empty,
                    Time = new Timestamp {
                        Seconds = 0
                    },
                    ChainId = _blockchainService.GetChainId()
                };

                var transactions = genesisTransactions.ToList();

                var block = await _blockExecutingService.ExecuteBlockAsync(blockHeader, transactions);

                var chain = await _blockchainService.CreateChainAsync(block, transactions);

                await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, BlockAttachOperationStatus.LongestChainFound);

                return(await _blockchainService.GetChainAsync());
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Create new chain failed.");
                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new chain with the provided and Smart Contract Zero.
        /// </summary>
        /// <returns>The new chain async.</returns>
        /// <param name="">The new chain id which will be derived from the creator address.</param>
        /// <param name="genesisTransactions">The transactions to be executed in the genesis block.</param>
        public async Task <Chain> CreateNewChainAsync(IEnumerable <Transaction> genesisTransactions)
        {
            try
            {
                var blockHeader = new BlockHeader
                {
                    Height            = KernelConstants.GenesisBlockHeight,
                    PreviousBlockHash = Hash.Empty,
                    Time    = Timestamp.FromDateTime(DateTime.MinValue.ToUniversalTime()),
                    ChainId = _blockchainService.GetChainId()
                };

                var block = await _blockExecutingService.ExecuteBlockAsync(blockHeader, genesisTransactions);

                var chain = await _blockchainService.CreateChainAsync(block);

                await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, BlockAttachOperationStatus.LongestChainFound);

                return(await _blockchainService.GetChainAsync());
            }
            catch (Exception e)
            {
                Logger.LogError("CreateNewChainAsync Error: " + e);
                throw;
            }
        }
Esempio n. 3
0
        public async Task AttachBlockAsync(Block block)
        {
            var chain = await _blockchainService.GetChainAsync();

            var status = await _blockchainService.AttachBlockToChainAsync(chain, block);

            await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);
        }
Esempio n. 4
0
 public void ExecuteBlocksAttachedToLongestChainTest()
 {
     AsyncHelper.RunSync(async() =>
     {
         var chain = await _blockchainService.GetChainAsync();
         await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain,
                                                                               BlockAttachOperationStatus.LongestChainFound);
     });
     _counter.Increment();
 }
Esempio n. 5
0
        public async Task AttachBlockAsync(Block block)
        {
            var existBlock = await _blockchainService.GetBlockHeaderByHashAsync(block.GetHash());

            if (existBlock != null)
            {
                Logger.LogDebug($"Try attaching block but already exist, {block}");
                return;
            }
            if (!await _blockValidationService.ValidateBlockBeforeAttachAsync(block))
            {
                Logger.LogWarning($"Validate block failed (before attach to chain), {block}");
                return;
            }

            await _blockchainService.AddBlockAsync(block);

            var chain = await _blockchainService.GetChainAsync();

            var status = await _blockchainService.AttachBlockToChainAsync(chain, block);

            await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);
        }