Exemple #1
0
        internal static ConsensusCommand ToConsensusCommand(this BytesValue bytesValue)
        {
            var consensusCommand = new ConsensusCommand();

            consensusCommand.MergeFrom(bytesValue.Value);
            return(consensusCommand);
        }
Exemple #2
0
        /// <summary>
        /// For generating in_value.
        /// </summary>
        /// <returns></returns>
        private Hash GetRandomHash(ConsensusCommand consensusCommand)
        {
            var data  = Hash.FromRawBytes(consensusCommand.ArrangedMiningTime.ToByteArray());
            var bytes = AsyncHelper.RunSync(() => _accountService.SignAsync(data.ToByteArray()));

            return(Hash.FromRawBytes(bytes));
        }
Exemple #3
0
        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}");
        }
Exemple #4
0
        /// <summary>
        /// For generating in_value.
        /// </summary>
        /// <returns></returns>
        private Hash GetRandomHash(ConsensusCommand consensusCommand)
        {
            var data = Hash.FromRawBytes(consensusCommand.NextBlockMiningLeftMilliseconds
                                         .DumpByteArray());
            var bytes = AsyncHelper.RunSync(() => _accountService.SignAsync(data.ToByteArray()));

            return(Hash.FromRawBytes(bytes));
        }
        /// <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}");
        }