public async Task Mine_Test() { var chain = await _blockchainService.GetChainAsync(); var blockStateSet = new BlockStateSet { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight, }; await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet); var transactions = _kernelTestHelper.GenerateTransactions(5, chain.BestChainHeight, chain.BestChainHash); await _transactionPoolService.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash, chain.BestChainHeight); await _transactionPoolService.AddTransactionsAsync(transactions); await Task.Delay(200); await _transactionPoolService.UpdateTransactionPoolByBestChainAsync(chain.BestChainHash, chain.BestChainHeight); { await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(new BlockIndex { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight }, false); var blockTime = TimestampHelper.GetUtcNow(); var result = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, blockTime, TimestampHelper.DurationFromSeconds(4)); await CheckMiningResultAsync(result, blockTime, 0); } { await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(new BlockIndex { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight }, true); await _blockTransactionLimitProvider.SetLimitAsync(new BlockIndex { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight }, 3); var blockTime = TimestampHelper.GetUtcNow(); var result = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, blockTime, TimestampHelper.DurationFromSeconds(4)); await CheckMiningResultAsync(result, blockTime, 2); } }
public async Task WrongParallelTest() { var chain = await _blockchainService.GetChainAsync(); await _blockchainService.SetIrreversibleBlockAsync(chain, chain.BestChainHeight, chain.BestChainHash); var transactions = _parallelTestHelper.GenerateBasicFunctionWithParallelTransactions(_groupCount, _transactionCount); await _parallelTestHelper.BroadcastTransactions(transactions); var poolSize = await _txHub.GetTransactionPoolSizeAsync(); poolSize.ShouldBe(transactions.Count); var groupedTransactions = await _grouper.GroupAsync( new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight }, transactions); groupedTransactions.Parallelizables.Count.ShouldBe(_transactionCount); groupedTransactions.NonParallelizables.Count.ShouldBe(0); var block = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4)); block.TransactionIds.Count().ShouldBe(_groupCount); await _blockchainService.AddBlockAsync(block); await _blockAttachService.AttachBlockAsync(block); var codeRemarks = await _codeRemarksManager.GetCodeRemarksAsync( Hash.FromRawBytes(_parallelTestHelper.BasicFunctionWithParallelContractCode)); codeRemarks.ShouldNotBeNull(); codeRemarks.NonParallelizable.ShouldBeTrue(); groupedTransactions = await _grouper.GroupAsync(new ChainContext { BlockHash = block.GetHash(), BlockHeight = block.Height }, transactions); groupedTransactions.Parallelizables.Count.ShouldBe(0); groupedTransactions.NonParallelizables.Count.ShouldBe(_transactionCount); poolSize = await _txHub.GetTransactionPoolSizeAsync(); poolSize.ShouldBe(transactions.Count - block.TransactionIds.Count()); block = await _minerService.MineAsync(block.GetHash(), block.Height, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4)); block.TransactionIds.Count().ShouldBe(poolSize); }
public async Task IterationSetup() { var chain = await _blockchainService.GetChainAsync(); var tokenAmount = TransactionCount / GroupCount; (_prepareTransactions, _keyPairs) = await _osTestHelper.PrepareTokenForParallel(GroupCount, tokenAmount); _block = _osTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, _prepareTransactions); await _blockExecutingService.ExecuteBlockAsync(_block.Header, _prepareTransactions); await _osTestHelper.BroadcastTransactions(_prepareTransactions); _block = (await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4))).Block; _systemTransactions = await _osTestHelper.GenerateTransferTransactions(1); _cancellableTransactions = await _osTestHelper.GenerateTransactionsWithoutConflictAsync(_keyPairs, tokenAmount); chain = await _blockchainService.GetChainAsync(); _block = _osTestHelper.GenerateBlock(chain.BestChainHash, chain.BestChainHeight, _systemTransactions.Concat(_cancellableTransactions)); }
public void MineTest() { AsyncHelper.RunSync(async() => { var chain = await _blockchainService.GetChainAsync(); _block = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, DateTime.UtcNow, TimeSpan.FromMilliseconds(4000)); }); _counter.Increment(); }
public async Task <BlockHeader> CreateTemplateCacheAsync(Hash previousBlockHash, long previousBlockHeight, Timestamp blockTime, Duration blockExecutionTime) { _blockCache = await _minerService.MineAsync(previousBlockHash, previousBlockHeight, blockTime, blockExecutionTime); _blockCache.Header.Signature = ByteString.Empty; return(_blockCache.Header); }
public void MineTest() { AsyncHelper.RunSync(async() => { var chain = await _blockchainService.GetChainAsync(); _block = await _minerService.MineAsync(chain.BestChainHash, chain.BestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4)); }); _counter.Increment(); }
public async Task MinAsync_Success_Test() { var chain = await _chainService.GetChainAsync(); var hash = chain.BestChainHash; var height = chain.BestChainHeight; var block = await _minerService.MineAsync(hash, height, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMinutes(1)); block.ShouldNotBeNull(); block.Header.Signature.ShouldNotBeNull(); }
public async Task MinAsync_Success() { var chain = await _chainService.GetChainAsync(); var hash = chain.BestChainHash; var height = chain.BestChainHeight; var block = await _minerService.MineAsync(hash, height, DateTime.UtcNow, TimeSpan.FromMinutes(1)); block.ShouldNotBeNull(); block.Body.TransactionList.Count.ShouldBe(1); block.Header.Signature.ShouldNotBeNull(); }
public async Task <Block> RequestMiningAsync(ConsensusRequestMiningDto requestMiningDto) { if (!ValidateBlockMiningTime(requestMiningDto.BlockTime, requestMiningDto.MiningDueTime, requestMiningDto.BlockExecutionTime)) { return(null); } var blockExecutionDuration = CalculateBlockMiningDuration(requestMiningDto.BlockTime, requestMiningDto.BlockExecutionTime); var block = (await _minerService.MineAsync(requestMiningDto.PreviousBlockHash, requestMiningDto.PreviousBlockHeight, requestMiningDto.BlockTime, blockExecutionDuration)).Block; return(block); }
public async Task HandleEventAsync(ConsensusRequestMiningEventData eventData) { try { var block = await _minerService.MineAsync(eventData.PreviousBlockHash, eventData.PreviousBlockHeight, eventData.BlockTime, eventData.TimeSpan); //TODO: Before attach block should add block signature verify _taskQueueManager.Enqueue(async() => await _blockAttachService.AttachBlockAsync(block), KernelConsts.UpdateChainQueueName); } catch (Exception e) { Logger.LogError(e.ToString()); throw; } }
public async Task <Block> MinedOneBlock(Hash previousBlockHash = null, long previousBlockHeight = 0) { if (previousBlockHash == null || previousBlockHeight == 0) { var chain = await _blockchainService.GetChainAsync(); previousBlockHash = chain.BestChainHash; previousBlockHeight = chain.BestChainHeight; } var block = await _minerService.MineAsync(previousBlockHash, previousBlockHeight, DateTime.UtcNow, TimeSpan.FromMilliseconds(4000)); await _blockAttachService.AttachBlockAsync(block); return(block); }
public async Task <Block> MinedOneBlock(Hash previousBlockHash = null, long previousBlockHeight = 0) { if (previousBlockHash == null || previousBlockHeight == 0) { var chain = await _blockchainService.GetChainAsync(); previousBlockHash = chain.BestChainHash; previousBlockHeight = chain.BestChainHeight; } var block = (await _minerService.MineAsync(previousBlockHash, previousBlockHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMilliseconds(4000))).Block; await _blockchainService.AddBlockAsync(block); await _blockAttachService.AttachBlockAsync(block); return(block); }
public async Task MineWithTxHubAsync() { var txCount = 0; var preBlockHash = _chain.BestChainHash; var preBlockHeight = _chain.BestChainHeight; while (txCount < TransactionCount) { var blockExecutedSet = await _minerService.MineAsync(preBlockHash, preBlockHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMilliseconds(4000)); txCount += blockExecutedSet.TransactionIds.Count(); _transactionIdList.AddRange(blockExecutedSet.TransactionIds.ToList()); await BlockchainService.SetBestChainAsync(_chain, preBlockHeight, preBlockHash); await TransactionPoolService.CleanByTransactionIdsAsync(blockExecutedSet.TransactionIds); await TransactionPoolService.UpdateTransactionPoolByBestChainAsync(preBlockHash, preBlockHeight); } }
public async Task HandleEventAsync(ConsensusRequestMiningEventData eventData) { try { _taskQueueManager.Enqueue(async() => { if (eventData.BlockTime > new Timestamp { Seconds = 3600 } && eventData.BlockTime + eventData.BlockExecutionTime < TimestampHelper.GetUtcNow()) { Logger.LogTrace( $"Will cancel mining due to timeout: Actual mining time: {eventData.BlockTime}, " + $"execution limit: {eventData.BlockExecutionTime.Milliseconds()} ms."); } var block = await _minerService.MineAsync(eventData.PreviousBlockHash, eventData.PreviousBlockHeight, eventData.BlockTime, eventData.BlockExecutionTime); await _blockchainService.AddBlockAsync(block); var chain = await _blockchainService.GetChainAsync(); await LocalEventBus.PublishAsync(new BlockMinedEventData() { BlockHeader = block.Header, HasFork = block.Height <= chain.BestChainHeight }); // Self mined block do not need do verify _taskQueueManager.Enqueue(async() => await _blockAttachService.AttachBlockAsync(block), KernelConstants.UpdateChainQueueName); }, KernelConstants.ConsensusRequestMiningQueueName); } catch (Exception e) { Logger.LogError(e.ToString()); throw; } }
public async Task MineBlockTest() { _block = await _minerService.MineAsync(_chain.BestChainHash, _chain.BestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(4)); }
public async Task MineBlockTest() { _block = await _minerService.MineAsync(_chain.BestChainHash, _chain.BestChainHeight, DateTime.UtcNow, TimeSpan.FromMilliseconds(4000)); }
public Task HandleEventAsync(ConsensusRequestMiningEventData eventData) { try { _taskQueueManager.Enqueue(async() => { var chain = await _blockchainService.GetChainAsync(); if (eventData.PreviousBlockHash != chain.BestChainHash) { Logger.LogWarning("Mining canceled because best chain already updated."); return; } if (!ValidateBlockMiningTime(eventData.BlockTime, eventData.MiningDueTime, eventData.BlockExecutionTime)) { await TriggerConsensusEventAsync(chain.BestChainHash, chain.BestChainHeight); return; } var blockExecutionDuration = CalculateBlockMiningDuration(eventData.BlockTime, eventData.BlockExecutionTime); Block block; try { block = await _minerService.MineAsync(eventData.PreviousBlockHash, eventData.PreviousBlockHeight, eventData.BlockTime, blockExecutionDuration); } catch (Exception) { await TriggerConsensusEventAsync(chain.BestChainHash, chain.BestChainHeight); throw; } if (TimestampHelper.GetUtcNow() <= eventData.MiningDueTime - blockExecutionDuration) { await _blockchainService.AddBlockAsync(block); Logger.LogTrace("Before enqueue attach job."); _taskQueueManager.Enqueue(async() => await _blockAttachService.AttachBlockAsync(block), KernelConstants.UpdateChainQueueName); Logger.LogTrace("Before publish block."); await LocalEventBus.PublishAsync(new BlockMinedEventData { BlockHeader = block.Header, // HasFork = block.Height <= chain.BestChainHeight }); } else { Logger.LogWarning( $"Discard block {block.Height} and trigger once again because mining time slot expired. " + $"MiningDueTime : {eventData.MiningDueTime}, " + $"block execution duration limit : {blockExecutionDuration}"); await TriggerConsensusEventAsync(chain.BestChainHash, chain.BestChainHeight); } }, KernelConstants.ConsensusRequestMiningQueueName); return(Task.CompletedTask); } catch (Exception e) { Logger.LogError(e.ToString()); throw; } }