private async Task HandleConsensusRequestMiningEventAsync( ConsensusRequestMiningEventData consensusRequestMiningEventData) { await _consensusRequestMiningEventHandler.HandleEventAsync(consensusRequestMiningEventData); await Task.Delay(500); }
public async Task TriggerConsensusAsync(ChainContext chainContext) { var now = TimestampHelper.GetUtcNow(); _blockTimeProvider.SetBlockTime(now); Logger.LogTrace($"Set block time to utc now: {now:hh:mm:ss.ffffff}. Trigger."); var triggerInformation = _triggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); // Upload the consensus command. _consensusCommand = await _readerFactory.Create(chainContext) .GetConsensusCommand.CallAsync(triggerInformation); Logger.LogDebug($"Updated consensus command: {_consensusCommand}"); // Update next mining time, also block time of both getting consensus extra data and txs. _nextMiningTime = TimestampHelper.GetUtcNow().AddMilliseconds(_consensusCommand .NextBlockMiningLeftMilliseconds); // Initial consensus scheduler. var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash, chainContext.BlockHeight, _nextMiningTime, TimestampHelper.DurationFromMilliseconds(_consensusCommand.LimitMillisecondsOfMiningBlock)); _consensusScheduler.CancelCurrentEvent(); _consensusScheduler.NewEvent(_consensusCommand.NextBlockMiningLeftMilliseconds, blockMiningEventData); Logger.LogTrace($"Set next mining time to: {_nextMiningTime:hh:mm:ss.ffffff}"); }
public async Task TriggerConsensusAsync(ChainContext chainContext) { var triggerInformation = _consensusInformationGenerationService.GetTriggerInformation(TriggerType.ConsensusCommand); // Upload the consensus command. _consensusControlInformation.ConsensusCommand = await _consensusInformationGenerationService.ExecuteContractAsync <ConsensusCommand>(chainContext, ConsensusConsts.GetConsensusCommand, triggerInformation, DateTime.UtcNow); Logger.LogDebug($"Updated consensus command: {_consensusControlInformation.ConsensusCommand}"); // Initial consensus scheduler. var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash, chainContext.BlockHeight, _consensusControlInformation.ConsensusCommand.ExpectedMiningTime.ToDateTime(), TimeSpan.FromMilliseconds(_consensusControlInformation.ConsensusCommand .LimitMillisecondsOfMiningBlock)); _consensusScheduler.CancelCurrentEvent(); // TODO: Remove NextBlockMiningLeftMilliseconds. _consensusScheduler.NewEvent(_consensusControlInformation.ConsensusCommand.NextBlockMiningLeftMilliseconds, blockMiningEventData); // Update next mining time, also block time of both getting consensus extra data and txs. _nextMiningTime = DateTime.UtcNow.AddMilliseconds(_consensusControlInformation.ConsensusCommand .NextBlockMiningLeftMilliseconds); }
public void NewEvent(long countingMilliseconds, ConsensusRequestMiningEventData consensusRequestMiningEventData) { JobManager.UseUtcTime(); var registry = new Registry(); registry.Schedule(() => LocalEventBus.PublishAsync(consensusRequestMiningEventData)) .ToRunOnceAt(TimestampHelper.GetUtcNow().AddMilliseconds(countingMilliseconds).ToDateTime()); JobManager.Initialize(registry); }
public async Task HandleEventAsyncTest() { var chain = await _chainService.GetChainAsync(); var hash = chain.BestChainHash; var height = chain.BestChainHeight; var eventData = new ConsensusRequestMiningEventData(hash, height, DateTime.UtcNow, TimeSpan.FromMilliseconds(60 * 1000)); await _miningEventHandler.HandleEventAsync(eventData); }
public async Task HandleEventAsync_Test() { var chain = await _chainService.GetChainAsync(); var hash = chain.BestChainHash; var height = chain.BestChainHeight; var eventData = new ConsensusRequestMiningEventData(hash, height, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromSeconds(60), TimestampHelper.GetUtcNow().AddDays(1)); await _miningEventHandler.HandleEventAsync(eventData); }
/// <summary> /// Basically update the consensus scheduler with latest consensus command. /// </summary> /// <param name="chainContext"></param> /// <returns></returns> public async Task TriggerConsensusAsync(ChainContext chainContext) { var now = TimestampHelper.GetUtcNow(); _blockTimeProvider.SetBlockTime(now); Logger.LogDebug($"Set block time to utc now: {now.ToDateTime():hh:mm:ss.ffffff}. Trigger."); var triggerInformation = _triggerInformationProvider.GetTriggerInformationForConsensusCommand(new BytesValue()); Logger.LogDebug($"Mining triggered, chain context: {chainContext.BlockHeight} - {chainContext.BlockHash}"); // Upload the consensus command. var contractReaderContext = await _consensusReaderContextService.GetContractReaderContextAsync(chainContext); _consensusCommand = await _contractReaderFactory .Create(contractReaderContext).GetConsensusCommand .CallAsync(triggerInformation); if (_consensusCommand == null) { Logger.LogWarning("Consensus command is null."); return; } Logger.LogDebug($"Updated consensus command: {_consensusCommand}"); // Update next mining time, also block time of both getting consensus extra data and txs. _nextMiningTime = _consensusCommand.ArrangedMiningTime; var leftMilliseconds = _consensusCommand.ArrangedMiningTime - TimestampHelper.GetUtcNow(); leftMilliseconds = leftMilliseconds.Seconds > ConsensusConstants.MaximumLeftMillisecondsForNextBlock ? new Duration { Seconds = ConsensusConstants.MaximumLeftMillisecondsForNextBlock } : leftMilliseconds; // Update consensus scheduler. var blockMiningEventData = new ConsensusRequestMiningEventData(chainContext.BlockHash, chainContext.BlockHeight, _nextMiningTime, TimestampHelper.DurationFromMilliseconds(_consensusCommand.LimitMillisecondsOfMiningBlock), _consensusCommand.MiningDueTime); _consensusScheduler.CancelCurrentEvent(); _consensusScheduler.NewEvent(leftMilliseconds.Milliseconds(), blockMiningEventData); Logger.LogDebug($"Set next mining time to: {_nextMiningTime.ToDateTime():hh:mm:ss.ffffff}"); }
public async Task HandleEvent_Test() { BlockMinedEventData blockMinedEventData = null; _localEventBus.Subscribe <BlockMinedEventData>(d => { blockMinedEventData = d; return(Task.CompletedTask); }); var chain = await _blockchainService.GetChainAsync(); var bestChainHash = chain.BestChainHash; var bestChainHeight = chain.BestChainHeight; _testContext.MockConsensusService.Verify( s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Exactly(10)); { var eventData = new ConsensusRequestMiningEventData(HashHelper.ComputeFrom("NotBestChain"), bestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMilliseconds(500), TimestampHelper.GetUtcNow().AddMilliseconds(499)); await HandleConsensusRequestMiningEventAsync(eventData); blockMinedEventData.ShouldBeNull(); chain = await _blockchainService.GetChainAsync(); chain.BestChainHeight.ShouldBe(bestChainHeight); chain.BestChainHash.ShouldBe(bestChainHash); _testContext.MockConsensusService.Verify( s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Exactly(10)); } { var eventData = new ConsensusRequestMiningEventData(bestChainHash, bestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMilliseconds(500), TimestampHelper.GetUtcNow().AddMilliseconds(499)); await HandleConsensusRequestMiningEventAsync(eventData); blockMinedEventData.ShouldBeNull(); chain = await _blockchainService.GetChainAsync(); chain.BestChainHeight.ShouldBe(bestChainHeight); chain.BestChainHash.ShouldBe(bestChainHash); _testContext.MockConsensusService.Verify( s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Exactly(11)); } { var eventData = new ConsensusRequestMiningEventData(bestChainHash, bestChainHeight, TimestampHelper.GetUtcNow(), TimestampHelper.DurationFromMilliseconds(500), TimestampHelper.GetUtcNow().AddSeconds(30)); await HandleConsensusRequestMiningEventAsync(eventData); blockMinedEventData.ShouldNotBeNull(); blockMinedEventData.BlockHeader.Height.ShouldBe(bestChainHeight + 1); blockMinedEventData.BlockHeader.PreviousBlockHash.ShouldBe(bestChainHash); chain = await _blockchainService.GetChainAsync(); chain.Branches.ShouldContainKey(blockMinedEventData.BlockHeader.GetHash().ToStorageKey()); (await _blockchainService.HasBlockAsync(blockMinedEventData.BlockHeader.GetHash())).ShouldBeTrue(); _testContext.MockConsensusService.Verify( s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Exactly(11)); } }