public async Task <Block> MineAsync(RequestMiningDto requestMiningDto, List <Transaction> transactions, Timestamp blockTime) { using (var cts = new CancellationTokenSource()) { cts.CancelAfter((int)requestMiningDto.BlockExecutionTime.Milliseconds()); var block = await GenerateBlock(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight, blockTime); var systemTransactions = await GenerateSystemTransactions(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight); var pending = transactions; block = await _blockExecutingService.ExecuteBlockAsync(block.Header, systemTransactions, pending, cts.Token); await SignBlockAsync(block); Logger.LogInformation($"Generated block: {block.ToDiagnosticString()}, " + $"previous: {block.Header.PreviousBlockHash}, " + $"transactions: {block.Body.TransactionsCount}"); return(block); } }
public async Task <BlockExecutedSet> MineAsync(RequestMiningDto requestMiningDto, List <Transaction> transactions, Timestamp blockTime) { try { using (var cts = new CancellationTokenSource()) { var expirationTime = blockTime + requestMiningDto.BlockExecutionTime; if (expirationTime < TimestampHelper.GetUtcNow()) { cts.Cancel(); } else { var ts = (expirationTime - TimestampHelper.GetUtcNow()).ToTimeSpan(); if (ts.TotalMilliseconds > int.MaxValue) { ts = TimeSpan.FromMilliseconds(int.MaxValue); } cts.CancelAfter(ts); } var block = await GenerateBlock(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight, blockTime); var systemTransactions = await GenerateSystemTransactions(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight); _systemTransactionExtraDataProvider.SetSystemTransactionCount(systemTransactions.Count, block.Header); var txTotalCount = transactions.Count + systemTransactions.Count; var pending = txTotalCount > requestMiningDto.TransactionCountLimit ? transactions .Take(requestMiningDto.TransactionCountLimit - systemTransactions.Count) .ToList() : transactions; var blockExecutedSet = await _blockExecutingService.ExecuteBlockAsync(block.Header, systemTransactions, pending, cts.Token); block = blockExecutedSet.Block; await SignBlockAsync(block); Logger.LogInformation($"Generated block: {block.ToDiagnosticString()}, " + $"previous: {block.Header.PreviousBlockHash}, " + $"executed transactions: {block.Body.TransactionsCount}, " + $"not executed transactions {pending.Count + systemTransactions.Count - block.Body.TransactionsCount} "); return(blockExecutedSet); } } catch (Exception e) { Logger.LogError(e, "Failed while mining block."); throw; } }
public async Task <Block> MineAsync(RequestMiningDto requestMiningDto, List <Transaction> transactions, Timestamp blockTime) { using (var cts = new CancellationTokenSource()) { var expirationTime = blockTime + requestMiningDto.BlockExecutionTime; if (expirationTime < TimestampHelper.GetUtcNow()) { cts.Cancel(); } else { var ts = (expirationTime - TimestampHelper.GetUtcNow()).ToTimeSpan(); if (ts.TotalMilliseconds > 4000) { ts = TimeSpan.FromMilliseconds(4000); } cts.CancelAfter(ts); } var block = await GenerateBlock(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight, blockTime); var systemTransactions = await GenerateSystemTransactions(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight); var pending = transactions; block = await _blockExecutingService.ExecuteBlockAsync(block.Header, systemTransactions, pending, cts.Token); await SignBlockAsync(block); Logger.LogInformation($"Generated block: {block.ToDiagnosticString()}, " + $"previous: {block.Header.PreviousBlockHash}, " + $"executed transactions: {block.Body.TransactionsCount}, " + $"not executed transactions {transactions.Count + systemTransactions.Count - block.Body.TransactionsCount}"); return(block); } }