Skip to content

Commit

Permalink
feat: add proposer info to block, add new endpoint to fetch last prop…
Browse files Browse the repository at this point in the history
…osed blocks, add new websocket event
  • Loading branch information
yevhen-burkovskyi committed Jul 2, 2024
1 parent a065878 commit 9abb18e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/modules/staking/enums/staking-endpoints.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export enum StakingEndpoints {
VALIDATORS_BY_ADDRESS = '/validators/:address',
VALIDATORS_UPTIME = '/validators/:address/uptime',
VALIDATORS_RECENTLY_PROPOSED_BLOCKS = '/validators/:address/recently-proposed-blocks',
RECENTLY_PROPOSED_BLOCKS = '/recently-proposed-blocks',
}
12 changes: 10 additions & 2 deletions src/modules/staking/services/staking.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,27 @@ export class StakingCache {
return signatures.map(signature => JSON.parse(signature!));
}

async setRecentlyProposedBlock(address: string, block: unknown) {
async setValidatorRecentlyProposedBlock(address: string, block: unknown) {
const key = this.createRedisKey(this.createRedisKey(StakingCachePrefix.VALIDATOR_RECENTLY_PROPOSED_BLOCKS, address), v4());
this.redisService.setWithTTL(key, JSON.stringify(block), config.cache.validatorRecentlyProposedBlock);
}

async getRecentlyProposedBlock(address: string) {
async getValidatorRecentlyProposedBlocks(address: string) {
const pattern = this.createRedisKey(this.createRedisKey(StakingCachePrefix.VALIDATOR_RECENTLY_PROPOSED_BLOCKS, address), '*');
const keys = await this.redisService.keys(pattern);
const recentlyProposedBlocks = await Promise.all(keys.map((key: string) => this.redisService.get(key)));

return recentlyProposedBlocks.map(block => JSON.parse(block!));
}

async getRecentlyProposedBlocks() {
const pattern = this.createRedisKey(this.createRedisKey(StakingCachePrefix.VALIDATOR_RECENTLY_PROPOSED_BLOCKS), '*');
const keys = await this.redisService.keys(pattern);
const recentlyProposedBlocks = await Promise.all(keys.map((key: string) => this.redisService.get(key)));

return recentlyProposedBlocks.map(block => JSON.parse(block!));
}

async setLastBlock(block: unknown) {
this.redisService.set(this.createRedisKey(StakingCachePrefix.LAST_BLOCK), JSON.stringify(block));
}
Expand Down
33 changes: 23 additions & 10 deletions src/modules/staking/services/staking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,21 @@ export class StakingService implements OnModuleInit {
}

async getValidatorByAddress(address: string) {
const validators: ValidatorsViewDto[] = await this.getValidators();
const validator = validators.find(
(validator) => validator.address === address
);

const validator = await this.findSingleValidator(address);
if (!validator) {
throw new BadRequestException(StakingError.VALIDATOR_ADDRESS_NOT_EXISTS);
}

return validator;
}

private async findSingleValidator(address: string) {
const validators: ValidatorsViewDto[] = await this.getValidators();
return validators.find(
(validator) => validator.address === address
);
}

async newBlock(res: BlocksResponse) {
try {
// Create a map of validator addresses to their public keys
Expand All @@ -379,16 +382,21 @@ export class StakingService implements OnModuleInit {
signature.validator_address === res.block.header.proposer_address
) {
signatureView.status = SignatureViewStatus.PROPOSED;

const validator = await this.findSingleValidator(validatorAddress);
const blockWithValidatorInfo = {
...blockView,
img: validator?.logo,
name: validator?.description.moniker
}
// Cache the recently proposed block and emit an event
await this.cache.setRecentlyProposedBlock(
await this.cache.setValidatorRecentlyProposedBlock(
validatorAddress,
blockView
blockWithValidatorInfo
);
this.eventEmitter.emit(
Event.BLOCK_CACHED,
validatorAddress,
blockView
blockWithValidatorInfo
);
}

Expand Down Expand Up @@ -510,7 +518,7 @@ export class StakingService implements OnModuleInit {
private async getSortedRecentlyProposedBlocks(address: string) {
try {
const recentlyBlocks: RecentlyProposedBlockDto[] =
await this.cache.getRecentlyProposedBlock(address);
await this.cache.getValidatorRecentlyProposedBlocks(address);
return recentlyBlocks.sort(
(a, b) => Number.parseFloat(b.height) - Number.parseFloat(a.height)
);
Expand Down Expand Up @@ -541,4 +549,9 @@ export class StakingService implements OnModuleInit {

return [];
}

async getRecentlyProposedBlocks() {
const recentlyProposedBlocks = await this.cache.getRecentlyProposedBlocks();
return recentlyProposedBlocks || [];
}
}
5 changes: 5 additions & 0 deletions src/modules/staking/staking.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ export class StakingController {
) {
return this.service.getValidatorRecentlyProposedBlocks(address);
}

@Get(StakingEndpoints.RECENTLY_PROPOSED_BLOCKS)
async getRecentlyProposedBlocks() {
return this.service.getRecentlyProposedBlocks();
}
}
1 change: 1 addition & 0 deletions src/modules/staking/staking.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export class StakingGateway {
@OnEvent(Event.BLOCK_CACHED)
async blockUpdate(addr: string, block: unknown) {
this.server.emit(`proposed_block.${addr}`, block);
this.server.emit(`proposed_block`, block);
}
}

0 comments on commit 9abb18e

Please sign in to comment.