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

support wasm #9

Merged
merged 10 commits into from
Jan 2, 2025
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USE_LIGHT_CLIENT_WASM=false
53 changes: 53 additions & 0 deletions .github/workflows/ci-data-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: ci-data-wasm

on:
push:
branches:
- master
pull_request:


jobs:
npm-install-1:
strategy:
fail-fast: false
matrix:
net: ['testnet_v1']
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- 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: prepare env
id: runtest
run: |
npm install --save-dev mocha @types/mocha chai @types/chai
bash prepare_wasm.sh
- name: run test
id: ci-test
run: |
npm run test-data

- name: Publish reports
if: failure()
uses: actions/upload-artifact@v4
with:
name: jfoa-build-reports-${{ runner.os }}
path: mochawesome-report/
54 changes: 54 additions & 0 deletions .github/workflows/ckb-light-client-ci-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: ci-ckb-wasm

on:
workflow_dispatch:
push:
branches:
- master
pull_request:


jobs:
npm-install-1:
strategy:
fail-fast: false
matrix:
net: ['testnet_v1']
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- 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@v4
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: prepare env
id: runtest
run: |
npm install --save-dev mocha @types/mocha chai @types/chai
bash prepare_wasm.sh
- name: run test
id: ci-test
run: |
npm run test-wasm

- name: Publish reports
if: failure()
uses: actions/upload-artifact@v4
with:
name: jfoa-build-reports-${{ runner.os }}
path: mochawesome-report/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ckb-light-wasm-demo"]
path = ckb-light-wasm-demo
url = https://github.com/gpBlockchain/ckb-light-wasm-demo.git
1 change: 1 addition & 0 deletions ckb-light-wasm-demo
Submodule ckb-light-wasm-demo added at 5055fe
29 changes: 17 additions & 12 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import {LightClientRPC} from "@ckb-lumos/light-client";

const CKB_CLIENT_CLI_PATH = "tmp/ckb-cli-light-client"
const CKB_LIGHT_CLIENT_PATH = "tmp/startBlockchain/ckbLightClient/ckb-light-client"
const DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndeLightClient"
const CKB_DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndeLightClient/ckb/target/release"
const CKB_DEV_INDEX_PATH = "tmp/startBlockchain/ckbDevWithIndexAndeLightClient/ckb-indexer/target/release"
const CKB_DEV_LIGHT_CLIENT_PATH = "tmp/startBlockchain/ckbDevWithIndexAndeLightClient/ckb-light-client/target/release"
const DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient"
const CKB_DEV_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb/target/release"
const CKB_DEV_INDEX_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb-indexer/target/release"
const CKB_DEV_LIGHT_CLIENT_PATH = "tmp/startBlockchain/ckbDevWithIndexAndLightClient/ckb-light-client/target/release"

const CKB_DEV_RPC_URL = "https://testnet.ckbapp.dev/";
const CKB_DEV_RPC_INDEX_URL = "https://testnet.ckbapp.dev/";
const CKB_DEV_RPC_URL = "http://localhost:8114/";
const CKB_DEV_RPC_INDEX_URL = "http://localhost:8116/";

const RPC_DEBUG = false
const RPC_DEBUG = true
const CKB_RPC_URL = "https://testnet.ckbapp.dev/";
// const CKB_RPC_URL = CKB_DEV_RPC_URL;
const CKB_RPC_INDEX_URL = "https://testnet.ckbapp.dev/";
// const CKB_RPC_INDEX_URL = CKB_DEV_RPC_INDEX_URL;
const CKB_LIGHT_RPC_URL = "http://127.0.0.1:9000";

