public async Task <Chain> CreateChainAsync()
        {
            _defaultContractZeroCodeProvider.SetDefaultContractZeroRegistrationByType(null);
            var blockHeader = new BlockHeader
            {
                Height            = AElfConstants.GenesisBlockHeight,
                PreviousBlockHash = Hash.Empty,
                Time = new Timestamp {
                    Seconds = 0
                }
            };
            var transactions = new List <Transaction>
            {
                new Transaction
                {
                    From       = _defaultContractZeroCodeProvider.ContractZeroAddress,
                    To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                    MethodName = nameof(ACS0Container.ACS0Stub.DeploySystemSmartContract),
                    Params     = new SystemContractDeploymentInput
                    {
                        Name     = ZeroSmartContractAddressNameProvider.Name,
                        Category = KernelConstants.DefaultRunnerCategory,
                        Code     = ByteString.CopyFrom(ContractCodes["AElf.Contracts.Genesis"]),
                    }.ToByteString()
                },
                new Transaction
                {
                    From       = _defaultContractZeroCodeProvider.ContractZeroAddress,
                    To         = _defaultContractZeroCodeProvider.ContractZeroAddress,
                    MethodName = nameof(ACS0Container.ACS0Stub.DeploySystemSmartContract),
                    Params     = new SystemContractDeploymentInput
                    {
                        Name     = ConfigurationSmartContractAddressNameProvider.Name,
                        Category = KernelConstants.DefaultRunnerCategory,
                        Code     = ByteString.CopyFrom(ContractCodes["AElf.Contracts.Configuration"])
                    }.ToByteString()
                }
            };

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

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

            var blockExecutionResult = await _blockchainExecutingService.ExecuteBlocksAsync(new[] { block.Block });

            await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, blockExecutionResult);

            return(await _blockchainService.GetChainAsync());
        }
        /// <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.Block, transactions);

                var blockExecutionResult = await _blockchainExecutingService.ExecuteBlocksAsync(new[] { block.Block });

                await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, blockExecutionResult);

                return(await _blockchainService.GetChainAsync());
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Create new chain failed.");
                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task AttachBlockAsync(Block block)
        {
            var chain = await _blockchainService.GetChainAsync();

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

            if (!status.HasFlag(BlockAttachOperationStatus.LongestChainFound))
            {
                Logger.LogDebug($"Try to attach to chain but the status is {status}.");
                return;
            }

            var notExecutedChainBlockLinks =
                await _chainBlockLinkService.GetNotExecutedChainBlockLinksAsync(chain.LongestChainHash);

            var notExecutedBlocks =
                await _blockchainService.GetBlocksAsync(notExecutedChainBlockLinks.Select(l => l.BlockHash));

            var executionResult = new BlockExecutionResult();

            try
            {
                executionResult = await _blockchainExecutingService.ExecuteBlocksAsync(notExecutedBlocks);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Block execute fails.");
                throw;
            }
            finally
            {
                await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, executionResult);
            }
        }
        public async Task Process_LessThanBestChainHeight_BlockExecutionResult()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.RemoveLongestBranchAsync(chain);

            var block = _kernelTestHelper.GenerateBlock(1, Hash.Empty);
            await _blockchainService.AttachBlockToChainAsync(chain, block);

            var executionResult = new BlockExecutionResult
            {
                SuccessBlockExecutedSets = { new BlockExecutedSet {
                                                 Block = block
                                             } }
            };
            await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, executionResult);

            chain = await _blockchainService.GetChainAsync();

            chain.LongestChainHash.ShouldBe(chain.BestChainHash);
            chain.LongestChainHeight.ShouldBe(chain.BestChainHeight);
        }