public async Task Mine_Test() { var chain = await _blockchainService.GetChainAsync(); Timestamp blockTime; BlockExecutedSet miningResult; { blockTime = TimestampHelper.GetUtcNow().AddSeconds(-4); miningResult = await _miningService.MineAsync(new RequestMiningDto { BlockExecutionTime = TimestampHelper.DurationFromSeconds(1), PreviousBlockHash = chain.BestChainHash, PreviousBlockHeight = chain.BestChainHeight, TransactionCountLimit = 100 }, _kernelTestHelper.GenerateTransactions(10), blockTime); await CheckMiningResultAsync(miningResult, blockTime, 0); } { blockTime = TimestampHelper.GetUtcNow().AddSeconds(4); miningResult = await _miningService.MineAsync(new RequestMiningDto { BlockExecutionTime = TimestampHelper.DurationFromMilliseconds(int.MaxValue), PreviousBlockHash = chain.BestChainHash, PreviousBlockHeight = chain.BestChainHeight, TransactionCountLimit = 100 }, _kernelTestHelper.GenerateTransactions(10), blockTime); await CheckMiningResultAsync(miningResult, blockTime, 10); } { blockTime = TimestampHelper.GetUtcNow(); miningResult = await _miningService.MineAsync(new RequestMiningDto { BlockExecutionTime = TimestampHelper.DurationFromSeconds(4), PreviousBlockHash = chain.BestChainHash, PreviousBlockHeight = chain.BestChainHeight, TransactionCountLimit = 100 }, _kernelTestHelper.GenerateTransactions(10), blockTime); await CheckMiningResultAsync(miningResult, blockTime, 10); } { blockTime = TimestampHelper.GetUtcNow(); miningResult = await _miningService.MineAsync(new RequestMiningDto { BlockExecutionTime = TimestampHelper.DurationFromSeconds(4), PreviousBlockHash = chain.BestChainHash, PreviousBlockHeight = chain.BestChainHeight, TransactionCountLimit = 5 }, _kernelTestHelper.GenerateTransactions(10), blockTime); await CheckMiningResultAsync(miningResult, blockTime, 4); } }
/// <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(_transactionPackingService.IsTransactionPackingEnabled() ?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)); }
private async Task <Block> ExecuteAsync(Transaction transaction, long previousBlockHeight, Hash previousBlockHash) { var transactionList = new List <Transaction>(); if (transaction != null) { transactionList.Add(transaction); } var block = await _miningService.MineAsync( new RequestMiningDto { PreviousBlockHash = previousBlockHash, PreviousBlockHeight = previousBlockHeight, BlockExecutionTime = TimestampHelper.DurationFromMilliseconds(int.MaxValue) }, transactionList, DateTime.UtcNow.ToTimestamp()); if (transaction != null) { await _blockchainService.AddTransactionsAsync(new List <Transaction> { transaction }); } await _blockchainService.AddBlockAsync(block); return(block); }
/// <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)); }
/// <inheritdoc /> /// <summary> /// Mine process. /// </summary> /// <returns></returns> public async Task <Block> MineAsync(Hash previousBlockHash, long previousBlockHeight, DateTime dateTime, TimeSpan timeSpan) { var executableTransactionSet = await _txHub.GetExecutableTransactionSetAsync(); 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}."); } return(await _miningService.MineAsync(previousBlockHash, previousBlockHeight, pending, dateTime, timeSpan)); }
private async Task <BlockExecutedSet> MineAsync(List <Transaction> txs, Timestamp blockTime, Hash preBlockHash, long preBlockHeight) { var blockExecutedSet = await _miningService.MineAsync( new RequestMiningDto { PreviousBlockHash = preBlockHash, PreviousBlockHeight = preBlockHeight, BlockExecutionTime = TimestampHelper.DurationFromMilliseconds(int.MaxValue), TransactionCountLimit = Int32.MaxValue }, txs, blockTime ?? DateTime.UtcNow.ToTimestamp()); var block = blockExecutedSet.Block; await _blockchainService.AddTransactionsAsync(txs); await _blockchainService.AddBlockAsync(block); await _blockAttachService.AttachBlockAsync(block); return(blockExecutedSet); }