Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new attestations calculation algorithm #270

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,6 @@ Independent of `CL_API_MAX_RETRIES`.
* **Required:** false
* **Default:** 155000
---
`DENCUN_FORK_EPOCH` - Ethereum consensus layer epoch when the Dencun hard fork has been released. This value must be set
only for custom networks that support the Dencun hard fork. If the value of this variable is not specified for a custom
network, it is supposed that this network doesn't support Dencun. For officially supported networks (Mainnet, Goerli and
Holesky) this value should be omitted.
* **Required:** false
---
`VALIDATOR_REGISTRY_SOURCE` - Validators registry source.
* **Required:** false
* **Values:** lido (Lido NodeOperatorsRegistry module keys) / keysapi (Lido keys from multiple modules) / file
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@lido-nestjs/execution": "^1.11.1",
"@lido-nestjs/logger": "^1.3.2",
"@lido-nestjs/registry": "^7.4.0",
"@lodestar/types": "^1.15.1",
"@lodestar/types": "^1.24.0",
"@mikro-orm/core": "^5.3.1",
"@mikro-orm/knex": "^5.3.1",
"@mikro-orm/nestjs": "^5.1.0",
Expand Down
1 change: 0 additions & 1 deletion src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class AppService implements OnModuleInit, OnApplicationBootstrap {
this.logger.log(`DRY RUN ${this.configService.get('DRY_RUN') ? 'enabled' : 'disabled'}`);
this.logger.log(`Slot time: ${this.configService.get('CHAIN_SLOT_TIME_SECONDS')} seconds`);
this.logger.log(`Epoch size: ${this.configService.get('FETCH_INTERVAL_SLOTS')} slots`);
this.logger.log(`Dencun fork epoch: ${this.configService.get('DENCUN_FORK_EPOCH')}`);
}

