Skip to content

Commit

Permalink
add sync with remove test
Browse files Browse the repository at this point in the history
  • Loading branch information
gpBlockchain committed Apr 18, 2024
1 parent df0a420 commit 559bb75
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 10 deletions.
83 changes: 73 additions & 10 deletions service/lightService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

}
Expand All @@ -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<BI> {

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)

Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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<LightClientScript>, 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)
}

58 changes: 58 additions & 0 deletions test/sync_after_min_block_delete.spec.ts
Original file line number Diff line number Diff line change
@@ -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)

})

});

0 comments on commit 559bb75

Please sign in to comment.