Skip to content

Commit

Permalink
fix: fix error when eth address is invalid (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e authored Aug 28, 2023
1 parent 65ddd7b commit 6cf42df
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
5 changes: 2 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ export default {
coverageProvider: 'v8',

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/', '<rootDir>/test/fixtures/'],
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/build/', '<rootDir>/test/fixtures/'],

preset: 'ts-jest',
testEnvironment: 'jest-environment-node-single-context',
setupFiles: ['dotenv/config'],
moduleFileExtensions: ['js', 'ts'],
testMatch: ['<rootDir>/test/e2e/**/?(*.)+(spec|test).(ts|js)'],
testPathIgnorePatterns: ['dist/'],
testPathIgnorePatterns: ['build/'],
verbose: true
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.18.0",
"@ethersproject/address": "^5.7.0",
"@snapshot-labs/keycard": "0.2.0",
"@snapshot-labs/snapshot-metrics": "^1.0.0",
"@snapshot-labs/snapshot-sentry": "^1.1.0",
Expand Down
27 changes: 18 additions & 9 deletions src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express';
import snapshot from '@snapshot-labs/strategies';
import { getAddress } from '@ethersproject/address';
import scores from './scores';
import { clone, formatStrategies, rpcSuccess, rpcError, blockNumByNetwork, getIp } from './utils';
import { version } from '../package.json';
Expand All @@ -14,14 +15,22 @@ const router = express.Router();
router.post('/', async (req, res) => {
const { id = null, method, params = {} } = req.body;

if (!method) return rpcError(res, 500, 'missing method', id);
if (
(method === 'get_vp' && !params.address) ||
(method === 'validate' && !params.author) ||
params.address === EMPTY_ADDRESS ||
params.author === EMPTY_ADDRESS
)
return rpcError(res, 500, 'invalid address', id);
if (!method) return rpcError(res, 400, 'missing method', id);

try {
if (
(method === 'get_vp' && !params.address) ||
(method === 'validate' && !params.author) ||
params.address === EMPTY_ADDRESS ||
params.author === EMPTY_ADDRESS
) {
throw new Error('invalid address');
}
getAddress(params.address || params.author);
} catch (e: any) {
return rpcError(res, 400, 'invalid address', id);
}

if (method === 'get_vp') {
try {
const response: any = await serve(JSON.stringify(params), getVp, [params]);
Expand Down Expand Up @@ -65,7 +74,7 @@ router.post('/', async (req, res) => {
}
}

return rpcError(res, 500, 'wrong method', id);
return rpcError(res, 400, 'wrong method', id);
});

router.get('/', (req, res) => {
Expand Down
15 changes: 11 additions & 4 deletions test/e2e/get_vp.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import request from 'supertest';

describe('getVp', () => {
describe('when the address params is missing', () => {
it('returns a 500 error', async () => {
const response = await request(process.env.HOST).post('/').send({ method: 'get_vp' });
describe('when the address is invalid', () => {
it.each([
['empty address', '0x0000000000000000000000000000000000000000'],
['empty string', ''],
['null', null],
['invalid address', 'test']
])('returns a 400 error on %s', async (title, address) => {
const response = await request(process.env.HOST)
.post('/')
.send({ method: 'get_vp', address });

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

Expand Down
30 changes: 4 additions & 26 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import request from 'supertest';

const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';

describe('/', () => {
describe('when method params is missing', () => {
it('returns a 500 error', async () => {
it('returns a 400 error', async () => {
const response = await request(process.env.HOST).post('/').send({});

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

describe('when method params is invalid', () => {
it('returns a 500 error', async () => {
it('returns a 400 error', async () => {
const response = await request(process.env.HOST).post('/').send({ method: 'test' });

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

describe('when the address params is blank', () => {
it('returns a 500 error', async () => {
const response = await request(process.env.HOST)
.post('/')
.send({ method: 'get_vp', address: EMPTY_ADDRESS });

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

describe('when the author params is blank', () => {
it('returns a 500 error', async () => {
const response = await request(process.env.HOST)
.post('/')
.send({ method: 'get_vp', author: EMPTY_ADDRESS });

expect(response.status).toEqual(500);
expect(response.status).toEqual(400);
});
});
});
17 changes: 17 additions & 0 deletions test/e2e/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import request from 'supertest';

describe('validate', () => {
describe('when the author is invalid', () => {
it.each([
['empty address', '0x0000000000000000000000000000000000000000'],
['empty string', ''],
['null', null],
['invalid address', 'test']
])('returns a 400 error on %s', async (title, author) => {
const response = await request(process.env.HOST)
.post('/')
.send({ method: 'validate', author });

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

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

0 comments on commit 6cf42df

Please sign in to comment.