Skip to content

Commit

Permalink
Merge pull request #34 from axone-protocol/fix/blocks
Browse files Browse the repository at this point in the history
fix: fixed blocks and signatures logic
  • Loading branch information
yevhen-burkovskyi authored Jun 19, 2024
2 parents 0bd0b65 + 5111f96 commit f76bc26
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 152 deletions.
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ import '@utils/config-loader';
await app.listen(config.app.port, config.app.host);

await showAvailableRoutes(app);

Log.success(`${config.app.host}:${config.app.port}`);
Log.success(`Application is running on: ${await app.getUrl()}`);
})().catch(AppExceptionsFilter.catch);
17 changes: 9 additions & 8 deletions src/modules/staking/enums/staking-cache-prefix.enum.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export enum StakingCachePrefix {
STAKING = 'staking',
GLOBAL_OVERVIEW = 'global_overview',
VALIDATORS = 'validators',
VALIDATOR_IMG = 'validator_img',
VALIDATOR_SIGNATURES = 'validator_signatures',
VALIDATOR_RECENTLY_PROPOSED_BLOCKS = 'validator_recently_propored_blocks',
PROPOSALS = 'proposals',
PROPOSAL = 'proposal',
STAKING = "staking",
GLOBAL_OVERVIEW = "global_overview",
VALIDATORS = "validators",
VALIDATOR_IMG = "validator_img",
VALIDATOR_SIGNATURES = "validator_signatures",
VALIDATOR_RECENTLY_PROPOSED_BLOCKS = "validator_recently_propored_blocks",
PROPOSALS = "proposals",
PROPOSAL = "proposal",
LAST_BLOCK = "last_block",
}
22 changes: 18 additions & 4 deletions src/modules/staking/services/staking.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,33 @@ export class StakingCache {
return signatures.map(signature => JSON.parse(signature!));
}

async setRecentlyProposedBlock(block: unknown) {
const key = this.createRedisKey(StakingCachePrefix.VALIDATOR_RECENTLY_PROPOSED_BLOCKS, v4());
async setRecentlyProposedBlock(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.validatorSignature);
}

async getRecentlyProposedBlock() {
const pattern = this.createRedisKey(StakingCachePrefix.VALIDATOR_RECENTLY_PROPOSED_BLOCKS, '*');
async getRecentlyProposedBlock(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 setLastBlock(block: unknown) {
this.redisService.set(this.createRedisKey(StakingCachePrefix.LAST_BLOCK), JSON.stringify(block));
}

async getLastBlock() {
const serialized = await this.redisService.get(this.createRedisKey(StakingCachePrefix.LAST_BLOCK));

if (!serialized) {
return null;
}

return JSON.parse(serialized as string);
}

private createRedisKey(...ids: string[]) {
return ids.reduce((acc, id) => acc + `_${id}`, `${StakingCachePrefix.STAKING}`);
}
Expand Down
Loading

0 comments on commit f76bc26

Please sign in to comment.