export enum FeeRate {
SLOW = 1000,
NORMAL = 1000000,
Expand All @@ -32,13 +33,13 @@ config.initializeConfig(
config.predefined.AGGRON4
);
const script = helpers.parseAddress(
"ckt1qyqvjdmh4re8t7mfjr0v0z27lwwjqu384vhs6lfftr"
"ckt1qyqvjdmh4re8t7mfjr0v0z27lwwjqu384vhs6lfftr", {config: config.predefined.AGGRON4}
);
const MINER_SCRIPT = helpers.parseAddress("ckt1qyqvjdmh4re8t7mfjr0v0z27lwwjqu384vhs6lfftr")
const MINER_SCRIPT2 = helpers.parseAddress("ckt1qyq8ph2ywxpvkl5l0rcsugcnwcfswqpqngeqqmfuwq")
const MINER_SCRIPT3 = helpers.parseAddress("ckt1qyqd5eyygtdmwdr7ge736zw6z0ju6wsw7rssu8fcve")
const MINER_SCRIPT = helpers.parseAddress("ckt1qyqvjdmh4re8t7mfjr0v0z27lwwjqu384vhs6lfftr", {config: config.predefined.AGGRON4})
const MINER_SCRIPT2 = helpers.parseAddress("ckt1qyq8ph2ywxpvkl5l0rcsugcnwcfswqpqngeqqmfuwq", {config: config.predefined.AGGRON4})
const MINER_SCRIPT3 = helpers.parseAddress("ckt1qyqd5eyygtdmwdr7ge736zw6z0ju6wsw7rssu8fcve", {config: config.predefined.AGGRON4})
const CkbClientNode = new LightClient(CKB_LIGHT_CLIENT_PATH)
const lightClientRPC = new LightClientRPC(CKB_LIGHT_RPC_URL)
const lightClientRPC = new LightClientRPC(CKB_LIGHT_RPC_URL)

const deprecatedAddr = helpers.generateAddress(script);
const newFullAddr = helpers.encodeToAddress(script);
Expand Down Expand Up @@ -79,6 +80,10 @@ const EVERY_ONE_CAN_PAY_TYPE_ID = {
}
const EVERY_ONE_CAN_PAY = EVERY_ONE_CAN_PAY_TYPE_ID

export function checkLightClientWasm(): boolean {
return process.env.USE_LIGHT_CLIENT_WASM === 'true';
}

export {
FEE,
CKB_RPC_URL,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"typescript": "^3.7.3"
},
"scripts": {
"test-wasm": "USE_LIGHT_CLIENT_WASM=true mocha --config test/runners/mocha/.mocharc.jsonc --reporter mochawesome",
"test": "mocha --config test/runners/mocha/.mocharc.jsonc --reporter mochawesome",
"test-load": "mocha --config test/runners/mocha/.mocharc.load.jsonc --reporter mochawesome",
"test-data": "mocha --config test/runners/mocha/.mocharc.data.jsonc --reporter mochawesome",
"test-data-wasm": "USE_LIGHT_CLIENT_WASM=true mocha mocha --config test/runners/mocha/.mocharc.data.jsonc --reporter mochawesome",
"test-cli": "mocha --config test/runners/mocha/.mocharc.cli.jsonc --reporter mochawesome",
"test-transfer-issue": "mocha --config test/runners/mocha/.mocharc.issue.jsonc --reporter mochawesome",
"test-index-panic": "mocha --config test/runners/mocha/.mocharc.index-panic.jsonc --reporter mochawesome",
Expand Down
2 changes: 2 additions & 0 deletions prepare_wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd ckb-light-wasm-demo
bash build.sh
2 changes: 1 addition & 1 deletion service/CkbDevService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function cut_miner_and_wait_lightClient_sync(cut_num: number, miner
export async function compare_cells_result(scriptObject1: Script) {
let compare = true;
const indexCells = await getCellsMsg(scriptObject1, CKB_DEV_RPC_INDEX_URL)
console.log("lightCells:")

const lightCells = await getCellsMsg(scriptObject1, CKB_LIGHT_RPC_URL)
// get indexCells but not in light
const indexNotInLightCells = indexCells.filter(cell => !lightCells.some(lightCell => {
Expand Down
19 changes: 14 additions & 5 deletions service/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {exec} from "child_process";
import {request} from "./index";
import {checkLightClientWasm} from "../config/config";


class LightClient {
Expand All @@ -12,23 +14,30 @@ class LightClient {
}

async start(): Promise<boolean> {
try{
await sh("rm "+this.dirPath + "/target/release/data/store/LOCK")
}catch(e){
console.log(e)
if (checkLightClientWasm()){
await request(1,this.url,"start",[])
return true
}
await sh("cd " + this.dirPath + "/target/release && RUST_LOG=info,ckb_light_client=trace ./ckb-light-client run --config-file ./config.toml > node.log 2>&1 &")
await sleep(5*1000)
return true
}

async stop(): Promise<boolean> {

if (checkLightClientWasm()){
await request(1,this.url,"stop",[])
return true
}
await sh("pkill ckb-light")
return true
}

async clean(): Promise<boolean> {
if (checkLightClientWasm()){
await request(1,this.url,"new_client",[])
await request(1,this.url,"stop",[])
return true
}
try {
await this.stop()

Expand Down
21 changes: 16 additions & 5 deletions test/get_cells.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ACCOUNT_PRIVATE, EVERY_ONE_CAN_PAY_TYPE_ID,
CKB_LIGHT_RPC_URL,
CKB_RPC_URL, MINER_SCRIPT, CKB_RPC_INDEX_URL, lightClientRPC, rpcCLient, indexerMockLightRpc
CKB_RPC_URL, MINER_SCRIPT, CKB_RPC_INDEX_URL, lightClientRPC, rpcCLient, indexerMockLightRpc, checkLightClientWasm
} from "../config/config";
import {AGGRON4, generateAccountFromPrivateKey, getBlockNumByTxHash, send_tx} from "../service/transfer";
import {BI} from "@ckb-lumos/bi";
Expand All @@ -22,6 +22,9 @@ describe('get_cell', function () {
describe('script', function () {
describe('code_hash', function () {
it('length not eq 64 ', async () => {
if (checkLightClientWasm()) {
return
}
let acc = generateAccountFromPrivateKey(ACCOUNT_PRIVATE)
acc.lockScript.codeHash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce"
await getCellsReturnFailed({
Expand Down Expand Up @@ -201,7 +204,9 @@ describe('get_cell', function () {
})

it(',should return failed', async () => {

if (checkLightClientWasm()) {
return
}
try {
await lightClientRPC.getCells(
{
Expand Down Expand Up @@ -626,13 +631,13 @@ describe('get_cell', function () {
});
describe('limit', function () {
it('0,should return error that limit should be greater than 0', async () => {
await getCellsReturnFailed({
await getCellsReturnFailed({
limit: "0x0", order: "asc",
searchKey: {
script: MINER_SCRIPT,
scriptType: "lock"
}
},CKB_LIGHT_RPC_URL)
}, CKB_LIGHT_RPC_URL)
})
it('1', async () => {
let cells = await getCellsRequest({
Expand Down Expand Up @@ -689,11 +694,17 @@ describe('get_cell', function () {
}
}
let cells = await getCellsRequest(getCellReq)
console.log("cells.objects.length:", cells.objects.length)
getCellReq.limit = "0x1"
let allCells = await getAllCellsRequest(getCellReq)
expect(cells.objects.length).to.be.equal(allCells.length)
console.log("---allCells---")
printCells(allCells)
console.log("---cells---")
printCells(cells.objects)

for (let i = 0; i < cells.objects.length; i++) {
expect(cells.objects[i].blockNumber).to.be.equal(allCells[i].blockNumber)
expect(JSON.stringify(cells.objects[i])).to.be.equals(JSON.stringify(allCells[i]))
}
})

Expand Down
Loading
Loading