@@ -2,7 +2,7 @@ import { Subject, Subscription } from 'rxjs'
22import Utils from 'services/sync/utils'
33import logger from 'utils/logger'
44import GetBlocks from 'services/sync/get-blocks'
5- import { Transaction } from 'types/cell-types'
5+ import { Transaction , TransactionWithStatus } from 'types/cell-types'
66import TypeConvert from 'types/type-convert'
77import BlockNumber from 'services/sync/block-number'
88import AddressesUsedSubject from 'models/subjects/addresses-used-subject'
@@ -13,6 +13,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
1313import IndexerRPC from './indexer-rpc'
1414import HexUtils from 'utils/hex'
1515import { TxUniqueFlagCache } from './tx-unique-flag'
16+ import { TransactionCache } from './transaction-cache'
1617import TransactionEntity from 'database/chain/entities/transaction'
1718
1819export interface LockHashInfo {
@@ -43,6 +44,7 @@ export default class IndexerQueue {
4344 private resetFlag = false
4445
4546 private latestCreatedBy : TxUniqueFlagCache = new TxUniqueFlagCache ( 100 )
47+ private txCache : TransactionCache = new TransactionCache ( 100 )
4648
4749 private url : string
4850
@@ -123,6 +125,7 @@ export default class IndexerQueue {
123125 const result = await this . getBlocksService . getTransaction ( tx . hash )
124126 if ( ! result ) {
125127 await IndexerTransaction . deleteTxWhenFork ( tx . hash )
128+ this . txCache . delete ( tx . hash )
126129 } else if ( tip - BigInt ( tx . blockNumber ) >= 1000 ) {
127130 await IndexerTransaction . confirm ( tx . hash )
128131 }
@@ -157,6 +160,16 @@ export default class IndexerQueue {
157160 } )
158161 }
159162
163+ public getTransaction = async ( txHash : string ) : Promise < TransactionWithStatus > => {
164+ let txWithStatus = this . txCache . get ( txHash )
165+ if ( ! txWithStatus ) {
166+ const transactionWithStatus = await this . getBlocksService . getTransaction ( txHash )
167+ txWithStatus = TypeConvert . toTransactionWithStatus ( transactionWithStatus )
168+ this . txCache . push ( txWithStatus )
169+ }
170+ return txWithStatus
171+ }
172+
160173 // type: 'createdBy' | 'consumedBy'
161174 public pipeline = async ( lockHash : string , type : TxPointType , startBlockNumber : bigint ) => {
162175 let page = 0
@@ -180,9 +193,8 @@ export default class IndexerQueue {
180193 txPoint &&
181194 ( BigInt ( txPoint . blockNumber ) >= startBlockNumber || this . tipBlockNumber - BigInt ( txPoint . blockNumber ) < 1000 )
182195 ) {
183- const transactionWithStatus = await this . getBlocksService . getTransaction ( txPoint . txHash )
184- const ckbTransaction : CKBComponents . Transaction = transactionWithStatus . transaction
185- const transaction : Transaction = TypeConvert . toTransaction ( ckbTransaction )
196+ const transactionWithStatus = await this . getTransaction ( txPoint . txHash )
197+ const transaction : Transaction = transactionWithStatus . transaction
186198 const txUniqueFlag = {
187199 txHash : transaction . hash ,
188200 blockHash : transactionWithStatus . txStatus . blockHash !
@@ -210,9 +222,11 @@ export default class IndexerQueue {
210222 const { blockHash } = transactionWithStatus . txStatus
211223 if ( blockHash ) {
212224 const blockHeader = await this . getBlocksService . getHeader ( blockHash )
213- transaction . blockHash = blockHash
214- transaction . blockNumber = blockHeader . number
215- transaction . timestamp = blockHeader . timestamp
225+ if ( blockHeader ) {
226+ transaction . blockHash = blockHash
227+ transaction . blockNumber = blockHeader . number
228+ transaction . timestamp = blockHeader . timestamp
229+ }
216230 }
217231 txEntity = await TransactionPersistor . saveFetchTx ( transaction )
218232 }
0 commit comments