Exemplo n.º 1
0
Arquivo: DPoS.cs Projeto: wyk125/AElf
        /// <summary>
        /// Related tx has 3 params:
        /// 1. Current round number
        /// 2. BP Address
        /// 3. In value
        /// 4. Round Id
        /// </summary>
        /// <returns></returns>
        private async Task PublishInValue()
        {
            _logger?.Trace(
                $"Trying to enter DPoS Mining Process - {nameof(PublishInValue)}.");

            if (_terminated)
            {
                return;
            }

            if (!CurrentState.AbleToMine())
            {
                return;
            }

            var lockWasTaken = false;

            try
            {
                lockWasTaken = Interlocked.CompareExchange(ref _flag, 1, 0) == 0;
                if (lockWasTaken)
                {
                    MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.PublishInValue, true));

                    if (MiningLocked())
                    {
                        return;
                    }

                    _logger?.Trace($"Mine - Entered DPoS Mining Process - {nameof(PublishInValue)}.");

                    var currentRoundNumber = Helper.CurrentRoundNumber;

                    if (!_consensusData.Any())
                    {
                        return;
                    }

                    var parameters = new List <byte[]>
                    {
                        currentRoundNumber.ToByteArray(),
                        new StringValue {
                            Value = _nodeKeyPair.Address.DumpHex().RemoveHexPrefix()
                        }.ToByteArray(),
                        _consensusData.Pop().ToByteArray(),
                        new Int64Value {
                            Value = Helper.GetCurrentRoundInfo(currentRoundNumber).RoundId
                        }.ToByteArray()
                    };

                    var txToPublishInValue = await GenerateTransactionAsync("PublishInValue", parameters);
                    await BroadcastTransaction(txToPublishInValue);
                }
            }
            catch (Exception e)
            {
                _logger?.Trace(e, $"Error in {nameof(MiningWithInitializingAElfDPoSInformation)}");
            }
            finally
            {
                if (lockWasTaken)
                {
                    Thread.VolatileWrite(ref _flag, 0);
                }

                MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.PublishInValue, false));

                _logger?.Trace($"Mine - Leaving DPoS Mining Process - {nameof(PublishInValue)}.");
            }
        }
Exemplo n.º 2
0
Arquivo: DPoS.cs Projeto: wyk125/AElf
        /// <summary>
        /// Related tx has 3 params:
        /// 1. Current round info
        /// 2. New round info
        /// 3. Extra block producer of new round
        /// </summary>
        /// <returns></returns>
        private async Task MiningWithUpdatingAElfDPoSInformation()
        {
            _logger?.Trace(
                $"Trying to enter DPoS Mining Process - {nameof(MiningWithUpdatingAElfDPoSInformation)}.");

            if (_terminated)
            {
                return;
            }

            if (!CurrentState.AbleToMine())
            {
                return;
            }

            var lockWasTaken = false;

            try
            {
                lockWasTaken = Interlocked.CompareExchange(ref _flag, 1, 0) == 0;
                if (lockWasTaken)
                {
                    MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.UpdateAElfDPoS, true));

                    if (MiningLocked())
                    {
                        return;
                    }

                    _logger?.Trace(
                        $"Mine - Entered DPoS Mining Process - {nameof(MiningWithUpdatingAElfDPoSInformation)}.");

                    var extraBlockResult = Helper.ExecuteTxsForExtraBlock();

                    var parameters = new List <byte[]>
                    {
                        extraBlockResult.Item1.ToByteArray(),
                        extraBlockResult.Item2.ToByteArray(),
                        extraBlockResult.Item3.ToByteArray(),
                        new Int64Value {
                            Value = Helper.GetCurrentRoundInfo().RoundId
                        }.ToByteArray()
                    };

                    var txForExtraBlock = await GenerateTransactionAsync("UpdateAElfDPoS", parameters);

                    await BroadcastTransaction(txForExtraBlock);
                    await Mine();
                }
            }
            catch (Exception e)
            {
                _logger?.Trace(e, $"Error in {nameof(MiningWithInitializingAElfDPoSInformation)}");
            }
            finally
            {
                if (lockWasTaken)
                {
                    Thread.VolatileWrite(ref _flag, 0);
                }

                MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.UpdateAElfDPoS, false));
                _logger?.Trace(
                    $"Mine - Leaving DPoS Mining Process - {nameof(MiningWithUpdatingAElfDPoSInformation)}.");
            }
        }
Exemplo n.º 3
0
Arquivo: DPoS.cs Projeto: wyk125/AElf
        /// <summary>
        /// Related tx has 4 params:
        /// 1. Miners list
        /// 2. Information of first rounds
        /// 3. Mining interval
        /// 4. Log level
        /// </summary>
        /// <returns></returns>
        private async Task MiningWithInitializingAElfDPoSInformation()
        {
            _logger?.Trace(
                $"Trying to enter DPoS Mining Process - {nameof(MiningWithInitializingAElfDPoSInformation)}.");

            if (_terminated)
            {
                return;
            }

            if (!CurrentState.AbleToMine())
            {
                return;
            }

            if (await Helper.DPoSInformationGenerated())
            {
                _logger?.Trace($"Failed to enter {nameof(MiningWithInitializingAElfDPoSInformation)} because " +
                               $"DPoS information already generated.");
                return;
            }

            var lockWasTaken = false;

            try
            {
                lockWasTaken = Interlocked.CompareExchange(ref _flag, 1, 0) == 0;
                if (lockWasTaken)
                {
                    MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.InitializeAElfDPoS, true));

                    if (MiningLocked())
                    {
                        return;
                    }

                    _logger?.Trace(
                        $"Mine - Entered DPoS Mining Process - {nameof(MiningWithInitializingAElfDPoSInformation)}.");

                    var logLevel = new Int32Value {
                        Value = LogManager.GlobalThreshold.Ordinal
                    };
                    var parameters = new List <byte[]>
                    {
                        Miners.ToByteArray(),
                        Helper.GenerateInfoForFirstTwoRounds().ToByteArray(),
                        new SInt32Value {
                            Value = ConsensusConfig.Instance.DPoSMiningInterval
                        }.ToByteArray(),
                        logLevel.ToByteArray()
                    };
                    var txToInitializeAElfDPoS = await GenerateTransactionAsync("InitializeAElfDPoS", parameters);
                    await BroadcastTransaction(txToInitializeAElfDPoS);
                    await Mine();
                }
            }
            catch (Exception e)
            {
                _logger?.Trace(e, $"Error in {nameof(MiningWithInitializingAElfDPoSInformation)}");
            }
            finally
            {
                if (lockWasTaken)
                {
                    Thread.VolatileWrite(ref _flag, 0);
                }

                MessageHub.Instance.Publish(new DPoSStateChanged(ConsensusBehavior.InitializeAElfDPoS, false));
                _logger?.Trace(
                    $"Mine - Leaving DPoS Mining Process - {nameof(MiningWithInitializingAElfDPoSInformation)}.");
            }
        }