Skip to content

Commit

Permalink
fix: Fix light client sync slow when fetch much transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Nov 17, 2023
1 parent c69258f commit ca90897
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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 Down Expand Up @@ -232,14 +232,32 @@ 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.forEach(tx => {
tx.transaction.inputs.forEach(input => {
const previousTxHash = input.previousOutput!.txHash
if (previousTxHash !== `0x${'0'.repeat(64)}`) {
previousTxHashes.add(previousTxHash)
}
})
})
await this.lightRpc.createBatchRequest([...previousTxHashes].map(v => ['fetchTransaction' as any, v])).exec()
}

private async collectLiveCellsByScript(query: LumosCellQuery) {
const { lock, type, data } = query
if (!lock && !type) {
Expand Down
5 changes: 4 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 @@ -171,7 +171,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

1 comment on commit ca90897

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 6900924326

Please sign in to comment.