From bddfdefcbb84171ba648f421bc03a7130289b397 Mon Sep 17 00:00:00 2001 From: Vladimir Gorkavenko <32727352+vgorkavenko@users.noreply.github.com> Date: Mon, 10 Apr 2023 19:39:31 +0500 Subject: [PATCH] fix: ignore cache while fetching data from head (#155) --- .../consensus-provider.service.ts | 30 +++++++++++-------- src/duty/duty.service.ts | 3 +- src/duty/propose/propose.service.ts | 4 +-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/common/eth-providers/consensus-provider/consensus-provider.service.ts b/src/common/eth-providers/consensus-provider/consensus-provider.service.ts index b4ff4f45..58f120ff 100644 --- a/src/common/eth-providers/consensus-provider/consensus-provider.service.ts +++ b/src/common/eth-providers/consensus-provider/consensus-provider.service.ts @@ -22,7 +22,7 @@ import { SyncCommitteeInfo, VersionResponse, } from './intefaces'; -import { BlockId, Epoch, RootHex, Slot, StateId } from './types'; +import { BlockId, Epoch, Slot, StateId } from './types'; interface RequestRetryOptions { maxRetries?: number; @@ -145,9 +145,9 @@ export class ConsensusProviderService { }); } - public async getBlockHeader(blockId: BlockId): Promise { + public async getBlockHeader(blockId: BlockId, ignoreCache = false): Promise { const cached: BlockCache = this.cache.get(String(blockId)); - if (cached && (cached.missed || cached.header)) { + if (!ignoreCache && cached && (cached.missed || cached.header)) { this.logger.debug(`Get ${blockId} header from blocks cache`); return cached.missed ? undefined : cached.header; } @@ -171,7 +171,9 @@ export class ConsensusProviderService { } }); - this.cache.set(String(blockId), { missed: !blockHeader, header: blockHeader }); + if (!ignoreCache) { + this.cache.set(String(blockId), { missed: !blockHeader, header: blockHeader }); + } return blockHeader; } @@ -210,8 +212,12 @@ export class ConsensusProviderService { return header; } - public async getPreviousNotMissedBlockHeader(slot: Slot, maxDeep = this.defaultMaxSlotDeepCount): Promise { - const header = await this.getBlockHeader(slot); + public async getPreviousNotMissedBlockHeader( + slot: Slot, + maxDeep = this.defaultMaxSlotDeepCount, + ignoreCache = false, + ): Promise { + const header = await this.getBlockHeader(slot, ignoreCache); if (!header) { if (maxDeep < 1) { throw new MaxDeepError(`Error when trying to get previous not missed block header. From ${slot} to ${slot - maxDeep}`); @@ -225,10 +231,10 @@ export class ConsensusProviderService { /** * Trying to get attester or proposer duty dependent block root */ - public async getDutyDependentRoot(epoch: Epoch): Promise { + public async getDutyDependentRoot(epoch: Epoch, ignoreCache = false): Promise { this.logger.log(`Getting duty dependent root for epoch ${epoch}`); const dutyRootSlot = epoch * this.config.get('FETCH_INTERVAL_SLOTS') - 1; - return (await this.getPreviousNotMissedBlockHeader(dutyRootSlot)).root; + return (await this.getPreviousNotMissedBlockHeader(dutyRootSlot, this.defaultMaxSlotDeepCount, ignoreCache)).root; } /** @@ -321,13 +327,11 @@ export class ConsensusProviderService { return await this.retryRequest(async (apiURL: string) => this.apiGet(apiURL, this.endpoints.syncCommittee(stateId, epoch))); } - public async getCanonicalProposerDuties( - epoch: Epoch, - dependentRoot: RootHex, - maxRetriesForGetCanonical = 3, - ): Promise { + public async getCanonicalProposerDuties(epoch: Epoch, maxRetriesForGetCanonical = 3, ignoreCache = false): Promise { const retry = retrier(this.logger, maxRetriesForGetCanonical, 100, 10000, true); const request = async () => { + const dependentRoot = await this.getDutyDependentRoot(epoch, ignoreCache); + this.logger.log(`Proposer Duty root: ${dependentRoot}`); const res = <{ dependent_root: string; data: ProposerDutyInfo[] }>await this.retryRequest( async (apiURL: string) => this.apiGet(apiURL, this.endpoints.proposerDutes(epoch)), { dataOnly: false }, diff --git a/src/duty/duty.service.ts b/src/duty/duty.service.ts index 3c9d0c1c..72e10355 100644 --- a/src/duty/duty.service.ts +++ b/src/duty/duty.service.ts @@ -96,10 +96,9 @@ export class DutyService { const actualSlotHeader = await this.clClient.getBlockHeader('head'); const headEpoch = Math.trunc(actualSlotHeader.header.message.slot / this.config.get('FETCH_INTERVAL_SLOTS')); this.logger.log('Getting possible high reward validator indexes'); - const propDependentRoot = await this.clClient.getDutyDependentRoot(headEpoch); const [sync, prop] = await allSettled([ this.clClient.getSyncCommitteeInfo('finalized', headEpoch), - this.clClient.getCanonicalProposerDuties(headEpoch, propDependentRoot), + this.clClient.getCanonicalProposerDuties(headEpoch, 3, true), ]); return [...new Set([...prop.map((v) => v.validator_index), ...sync.validators])]; } diff --git a/src/duty/propose/propose.service.ts b/src/duty/propose/propose.service.ts index 319b073f..d7bf80a4 100644 --- a/src/duty/propose/propose.service.ts +++ b/src/duty/propose/propose.service.ts @@ -20,10 +20,8 @@ export class ProposeService { @TrackTask('check-proposer-duties') public async check(epoch: Epoch): Promise { - const propDutyDependentRoot = await this.clClient.getDutyDependentRoot(epoch); - this.logger.log(`Proposer Duty root: ${propDutyDependentRoot}`); this.logger.log(`Start getting proposers duties info`); - const proposersDutyInfo = await this.clClient.getCanonicalProposerDuties(epoch, propDutyDependentRoot); + const proposersDutyInfo = await this.clClient.getCanonicalProposerDuties(epoch); this.logger.log(`Processing proposers duties info`); for (const prop of proposersDutyInfo) { const index = Number(prop.validator_index);