Skip to content

Commit

Permalink
perf: skip createdBy if already created in indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 26, 2019
1 parent dfa2f1e commit b1d694b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'

import IndexerRPC from './indexer-rpc'
import HexUtils from 'utils/hex'
import { TxUniqueFlagCache } from './tx-unique-flag'

export interface LockHashInfo {
lockHash: string
Expand Down Expand Up @@ -41,6 +42,8 @@ export default class IndexerQueue {

private resetFlag = false

private latestCreatedBy: TxUniqueFlagCache = new TxUniqueFlagCache(100)

constructor(url: string, lockHashInfos: LockHashInfo[], tipNumberSubject: Subject<string | undefined>) {
// this.lockHashes = lockHashes
this.lockHashInfos = lockHashInfos
Expand Down Expand Up @@ -177,11 +180,19 @@ export default class IndexerQueue {
txPoint &&
(BigInt(txPoint.blockNumber) >= startBlockNumber || this.tipBlockNumber - BigInt(txPoint.blockNumber) < 1000)
) {
logger.debug('indexer fetched tx:', type, txPoint.txHash)

const transactionWithStatus = await this.getBlocksService.getTransaction(txPoint.txHash)
const ckbTransaction: CKBComponents.Transaction = transactionWithStatus.transaction
const transaction: Transaction = TypeConvert.toTransaction(ckbTransaction)
const txUniqueFlag = {
txHash: transaction.hash,
blockHash: transactionWithStatus.txStatus.blockHash!
}
if (type === TxPointType.CreatedBy && this.latestCreatedBy.includes(txUniqueFlag)) {
return
}

logger.debug('indexer fetched tx:', type, txPoint.txHash)

// tx timestamp / blockNumber / blockHash
const { blockHash } = transactionWithStatus.txStatus
if (blockHash) {
Expand All @@ -196,6 +207,7 @@ export default class IndexerQueue {
let address: string | undefined
if (type === TxPointType.CreatedBy) {
address = LockUtils.lockScriptToAddress(transaction.outputs![parseInt(txPoint.index, 16)].lock)
this.latestCreatedBy.push(txUniqueFlag)
} else if (type === TxPointType.ConsumedBy) {
const input = txEntity.inputs[parseInt(txPoint.index, 16)]
const output = await IndexerTransaction.updateInputLockHash(input.outPointTxHash!, input.outPointIndex!)
Expand Down
34 changes: 34 additions & 0 deletions packages/neuron-wallet/src/services/indexer/tx-unique-flag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface TxUniqueFlag {
txHash: string
blockHash: string
}

export class TxUniqueFlagCache {
private arr: TxUniqueFlag[] = []

private limit: number

constructor(limit: number) {
this.limit = limit
}

public push(value: TxUniqueFlag) {
if (this.includes(value)) {
return
}
this.arr.push(value)
if (this.arr.length > this.limit) {
this.arr.shift()
}
}

public includes(value: TxUniqueFlag) {
return this.arr.some(txPoint => {
return txPoint.txHash == value.txHash && txPoint.blockHash == value.blockHash
})
}

public length() {
return this.arr.length
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TxUniqueFlag, TxUniqueFlagCache } from '../../../src/services/indexer/tx-unique-flag'

describe('TxUniqueFlagCache', () => {
const txUniqueFlag: TxUniqueFlag = {
txHash: '0x1742426b21175c35cac63f0fb25318e2dd1facd1c02a244caa9144a0e843e08a',
blockHash: '0x42f917e4c00e39cec94696ee5820b5f79f603bf7a2b7aa7abebf4d13f97110e4',
}

it('unique', () => {
const cache = new TxUniqueFlagCache(10)
cache.push(txUniqueFlag)
cache.push(txUniqueFlag)
expect(cache.length()).toEqual(1)
})

it('limit', () => {
const cache = new TxUniqueFlagCache(10)
Array.from({ length: 20 }).map((_value, index) => {
cache.push({
blockHash: index.toString(),
txHash: index.toString(),
})
})

expect(cache.length()).toEqual(10)
})

it('pop head', () => {
const cache = new TxUniqueFlagCache(10)
Array.from({ length: 20 }).map((_value, index) => {
cache.push({
blockHash: index.toString(),
txHash: index.toString(),
})
})

const result = Array.from({ length: 10 }).map((_value, index) => {
return {
txHash: (index+10).toString(),
blockHash: (index+10).toString(),
}
})

expect(cache.length()).toEqual(10)
result.map(value => {
expect(cache.includes(value)).toBe(true)
})
})
})

0 comments on commit b1d694b

Please sign in to comment.