Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract cache logic from getVp #938

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/helpers/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import snapshot from '@snapshot-labs/strategies';
import redis from '../redis';

type VpResult = ReturnType<typeof snapshot.utils.getVp>;

const VP_KEY_PREFIX = 'vp';

export async function cachedVp(key: string, callback: () => VpResult, toCache = false) {
if (!toCache || !redis) {
return { result: await callback(), cache: false };
}

const cache = await redis.hGetAll(`${VP_KEY_PREFIX}:${key}`);

if (cache?.vp_state) {
cache.vp = parseFloat(cache.vp);
cache.vp_by_strategy = JSON.parse(cache.vp_by_strategy);

return { result: cache as Awaited<VpResult>, cache: true };
}

const result = await callback();

if (result.vp_state === 'final') {
const multi = redis.multi();
multi.hSet(`${VP_KEY_PREFIX}:${key}`, 'vp', result.vp);
multi.hSet(`${VP_KEY_PREFIX}:${key}`, 'vp_by_strategy', JSON.stringify(result.vp_by_strategy));
multi.hSet(`${VP_KEY_PREFIX}:${key}`, 'vp_state', result.vp_state);
multi.exec();
}

return { result, cache: false };
}
54 changes: 18 additions & 36 deletions src/methods.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import snapshot from '@snapshot-labs/strategies';
import redis from './redis';
import { sha256, getCurrentBlockNum } from './utils';
import disabled from './disabled.json';
import { sha256, getCurrentBlockNum } from './utils';
import { cachedVp } from './helpers/cache';

interface GetVpRequestParams {
address: string;
Expand All @@ -24,48 +24,30 @@ interface ValidateRequestParams {
const disableCachingForSpaces = ['magicappstore.eth', 'moonbeam-foundation.eth'];

export async function getVp(params: GetVpRequestParams) {
if (['1319'].includes(params.network) || disabled.includes(params.space))
throw 'something wrong with the strategies';

if (typeof params.snapshot !== 'number') params.snapshot = 'latest';

if (params.snapshot !== 'latest') {
const currentBlockNum = await getCurrentBlockNum(params.snapshot, params.network);
params.snapshot = currentBlockNum < params.snapshot ? 'latest' : params.snapshot;
}

const key = sha256(JSON.stringify(params));
const useCache =
redis && params.snapshot !== 'latest' && !disableCachingForSpaces.includes(params.space);
if (useCache) {
const cache = await redis.hGetAll(`vp:${key}`);

if (cache?.vp_state) {
cache.vp = parseFloat(cache.vp);

cache.vp_by_strategy = JSON.parse(cache.vp_by_strategy);
return { result: cache, cache: true };
}
}

if (['1319'].includes(params.network) || disabled.includes(params.space))
throw 'something wrong with the strategies';

const result = await snapshot.utils.getVp(
params.address,
params.network,
params.strategies,
params.snapshot,
params.space,
params.delegation
return await cachedVp(
sha256(JSON.stringify(params)),
async () => {
return await snapshot.utils.getVp(
params.address,
params.network,
params.strategies,
params.snapshot,
params.space,
params.delegation
);
},
params.snapshot !== 'latest' && !disableCachingForSpaces.includes(params.space)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never seen our team using callbacks 🙈 especially less

should that be fine?

Copy link
Contributor Author

@wa0x6e wa0x6e Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the most elegant way to do this thing.

We use it in the requestDeduplicator

);

if (useCache && result.vp_state === 'final') {
const multi = redis.multi();
multi.hSet(`vp:${key}`, 'vp', result.vp);
multi.hSet(`vp:${key}`, 'vp_by_strategy', JSON.stringify(result.vp_by_strategy));
multi.hSet(`vp:${key}`, 'vp_state', result.vp_state);
multi.exec();
}

return { result, cache: false };
}

export async function validate(params: ValidateRequestParams) {
Expand Down