Skip to content

Commit

Permalink
fix: added sort for blocks, return last 5
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen-burkovskyi committed Jul 9, 2024
1 parent 80e20f5 commit a458693
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/modules/staking/services/staking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,7 @@ export class StakingService implements OnModuleInit {
try {
const recentlyBlocks: RecentlyProposedBlockDto[] =
await this.cache.getValidatorRecentlyProposedBlocks(address);
return recentlyBlocks.sort(
(a, b) => Number.parseFloat(b.height) - Number.parseFloat(a.height)
);
return this.sortRecentlyProposedBlocks(recentlyBlocks);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
Log.warn(
Expand All @@ -532,6 +530,12 @@ export class StakingService implements OnModuleInit {
return [];
}

private sortRecentlyProposedBlocks(blocks: RecentlyProposedBlockDto[]) {
return blocks.sort(
(a, b) => Number.parseFloat(b.height) - Number.parseFloat(a.height)
);
}

private async getSortedValidatorSignatures(address: string) {
try {
const signatures: SignatureDto[] =
Expand All @@ -551,7 +555,13 @@ export class StakingService implements OnModuleInit {
}

async getRecentlyProposedBlocks() {
const recentlyProposedBlocks = await this.cache.getRecentlyProposedBlocks();
return recentlyProposedBlocks || [];
const blocks = await this.cache.getRecentlyProposedBlocks();

if(!blocks) {
return [];
}

const sorted = this.sortRecentlyProposedBlocks(blocks);
return sorted.slice(0, 5);
}
}

0 comments on commit a458693

Please sign in to comment.