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

fix: Increase strategies limit #998

Merged
merged 7 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
8 changes: 3 additions & 5 deletions src/aws.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as AWS from '@aws-sdk/client-s3';
import { Readable } from 'stream';
import { capture } from '@snapshot-labs/snapshot-sentry';

const name = 'score-api';
const cb = '4';
import { AWS_CACHE_KEY, APP_NAME } from './constants';

let client;
const bucket = process.env.AWS_BUCKET_NAME;
Expand All @@ -24,7 +22,7 @@ export async function set(key, value) {
try {
return await client.putObject({
Bucket: bucket,
Key: `public/${name}/${cb}/${key}.json`,
Key: `public/${APP_NAME}/${AWS_CACHE_KEY}/${key}.json`,
Body: JSON.stringify(value),
ContentType: 'application/json; charset=utf-8'
});
Expand All @@ -38,7 +36,7 @@ export async function get(key) {
try {
const { Body } = await client.getObject({
Bucket: bucket,
Key: `public/${name}/${cb}/${key}.json`
Key: `public/${APP_NAME}/${AWS_CACHE_KEY}/${key}.json`
});
// @ts-ignore
const str = await streamToString(Body);
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
export const MAX_STRATEGIES = 10;
export const APP_NAME = 'score-api';
export const AWS_CACHE_KEY = '4';
3 changes: 2 additions & 1 deletion src/helpers/keycard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Keycard } from '@snapshot-labs/keycard';
import { rpcError } from '../utils';
import { APP_NAME } from '../constants';

const keycard = new Keycard({
app: 'score-api',
app: APP_NAME,
secret: process.env.KEYCARD_SECRET || '',
URL: process.env.KEYCARD_URL || 'https://keycard.snapshot.org'
});
Expand Down
8 changes: 4 additions & 4 deletions src/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ describe('API Routes', () => {
.post('/')
.send({
method: 'get_vp',
params: { address: '0x123' }
params: { address: '0x123', strategies: [{ name: 'ticket' }] }
});
expect(serve).toBeCalledWith(
JSON.stringify({ address: '0x123' }),
JSON.stringify({ address: '0x123', strategies: [{ name: 'ticket' }] }),
getVp,
[{ address: '0x123' }]
[{ address: '0x123', strategies: [{ name: 'ticket' }] }]
);
expect(response.status).toBe(200);
});
Expand All @@ -103,7 +103,7 @@ describe('API Routes', () => {
.post('/')
.send({
method: 'get_vp',
params: { address: '0x123' }
params: { address: '0x123', strategies: [{ name: 'ticket' }] }
});
expect(utils.rpcError).toBeCalledWith(mockedRes, 500, err, null);
expect(response.status).toBe(500);
Expand Down
12 changes: 9 additions & 3 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import getValidations from './helpers/validations';
import disabled from './disabled.json';
import serve from './requestDeduplicator';
import { capture } from '@snapshot-labs/snapshot-sentry';
import { EMPTY_ADDRESS, MAX_STRATEGIES } from './constants';

const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
const router = express.Router();

router.post('/', async (req, res) => {
Expand All @@ -39,8 +39,14 @@ router.post('/', async (req, res) => {

if (method === 'get_vp') {
if (params.space && disabled.includes(params.space))
return rpcError(res, 429, 'too many requests', null);

return rpcError(res, 429, 'too many requests', id);
if (
!params.strategies ||
params.strategies.length === 0 ||
params.strategies.length > MAX_STRATEGIES
) {
return rpcError(res, 400, 'invalid strategies length', id);
}
try {
const response: any = await serve(JSON.stringify(params), getVp, [
params
Expand Down
7 changes: 4 additions & 3 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './utils';
import snapshot from '@snapshot-labs/strategies';
import { createHash } from 'crypto';
import { MAX_STRATEGIES } from './constants';

jest.mock('@snapshot-labs/strategies');
jest.mock('crypto', () => ({
Expand Down Expand Up @@ -258,13 +259,13 @@ describe('formatStrategies function', () => {
expect(formattedStrategies).toHaveLength(2);
});

it('should limit strategies to 8', () => {
const strategies = new Array(10).fill({ name: 'strategy', param: 'a' });
it(`should limit strategies to ${MAX_STRATEGIES}`, () => {
const strategies = new Array(12).fill({ name: 'strategy', param: 'a' });
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
const network = 'defaultNetwork';

const formattedStrategies = formatStrategies(network, strategies);

expect(formattedStrategies).toHaveLength(8);
expect(formattedStrategies).toHaveLength(MAX_STRATEGIES);
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import snapshot from '@snapshot-labs/strategies';
import { createHash } from 'crypto';
import { MAX_STRATEGIES } from './constants';

const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';

Expand Down Expand Up @@ -28,14 +29,14 @@ function sortObjectByParam(obj) {

export function formatStrategies(network, strategies: Array<any> = []) {
strategies = Array.isArray(strategies) ? strategies : [];
// update strategy network, strategy parameters should be same order to maintain consistent key hashes and limit to 8 strategies
// update strategy network, strategy parameters should be same order to maintain consistent key hashes and limit to max strategies
return strategies
.map(strategy => ({
...strategy,
network: strategy?.network || network
}))
.map(sortObjectByParam)
.slice(0, 8);
.slice(0, MAX_STRATEGIES);
}

export function rpcSuccess(res, result, id, cache = false) {
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/get_vp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,21 @@ describe('getVp', () => {
});
});

describe('when the strategies are invalid', () => {
it.each([
['no strategies', null],
['empty strategies', []],
['too many strategies', Array(12).fill({ name: 'test', param: 'a' })]
])('returns a 400 error on %s', async (title, strategies) => {
const response = await request(process.env.HOST).post('/').send({
method: 'get_vp',
address: '0x662a9706c7122D620D410ba565CAfaB29e4CB47f',
strategies
});

expect(response.status).toEqual(400);
});
});

it.todo('returns the voting power');
});