/// <inheritdoc />
        /// <summary>
        /// Mine process.
        /// </summary>
        /// <returns></returns>
        public async Task <BlockExecutedSet> MineAsync(Hash previousBlockHash, long previousBlockHeight,
                                                       Timestamp blockTime,
                                                       Duration blockExecutionTime)
        {
            var txList = new List <Transaction>();

            var chainContext = new ChainContext
            {
                BlockHash   = previousBlockHash,
                BlockHeight = previousBlockHeight
            };

            var limit = await _blockTransactionLimitProvider.GetLimitAsync(chainContext);

            if (_transactionPackingOptionProvider.IsTransactionPackable(chainContext))
            {
                var executableTransactionSet = await _transactionPoolService.GetExecutableTransactionSetAsync(
                    previousBlockHash, limit);

                txList.AddRange(executableTransactionSet.Transactions);
            }


            Logger.LogInformation(
                $"Start mining with previous hash: {previousBlockHash}, previous height: {previousBlockHeight}.");
            return(await _miningService.MineAsync(
                       new RequestMiningDto
            {
                PreviousBlockHash = previousBlockHash,
                PreviousBlockHeight = previousBlockHeight,
                BlockExecutionTime = blockExecutionTime,
                TransactionCountLimit = limit
            }, txList, blockTime));
        }
        public async Task TransactionPackingOption_Test()
        {
            await AddBlockStateSetAsync(_kernelTestHelper.BestBranchBlockList[9]);
            await AddBlockStateSetAsync(_kernelTestHelper.BestBranchBlockList[10]);
            await AddBlockStateSetAsync(_kernelTestHelper.ForkBranchBlockList[4]);

            var context = new ChainContext
            {
                BlockHash   = _kernelTestHelper.BestBranchBlockList[9].GetHash(),
                BlockHeight = _kernelTestHelper.BestBranchBlockList[9].Height
            };

            var isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);

            isTransactionPackable.ShouldBeTrue();

            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(context, false);

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);
            isTransactionPackable.ShouldBeFalse();

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(new ChainContext
            {
                BlockHash   = _kernelTestHelper.BestBranchBlockList[10].GetHash(),
                BlockHeight = _kernelTestHelper.BestBranchBlockList[10].Height
            });
            isTransactionPackable.ShouldBeFalse();

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(new ChainContext
            {
                BlockHash   = _kernelTestHelper.ForkBranchBlockList[4].GetHash(),
                BlockHeight = _kernelTestHelper.ForkBranchBlockList[4].Height
            });
            isTransactionPackable.ShouldBeTrue();

            await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(context, true);

            isTransactionPackable = _transactionPackingOptionProvider.IsTransactionPackable(context);
            isTransactionPackable.ShouldBeTrue();
        }
Beispiel #3
0
        public async Task <List <Transaction> > GenerateTransactionsAsync(Address from, long preBlockHeight,
                                                                          Hash preBlockHash)
        {
            var generatedTransactions = new List <Transaction>();
            var chainContext          = new ChainContext
            {
                BlockHash = preBlockHash, BlockHeight = preBlockHeight
            };

            if (!_transactionPackingOptionProvider.IsTransactionPackable(chainContext))
            {
                return(generatedTransactions);
            }

            var parliamentContractAddress = await _smartContractAddressService.GetAddressByContractNameAsync(
                chainContext, ParliamentSmartContractAddressNameProvider.StringName);

            if (parliamentContractAddress == null)
            {
                return(generatedTransactions);
            }

            var proposalIdList =
                await _proposalService.GetNotApprovedProposalIdListAsync(from, preBlockHash, preBlockHeight);

            if (proposalIdList == null || proposalIdList.Count == 0)
            {
                return(generatedTransactions);
            }

            var generatedTransaction = new Transaction
            {
                From           = from,
                MethodName     = nameof(ParliamentContractContainer.ParliamentContractStub.ApproveMultiProposals),
                To             = parliamentContractAddress,
                RefBlockNumber = preBlockHeight,
                RefBlockPrefix = BlockHelper.GetRefBlockPrefix(preBlockHash),
                Params         = new ProposalIdList
                {
                    ProposalIds = { proposalIdList }
                }.ToByteString()
            };

            generatedTransactions.Add(generatedTransaction);

            Logger.LogTrace("Proposal approval transaction generated.");

            return(generatedTransactions);
        }
        public async Task <ByteString> GetBlockHeaderExtraDataAsync(BlockHeader blockHeader)
        {
            if (blockHeader.Height == AElfConstants.GenesisBlockHeight)
            {
                return(ByteString.Empty);
            }

            if (!_transactionPackingOptionProvider.IsTransactionPackable(new ChainContext
            {
                BlockHash = blockHeader.PreviousBlockHash, BlockHeight = blockHeader.Height - 1
            }))
            {
                return(ByteString.Empty);
            }

            var bytes = await _crossChainIndexingDataService.PrepareExtraDataForNextMiningAsync(
                blockHeader.PreviousBlockHash, blockHeader.Height - 1);

            return(bytes);
        }
Beispiel #5
0
        private bool ValidateTransactionCount(IBlock block)
        {
            var chainContext = new ChainContext
            {
                BlockHash   = block.Header.PreviousBlockHash,
                BlockHeight = block.Header.Height - 1
            };

            if (_transactionPackingOptionProvider.IsTransactionPackable(chainContext))
            {
                return(true);
            }

            if (block.Body.TransactionsCount > _systemTransactionCount)
            {
                Logger.LogDebug("Cannot package normal transaction.");
                return(false);
            }

            return(true);
        }