From 12f6873abff070e6f20582edbb170c2ef4af08c7 Mon Sep 17 00:00:00 2001 From: gpBlockchain <744158715@qq.com> Date: Sun, 14 Jan 2024 22:47:30 +0800 Subject: [PATCH] support light mock --- .github/workflows/ci_light.yml | 43 ++++++++++++++++++++ package.json | 3 +- prepare.sh | 8 +++- test/mock/test_util.ts | 17 ++++++++ test/mockLight/fetch_header.test.ts | 10 +++++ test/mockLight/fetch_transaction.test.ts | 9 +++++ test/mockLight/get_cells.test.ts | 34 ++++++++++++++++ test/mockLight/get_cells_capacity.test.ts | 17 ++++++++ test/mockLight/get_genesis_block.test.ts | 11 ++++++ test/mockLight/get_header.test.ts | 13 ++++++ test/mockLight/get_peers.test.ts | 11 ++++++ test/mockLight/get_scripts.test.ts | 11 ++++++ test/mockLight/get_tip_header.test.ts | 10 +++++ test/mockLight/get_transaction.test.ts | 10 +++++ test/mockLight/get_transactions.test.ts | 30 ++++++++++++++ test/mockLight/local_node_info.test.ts | 11 ++++++ test/mockLight/send_transaction.test.ts | 12 ++++++ test/mockLight/set_scripts.test.ts | 48 +++++++++++++++++++++++ 18 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/ci_light.yml create mode 100644 test/mockLight/fetch_header.test.ts create mode 100644 test/mockLight/fetch_transaction.test.ts create mode 100644 test/mockLight/get_cells.test.ts create mode 100644 test/mockLight/get_cells_capacity.test.ts create mode 100644 test/mockLight/get_genesis_block.test.ts create mode 100644 test/mockLight/get_header.test.ts create mode 100644 test/mockLight/get_peers.test.ts create mode 100644 test/mockLight/get_scripts.test.ts create mode 100644 test/mockLight/get_tip_header.test.ts create mode 100644 test/mockLight/get_transaction.test.ts create mode 100644 test/mockLight/get_transactions.test.ts create mode 100644 test/mockLight/local_node_info.test.ts create mode 100644 test/mockLight/send_transaction.test.ts create mode 100644 test/mockLight/set_scripts.test.ts diff --git a/.github/workflows/ci_light.yml b/.github/workflows/ci_light.yml new file mode 100644 index 0000000..a650afd --- /dev/null +++ b/.github/workflows/ci_light.yml @@ -0,0 +1,43 @@ +name: ci-light + +on: + push: + branches: + - main + pull_request: + + +jobs: + npm-install-1: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '16' + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + - name: Node Cache + uses: actions/cache@v2 + id: npm-and-yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) + with: + path: | + ${{ steps.yarn-cache-dir-path.outputs.dir }} + ~/.npm + key: ${{ runner.os }}-node_modules-${{ hashFiles('/home/runner/work/**/package-lock.json', '/home/runner/work/**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-node_modules- + - name: Run tests + id: runtest + run: | + sh prepare.sh + npm install --save-dev mocha @types/mocha chai @types/chai + npm run build:lumos + npm run test-mock-light + - name: Publish reports + if: failure() + uses: actions/upload-artifact@v2 + with: + name: jfoa-build-reports-${{ runner.os }} + path: test-report.html \ No newline at end of file diff --git a/package.json b/package.json index 5812a73..7585eec 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "scripts": { "build:lumos": "cd lumos && pnpm install && pnpm run build", "test": "mocha --config test/runners/mocha/.mocharc.jsonc --reporter mochawesome", - "test-mock": "jest --verbose ./test/mock" + "test-mock": "jest --verbose ./test/mock", + "test-mock-light": "jest --verbose ./test/mockLight" } } diff --git a/prepare.sh b/prepare.sh index 82296c0..7f378f8 100644 --- a/prepare.sh +++ b/prepare.sh @@ -1,6 +1,6 @@ git clone https://github.com/ckb-js/lumos.git cd lumos -git checkout ckb2023 +git checkout develop cd ../ git clone https://github.com/gpBlockchain/ckb-rpc-mock-data.git cd ckb-rpc-mock-data @@ -9,4 +9,10 @@ pip install -r requirements.txt pip install Werkzeug==2.2.2 python3 api/index.py > index.log 2>&1 & sleep 5 +cat index.log +cd ../ +git clone https://github.com/gpBlockchain/ckb-light-client-rpc-mock-data.git +cd ckb-light-client-rpc-mock-data +python3 api/index.py > index.log 2>&1 & +sleep 5 cat index.log \ No newline at end of file diff --git a/test/mock/test_util.ts b/test/mock/test_util.ts index fc5727e..9bb5070 100644 --- a/test/mock/test_util.ts +++ b/test/mock/test_util.ts @@ -1,10 +1,17 @@ import {RPC} from "@ckb-lumos/rpc"; import {get} from "../util/util"; +import {LightClientRPC} from "@ckb-lumos/light-client"; export async function mock_rpc(){ let data = get_method_and_params(expect.getState().currentTestName) return get_mock_test_data(data.method,data.params) } + +export async function mock_light_rpc(){ + let data = get_method_and_params(expect.getState().currentTestName) + return get_mock_light_test_data(data.method,data.params) +} + async function get_mock_test_data(method: string, params: string) { let data = await get(`http://127.0.0.1:5000/test/${method}/${params}`) @@ -15,6 +22,16 @@ async function get_mock_test_data(method: string, params: string) { } +async function get_mock_light_test_data(method: string, params: string) { + + let data = await get(`http://127.0.0.1:5001/test/${method}/${params}`) + let requestData = data['request'] + let responseData = data['response'] + let LightRPCClient = new LightClientRPC(`http://127.0.0.1:5001/test/${method}/${params}`); + return {LightRPCClient,requestData, responseData} + +} + function get_method_and_params(currentTestName: string): { method: string, params: string } { let [name, params] = splitFirstSpace(currentTestName) return { diff --git a/test/mockLight/fetch_header.test.ts b/test/mockLight/fetch_header.test.ts new file mode 100644 index 0000000..3398a2b --- /dev/null +++ b/test/mockLight/fetch_header.test.ts @@ -0,0 +1,10 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("fetch_header", function () { + + it("fetched",async ()=>{ + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.fetchHeader(requestData["params"][0]) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/fetch_transaction.test.ts b/test/mockLight/fetch_transaction.test.ts new file mode 100644 index 0000000..2eb86a5 --- /dev/null +++ b/test/mockLight/fetch_transaction.test.ts @@ -0,0 +1,9 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("fetch_transaction", function () { + it("fetched", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.fetchTransaction(requestData["params"][0]) + expect(camelCaseToUnderscore(response['transaction'])).toEqual(responseData["result"]['transaction']) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_cells.test.ts b/test/mockLight/get_cells.test.ts new file mode 100644 index 0000000..37f20b1 --- /dev/null +++ b/test/mockLight/get_cells.test.ts @@ -0,0 +1,34 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; +import {toSearchKey} from "@ckb-lumos/ckb-indexer/lib/resultFormatter"; + +describe("get_cells", function () { + + it("[search_key,order,limit,after_cursor]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getCells( + { + script: toSearchKey(requestData['params'][0]).script, + scriptType: toSearchKey(requestData['params'][0]).scriptType, + withData: requestData["params"][0]['with_data'] + }, + requestData["params"][1], + requestData["params"][2], + requestData["params"][3] + ) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) + it("[search_key,order,limit,null]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.getCells( + { + script: toSearchKey(requestData['params'][0]).script, + scriptType: toSearchKey(requestData['params'][0]).scriptType, + withData: requestData["params"][0]['with_data'] + }, requestData["params"][1], + requestData["params"][2] + ) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) + +}); \ No newline at end of file diff --git a/test/mockLight/get_cells_capacity.test.ts b/test/mockLight/get_cells_capacity.test.ts new file mode 100644 index 0000000..c807c25 --- /dev/null +++ b/test/mockLight/get_cells_capacity.test.ts @@ -0,0 +1,17 @@ +import {camelCaseToUnderscore, mock_light_rpc, mock_rpc} from "../mock/test_util"; +import {toSearchKey} from "@ckb-lumos/ckb-indexer/lib/resultFormatter"; + +describe("get_cells_capacity", function () { + + it("[search_key]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getCellsCapacity( + { + script: toSearchKey(requestData['params'][0]).script, + scriptType: toSearchKey(requestData['params'][0]).scriptType, + } + ) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_genesis_block.test.ts b/test/mockLight/get_genesis_block.test.ts new file mode 100644 index 0000000..8a63a20 --- /dev/null +++ b/test/mockLight/get_genesis_block.test.ts @@ -0,0 +1,11 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_genesis_block", function () { + + it("[]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getGenesisBlock() + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_header.test.ts b/test/mockLight/get_header.test.ts new file mode 100644 index 0000000..f3fedf4 --- /dev/null +++ b/test/mockLight/get_header.test.ts @@ -0,0 +1,13 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_header", function () { + + it("[block_hash]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getHeader( + requestData["params"][0] + ) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_peers.test.ts b/test/mockLight/get_peers.test.ts new file mode 100644 index 0000000..054c5c1 --- /dev/null +++ b/test/mockLight/get_peers.test.ts @@ -0,0 +1,11 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_peers", function () { + + it("[]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getPeers() + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_scripts.test.ts b/test/mockLight/get_scripts.test.ts new file mode 100644 index 0000000..a087fc7 --- /dev/null +++ b/test/mockLight/get_scripts.test.ts @@ -0,0 +1,11 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_scripts", function () { + + it("[]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.getScripts() + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_tip_header.test.ts b/test/mockLight/get_tip_header.test.ts new file mode 100644 index 0000000..140f1ab --- /dev/null +++ b/test/mockLight/get_tip_header.test.ts @@ -0,0 +1,10 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_tip_header", function () { + + it("[]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.getTipHeader() + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_transaction.test.ts b/test/mockLight/get_transaction.test.ts new file mode 100644 index 0000000..7421623 --- /dev/null +++ b/test/mockLight/get_transaction.test.ts @@ -0,0 +1,10 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("get_transaction", function () { + + it("[transaction_hash]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.getTransaction(requestData["params"][0]) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/get_transactions.test.ts b/test/mockLight/get_transactions.test.ts new file mode 100644 index 0000000..d96301a --- /dev/null +++ b/test/mockLight/get_transactions.test.ts @@ -0,0 +1,30 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; +import {toSearchKey} from "@ckb-lumos/ckb-indexer/lib/resultFormatter"; + +describe("get_transactions", function () { + + it("groupedTxWithCells:js", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.getTransactions( + { + script: toSearchKey(requestData['params'][0]).script, + scriptType: toSearchKey(requestData['params'][0]).scriptType, + groupByTransaction: true, + }, requestData['params'][1], requestData['params'][2] + ) + console.log(response.objects) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) + + it("ungroupedTxWithCell:js",async ()=>{ + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let response = await LightRPCClient.getTransactions( + { + script: toSearchKey(requestData['params'][0]).script, + scriptType: toSearchKey(requestData['params'][0]).scriptType + }, requestData['params'][1], requestData['params'][2] + ) + console.log(response.objects) + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/local_node_info.test.ts b/test/mockLight/local_node_info.test.ts new file mode 100644 index 0000000..73a0bd6 --- /dev/null +++ b/test/mockLight/local_node_info.test.ts @@ -0,0 +1,11 @@ +import {camelCaseToUnderscore, mock_light_rpc} from "../mock/test_util"; + +describe("local_node_info", function () { + + it("[]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + + let response = await LightRPCClient.localNodeInfo() + expect(camelCaseToUnderscore(response)).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/send_transaction.test.ts b/test/mockLight/send_transaction.test.ts new file mode 100644 index 0000000..5548332 --- /dev/null +++ b/test/mockLight/send_transaction.test.ts @@ -0,0 +1,12 @@ +import { mock_light_rpc} from "../mock/test_util"; +import {toTransaction} from "@ckb-lumos/rpc/lib/resultFormatter"; + +describe("send_transaction", function () { + + it("[tx]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let tx = toTransaction(requestData["params"][0]) + let txHash = await LightRPCClient.sendTransaction(tx) + expect(txHash).toEqual(responseData["result"]) + }) +}); \ No newline at end of file diff --git a/test/mockLight/set_scripts.test.ts b/test/mockLight/set_scripts.test.ts new file mode 100644 index 0000000..9dfb586 --- /dev/null +++ b/test/mockLight/set_scripts.test.ts @@ -0,0 +1,48 @@ +import {mock_light_rpc} from "../mock/test_util"; +import {toScript} from "@ckb-lumos/rpc/lib/resultFormatter"; + + + +describe("set_scripts", function () { + + it("[Vec,SetScriptCommand:all]", async () => { + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let setLightScript = requestData["params"][0].map(({script, script_type, block_number}) => ({ + script: toScript(script), + scriptType: script_type, + blockNumber: block_number, + })); + await LightRPCClient.setScripts(setLightScript, requestData["params"][1]) + }) + it("[Vec,SetScriptCommand:delete]",async ()=>{ + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let setLightScript = requestData["params"][0].map(({script, script_type, block_number}) => ({ + script: toScript(script), + scriptType: script_type, + blockNumber: block_number, + })); + await LightRPCClient.setScripts(setLightScript, requestData["params"][1]) + }) + + it("[Vec,SetScriptCommand:partial]",async ()=>{ + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let setLightScript = requestData["params"][0].map(({script, script_type, block_number}) => ({ + script: toScript(script), + scriptType: script_type, + blockNumber: block_number, + })); + await LightRPCClient.setScripts(setLightScript, requestData["params"][1]) + }) + + it("[Vec,null]",async ()=>{ + let {LightRPCClient, requestData, responseData} = await mock_light_rpc() + let setLightScript = requestData["params"][0].map(({script, script_type, block_number}) => ({ + script: toScript(script), + scriptType: script_type, + blockNumber: block_number, + })); + await LightRPCClient.setScripts(setLightScript) + }) + + +}); \ No newline at end of file