Skip to content

Commit

Permalink
fix(rpc): Fix bigint serialisation in API responses (#1644)
Browse files Browse the repository at this point in the history
Fixes error in Sandbox getting started where querying the balance for a
user would cause a Koa error.
spalladino authored Aug 18, 2023
1 parent 044b9a9 commit d1ce814
Showing 5 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@
"koa-router": "^12.0.0",
"leveldown": "^6.1.1",
"levelup": "^5.1.1",
"lodash.clonedeepwith": "^4.5.0",
"memdown": "^6.1.1",
"sha3": "^2.1.4"
},
@@ -81,6 +82,7 @@
"@types/koa__cors": "^4.0.0",
"@types/leveldown": "^4.0.3",
"@types/levelup": "^5.1.2",
"@types/lodash.clonedeepwith": "^4.5.7",
"@types/memdown": "^3.0.1",
"@types/node": "^18.7.23",
"@types/supertest": "^2.0.12",
13 changes: 12 additions & 1 deletion yarn-project/foundation/src/json-rpc/convert.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from 'buffer';

import { ClassConverter } from './class_converter.js';
import { convertFromJsonObj, convertToJsonObj } from './convert.js';
import { convertBigintsInObj, convertFromJsonObj, convertToJsonObj } from './convert.js';
import { TestNote } from './fixtures/test_state.js';

const TEST_BASE64 = 'YmFzZTY0IGRlY29kZXI=';
@@ -13,3 +13,14 @@ test('test an RPC function over client', () => {
expect(convertFromJsonObj(cc, convertToJsonObj(cc, note))).toBeInstanceOf(TestNote);
expect(convertFromJsonObj(cc, convertToJsonObj(cc, note)).toString()).toBe('1');
});

test('converts a bigint', () => {
expect(convertBigintsInObj(10n)).toEqual({ type: 'bigint', data: '10' });
expect(convertBigintsInObj({ value: 10n })).toEqual({ value: { type: 'bigint', data: '10' } });
expect(convertBigintsInObj([10n])).toEqual([{ type: 'bigint', data: '10' }]);
});

test('does not convert a string', () => {
expect(convertBigintsInObj('hello')).toEqual('hello');
expect(convertBigintsInObj({ msg: 'hello' })).toEqual({ msg: 'hello' });
});
15 changes: 5 additions & 10 deletions yarn-project/foundation/src/json-rpc/convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer';
import cloneDeepWith from 'lodash.clonedeepwith';

import { ClassConverter } from './class_converter.js';

@@ -8,17 +9,11 @@ import { ClassConverter } from './class_converter.js';
* @returns The converted object with stringified bigints.
*/
export const convertBigintsInObj = (obj: any) => {
for (const i in obj) {
if (typeof obj[i] === 'bigint') {
obj[i] = {
type: 'bigint',
data: obj[i].toString(),
};
} else if (typeof obj[i] === 'object') {
convertBigintsInObj(obj[i]);
return cloneDeepWith(obj, (value: any) => {
if (typeof value === 'bigint') {
return { type: 'bigint', data: value.toString() };
}
}
return obj;
});
};

/**
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ export class JsonRpcServer {
};
const app = new Koa();
app.on('error', error => {
this.log.error(`KOA app-level error. ${JSON.stringify({ error })}`);
this.log.error(`Error on API handler: ${error}`);
});
app.use(compress({ br: false } as any));
app.use(bodyParser());
@@ -111,7 +111,6 @@ export class JsonRpcServer {
} else {
try {
const result = await this.proxy.call(method, params);

ctx.body = {
jsonrpc,
id,
18 changes: 18 additions & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
@@ -435,6 +435,7 @@ __metadata:
"@types/koa__cors": ^4.0.0
"@types/leveldown": ^4.0.3
"@types/levelup": ^5.1.2
"@types/lodash.clonedeepwith": ^4.5.7
"@types/memdown": ^3.0.1
"@types/node": ^18.7.23
"@types/supertest": ^2.0.12
@@ -455,6 +456,7 @@ __metadata:
koa-router: ^12.0.0
leveldown: ^6.1.1
levelup: ^5.1.1
lodash.clonedeepwith: ^4.5.0
memdown: ^6.1.1
prettier: ^2.7.1
sha3: ^2.1.4
@@ -3256,6 +3258,15 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash.clonedeepwith@npm:^4.5.7":
version: 4.5.7
resolution: "@types/lodash.clonedeepwith@npm:4.5.7"
dependencies:
"@types/lodash": "*"
checksum: 2cfc7c55533abf491a2b6897a233244b187bf55ac51545e4f0b937721fdfb7e82cb3743290aa4dcb41487d653d95fdddc172b39318a893c9a2ca6658b0866430
languageName: node
linkType: hard

"@types/lodash.compact@npm:^3.0.7":
version: 3.0.7
resolution: "@types/lodash.compact@npm:3.0.7"
@@ -8786,6 +8797,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.clonedeepwith@npm:^4.5.0":
version: 4.5.0
resolution: "lodash.clonedeepwith@npm:4.5.0"
checksum: 9fbf4ebfa04b381df226a2298eba680327bea3d0d5d19c5118de7ae218fd219186e30e9fd0d33b13729f34ffbc83c1cf09cb27aff265ba94cb602b8a2b1e71c9
languageName: node
linkType: hard

"lodash.compact@npm:^3.0.1":
version: 3.0.1
resolution: "lodash.compact@npm:3.0.1"

0 comments on commit d1ce814

Please sign in to comment.