Skip to content

Commit

Permalink
Adds sync committee metrics (#6891)
Browse files Browse the repository at this point in the history
`validator_scheduled_sync_committee_duties_current`
This new metric is a visualisation to see how many validators are currently assigned to performing sync committee duties.

There were already metrics showing the attestation duties and block production duties, named in the same way, so this was just filling in the gap, although the other duties are accounted in a slightly different way, as they work differently.

This new metric is effectively the number of validators that you have that are in a sync committee currently.

Also added
`validator_current_sync_committee_last_epoch`, which will be the last epoch of the current sync committee.

This will allow validator maintainers to better determine if they are in a sync committee currently, and when the sync committee will be changing.

Fixes #6270

Signed-off-by: Paul Harris <paul.harris@consensys.net>
  • Loading branch information
rolfyone committed Mar 3, 2023
1 parent a448ea3 commit e895b42
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ For information on changes in released versions of Teku, see the [releases page]
- Added `/eth/v1/builder/states/{state_id}/expected_withdrawals` rest api endpoint.
- Added `/eth/v1/beacon/rewards/sync_committee/{block_id}` rest api endpoint.
- Added Capella fork information for Goerli network configuration.
- Added `validator_scheduled_sync_committee_duties_current` metric - the number of validators that you have active with current sync committee duties
- Added `validator_current_sync_committee_last_epoch` metric - the last epoch of the current sync committee.

### Bug Fixes
- Included All forks in fork schedule if they're defined in configuration.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ALTAIR_FORK_EPOCH: 36660
# BELLATRIX
BELLATRIX_FORK_VERSION: 0x02001020
BELLATRIX_FORK_EPOCH: 112260
#CAPELLA
# CAPELLA
CAPELLA_FORK_VERSION: 0x03001020
CAPELLA_FORK_EPOCH: 162304
# Sharding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import it.unimi.dsi.fastutil.ints.IntCollection;
import java.util.Optional;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.validator.api.SyncCommitteeDuties;
Expand All @@ -33,42 +35,62 @@ public class SyncCommitteeDutyLoader
private final ChainHeadTracker chainHeadTracker;
private final ForkProvider forkProvider;

private final MetricsSystem metricsSystem;

public SyncCommitteeDutyLoader(
final OwnedValidators validators,
final ValidatorIndexProvider validatorIndexProvider,
final Spec spec,
final ValidatorApiChannel validatorApiChannel,
final ChainHeadTracker chainHeadTracker,
final ForkProvider forkProvider) {
final ForkProvider forkProvider,
final MetricsSystem metricsSystem) {
super(validators, validatorIndexProvider);
this.spec = spec;
this.validatorApiChannel = validatorApiChannel;
this.chainHeadTracker = chainHeadTracker;
this.forkProvider = forkProvider;
this.metricsSystem = metricsSystem;
}

@Override
protected SafeFuture<Optional<SyncCommitteeDuties>> requestDuties(
final UInt64 epoch, final IntCollection validatorIndices) {
return validatorApiChannel.getSyncCommitteeDuties(epoch, validatorIndices);
return validatorApiChannel
.getSyncCommitteeDuties(epoch, validatorIndices)
.thenPeek(
maybeDuties -> {
metricsSystem.createIntegerGauge(
TekuMetricCategory.VALIDATOR,
"scheduled_sync_committee_duties_current",
"Current number of Sync committee members performing duties",
() -> maybeDuties.map(d -> d.getDuties().size()).orElse(0));
});
}

@Override
protected SafeFuture<SyncCommitteeScheduledDuties> scheduleAllDuties(
final UInt64 epoch, final SyncCommitteeDuties duties) {
final UInt64 lastEpochInCommitteePeriod =
spec.getSyncCommitteeUtilRequired(spec.computeStartSlotAtEpoch(epoch))
.computeFirstEpochOfNextSyncCommitteePeriod(epoch)
.minusMinZero(1);
final SyncCommitteeScheduledDuties.Builder dutyBuilder =
SyncCommitteeScheduledDuties.builder()
.forkProvider(forkProvider)
.validatorApiChannel(validatorApiChannel)
.chainHeadTracker(chainHeadTracker)
.spec(spec)
.lastEpochInCommitteePeriod(
spec.getSyncCommitteeUtilRequired(spec.computeStartSlotAtEpoch(epoch))
.computeFirstEpochOfNextSyncCommitteePeriod(epoch)
.minusMinZero(1));
.lastEpochInCommitteePeriod(lastEpochInCommitteePeriod);
duties.getDuties().forEach(duty -> scheduleDuty(dutyBuilder, duty));
final SyncCommitteeScheduledDuties scheduledDuties = dutyBuilder.build();
scheduledDuties.subscribeToSubnets();

metricsSystem.createIntegerGauge(
TekuMetricCategory.VALIDATOR,
"current_sync_committee_last_epoch",
"The final epoch of the current sync committee period",
lastEpochInCommitteePeriod::intValue);
return SafeFuture.completedFuture(scheduledDuties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ private void scheduleValidatorsDuties(
spec,
validatorApiChannel,
chainHeadTracker,
forkProvider));
forkProvider,
metricsSystem));
validatorTimingChannels.add(
new SyncCommitteeScheduler(
metricsSystem, spec, syncCommitteeDutyLoader, new Random()::nextInt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem;
import tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
Expand Down Expand Up @@ -60,14 +62,17 @@ class SyncCommitteeDutyLoaderTest {
private final ChainHeadTracker chainHeadTracker = mock(ChainHeadTracker.class);
private final ForkProvider forkProvider = mock(ForkProvider.class);

private final StubMetricsSystem metricsSystem = new StubMetricsSystem();

private final SyncCommitteeDutyLoader dutyLoader =
new SyncCommitteeDutyLoader(
validators,
validatorIndexProvider,
spec,
validatorApiChannel,
chainHeadTracker,
forkProvider);
forkProvider,
metricsSystem);

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -109,6 +114,38 @@ void shouldRetrieveDuties() {
validator1Index, IntSet.of(1, 6, 25), untilEpoch.increment()),
new SyncCommitteeSubnetSubscription(
validator2Index, IntSet.of(7, 50, 38), untilEpoch.increment())));
assertThat(
metricsSystem
.getGauge(TekuMetricCategory.VALIDATOR, "scheduled_sync_committee_duties_current")
.getValue())
.isEqualTo(2.0);

assertThat(
metricsSystem
.getGauge(TekuMetricCategory.VALIDATOR, "current_sync_committee_last_epoch")
.getValue())
.isEqualTo(63.0);
}

@Test
void shouldGetCountOfValidatorsInSyncCommitteeThroughMetrics() {
final UInt64 epoch = UInt64.valueOf(56);
when(validatorApiChannel.getSyncCommitteeDuties(epoch, validatorIndices))
.thenReturn(
SafeFuture.completedFuture(Optional.of(new SyncCommitteeDuties(false, List.of()))));
final SyncCommitteeScheduledDuties duties = loadDuties(epoch);
assertThat(duties.countDuties()).isEqualTo(0);
assertThat(
metricsSystem
.getGauge(TekuMetricCategory.VALIDATOR, "scheduled_sync_committee_duties_current")
.getValue())
.isEqualTo(0.0);

assertThat(
metricsSystem
.getGauge(TekuMetricCategory.VALIDATOR, "current_sync_committee_last_epoch")
.getValue())
.isEqualTo(63.0);
}

private SyncCommitteeScheduledDuties loadDuties(final UInt64 epoch) {
Expand Down

0 comments on commit e895b42

Please sign in to comment.