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

fix: Fix light client sync slow when fetch much transactions. #2944

Merged
merged 3 commits into from
Nov 22, 2023
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BI } from '@ckb-lumos/bi'
import { Subject } from 'rxjs'
import { queue, QueueObject } from 'async'
import { HexString, QueryOptions } from '@ckb-lumos/base'
import type { HexString, QueryOptions, TransactionWithStatus } from '@ckb-lumos/base'
import { Indexer as CkbIndexer, CellCollector } from '@ckb-lumos/ckb-indexer'
import logger from '../../utils/logger'
import { Address } from '../../models/address'
Expand All @@ -18,6 +18,7 @@ import AssetAccountInfo from '../../models/asset-account-info'
import { DepType } from '../../models/chain/cell-dep'
import { molecule } from '@ckb-lumos/codec'
import { blockchain } from '@ckb-lumos/base'
import type { Base } from '@ckb-lumos/rpc/lib/Base'

interface SyncQueueParam {
script: CKBComponents.Script
Expand Down Expand Up @@ -232,14 +233,33 @@ export default class LightConnector extends Connector<CKBComponents.Hash> {
})
return
}
this.transactionsSubject.next({ txHashes: result.txs.map(v => v.txHash), params: syncProgress.hash })
const txHashes = result.txs.map(v => v.txHash)
await this.fetchPreviousOutputs(txHashes)
this.transactionsSubject.next({ txHashes, params: syncProgress.hash })
this.syncInQueue.set(syncProgress.hash, {
blockStartNumber: result.lastCursor === '0x' ? parseInt(blockRange[1]) : parseInt(blockRange[0]),
blockEndNumber: parseInt(blockRange[1]),
cursor: result.lastCursor === '0x' ? undefined : result.lastCursor,
})
}

private async fetchPreviousOutputs(txHashes: string[]) {
const transactions = await this.lightRpc
.createBatchRequest<'getTransaction', string[], TransactionWithStatus[]>(txHashes.map(v => ['getTransaction', v]))
.exec()
const previousTxHashes = new Set<string>()
transactions
.flatMap(tx => tx.transaction.inputs)
.forEach(input => {
const previousTxHash = input.previousOutput!.txHash
// exclude the cell base transaction in a block
if (previousTxHash !== `0x${'0'.repeat(64)}`) {
yanguoyu marked this conversation as resolved.
Show resolved Hide resolved
previousTxHashes.add(previousTxHash)
}
})
await this.lightRpc.createBatchRequest([...previousTxHashes].map(v => ['fetchTransaction' as keyof Base, v])).exec()
}

private async collectLiveCellsByScript(query: LumosCellQuery) {
const { lock, type, data } = query
if (!lock && !type) {
Expand Down
6 changes: 5 additions & 1 deletion packages/neuron-wallet/src/block-sync-renderer/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class Queue {
while (true) {
try {
await this.#checkAndSave(txHashes)
process.send?.({ channel: 'tx-db-changed' })
break
} catch (error) {
logger.error('retry saving transactions in 2 seconds due to error:', error)
Expand Down Expand Up @@ -171,7 +172,10 @@ export default class Queue {
}, 1)

const drainFetchTxQueue = new Promise((resolve, reject) => {
fetchTxQueue.error(reject)
fetchTxQueue.error(err => {
fetchTxQueue.kill()
reject(err)
})
fetchTxQueue.drain(() => resolve(0))
})

Expand Down