Skip to content

Commit

Permalink
fix test data failed
Browse files Browse the repository at this point in the history
  • Loading branch information
gpBlockchain committed Nov 28, 2024
1 parent 9aa569a commit 676434d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
43 changes: 40 additions & 3 deletions service/txService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<string[]> {
export async function getCkbTransactionList(scriptObject: Script, script_type: ScriptType, lastCursor: string, ckbLightClientUrl: string, block_range?: HexadecimalRange): Promise<string[]> {
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<string[]> {
const ckbLightClient = new LightClientRPC(ckbLightClientUrl)

let txList: string[] = []
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 17 additions & 14 deletions test/data/demo.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 => {
Expand All @@ -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)
Expand All @@ -62,16 +64,17 @@ describe('demo', function () {
let tds = td.getScriptSet()
let lightTotalTxs: Set<String> = 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,
Expand All @@ -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)
}
})
Expand Down
15 changes: 8 additions & 7 deletions test/data/test_data.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
)
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 676434d

Please sign in to comment.