Skip to content

Commit

Permalink
fix: ignore cache while fetching data from head (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgorkavenko committed Apr 11, 2023
1 parent f2db13c commit bddfdef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,9 +145,9 @@ export class ConsensusProviderService {
});
}

public async getBlockHeader(blockId: BlockId): Promise<BlockHeaderResponse | void> {
public async getBlockHeader(blockId: BlockId, ignoreCache = false): Promise<BlockHeaderResponse | void> {
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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -210,8 +212,12 @@ export class ConsensusProviderService {
return header;
}

public async getPreviousNotMissedBlockHeader(slot: Slot, maxDeep = this.defaultMaxSlotDeepCount): Promise<BlockHeaderResponse> {
const header = await this.getBlockHeader(slot);
public async getPreviousNotMissedBlockHeader(
slot: Slot,
maxDeep = this.defaultMaxSlotDeepCount,
ignoreCache = false,
): Promise<BlockHeaderResponse> {
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}`);
Expand All @@ -225,10 +231,10 @@ export class ConsensusProviderService {
/**
* Trying to get attester or proposer duty dependent block root
*/
public async getDutyDependentRoot(epoch: Epoch): Promise<string> {
public async getDutyDependentRoot(epoch: Epoch, ignoreCache = false): Promise<string> {
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;
}

/**
Expand Down Expand Up @@ -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<ProposerDutyInfo[]> {
public async getCanonicalProposerDuties(epoch: Epoch, maxRetriesForGetCanonical = 3, ignoreCache = false): Promise<ProposerDutyInfo[]> {
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 },
Expand Down
3 changes: 1 addition & 2 deletions src/duty/duty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ export class DutyService {
const actualSlotHeader = <BlockHeaderResponse>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])];
}
Expand Down
4 changes: 1 addition & 3 deletions src/duty/propose/propose.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export class ProposeService {

@TrackTask('check-proposer-duties')
public async check(epoch: Epoch): Promise<void> {
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);
Expand Down

0 comments on commit bddfdef

Please sign in to comment.