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(rpc): Fix bigint serialisation in API responses #1644

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
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=';
Expand All @@ -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' });
Copy link
Contributor

Choose a reason for hiding this comment

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

is this expected over a throw Error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, the idea is that convertBigints would just returns the same data structure it received, but replacing any bigints it sees along the way. If there are no bigints, no changes needed.

The reason for this test is that the original implementation looped over strings as if they were collections, so I wanted to make sure there were no errors related to dealing with strings.

});
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';

Expand All @@ -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;
});
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -111,7 +111,6 @@ export class JsonRpcServer {
} else {
try {
const result = await this.proxy.call(method, params);

ctx.body = {
jsonrpc,
id,
Expand Down
18 changes: 18 additions & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down