@@ -2,7 +2,7 @@ import { Subject, Subscription } from 'rxjs'
2
2
import Utils from 'services/sync/utils'
3
3
import logger from 'utils/logger'
4
4
import GetBlocks from 'services/sync/get-blocks'
5
- import { Transaction } from 'types/cell-types'
5
+ import { Transaction , TransactionWithStatus } from 'types/cell-types'
6
6
import TypeConvert from 'types/type-convert'
7
7
import BlockNumber from 'services/sync/block-number'
8
8
import AddressesUsedSubject from 'models/subjects/addresses-used-subject'
@@ -13,6 +13,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
13
13
import IndexerRPC from './indexer-rpc'
14
14
import HexUtils from 'utils/hex'
15
15
import { TxUniqueFlagCache } from './tx-unique-flag'
16
+ import { TransactionCache } from './transaction-cache'
16
17
import TransactionEntity from 'database/chain/entities/transaction'
17
18
18
19
export interface LockHashInfo {
@@ -43,6 +44,7 @@ export default class IndexerQueue {
43
44
private resetFlag = false
44
45
45
46
private latestCreatedBy : TxUniqueFlagCache = new TxUniqueFlagCache ( 100 )
47
+ private txCache : TransactionCache = new TransactionCache ( 100 )
46
48
47
49
private url : string
48
50
@@ -123,6 +125,7 @@ export default class IndexerQueue {
123
125
const result = await this . getBlocksService . getTransaction ( tx . hash )
124
126
if ( ! result ) {
125
127
await IndexerTransaction . deleteTxWhenFork ( tx . hash )
128
+ this . txCache . delete ( tx . hash )
126
129
} else if ( tip - BigInt ( tx . blockNumber ) >= 1000 ) {
127
130
await IndexerTransaction . confirm ( tx . hash )
128
131
}
@@ -157,6 +160,16 @@ export default class IndexerQueue {
157
160
} )
158
161
}
159
162
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
+
160
173
// type: 'createdBy' | 'consumedBy'
161
174
public pipeline = async ( lockHash : string , type : TxPointType , startBlockNumber : bigint ) => {
162
175
let page = 0
@@ -180,9 +193,8 @@ export default class IndexerQueue {
180
193
txPoint &&
181
194
( BigInt ( txPoint . blockNumber ) >= startBlockNumber || this . tipBlockNumber - BigInt ( txPoint . blockNumber ) < 1000 )
182
195
) {
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
186
198
const txUniqueFlag = {
187
199
txHash : transaction . hash ,
188
200
blockHash : transactionWithStatus . txStatus . blockHash !
@@ -210,9 +222,11 @@ export default class IndexerQueue {
210
222
const { blockHash } = transactionWithStatus . txStatus
211
223
if ( blockHash ) {
212
224
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
+ }
216
230
}
217
231
txEntity = await TransactionPersistor . saveFetchTx ( transaction )
218
232
}
0 commit comments