|
| 1 | +import Utils from 'services/sync/utils' |
| 2 | +import logger from 'utils/logger' |
| 3 | +import GetBlocks from 'services/sync/get-blocks' |
| 4 | +import { Transaction } from 'types/cell-types' |
| 5 | +import TypeConvert from 'types/type-convert' |
| 6 | +import BlockNumber from 'services/sync/block-number' |
| 7 | + |
| 8 | +import IndexerRPC from './indexer-rpc' |
| 9 | +import TransactionPersistor from '../tx/transaction-persistor' |
| 10 | + |
| 11 | +export default class Queue { |
| 12 | + private lockHashes: string[] |
| 13 | + private indexerRPC: IndexerRPC |
| 14 | + private getBlocksService: GetBlocks |
| 15 | + private per = 50 |
| 16 | + private interval = 5000 |
| 17 | + private blockNumberService: BlockNumber |
| 18 | + |
| 19 | + private stopped = false |
| 20 | + private indexed = false |
| 21 | + |
| 22 | + private inProcess = false |
| 23 | + |
| 24 | + constructor(lockHashes: string[]) { |
| 25 | + this.lockHashes = lockHashes |
| 26 | + this.indexerRPC = new IndexerRPC() |
| 27 | + this.getBlocksService = new GetBlocks() |
| 28 | + this.blockNumberService = new BlockNumber() |
| 29 | + } |
| 30 | + |
| 31 | + public setLockHashes = (lockHashes: string[]): void => { |
| 32 | + this.lockHashes = lockHashes |
| 33 | + this.indexed = false |
| 34 | + } |
| 35 | + |
| 36 | + /* eslint no-await-in-loop: "off" */ |
| 37 | + /* eslint no-restricted-syntax: "off" */ |
| 38 | + public start = async () => { |
| 39 | + while (!this.stopped) { |
| 40 | + try { |
| 41 | + this.inProcess = true |
| 42 | + const { lockHashes } = this |
| 43 | + if (!this.indexed) { |
| 44 | + await this.indexLockHashes(lockHashes) |
| 45 | + this.indexed = true |
| 46 | + } |
| 47 | + const currentBlockNumber = await this.getCurrentBlockNumber(lockHashes) |
| 48 | + for (const lockHash of lockHashes) { |
| 49 | + await this.pipeline(lockHash, 'createdBy') |
| 50 | + } |
| 51 | + for (const lockHash of lockHashes) { |
| 52 | + await this.pipeline(lockHash, 'consumedBy') |
| 53 | + } |
| 54 | + if (currentBlockNumber) { |
| 55 | + await this.blockNumberService.updateCurrent(currentBlockNumber) |
| 56 | + } |
| 57 | + await this.yield(this.interval) |
| 58 | + } catch (err) { |
| 59 | + logger.error('sync indexer error:', err) |
| 60 | + } finally { |
| 61 | + await this.yield() |
| 62 | + this.inProcess = false |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public getCurrentBlockNumber = async (lockHashes: string[]) => { |
| 68 | + // get lock hash indexer status |
| 69 | + const lockHashIndexStates = await this.indexerRPC.getLockHashIndexStates() |
| 70 | + const blockNumbers = lockHashIndexStates |
| 71 | + .filter(state => lockHashes.includes(state.lockHash)) |
| 72 | + .map(state => state.blockNumber) |
| 73 | + const uniqueBlockNumbers = [...new Set(blockNumbers)] |
| 74 | + const blockNumbersBigInt = uniqueBlockNumbers.map(num => BigInt(num)) |
| 75 | + const minBlockNumber = blockNumbersBigInt.sort()[0] |
| 76 | + return minBlockNumber |
| 77 | + } |
| 78 | + |
| 79 | + public indexLockHashes = async (lockHashes: string[]) => { |
| 80 | + await Utils.mapSeries(lockHashes, async (lockHash: string) => { |
| 81 | + await this.indexerRPC.indexLockHash(lockHash) |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + // type: 'createdBy' | 'consumedBy' |
| 86 | + public pipeline = async (lockHash: string, type: string) => { |
| 87 | + let page = 0 |
| 88 | + let stopped = false |
| 89 | + while (!stopped) { |
| 90 | + const txs = await this.indexerRPC.getTransactionByLockHash(lockHash, page.toString(), this.per.toString()) |
| 91 | + if (txs.length < this.per) { |
| 92 | + stopped = true |
| 93 | + } |
| 94 | + for (const tx of txs) { |
| 95 | + let txPoint: CKBComponents.TransactionPoint | null = null |
| 96 | + if (type === 'createdBy') { |
| 97 | + txPoint = tx.createdBy |
| 98 | + } else if (type === 'consumedBy') { |
| 99 | + txPoint = tx.consumedBy |
| 100 | + } |
| 101 | + if (txPoint) { |
| 102 | + const transactionWithStatus = await this.getBlocksService.getTransaction(txPoint.txHash) |
| 103 | + const ckbTransaction: CKBComponents.Transaction = transactionWithStatus.transaction |
| 104 | + const transaction: Transaction = TypeConvert.toTransaction(ckbTransaction) |
| 105 | + // tx timestamp / blockNumber / blockHash |
| 106 | + const { blockHash } = transactionWithStatus.txStatus |
| 107 | + if (blockHash) { |
| 108 | + const blockHeader = await this.getBlocksService.getHeader(blockHash) |
| 109 | + transaction.blockHash = blockHash |
| 110 | + transaction.blockNumber = blockHeader.number |
| 111 | + transaction.timestamp = blockHeader.timestamp |
| 112 | + } |
| 113 | + await TransactionPersistor.saveFetchTx(transaction) |
| 114 | + } |
| 115 | + } |
| 116 | + page += 1 |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public stop = () => { |
| 121 | + this.stopped = true |
| 122 | + } |
| 123 | + |
| 124 | + public waitForDrained = async (timeout: number = 5000) => { |
| 125 | + const startAt: number = +new Date() |
| 126 | + while (this.inProcess) { |
| 127 | + const now: number = +new Date() |
| 128 | + if (now - startAt > timeout) { |
| 129 | + return |
| 130 | + } |
| 131 | + await this.yield(50) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public stopAndWait = async () => { |
| 136 | + this.stop() |
| 137 | + await this.waitForDrained() |
| 138 | + } |
| 139 | + |
| 140 | + private yield = async (millisecond: number = 1) => { |
| 141 | + await Utils.sleep(millisecond) |
| 142 | + } |
| 143 | +} |
0 commit comments