示例#1
0
        /// <inheritdoc />
        /// <summary>
        /// Mine process.
        /// </summary>
        /// <returns></returns>
        public async Task <Block> MineAsync(Hash previousBlockHash, long previousBlockHeight, Timestamp blockTime,
                                            Duration blockExecutionTime)
        {
            var limit = await _blockTransactionLimitProvider.GetLimitAsync();

            var executableTransactionSet =
                await _txHub.GetExecutableTransactionSetAsync(_transactionInclusivenessProvider.IsTransactionPackable
                                                              ?limit
                                                              : -1);

            var pending = new List <Transaction>();

            if (executableTransactionSet.PreviousBlockHash == previousBlockHash)
            {
                pending = executableTransactionSet.Transactions;
            }
            else
            {
                Logger.LogWarning($"Transaction pool gives transactions to be appended to " +
                                  $"{executableTransactionSet.PreviousBlockHash} which doesn't match the current " +
                                  $"best chain hash {previousBlockHash}.");
            }

            Logger.LogTrace(
                $"Start mining with previous hash: {previousBlockHash}, previous height: {previousBlockHeight}.");
            return(await _miningService.MineAsync(
                       new RequestMiningDto
            {
                PreviousBlockHash = previousBlockHash,
                PreviousBlockHeight = previousBlockHeight,
                BlockExecutionTime = blockExecutionTime
            }, pending, blockTime));
        }
        /// <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));
        }
示例#3
0
        public async Task <bool> ValidateBlockBeforeExecuteAsync(IBlock block)
        {
            var txCountLimit = await _blockTransactionLimitProvider.GetLimitAsync(new ChainContext
            {
                BlockHash   = block.Header.PreviousBlockHash,
                BlockHeight = block.Header.Height - 1
            });

            return(Math.Max(txCountLimit, _systemTransactionCount) >= block.TransactionIds.Count());
        }
        public async Task ProcessConfigurationAsync_Test(int targetLimit, bool isSuccessful)
        {
            var blockTransactionLimitConfigurationProcessor = GetRequiredService <IConfigurationProcessor>();
            var configurationName = blockTransactionLimitConfigurationProcessor.ConfigurationName;
            var targetValue       = new Int32Value
            {
                Value = targetLimit
            }.ToByteString();
            var chain = await _blockchainService.GetChainAsync();

            var blockIndex = new BlockIndex
            {
                BlockHeight = chain.BestChainHeight,
                BlockHash   = chain.BestChainHash
            };
            await _configurationService.ProcessConfigurationAsync(configurationName, targetValue, blockIndex);

            await _blockchainService.SetIrreversibleBlockAsync(chain, chain.BestChainHeight, chain.BestChainHash);

            var getValue = await _blockTransactionLimitProvider.GetLimitAsync(blockIndex);

            getValue.ShouldBe(isSuccessful ? targetLimit : int.MaxValue);
        }
        public async Task LimitCanBeSetByExecutingContract_Test()
        {
            const int targetLimit = 55;

            await DeployContractsAsync();

            var proposalId = (await _parliamentContractStub.CreateProposal.SendAsync(new CreateProposalInput
            {
                ContractMethodName = "SetConfiguration",
                ExpiredTime = TimestampHelper.GetUtcNow().AddDays(1),
                Params = new SetConfigurationInput
                {
                    Key = BlockTransactionLimitConfigurationNameProvider.Name,
                    Value = new Int32Value {
                        Value = targetLimit
                    }.ToByteString()
                }.ToByteString(),
                ToAddress = ConfigurationContractAddress,
                OrganizationAddress = await _parliamentContractStub.GetDefaultOrganizationAddress.CallAsync(new Empty())
            })).Output;
            await _parliamentContractStub.Approve.SendAsync(proposalId);

            // Before
            {
                var result = await _configurationStub.GetConfiguration.CallAsync(new StringValue
                {
                    Value = BlockTransactionLimitConfigurationNameProvider.Name
                });

                var limit = new Int32Value();
                limit.MergeFrom(BytesValue.Parser.ParseFrom(result.ToByteString()).Value);
                Assert.Equal(0, limit.Value);
            }

            var txResult = await _parliamentContractStub.Release.SendAsync(proposalId);

            var configurationSet = ConfigurationSet.Parser.ParseFrom(txResult.TransactionResult.Logs
                                                                     .First(l => l.Name == nameof(ConfigurationSet)).NonIndexed);
            var limitFromLogEvent = new Int32Value();

            limitFromLogEvent.MergeFrom(configurationSet.Value.ToByteArray());
            Assert.Equal(limitFromLogEvent.Value, targetLimit);

            // After
            {
                var result = await _configurationStub.GetConfiguration.CallAsync(new StringValue
                {
                    Value = BlockTransactionLimitConfigurationNameProvider.Name
                });

                var limit = new Int32Value();
                limit.MergeFrom(BytesValue.Parser.ParseFrom(result.ToByteString()).Value);
                Assert.Equal(targetLimit, limit.Value);
            }
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainStateService.MergeBlockStateAsync(chain.BestChainHeight, chain.BestChainHash);

            var limitNum = await _blockTransactionLimitProvider.GetLimitAsync(
                new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            });

            Assert.Equal(55, limitNum);
        }