Skip to content

Commit

Permalink
perf: cache getTransaction result when sync in indexer mode
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Oct 28, 2019
1 parent c5b5c86 commit 756bd30
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
10 changes: 6 additions & 4 deletions packages/neuron-wallet/src/listeners/tx-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ const trackingStatus = async () => {
const transaction = successTx.tx
const { blockHash } = successTx
const blockHeader = await getBlockService.getHeader(blockHash!)
transaction.blockHash = blockHash!
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
await TransactionPersistor.saveFetchTx(transaction)
if (blockHeader) {
transaction.blockHash = blockHash!
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
await TransactionPersistor.saveFetchTx(transaction)
}
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Subject, Subscription } from 'rxjs'
import Utils from 'services/sync/utils'
import logger from 'utils/logger'
import GetBlocks from 'services/sync/get-blocks'
import { Transaction } from 'types/cell-types'
import { Transaction, TransactionWithStatus } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
import BlockNumber from 'services/sync/block-number'
import AddressesUsedSubject from 'models/subjects/addresses-used-subject'
Expand All @@ -13,6 +13,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
import IndexerRPC from './indexer-rpc'
import HexUtils from 'utils/hex'
import { TxUniqueFlagCache } from './tx-unique-flag'
import { TransactionCache } from './transaction-cache'
import TransactionEntity from 'database/chain/entities/transaction'

export interface LockHashInfo {
Expand Down Expand Up @@ -43,6 +44,7 @@ export default class IndexerQueue {
private resetFlag = false

private latestCreatedBy: TxUniqueFlagCache = new TxUniqueFlagCache(100)
private txCache: TransactionCache = new TransactionCache(100)

private url: string

Expand Down Expand Up @@ -123,6 +125,7 @@ export default class IndexerQueue {
const result = await this.getBlocksService.getTransaction(tx.hash)
if (!result) {
await IndexerTransaction.deleteTxWhenFork(tx.hash)
this.txCache.delete(tx.hash)
} else if (tip - BigInt(tx.blockNumber) >= 1000) {
await IndexerTransaction.confirm(tx.hash)
}
Expand Down Expand Up @@ -157,6 +160,16 @@ export default class IndexerQueue {
})
}

public getTransaction = async (txHash: string): Promise<TransactionWithStatus> => {
let txWithStatus = this.txCache.get(txHash)
if (!txWithStatus) {
const transactionWithStatus = await this.getBlocksService.getTransaction(txHash)
txWithStatus = TypeConvert.toTransactionWithStatus(transactionWithStatus)
this.txCache.push(txWithStatus)
}
return txWithStatus
}

// type: 'createdBy' | 'consumedBy'
public pipeline = async (lockHash: string, type: TxPointType, startBlockNumber: bigint) => {
let page = 0
Expand All @@ -180,9 +193,8 @@ export default class IndexerQueue {
txPoint &&
(BigInt(txPoint.blockNumber) >= startBlockNumber || this.tipBlockNumber - BigInt(txPoint.blockNumber) < 1000)
) {
const transactionWithStatus = await this.getBlocksService.getTransaction(txPoint.txHash)
const ckbTransaction: CKBComponents.Transaction = transactionWithStatus.transaction
const transaction: Transaction = TypeConvert.toTransaction(ckbTransaction)
const transactionWithStatus = await this.getTransaction(txPoint.txHash)
const transaction: Transaction = transactionWithStatus.transaction
const txUniqueFlag = {
txHash: transaction.hash,
blockHash: transactionWithStatus.txStatus.blockHash!
Expand Down Expand Up @@ -210,9 +222,11 @@ export default class IndexerQueue {
const { blockHash } = transactionWithStatus.txStatus
if (blockHash) {
const blockHeader = await this.getBlocksService.getHeader(blockHash)
transaction.blockHash = blockHash
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
if (blockHeader) {
transaction.blockHash = blockHash
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
}
}
txEntity = await TransactionPersistor.saveFetchTx(transaction)
}
Expand Down
32 changes: 32 additions & 0 deletions packages/neuron-wallet/src/services/indexer/transaction-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TransactionWithStatus } from "types/cell-types"

export class TransactionCache {
private limit: number

private store = new Map<string, TransactionWithStatus>()

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

public push(value: TransactionWithStatus) {
const key = value.transaction.hash

if (this.store.has(key)) {
return
}
this.store.set(key, value)
if (this.store.size > this.limit) {
const firstKey = Array.from(this.store.keys())[0]
this.store.delete(firstKey)
}
}

public get(key: string): TransactionWithStatus | undefined {
return this.store.get(key)
}

public delete(key: string): boolean {
return this.store.delete(key)
}
}
7 changes: 5 additions & 2 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ export default class GetBlocks {
return await this.core.rpc.getTransaction(hash)
}

public getHeader = async (hash: string): Promise<BlockHeader> => {
public getHeader = async (hash: string): Promise<BlockHeader | undefined> => {
const result = await this.core.rpc.getHeader(hash)
return TypeConvert.toBlockHeader(result)
if (result) {
return TypeConvert.toBlockHeader(result)
}
return undefined
}

public getBlockByNumber = async (num: string): Promise<Block> => {
Expand Down
10 changes: 10 additions & 0 deletions packages/neuron-wallet/src/types/cell-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export interface Transaction extends TransactionWithoutHash {
hash: string
}

export interface TxStatus {
blockHash: string | null
status: 'pending' | 'proposed' | 'committed'
}

export interface TransactionWithStatus {
transaction: Transaction
txStatus: TxStatus
}

export interface Input {
previousOutput: OutPoint | null
since?: string
Expand Down
11 changes: 11 additions & 0 deletions packages/neuron-wallet/src/types/type-convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import HexUtils from 'utils/hex';
import { TransactionWithStatus } from './cell-types';
import {
Block,
BlockHeader,
Expand Down Expand Up @@ -51,6 +52,16 @@ export default class TypeConvert {
return tx
}

static toTransactionWithStatus(transactionWithStatus: CKBComponents.TransactionWithStatus): TransactionWithStatus {
return {
transaction: TypeConvert.toTransaction(transactionWithStatus.transaction),
txStatus: {
blockHash: transactionWithStatus.txStatus.blockHash,
status: transactionWithStatus.txStatus.status,
}
}
}

static toCellDep(cellDep: CKBComponents.CellDep): CellDep {
return {
outPoint: cellDep.outPoint,
Expand Down

0 comments on commit 756bd30

Please sign in to comment.