Skip to content

Commit 76f8a0b

Browse files
committed
feat: added sweep calc to validators job
1 parent 87e5762 commit 76f8a0b

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

src/common/sweep/sweep.service.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Inject, Injectable, LoggerService, OnModuleInit } from '@nestjs/common'
22
import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
33
import { GenesisTimeService, SLOTS_PER_EPOCH } from '../genesis-time';
44
import {
5-
getMaxEffectiveBalance,
65
isFullyWithdrawableValidator,
76
isPartiallyWithdrawableValidator,
87
} from '../../jobs/validators/utils/validator-state-utils';
@@ -47,19 +46,24 @@ export class SweepService implements OnModuleInit {
4746
}
4847

4948
private getSweepDelayInEpochsPreElectra(indexedValidators: IndexedValidator[], epoch: number): number {
50-
const totalWithdrawableValidators = this.getWithdrawableValidators(indexedValidators, epoch).length;
49+
const totalWithdrawableValidators = this.getWithdrawableValidatorsNumber(indexedValidators, epoch);
5150

5251
const fullSweepInEpochs = totalWithdrawableValidators / MAX_WITHDRAWALS_PER_PAYLOAD / SLOTS_PER_EPOCH;
5352
return Math.floor(fullSweepInEpochs * 0.5);
5453
}
5554

