-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load rpc additional methods by cli (#756)
* feat: load rpc additional methods by cli * test: add e2e test for rpc extension methods & add a guide to README.MD * fix: remove unused imports & console.log -> logger.info * fix: no need to lint the loading test scripts * fix: do yarn fix to format
- Loading branch information
Showing
6 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { env, setupApi, ws } from './helper.js' | ||
import { getRpcExtensionMethods, loadRpcMethodsByScripts } from '@acala-network/chopsticks/plugins/index.js' | ||
import { join, resolve } from 'path' | ||
|
||
setupApi(env.acala) | ||
|
||
describe('rpc methods load by scripts', () => { | ||
it('before load', async () => { | ||
const methods = getRpcExtensionMethods() | ||
console.log(methods) | ||
expect(methods.includes('dev_runBlock')).eq(true) | ||
expect(methods.includes('testdev_testRpcMethod1')).eq(false) | ||
expect(methods.includes('testdev_testRpcMethod2')).eq(false) | ||
}) | ||
it('loaded', async () => { | ||
loadRpcMethodsByScripts(resolve(join(__dirname, 'rpc-methods-test-scripts.js'))) | ||
|
||
const methods = getRpcExtensionMethods() | ||
expect(methods.includes('dev_runBlock')).eq(true) | ||
expect(methods.includes('testdev_testRpcMethod1')).eq(true) | ||
expect(methods.includes('testdev_testRpcMethod2')).eq(true) | ||
}) | ||
it('server rpc test', async () => { | ||
const port = /:(\d+$)/.exec(ws.endpoint)?.[1] | ||
if (!port) { | ||
throw new Error('cannot found port') | ||
} | ||
|
||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'testdev_testRpcMethod1', params: [] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"methods": 1, | ||
"params": [], | ||
}, | ||
} | ||
`, | ||
) | ||
} | ||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'testdev_testRpcMethod2', params: [2] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"methods": 2, | ||
"params": [ | ||
2, | ||
], | ||
}, | ||
} | ||
`, | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* eslint-disable */ | ||
// used to test rpc methods, no need to lint | ||
return { | ||
async testdev_testRpcMethod1(context, params) { | ||
console.log('testdev_testRpcMethod 1', params) | ||
return { methods: 1, params } | ||
}, | ||
async testdev_testRpcMethod2(context, params) { | ||
console.log('testdev_testRpcMethod 2', params) | ||
return { methods: 2, params } | ||
}, | ||
} |