示例#1
0
        public async Task UpdateDutiesAsync(Epoch epoch, CancellationToken cancellationToken)
        {
            IEnumerable <BlsPublicKey> publicKeys = _validatorKeyProvider.GetPublicKeys();

            IAsyncEnumerable <ValidatorDuty> validatorDuties = _beaconNodeApi.ValidatorDutiesAsync(publicKeys, epoch, cancellationToken);

            await foreach (ValidatorDuty validatorDuty in validatorDuties.ConfigureAwait(false))
            {
                Slot?currentAttestationSlot =
                    _validatorState.AttestationSlot.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                Shard?currentAttestationShard =
                    _validatorState.AttestationShard.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                if (validatorDuty.AttestationSlot != currentAttestationSlot ||
                    validatorDuty.AttestationShard != currentAttestationShard)
                {
                    _validatorState.SetAttestationDuty(validatorDuty.ValidatorPublicKey, validatorDuty.AttestationSlot,
                                                       validatorDuty.AttestationShard);
                    if (_logger.IsInfo())
                    {
                        Log.ValidatorDutyAttestationChanged(_logger, validatorDuty.ValidatorPublicKey, epoch,
                                                            validatorDuty.AttestationSlot, validatorDuty.AttestationShard, null);
                    }
                }
            }

            await foreach (ValidatorDuty validatorDuty in validatorDuties.ConfigureAwait(false))
            {
                Slot?currentProposalSlot = _validatorState.ProposalSlot.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                if (validatorDuty.BlockProposalSlot != Slot.None &&
                    validatorDuty.BlockProposalSlot != currentProposalSlot)
                {
                    _validatorState.SetProposalDuty(validatorDuty.ValidatorPublicKey, validatorDuty.BlockProposalSlot);
                    if (_logger.IsInfo())
                    {
                        Log.ValidatorDutyProposalChanged(_logger, validatorDuty.ValidatorPublicKey, epoch,
                                                         validatorDuty.BlockProposalSlot, null);
                    }
                }
            }
        }
        public async Task UpdateDutiesActivityAsync(Epoch epoch, CancellationToken cancellationToken)
        {
            Activity activity = new Activity("update-duties");

            activity.Start();
            try
            {
                IList <BlsPublicKey> publicKeys = _validatorKeyProvider.GetPublicKeys();

                ApiResponse <IList <ValidatorDuty> > validatorDutiesResponse =
                    await _beaconNodeApi.ValidatorDutiesAsync(publicKeys, epoch, cancellationToken);

                if (validatorDutiesResponse.StatusCode != StatusCode.Success)
                {
                    Log.ErrorGettingValidatorDuties(_logger, (int)validatorDutiesResponse.StatusCode,
                                                    validatorDutiesResponse.StatusCode, null);
                    return;
                }

                // Record proposal duties first, in case there is an error
                foreach (ValidatorDuty validatorDuty in validatorDutiesResponse.Content)
                {
                    Slot?currentProposalSlot =
                        _validatorState.ProposalSlot.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                    if (validatorDuty.BlockProposalSlot != Slot.None &&
                        validatorDuty.BlockProposalSlot != currentProposalSlot)
                    {
                        _validatorState.SetProposalDuty(validatorDuty.ValidatorPublicKey,
                                                        validatorDuty.BlockProposalSlot);
                        if (_logger.IsInfo())
                        {
                            Log.ValidatorDutyProposalChanged(_logger, validatorDuty.ValidatorPublicKey, epoch,
                                                             validatorDuty.BlockProposalSlot, null);
                        }
                    }
                }

                foreach (ValidatorDuty validatorDuty in validatorDutiesResponse.Content)
                {
                    Slot?currentAttestationSlot =
                        _validatorState.AttestationSlot.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                    Shard?currentAttestationShard =
                        _validatorState.AttestationShard.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                    if (validatorDuty.AttestationSlot != currentAttestationSlot ||
                        validatorDuty.AttestationShard != currentAttestationShard)
                    {
                        _validatorState.SetAttestationDuty(validatorDuty.ValidatorPublicKey,
                                                           validatorDuty.AttestationSlot,
                                                           validatorDuty.AttestationShard);
                        if (_logger.IsDebug())
                        {
                            LogDebug.ValidatorDutyAttestationChanged(_logger, validatorDuty.ValidatorPublicKey, epoch,
                                                                     validatorDuty.AttestationSlot, validatorDuty.AttestationShard, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ExceptionGettingValidatorDuties(_logger, ex.Message, ex);
            }
            finally
            {
                activity.Stop();
            }
        }
示例#3
0
        public async Task UpdateDutiesActivityAsync(Epoch epoch, CancellationToken cancellationToken)
        {
            Activity activity = new Activity("update-duties");

            activity.Start();
            using IDisposable activityScope = _logger.BeginScope("[TraceId, {TraceId}], [SpanId, {SpanId}]",
                                                                 activity.TraceId, activity.SpanId);
            try
            {
                IList <BlsPublicKey> publicKeys = _validatorKeyProvider.GetPublicKeys();

                ApiResponse <IList <ValidatorDuty> > validatorDutiesResponse =
                    await _beaconNodeApi.ValidatorDutiesAsync(publicKeys, epoch, cancellationToken);

                if (validatorDutiesResponse.StatusCode != StatusCode.Success)
                {
                    Log.ErrorGettingValidatorDuties(_logger, (int)validatorDutiesResponse.StatusCode,
                                                    validatorDutiesResponse.StatusCode, null);
                    return;
                }

                // Record proposal duties first, in case there is an error
                foreach (ValidatorDuty validatorDuty in validatorDutiesResponse.Content)
                {
                    if (validatorDuty.BlockProposalSlot.HasValue)
                    {
                        Slot?currentProposalSlot =
                            _validatorState.ProposalSlot.GetValueOrDefault(validatorDuty.ValidatorPublicKey);
                        bool needsProposalUpdate = validatorDuty.BlockProposalSlot != currentProposalSlot;
                        if (needsProposalUpdate)
                        {
                            _validatorState.SetProposalDuty(validatorDuty.ValidatorPublicKey,
                                                            validatorDuty.BlockProposalSlot.Value);
                            if (_logger.IsInfo())
                            {
                                Log.ValidatorDutyProposalChanged(_logger, validatorDuty.ValidatorPublicKey, epoch,
                                                                 validatorDuty.BlockProposalSlot.Value, null);
                            }
                        }
                    }
                }

                foreach (ValidatorDuty validatorDuty in validatorDutiesResponse.Content)
                {
                    if (validatorDuty.AttestationSlot.HasValue)
                    {
                        bool needsAttestationUpdate;
                        if (_validatorState.AttestationSlotAndIndex.TryGetValue(validatorDuty.ValidatorPublicKey,
                                                                                out (Slot, CommitteeIndex)currentValue))
                        {
                            (Slot currentAttestationSlot, CommitteeIndex currentAttestationIndex) = currentValue;
                            needsAttestationUpdate = validatorDuty.AttestationSlot != currentAttestationSlot ||
                                                     validatorDuty.AttestationIndex != currentAttestationIndex;
                        }
                        else
                        {
                            needsAttestationUpdate = true;
                        }

                        if (needsAttestationUpdate)
                        {
                            _validatorState.SetAttestationDuty(validatorDuty.ValidatorPublicKey,
                                                               validatorDuty.AttestationSlot.Value,
                                                               validatorDuty.AttestationIndex !.Value);
                            if (_logger.IsDebug())
                            {
                                LogDebug.ValidatorDutyAttestationChanged(_logger, validatorDuty.ValidatorPublicKey,
                                                                         epoch,
                                                                         validatorDuty.AttestationSlot.Value, validatorDuty.AttestationIndex.Value, null);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ExceptionGettingValidatorDuties(_logger, ex.Message, ex);
            }
            finally
            {
                activity.Stop();
            }
        }