5655
// pre pectra
57-
private getWithdrawableValidators(indexedValidators: IndexedValidator[], epoch: number) {
58-
return indexedValidators.filter(
59-
(v) =>
56+
private getWithdrawableValidatorsNumber(indexedValidators: IndexedValidator[], epoch: number) {
57+
let count = 0;
58+
for (const v of indexedValidators) {
59+
if (
6060
isPartiallyWithdrawableValidator(v.validator, parseGwei(v.balance)) ||
61-
isFullyWithdrawableValidator(v.validator, parseGwei(v.balance), epoch),
62-
);
61+
isFullyWithdrawableValidator(v.validator, parseGwei(v.balance), epoch)
62+
) {
63+
count++;
64+
}
65+
}
66+
return count;
6367
}
6468

6569
private getSweepDelayInEpochsPostElectra(state: BeaconState, indexedValidators: IndexedValidator[]): number {
@@ -72,10 +76,13 @@ export class SweepService implements OnModuleInit {
7276

7377
private predictWithdrawalsNumberInSweepCycle(state: BeaconState, indexedValidators: IndexedValidator[]): number {
7478
const pendingPartialWithdrawals = this.getPendingPartialWithdrawals(state);
75-
const validatorsWithdrawals = this.getValidatorsWithdrawals(state, pendingPartialWithdrawals, indexedValidators);
79+
const validatorsWithdrawalsNumber = this.getValidatorsWithdrawalsNumber(
80+
state,
81+
pendingPartialWithdrawals,
82+
indexedValidators,
83+
);
7684

7785
const pendingPartialWithdrawalsNumber = pendingPartialWithdrawals.length;
78-
const validatorsWithdrawalsNumber = validatorsWithdrawals.length;
7986

8087
const partialWithdrawalsMaxRatio =
8188
MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP /
@@ -114,14 +121,14 @@ export class SweepService implements OnModuleInit {
114121
}
115122

116123
// post pectra
117-
getValidatorsWithdrawals(
124+
getValidatorsWithdrawalsNumber(
118125
state: BeaconState,
119126
partialWithdrawals: Withdrawal[],
120127
indexedValidators: IndexedValidator[],
121-
): Withdrawal[] {
128+
): number {
122129
const epoch = Math.ceil(+state.slot / SLOTS_PER_EPOCH);
123-
const withdrawals: Withdrawal[] = [];
124130
const partiallyWithdrawnMap: Record<number, number> = {};
131+
let withdrawalsNumber = 0;
125132

126133
for (const withdrawal of partialWithdrawals) {
127134
partiallyWithdrawnMap[withdrawal.validatorIndex] =
@@ -135,13 +142,12 @@ export class SweepService implements OnModuleInit {
135142
const balance = parseGwei(state.balances[validatorIndex]).sub(partiallyWithdrawnBalance);
136143

137144
if (isFullyWithdrawableValidator(validator, balance, epoch)) {
138-
withdrawals.push({ validatorIndex, amount: balance });
145+
withdrawalsNumber++;
139146
} else if (isPartiallyWithdrawableValidator(validator, balance)) {
140-
const maxEffectiveBalance = getMaxEffectiveBalance(validator);
141-
withdrawals.push({ validatorIndex, amount: balance.sub(maxEffectiveBalance) });
147+
withdrawalsNumber++;
142148
}
143149
}
144150

145-
return withdrawals;
151+
return withdrawalsNumber;
146152
}
147153
}

src/jobs/validators/validators.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { PrometheusService } from 'common/prometheus';
2020
import { stringifyFrameBalances } from 'common/validators/strigify-frame-balances';
2121
import { getValidatorWithdrawalTimestamp } from './utils/get-validator-withdrawal-timestamp';
2222
import { IndexedValidator, ResponseValidatorsData } from '../../common/consensus-provider/consensus-provider.types';
23+
import { SweepService } from '../../common/sweep';
2324

2425
export class ValidatorsService {
2526
static SERVICE_LOG_NAME = 'validators';
@@ -36,6 +37,7 @@ export class ValidatorsService {
3637
protected readonly validatorsCacheService: ValidatorsCacheService,
3738
protected readonly genesisTimeService: GenesisTimeService,
3839
protected readonly lidoKeys: LidoKeysService,
40+
protected readonly sweepService: SweepService,
3941
) {}
4042

4143
/**
@@ -65,13 +67,16 @@ export class ValidatorsService {
6567
const stream = await this.consensusProviderService.getStateValidatorsStream({
6668
stateId: 'head',
6769
});
68-
const data: ResponseValidatorsData = await processValidatorsStream(stream);
70+
const indexedValidators: ResponseValidatorsData = await processValidatorsStream(stream);
6971
const currentEpoch = this.genesisTimeService.getCurrentEpoch();
7072

7173
let activeValidatorCount = 0;
7274
let latestEpoch = `${currentEpoch + MAX_SEED_LOOKAHEAD + 1}`;
7375

74-
for (const item of data) {
76+
const sweepMeanEpochs = await this.sweepService.getSweepDelayInEpochs(indexedValidators, currentEpoch);
77+
this.validatorsStorageService.setSweepMeanEpochs(sweepMeanEpochs);
78+
79+
for (const item of indexedValidators) {
7580
if (['active_ongoing', 'active_exiting', 'active_slashed'].includes(item.status)) {
7681
activeValidatorCount++;
7782
}
@@ -86,10 +91,10 @@ export class ValidatorsService {
8691
}
8792

8893
this.validatorsStorageService.setActiveValidatorsCount(activeValidatorCount);
89-
this.validatorsStorageService.setTotalValidatorsCount(data.length);
94+
this.validatorsStorageService.setTotalValidatorsCount(indexedValidators.length);
9095
this.validatorsStorageService.setMaxExitEpoch(latestEpoch);
9196

92-
const frameBalances = await this.getLidoValidatorsWithdrawableBalances(data);
97+
const frameBalances = await this.getLidoValidatorsWithdrawableBalances(indexedValidators);
9398
this.validatorsStorageService.setFrameBalances(frameBalances);
9499
await this.validatorsCacheService.saveDataToCache();
95100

src/storage/validators/validators.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class ValidatorsStorageService {
88
protected totalValidatorsCount: number;
99
protected lastUpdate: number;
1010
protected frameBalances: Record<string, BigNumber>;
11+
protected sweepMeanEpochs: number;
1112

1213
/**
1314
* Get max exit epoch for all validators
@@ -80,4 +81,12 @@ export class ValidatorsStorageService {
8081
public getTotalValidatorsCount() {
8182
return this.totalValidatorsCount;
8283
}
84+
85+
public setSweepMeanEpochs(sweepMeanEpochs: number) {
86+
this.sweepMeanEpochs = sweepMeanEpochs;
87+
}
88+
89+
public getSweepMeanEpochs() {
90+
return this.sweepMeanEpochs;
91+
}
8392
}

0 commit comments

Comments
 (0)