From b44892adf2784a7e58b99e83defac134c9862dd0 Mon Sep 17 00:00:00 2001 From: Tom Wang Date: Mon, 27 May 2024 10:51:11 +0800 Subject: [PATCH] refactor: replace LumosCell, LumosCellQuery with Cell, QueryOptions (#3163) * refactor: replace LumosCell, LumosCellQuery with Cell, QueryOptions * refactor: use type CellWithOutPoint for live cells data --- .../src/block-sync-renderer/index.ts | 6 ++-- .../block-sync-renderer/sync/synchronizer.ts | 36 +++---------------- .../src/block-sync-renderer/task.ts | 4 +-- .../src/models/chain/live-cell.ts | 6 ++-- .../src/services/live-cell-service.ts | 12 +++---- .../block-sync-renderer/synchronizer.test.ts | 34 +++++++++--------- .../tests/models/chain/live-cell.test.ts | 5 ++- .../services/tx/transaction-generator.test.ts | 14 ++++---- 8 files changed, 44 insertions(+), 73 deletions(-) diff --git a/packages/neuron-wallet/src/block-sync-renderer/index.ts b/packages/neuron-wallet/src/block-sync-renderer/index.ts index cf690f014b..19b0394649 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/index.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/index.ts @@ -9,7 +9,7 @@ import DataUpdateSubject from '../models/subjects/data-update' import AddressCreatedSubject from '../models/subjects/address-created-subject' import WalletDeletedSubject from '../models/subjects/wallet-deleted-subject' import TxDbChangedSubject from '../models/subjects/tx-db-changed-subject' -import { LumosCellQuery, LumosCell } from './sync/synchronizer' +import { type Cell, type QueryOptions } from '@ckb-lumos/base' import { WorkerMessage, StartParams, QueryIndexerParams } from './task' import logger from '../utils/logger' import CommonUtils from '../utils/common' @@ -88,7 +88,7 @@ export const switchToNetwork = async (newNetwork: Network, reconnected = false, await resetSyncTaskQueue.asyncPush(shouldSync) } -export const queryIndexer = async (query: LumosCellQuery): Promise => { +export const queryIndexer = async (query: QueryOptions): Promise => { const _child = child if (!_child) { return [] @@ -102,7 +102,7 @@ export const queryIndexer = async (query: LumosCellQuery): Promise return registerRequest(_child, msg).catch(err => { logger.error(`Sync:\tfailed to register query indexer task`, err) return [] - }) as Promise + }) as Promise } export const createBlockSyncTask = async () => { diff --git a/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts b/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts index b8d1a789a2..01fcfa3a12 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts @@ -1,7 +1,7 @@ import { Subject } from 'rxjs' import { queue, QueueObject } from 'async' +import { type QueryOptions } from '@ckb-lumos/base' import { Indexer as CkbIndexer, CellCollector } from '@ckb-lumos/ckb-indexer' -import { QueryOptions } from '@ckb-lumos/base' import AddressMeta from '../../database/address/meta' import { Address } from '../../models/address' import { SyncAddressType } from '../../database/chain/entities/sync-progress' @@ -14,34 +14,6 @@ export interface BlockTips { indexerTipNumber: number | undefined } -export interface LumosCellQuery { - lock: CKBComponents.Script | null - type: CKBComponents.Script | null - data: string | null -} - -export interface LumosCell { - blockHash: string - outPoint: { - txHash: string - index: string - } - cellOutput: { - capacity: string - lock: { - codeHash: string - args: string - hashType: string - } - type?: { - codeHash: string - args: string - hashType: string - } - } - data?: string -} - export interface AppendScript { walletId: string script: CKBComponents.Script @@ -57,7 +29,7 @@ export abstract class Synchronizer { protected processingBlockNumber?: string protected addressesByWalletId: Map = new Map() protected pollingIndexer: boolean = false - private indexerQueryQueue: QueueObject | undefined + private indexerQueryQueue: QueueObject | undefined protected _needGenerateAddress: boolean = false abstract connect(): Promise @@ -155,7 +127,7 @@ export abstract class Synchronizer { return false } - public async getLiveCellsByScript(query: LumosCellQuery) { + public async getLiveCellsByScript(query: QueryOptions) { return new Promise((resolve, reject) => { this.indexerQueryQueue!.push(query, (err: any, result: unknown) => { if (err) { @@ -166,7 +138,7 @@ export abstract class Synchronizer { }) } - private async collectLiveCellsByScript(query: LumosCellQuery) { + private async collectLiveCellsByScript(query: QueryOptions) { const { lock, type, data } = query if (!lock && !type) { throw new Error('at least one parameter is required') diff --git a/packages/neuron-wallet/src/block-sync-renderer/task.ts b/packages/neuron-wallet/src/block-sync-renderer/task.ts index e10291360e..4d5c4b9d5a 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/task.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/task.ts @@ -1,4 +1,4 @@ -import type { LumosCellQuery } from './sync/synchronizer' +import { type QueryOptions } from '@ckb-lumos/base' import initConnection from '../database/chain/ormconfig' import { register as registerTxStatusListener } from './tx-status-listener' import SyncQueue from './sync/queue' @@ -35,7 +35,7 @@ export interface StartParams { nodeType: SyncQueueParams[3] } -export type QueryIndexerParams = LumosCellQuery +export type QueryIndexerParams = QueryOptions export const listener = async ({ type, id, channel, message }: WorkerMessage) => { if (type === 'kill') { diff --git a/packages/neuron-wallet/src/models/chain/live-cell.ts b/packages/neuron-wallet/src/models/chain/live-cell.ts index c57fb25fed..9a7cda74a2 100644 --- a/packages/neuron-wallet/src/models/chain/live-cell.ts +++ b/packages/neuron-wallet/src/models/chain/live-cell.ts @@ -1,6 +1,6 @@ import Script, { ScriptHashType } from './script' import OutPoint from './out-point' -import { LumosCell } from '../../block-sync-renderer/sync/synchronizer' +import { type Cell, type OutPoint as IOutPoint } from '@ckb-lumos/base' const LUMOS_HASH_TYPE_MAP: Record = { type: ScriptHashType.Type, @@ -8,6 +8,8 @@ const LUMOS_HASH_TYPE_MAP: Record = { data: ScriptHashType.Data, } +export type CellWithOutPoint = Cell & { outPoint: IOutPoint } + export default class LiveCell { public txHash: string public outputIndex: string @@ -54,7 +56,7 @@ export default class LiveCell { return undefined } - public static fromLumos(cell: LumosCell): LiveCell { + public static fromLumos(cell: CellWithOutPoint): LiveCell { const type = cell.cellOutput.type ? new Script( cell.cellOutput.type.codeHash, diff --git a/packages/neuron-wallet/src/services/live-cell-service.ts b/packages/neuron-wallet/src/services/live-cell-service.ts index 4b7766d3db..3fe38814a6 100644 --- a/packages/neuron-wallet/src/services/live-cell-service.ts +++ b/packages/neuron-wallet/src/services/live-cell-service.ts @@ -1,7 +1,7 @@ import Script from '../models/chain/script' -import LiveCell from '../models/chain/live-cell' +import LiveCell, { CellWithOutPoint } from '../models/chain/live-cell' import { queryIndexer } from '../block-sync-renderer/index' -import { LumosCell, LumosCellQuery } from '../block-sync-renderer/sync/synchronizer' +import { type QueryOptions } from '@ckb-lumos/base' export default class LiveCellService { private static instance: LiveCellService @@ -20,14 +20,14 @@ export default class LiveCellService { lock: Script | null, type: Script | null, data: string | null - ): Promise { + ): Promise { if (!lock && !type) { throw new Error('at least one parameter is required') } - const query: LumosCellQuery = { lock, type, data } - const liveCells: LumosCell[] = await queryIndexer(query) - return liveCells + const query = { lock, type, data } as QueryOptions + const liveCells = await queryIndexer(query) + return liveCells as CellWithOutPoint[] } public async getOneByLockScriptAndTypeScript(lock: Script | null, type: Script | null) { diff --git a/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts b/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts index c3c1e7e325..f237f7068d 100644 --- a/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts +++ b/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts @@ -1,8 +1,9 @@ import { scriptToAddress } from '../../src/utils/scriptAndAddress' import { AddressType } from '@ckb-lumos/hd' +import { QueryOptions, type Cell } from '@ckb-lumos/base' import { Address, AddressVersion } from '../../src/models/address' import SystemScriptInfo from '../../src/models/system-script-info' -import { Synchronizer, type LumosCell, type LumosCellQuery } from '../../src/block-sync-renderer/sync/synchronizer' +import { Synchronizer } from '../../src/block-sync-renderer/sync/synchronizer' import AddressMeta from '../../src/database/address/meta' import IndexerTxHashCache from '../../src/database/chain/entities/indexer-tx-hash-cache' import { ScriptHashType } from '../../src/models/chain/script' @@ -220,10 +221,11 @@ describe('unit tests for IndexerConnector', () => { }) describe('#getLiveCellsByScript', () => { - let fakeCell1: LumosCell, fakeCell2: LumosCell - let cells: LumosCell[] + let fakeCell1: Cell, fakeCell2: Cell + let cells: Cell[] fakeCell1 = { + data: '0x', blockHash: '0x', outPoint: { txHash: '0x', @@ -244,6 +246,7 @@ describe('unit tests for IndexerConnector', () => { }, } fakeCell2 = { + data: '0x', blockHash: '0x', outPoint: { txHash: '0x', @@ -257,7 +260,7 @@ describe('unit tests for IndexerConnector', () => { args: '0x2', }, type: { - hashType: 'lock', + hashType: 'data', codeHash: '0xcode', args: '0x2', }, @@ -272,7 +275,7 @@ describe('unit tests for IndexerConnector', () => { }) describe('when success', () => { - const query: LumosCellQuery = { + const query = { lock: { hashType: ScriptHashType.Data, codeHash: '0xcode', @@ -283,7 +286,6 @@ describe('unit tests for IndexerConnector', () => { codeHash: '0xcode', args: '0x', }, - data: null, } beforeEach(async () => { @@ -298,14 +300,14 @@ describe('unit tests for IndexerConnector', () => { it('transform the query parameter', () => { expect(stubbedCellCollectorConstructor.mock.calls[0][1]).toEqual({ lock: { - hashType: query.lock!.hashType, - codeHash: query.lock!.codeHash, - args: query.lock!.args, + hashType: query.lock.hashType, + codeHash: query.lock.codeHash, + args: query.lock.args, }, type: { - hashType: query.type!.hashType, - codeHash: query.type!.codeHash, - args: query.type!.args, + hashType: query.type.hashType, + codeHash: query.type.codeHash, + args: query.type.args, }, data: 'any', }) @@ -316,7 +318,7 @@ describe('unit tests for IndexerConnector', () => { }) }) describe('when handling concurrent requests', () => { - const query1: LumosCellQuery = { + const query1: QueryOptions = { lock: { hashType: ScriptHashType.Data, codeHash: '0xcode', @@ -327,9 +329,8 @@ describe('unit tests for IndexerConnector', () => { codeHash: '0xcode', args: '0x1', }, - data: null, } - const query2: LumosCellQuery = { + const query2: QueryOptions = { lock: { hashType: ScriptHashType.Type, codeHash: '0xcode', @@ -340,7 +341,6 @@ describe('unit tests for IndexerConnector', () => { codeHash: '0xcode', args: '0x2', }, - data: null, } const results: unknown[] = [] @@ -401,7 +401,7 @@ describe('unit tests for IndexerConnector', () => { it('throws error', async () => { let err try { - await synchronizer.getLiveCellsByScript({ lock: null, type: null, data: null }) + await synchronizer.getLiveCellsByScript({}) } catch (error) { err = error } diff --git a/packages/neuron-wallet/tests/models/chain/live-cell.test.ts b/packages/neuron-wallet/tests/models/chain/live-cell.test.ts index 84a9b97830..1bc5cba2fd 100644 --- a/packages/neuron-wallet/tests/models/chain/live-cell.test.ts +++ b/packages/neuron-wallet/tests/models/chain/live-cell.test.ts @@ -1,6 +1,5 @@ import Script, { ScriptHashType } from '../../../src/models/chain/script' -import { LumosCell } from '../../../src/block-sync-renderer/sync/synchronizer' -import LiveCell from '../../../src/models/chain/live-cell' +import LiveCell, { CellWithOutPoint } from '../../../src/models/chain/live-cell' describe('LiveCell Test', () => { const INITIAL_DATA = { @@ -88,7 +87,7 @@ describe('LiveCell Test', () => { }) describe('get cells from lumos cell', () => { - const LUMOS_CELL: LumosCell = { + const LUMOS_CELL: CellWithOutPoint = { blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000', outPoint: { txHash: INITIAL_DATA.txHash, diff --git a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts index c5217e5f11..c8ba7d414a 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -1,6 +1,6 @@ import { when } from 'jest-when' -import { since } from '@ckb-lumos/base' import { bytes } from '@ckb-lumos/codec' +import { since } from '@ckb-lumos/base' import OutputEntity from '../../../src/database/chain/entities/output' import InputEntity from '../../../src/database/chain/entities/input' import TransactionEntity from '../../../src/database/chain/entities/transaction' @@ -28,7 +28,7 @@ import { SudtAcpHaveDataError, TargetOutputNotFoundError, } from '../../../src/exceptions' -import LiveCell from '../../../src/models/chain/live-cell' +import LiveCell, { CellWithOutPoint } from '../../../src/models/chain/live-cell' import { keyInfos } from '../../setupAndTeardown/public-key-info.fixture' const randomHex = (length: number = 64): string => { @@ -86,7 +86,6 @@ import HdPublicKeyInfo from '../../../src/database/chain/entities/hd-public-key- import AssetAccount from '../../../src/models/asset-account' import MultisigConfigModel from '../../../src/models/multisig-config' import MultisigOutput from '../../../src/database/chain/entities/multisig-output' -import { LumosCell } from '../../../src/block-sync-renderer/sync/synchronizer' import { closeConnection, getConnection, initConnection } from '../../setupAndTeardown' describe('TransactionGenerator', () => { @@ -1097,8 +1096,8 @@ describe('TransactionGenerator', () => { tokenID: string | undefined = undefined, lockScript: Script = bobAnyoneCanPayLockScript, customData: string = '0x' - ): LumosCell => { - const liveCell: LumosCell = { + ): CellWithOutPoint => { + const liveCell: CellWithOutPoint = { blockHash: randomHex(), outPoint: { txHash: randomHex(), @@ -1109,18 +1108,17 @@ describe('TransactionGenerator', () => { lock: { codeHash: lockScript.codeHash, args: lockScript.args, - hashType: lockScript.hashType.toString(), + hashType: lockScript.hashType, }, }, data: '0x', } if (tokenID) { const typeScript = assetAccountInfo.generateSudtScript(tokenID) - // @ts-ignore liveCell.cellOutput.type = { codeHash: typeScript.codeHash, args: typeScript.args, - hashType: typeScript.hashType.toString(), + hashType: typeScript.hashType, } } liveCell.data = amount ? BufferUtils.writeBigUInt128LE(BigInt(amount)) : '0x'