From 559bb75d8bde077483cdbcdea2bd59e445ff3600 Mon Sep 17 00:00:00 2001 From: gpBlockchain <744158715@qq.com> Date: Thu, 18 Apr 2024 15:17:31 +0800 Subject: [PATCH] add sync with remove test --- service/lightService.ts | 83 +++++++++++++++++++++--- test/sync_after_min_block_delete.spec.ts | 58 +++++++++++++++++ 2 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 test/sync_after_min_block_delete.spec.ts diff --git a/service/lightService.ts b/service/lightService.ts index 925921f..7ef4be1 100644 --- a/service/lightService.ts +++ b/service/lightService.ts @@ -3,13 +3,14 @@ import {CKB_LIGHT_RPC_URL, lightClientRPC} from "../config/config"; import {LightClientRPC} from "@ckb-lumos/light-client"; import {Sleep} from "./util"; import {request} from "./index"; -import { HexString, Script} from "@ckb-lumos/base"; +import {HexString, Script} from "@ckb-lumos/base"; import {Cell} from "@ckb-lumos/base/lib/api"; import { toGetCellsSearchKey, } from "@ckb-lumos/ckb-indexer/lib/paramsFormatter"; import {GetCellsSearchKey, SearchKey} from "@ckb-lumos/ckb-indexer/lib/type"; import {RPC} from "@ckb-lumos/lumos"; +import {LightClientScript} from "@ckb-lumos/light-client/src/type"; const ckbLightClientRPC = CKB_LIGHT_RPC_URL; @@ -28,7 +29,8 @@ export async function getHeader(hash: string, ckbLightClient: string = ckbLightC const res = await request(1, ckbLightClient, "get_header", [hash]); return res; } -export async function getTransaction(hash:string,ckbLightClient = ckbLightClientRPC){ + +export async function getTransaction(hash: string, ckbLightClient = ckbLightClientRPC) { return await request(1, ckbLightClient, "get_transaction", [hash]) } @@ -51,6 +53,50 @@ export async function sendTransaction(tx: any, ckbLightClient: string = ckbLight } +export async function waitScriptsUpdateWithTime(block_num: BI, ckbLightClient: string = ckbLightClientRPC, timeOut: number) { + const lightClientRPC = new LightClientRPC(ckbLightClient) + + while (timeOut > 0) { + timeOut--; + let res = await lightClientRPC.getScripts() + // @ts-ignore + if (res.length == 0) { + return + } + // get lowest blocker + let lower_block_num = BI.from("0xffffffffff") + // @ts-ignore + for (let i = 0; i < res.length; i++) { + if (lower_block_num.gt(res[i].blockNumber)) { + lower_block_num = BI.from(res[i].blockNumber); + } + } + console.log('[waitScriptsUpdate] current get script Height:', lower_block_num.toNumber(), ",wait sync height:", block_num.toNumber(),) + if (block_num.lte(lower_block_num)) { + return + } + await Sleep(1000) + } + throw new Error(`waitScriptsUpdateWithTime time out`) +} + +export async function getLightSyncHeight(ckbLightClient: string = ckbLightClientRPC): Promise { + + const lightClientRPC = new LightClientRPC(ckbLightClient) + let res = await lightClientRPC.getScripts() + if (res.length == 0) { + return BI.from("0") + } + let lower_block_num = BI.from(res[0].blockNumber); + for (let i = 0; i < res.length; i++) { + if (lower_block_num.gt(res[i].blockNumber)) { + lower_block_num = BI.from(res[i].blockNumber); + } + } + return lower_block_num; + +} + export async function waitScriptsUpdate(block_num: BI, ckbLightClient: string = ckbLightClientRPC) { const lightClientRPC = new LightClientRPC(ckbLightClient) @@ -78,19 +124,19 @@ export async function waitScriptsUpdate(block_num: BI, ckbLightClient: string = } -export async function checkScriptsInLightClient(scripts:Script[],ckbLightClient:string = ckbLightClientRPC){ +export async function checkScriptsInLightClient(scripts: Script[], ckbLightClient: string = ckbLightClientRPC) { let res = await lightClientRPC.getScripts() - let getScriptList = res.map(result=>{ + let getScriptList = res.map(result => { return { - codeHash:result.script.codeHash, - hashType:result.script.hashType, - args:result.script.args + codeHash: result.script.codeHash, + hashType: result.script.hashType, + args: result.script.args } }) - return scripts.some(checkScript=>{ - return getScriptList.some(getScript=>{ + return scripts.some(checkScript => { + return getScriptList.some(getScript => { return getScript.codeHash == checkScript.codeHash && getScript.hashType == checkScript.hashType && getScript.args == checkScript.args @@ -101,7 +147,7 @@ export async function checkScriptsInLightClient(scripts:Script[],ckbLightClient: export async function getCellsRequest(getCellsReq: GetCellsRequest, ckbLightClient: string = ckbLightClientRPC) { const lightRpc = new LightClientRPC(ckbLightClient) const infos: Cell[] = []; - let res = await lightRpc.getCells(getCellsReq.searchKey,getCellsReq.order,getCellsReq.limit,getCellsReq.afterCursor) + let res = await lightRpc.getCells(getCellsReq.searchKey, getCellsReq.order, getCellsReq.limit, getCellsReq.afterCursor) const liveCells = res.objects; for (const liveCell of liveCells) { const cell: Cell = { @@ -117,3 +163,20 @@ export async function getCellsRequest(getCellsReq: GetCellsRequest, ckbLightClie lastCursor: res.lastCursor }; } + + +export async function setScriptWithCommand(ckb_light_rpc_url: string, scripts: Array, setScriptCommand: "all" | "partial" | "delete" | "partial1") { + const params = [ + scripts.map(({script, scriptType, blockNumber}) => ({ + script: { + code_hash: script.codeHash, + hash_type: script.hashType, + args: script.args, + }, + script_type: scriptType, + block_number: blockNumber, + })), setScriptCommand + ]; + return request(1, ckb_light_rpc_url, "set_scripts", params) +} + diff --git a/test/sync_after_min_block_delete.spec.ts b/test/sync_after_min_block_delete.spec.ts new file mode 100644 index 0000000..edaf4cd --- /dev/null +++ b/test/sync_after_min_block_delete.spec.ts @@ -0,0 +1,58 @@ +import {CKB_LIGHT_RPC_URL, lightClientRPC, rpcCLient} from "../config/config"; +import {getLightSyncHeight, setScriptWithCommand, waitScriptsUpdateWithTime} from "../service/lightService"; +import {BI} from "@ckb-lumos/bi"; +import {helpers} from "@ckb-lumos/lumos"; + +describe('sync after min block delete', function () { + + this.timeout(1000000) + before(async () => { + + await lightClientRPC.setScripts([]) + + }) + after(async () => { + await lightClientRPC.setScripts([]) + }) + + it('sync after min block delete', async () => { + + let syncHeightBlockAddress = "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2t8syzsvtyt8hkc68d7hkpjwy4u6nwjjg08lwpz" + let syncLowHeightBLockAddress = "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgj8re09l7gk267ank62hadpp3t2kp20kqnud0jc" + // add height block : 10000000 + await setScriptWithCommand(CKB_LIGHT_RPC_URL, [{ + script: helpers.addressToScript(syncHeightBlockAddress), + scriptType: "lock", + blockNumber: BI.from("10000000").toHexString() + }], "all") + // wait height block sync + await waitScriptsUpdateWithTime(BI.from(10000100), CKB_LIGHT_RPC_URL, 120) + // add low block :10000 + await setScriptWithCommand(CKB_LIGHT_RPC_URL, [{ + script: helpers.addressToScript(syncLowHeightBLockAddress), + scriptType: "lock", + blockNumber: BI.from("10000").toHexString() + }], "partial") + + console.log("wait low block sync 11000") + // wait low block sync + await waitScriptsUpdateWithTime(BI.from(11000), CKB_LIGHT_RPC_URL, 120) + // remove low block + await setScriptWithCommand(CKB_LIGHT_RPC_URL, [{ + script: helpers.addressToScript(syncLowHeightBLockAddress), + scriptType: "lock", + blockNumber: BI.from("10000").toHexString() + }], "delete") + + //should empty + // log contains match ,but query result is empty, so remove it + // console.log(`query not match header:`, header) + + // wait height block sync + let height = await getLightSyncHeight(CKB_LIGHT_RPC_URL) + console.log("wait height sync :", height.add(1000).toNumber()) + await waitScriptsUpdateWithTime(height.add(1000), CKB_LIGHT_RPC_URL, 60) + + }) + +}); \ No newline at end of file