public async onApplicationBootstrap(): Promise<void> {
Expand Down
20 changes: 1 addition & 19 deletions src/common/config/env.validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expose, Transform, plainToInstance } from 'class-transformer';
import { Transform, plainToInstance } from 'class-transformer';
import {
ArrayMinSize,
IsArray,
Expand All @@ -9,7 +9,6 @@ import {
IsNumber,
IsObject,
IsPort,
IsPositive,
IsString,
Max,
Min,
Expand All @@ -18,8 +17,6 @@ import {
validateSync,
} from 'class-validator';

import { Epoch } from 'common/consensus-provider/types';

import { Environment, LogFormat, LogLevel } from './interfaces';

export enum Network {
Expand All @@ -39,12 +36,6 @@ export enum WorkingMode {
Head = 'head',
}

const dencunForkEpoch = {
'1': 269568,
'5': 231680,
'17000': 29696,
};

const toBoolean = (value: any): boolean => {
if (typeof value === 'boolean') {
return value;
Expand Down Expand Up @@ -171,15 +162,6 @@ export class EnvironmentVariables {
@ValidateIf((vars) => vars.ETH_NETWORK === Network.Mainnet)
public START_EPOCH = 155000;

@IsInt()
@IsPositive()
@Expose()
@Transform(
({ value, obj }) =>
dencunForkEpoch[obj.ETH_NETWORK] || (value != null && value.trim() !== '' ? parseInt(value, 10) : Number.MAX_SAFE_INTEGER),
)
public DENCUN_FORK_EPOCH: Epoch;

@IsNumber()
@Min(32)
@Transform(({ value }) => parseInt(value, 10), { toClassOnly: true })
Expand Down
34 changes: 33 additions & 1 deletion src/common/consensus-provider/consensus-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import { EpochProcessingState } from 'storage/clickhouse';

import { BlockCache, BlockCacheService } from './block-cache';
import { MaxDeepError, ResponseError, errCommon, errRequest } from './errors';
import { BlockHeaderResponse, BlockInfoResponse, GenesisResponse, ProposerDutyInfo, SyncCommitteeInfo, VersionResponse } from './intefaces';
import {
BlockHeaderResponse,
BlockInfoResponse,
GenesisResponse,
ProposerDutyInfo,
SpecResponse,
SyncCommitteeInfo,
VersionResponse,
} from './intefaces';
import { BlockId, Epoch, Slot, StateId } from './types';

let ssz: typeof import('@lodestar/types').ssz;
Expand All @@ -29,6 +37,11 @@ interface RequestRetryOptions {
useFallbackOnResolved?: (r: any) => boolean;
}

export interface ForkEpochs {
deneb: number;
electra: number;
}

@Injectable()
export class ConsensusProviderService {
protected apiUrls: string[];
Expand All @@ -37,10 +50,12 @@ export class ConsensusProviderService {
protected genesisTime = 0;
protected defaultMaxSlotDeepCount = 32;
protected latestSlot = { slot: 0, fetchTime: 0 };
protected forkEpochs: ForkEpochs;

protected endpoints = {
version: 'eth/v1/node/version',
genesis: 'eth/v1/beacon/genesis',
spec: 'eth/v1/config/spec',
blockInfo: (blockId: BlockId): string => `eth/v2/beacon/blocks/${blockId}`,
beaconHeaders: (blockId: BlockId): string => `eth/v1/beacon/headers/${blockId}`,
attestationCommittees: (stateId: StateId, epoch: Epoch): string => `eth/v1/beacon/states/${stateId}/committees?epoch=${epoch}`,
Expand Down Expand Up @@ -68,6 +83,23 @@ export class ConsensusProviderService {
return (this.version = version);
}

public async getForkEpochs(): Promise<ForkEpochs> {
if (this.forkEpochs != null) {
return this.forkEpochs;
}

const spec = await this.retryRequest<SpecResponse>(async (apiURL: string) => this.apiGet(apiURL, this.endpoints.spec));
this.forkEpochs = {
deneb: spec.DENEB_FORK_EPOCH != null ? parseInt(spec.DENEB_FORK_EPOCH, 10) : Number.MAX_SAFE_INTEGER,
electra: spec.ELECTRA_FORK_EPOCH != null ? parseInt(spec.ELECTRA_FORK_EPOCH, 10) : Number.MAX_SAFE_INTEGER,
};

this.logger.log(`Deneb fork epoch: ${this.forkEpochs.deneb}`);
this.logger.log(`Electra fork epoch: ${this.forkEpochs.electra}`);

return this.forkEpochs;
}

public async getGenesisTime(): Promise<number> {
if (this.genesisTime > 0) {
return this.genesisTime;
Expand Down
51 changes: 6 additions & 45 deletions src/common/consensus-provider/intefaces/response.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ export enum ValStatus {
WithdrawalDone = 'withdrawal_done',
}

export interface AttesterDutyInfo {
pubkey: string;
validator_index: string;
committee_index: string;
committee_length: string;
committees_at_slot: string;
validator_committee_index: string;
slot: string;
}

export interface CheckedAttesterDutyInfo extends AttesterDutyInfo {
attested: boolean;
valid_head: boolean;
valid_target: boolean;
valid_source: boolean;
inclusion_delay: number;
in_block: string | undefined;
}

export interface BlockHeaderResponse {
root: RootHex;
canonical: boolean;
Expand Down Expand Up @@ -99,6 +80,7 @@ export interface ProposerDutyInfo {

export interface BeaconBlockAttestation {
aggregation_bits: string;
committee_bits?: string;
data: {
slot: string;
index: string;
Expand All @@ -114,32 +96,6 @@ export interface BeaconBlockAttestation {
};
}

export interface StateValidatorResponse {
index: string;
balance: string;
status: (typeof ValStatus)[keyof typeof ValStatus];
validator: {
pubkey: string;
withdrawal_credentials: string;
effective_balance: string;
slashed: boolean;
activation_eligibility_epoch: string;
activation_epoch: string;
exit_epoch: string;
withdrawable_epoch: string;
};
}

export interface SyncCommitteeDutyInfo {
pubkey: string;
validator_index: ValidatorIndex;
validator_sync_committee_indices: string[];
results: {
block: string;
sync: boolean;
}[];
}

export interface SyncCommitteeInfo {
validators: ValidatorIndex[];
}
Expand All @@ -159,3 +115,8 @@ export interface SyncCommitteeValidator {
export interface VersionResponse {
version: string;
}

export interface SpecResponse {
DENEB_FORK_EPOCH?: string;
ELECTRA_FORK_EPOCH?: string;
}
8 changes: 4 additions & 4 deletions src/duty/attestation/attestation.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ const timelyTarget = (attIncDelay: number, attValidSource: boolean, attValidTarg
return attValidSource && attValidTarget && attIncDelay <= 32;
};

const timelyTargetDencun = (attValidSource: boolean, attValidTarget: boolean): boolean => {
const timelyTargetDeneb = (attValidSource: boolean, attValidTarget: boolean): boolean => {
return attValidSource && attValidTarget;
};

const timelyHead = (attIncDelay: number, attValidSource: boolean, attValidTarget: boolean, attValidHead: boolean): boolean => {
return attValidSource && attValidTarget && attValidHead && attIncDelay === 1;
};

export const getFlags = (
export const getAttestationFlags = (
attIncDelay: number,
attValidSource: boolean,
attValidTarget: boolean,
attValidHead: boolean,
isDencunFork: boolean,
isDenebFork: boolean,
) => {
return {
source: timelySource(attIncDelay, attValidSource),
target: isDencunFork ? timelyTargetDencun(attValidSource, attValidTarget) : timelyTarget(attIncDelay, attValidSource, attValidTarget),
target: isDenebFork ? timelyTargetDeneb(attValidSource, attValidTarget) : timelyTarget(attIncDelay, attValidSource, attValidTarget),
head: timelyHead(attIncDelay, attValidSource, attValidTarget, attValidHead),
};
};
Expand Down
Loading
Loading