Skip to content

Commit b1d694b

Browse files
committed
perf: skip createdBy if already created in indexer
1 parent dfa2f1e commit b1d694b

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

packages/neuron-wallet/src/services/indexer/queue.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
1212

1313
import IndexerRPC from './indexer-rpc'
1414
import HexUtils from 'utils/hex'
15+
import { TxUniqueFlagCache } from './tx-unique-flag'
1516

1617
export interface LockHashInfo {
1718
lockHash: string
@@ -41,6 +42,8 @@ export default class IndexerQueue {
4142

4243
private resetFlag = false
4344

45+
private latestCreatedBy: TxUniqueFlagCache = new TxUniqueFlagCache(100)
46+
4447
constructor(url: string, lockHashInfos: LockHashInfo[], tipNumberSubject: Subject<string | undefined>) {
4548
// this.lockHashes = lockHashes
4649
this.lockHashInfos = lockHashInfos
@@ -177,11 +180,19 @@ export default class IndexerQueue {
177180
txPoint &&
178181
(BigInt(txPoint.blockNumber) >= startBlockNumber || this.tipBlockNumber - BigInt(txPoint.blockNumber) < 1000)
179182
) {
180-
logger.debug('indexer fetched tx:', type, txPoint.txHash)
181-
182183
const transactionWithStatus = await this.getBlocksService.getTransaction(txPoint.txHash)
183184
const ckbTransaction: CKBComponents.Transaction = transactionWithStatus.transaction
184185
const transaction: Transaction = TypeConvert.toTransaction(ckbTransaction)
186+
const txUniqueFlag = {
187+
txHash: transaction.hash,
188+
blockHash: transactionWithStatus.txStatus.blockHash!
189+
}
190+
if (type === TxPointType.CreatedBy && this.latestCreatedBy.includes(txUniqueFlag)) {
191+
return
192+
}
193+
194+
logger.debug('indexer fetched tx:', type, txPoint.txHash)
195+
185196
// tx timestamp / blockNumber / blockHash
186197
const { blockHash } = transactionWithStatus.txStatus
187198
if (blockHash) {
@@ -196,6 +207,7 @@ export default class IndexerQueue {
196207
let address: string | undefined
197208
if (type === TxPointType.CreatedBy) {
198209
address = LockUtils.lockScriptToAddress(transaction.outputs![parseInt(txPoint.index, 16)].lock)
210+
this.latestCreatedBy.push(txUniqueFlag)
199211
} else if (type === TxPointType.ConsumedBy) {
200212
const input = txEntity.inputs[parseInt(txPoint.index, 16)]
201213
const output = await IndexerTransaction.updateInputLockHash(input.outPointTxHash!, input.outPointIndex!)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export interface TxUniqueFlag {
2+
txHash: string
3+
blockHash: string
4+
}
5+
6+
export class TxUniqueFlagCache {
7+
private arr: TxUniqueFlag[] = []
8+
9+
private limit: number
10+
11+
constructor(limit: number) {
12+
this.limit = limit
13+
}
14+
15+
public push(value: TxUniqueFlag) {
16+
if (this.includes(value)) {
17+
return
18+
}
19+
this.arr.push(value)
20+
if (this.arr.length > this.limit) {
21+
this.arr.shift()
22+
}
23+
}
24+
25+
public includes(value: TxUniqueFlag) {
26+
return this.arr.some(txPoint => {
27+
return txPoint.txHash == value.txHash && txPoint.blockHash == value.blockHash
28+
})
29+
}
30+
31+
public length() {
32+
return this.arr.length
33+
}
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { TxUniqueFlag, TxUniqueFlagCache } from '../../../src/services/indexer/tx-unique-flag'
2+
3+
describe('TxUniqueFlagCache', () => {
4+
const txUniqueFlag: TxUniqueFlag = {
5+
txHash: '0x1742426b21175c35cac63f0fb25318e2dd1facd1c02a244caa9144a0e843e08a',
6+
blockHash: '0x42f917e4c00e39cec94696ee5820b5f79f603bf7a2b7aa7abebf4d13f97110e4',
7+
}
8+
9+
it('unique', () => {
10+
const cache = new TxUniqueFlagCache(10)
11+
cache.push(txUniqueFlag)
12+
cache.push(txUniqueFlag)
13+
expect(cache.length()).toEqual(1)
14+
})
15+
16+
it('limit', () => {
17+
const cache = new TxUniqueFlagCache(10)
18+
Array.from({ length: 20 }).map((_value, index) => {
19+
cache.push({
20+
blockHash: index.toString(),
21+
txHash: index.toString(),
22+
})
23+
})
24+
25+
expect(cache.length()).toEqual(10)
26+
})
27+
28+
it('pop head', () => {
29+
const cache = new TxUniqueFlagCache(10)
30+
Array.from({ length: 20 }).map((_value, index) => {
31+
cache.push({
32+
blockHash: index.toString(),
33+
txHash: index.toString(),
34+
})
35+
})
36+
37+
const result = Array.from({ length: 10 }).map((_value, index) => {
38+
return {
39+
txHash: (index+10).toString(),
40+
blockHash: (index+10).toString(),
41+
}
42+
})
43+
44+
expect(cache.length()).toEqual(10)
45+
result.map(value => {
46+
expect(cache.includes(value)).toBe(true)
47+
})
48+
})
49+
})

0 commit comments

Comments
 (0)