Skip to content

Commit

Permalink
fix: Fix light client sync slow when fetch much transactions. (#2944)
Browse files Browse the repository at this point in the history
* fix: Fix light client sync slow when fetch much transactions.

* fix: Add message send to avoid the tx db has not submit finish yet.

* Add annotation

Co-authored-by: homura <[email protected]>

---------

Co-authored-by: homura <[email protected]>
  • Loading branch information
yanguoyu and homura authored Nov 22, 2023
1 parent dffd921 commit 1f53a28
Show file tree
Hide file tree
Showing 2 changed files with 27 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 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)}`) {
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

2 comments on commit 1f53a28

@github-actions
Copy link
Contributor

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 6954524910

@github-actions
Copy link
Contributor

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 6954523867

Please sign in to comment.