From 676434dd404d031cc83854a6df82fe4dc1913c0c Mon Sep 17 00:00:00 2001 From: gpBlockchain <744158715@qq.com> Date: Thu, 28 Nov 2024 14:03:12 +0800 Subject: [PATCH] fix test data failed --- service/txService.ts | 43 +++++++++++++++++++++++++++++++++++++++--- test/data/demo.test.ts | 31 ++++++++++++++++-------------- test/data/test_data.ts | 15 ++++++++------- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/service/txService.ts b/service/txService.ts index 40a4b6f..85f3f47 100644 --- a/service/txService.ts +++ b/service/txService.ts @@ -8,6 +8,7 @@ import {FetchFlag} from "@ckb-lumos/light-client/lib/type"; import {Script, utils} from "@ckb-lumos/base"; import {RPC} from "@ckb-lumos/rpc/lib/types/rpc"; import ScriptType = RPC.ScriptType; +import {CKBRPC} from "@ckb-lumos/rpc"; export async function fetchTransactionUntilFetched(hash: string, ckbLightClientUrl, waitSize: number) { @@ -106,7 +107,43 @@ export function getTransferExtraLockCell(inputCell: Cell[], script: Script, extr } -export async function getTransactionList(scriptObject: Script, script_type: ScriptType, lastCursor: string, ckbLightClientUrl: string, block_range?: HexadecimalRange): Promise { +export async function getCkbTransactionList(scriptObject: Script, script_type: ScriptType, lastCursor: string, ckbLightClientUrl: string, block_range?: HexadecimalRange): Promise { + const ckbLightClient = new CKBRPC(ckbLightClientUrl) + + let txList: string[] = [] + while (true) { + let result = await ckbLightClient.getTransactions({ + script: scriptObject, + scriptType: script_type, + groupByTransaction: true, + filter: { + blockRange: block_range + } + + }, + "asc", + BI.from(3000).toHexString(), lastCursor + ) + if (result.objects.length == 0) { + return txList + } + for (let i = 0; i < result.objects.length; i++) { + let tx = result.objects[i] + if (tx.txHash != null) { + txList.push(tx.txHash) + continue + } + txList.push(tx.txHash) + } + lastCursor = result.lastCursor + if (RPC_DEBUG) { + console.log('current totalSize:', txList.length, 'cursor:', lastCursor) + } + } +} + + +export async function getLightTransactionList(scriptObject: Script, script_type: ScriptType, lastCursor: string, ckbLightClientUrl: string, block_range?: HexadecimalRange): Promise { const ckbLightClient = new LightClientRPC(ckbLightClientUrl) let txList: string[] = [] @@ -121,7 +158,7 @@ export async function getTransactionList(scriptObject: Script, script_type: Scri }, "asc", - BI.from(3000).toHexString() + BI.from(3000).toHexString(),lastCursor ) if (result.objects.length == 0) { return txList @@ -179,7 +216,7 @@ export async function getCellsByRange(scriptObject: Script, script_type: ScriptT filter: { blockRange: block_range } - }, "asc", "0xfff",lastCursor) + }, "asc", "0xfff", lastCursor) if (result.objects.length == 0) { break } diff --git a/test/data/demo.test.ts b/test/data/demo.test.ts index 6c3aac9..4ac0ec1 100644 --- a/test/data/demo.test.ts +++ b/test/data/demo.test.ts @@ -1,21 +1,23 @@ import {getTestDataByFile} from "./test_data"; import {CKB_LIGHT_RPC_URL, CKB_RPC_INDEX_URL, CkbClientNode, lightClientRPC} from "../../config/config"; import {BI} from "@ckb-lumos/bi"; -import {getTransactionList} from "../../service/txService"; +import {getCkbTransactionList, getLightTransactionList} from "../../service/txService"; import {expect} from "chai"; import * as fs from "fs"; import {waitScriptsUpdate} from "../../service/lightService"; +import {toScript} from "@ckb-lumos/rpc/lib/resultFormatter"; describe('demo', function () { this.timeout(1000000000) let idx = 0 let step = 1 - before(async ()=>{ + before(async () => { await CkbClientNode.clean() await CkbClientNode.start() await CkbClientNode.status() }) + function SkipStep(step: number): boolean { idx++ return (idx % step) == 0 @@ -32,11 +34,11 @@ describe('demo', function () { for (let i = 0; i < files.length; i++) { if (!SkipStep(step)) continue; - if(files[i].search("json") == -1) continue; + if (files[i].search("json") == -1) continue; describe(files[i], function () { let td; before(async () => { - console.log("file:",files[i]) + console.log("file:", files[i]) td = getTestDataByFile(files[i]) let setScriptData = td.getScriptSet().map(t => { @@ -47,9 +49,9 @@ describe('demo', function () { minUpdateNum = BI.from(t.block_num).sub(BI.from(1)) } return { - script: t.script, - script_type: t.script_type, - block_number: minUpdateNum.toHexString() + script: toScript(t.script), + scriptType: t.script_type, + blockNumber: minUpdateNum.toHexString() } }) await lightClientRPC.setScripts(setScriptData) @@ -62,16 +64,17 @@ describe('demo', function () { let tds = td.getScriptSet() let lightTotalTxs: Set = new Set() for (let j = 0; j < tds.length; j++) { + console.log(`current ${j}/${tds.length}`) let testScpt = tds[j] - let indexTxs = await getTransactionList( - testScpt.script, + let indexTxs = await getCkbTransactionList( + toScript(testScpt.script), testScpt.script_type, undefined, CKB_RPC_INDEX_URL, [BI.from(td.begin_block_num).toHexString(), BI.from(td.end_block_num).toHexString()]) - let lightTxs = await getTransactionList( - testScpt.script, + let lightTxs = await getLightTransactionList( + toScript(testScpt.script), testScpt.script_type, undefined, CKB_LIGHT_RPC_URL, @@ -92,10 +95,10 @@ describe('demo', function () { console.log('getTransactionList Length:', indexTotalTxs.size) console.log('td.script_types length:', tdList.size) console.log('light client length:', lightTotalTxs.size) - + // response['objects'][0]['transaction']['hash'] let outPutNotInLightList = []; - tdList.forEach(tx =>{ - if(!lightTotalTxs.has(tx)){ + tdList.forEach(tx => { + if (!lightTotalTxs.has(tx)) { outPutNotInLightList.push(tx) } }) diff --git a/test/data/test_data.ts b/test/data/test_data.ts index 09fdac2..6db1686 100644 --- a/test/data/test_data.ts +++ b/test/data/test_data.ts @@ -1,10 +1,11 @@ import {rpcCLient} from "../../config/config"; import {BI} from "@ckb-lumos/bi"; -import {Script} from "@ckb-lumos/base/lib/api"; import * as fs from "fs"; +import {RPC} from "@ckb-lumos/rpc/lib/types/rpc"; +import {formatter} from "@ckb-lumos/rpc/lib/paramsFormatter"; export class TestScript { - script: Script + script: RPC.Script hash: string block_num: number script_type: "lock" | "type" @@ -45,7 +46,7 @@ export class test_data { getScriptByHashType(hash_type: string): TestScript[] { console.log('getScriptByHashType') return this.script_types.filter( - script => script.script.hashType == hash_type + script => script.script.hash_type == hash_type ) } @@ -72,9 +73,9 @@ export class test_data { }) for (let i = 0; i < test_sort_scripts.length; i++) { if (scripts.some(spt => - spt.script.codeHash == this.script_types[i].script.codeHash && + spt.script.code_hash == this.script_types[i].script.code_hash && spt.script.args == this.script_types[i].script.args && - spt.script.hashType == this.script_types[i].script.hashType + spt.script.hash_type == this.script_types[i].script.hash_type )) { continue } @@ -109,7 +110,7 @@ export async function genTestData(begin_block_num: number, end_block_num: number if (outPut.type != null) { script_type_total++ testScripts.push({ - script: outPut.type, + script: formatter.toScript(outPut.type), hash: tx.hash, block_num: i, script_type: "type" @@ -123,7 +124,7 @@ export async function genTestData(begin_block_num: number, end_block_num: number script_data1_total++ } testScripts.push({ - script: outPut.lock, + script: formatter.toScript(outPut.lock), hash: tx.hash, block_num: i, script_type: "lock"