Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace LumosCell, LumosCellQuery with Cell, QueryOptions #3163

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/block-sync-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -88,7 +88,7 @@ export const switchToNetwork = async (newNetwork: Network, reconnected = false,
await resetSyncTaskQueue.asyncPush(shouldSync)
}

export const queryIndexer = async (query: LumosCellQuery): Promise<LumosCell[]> => {
export const queryIndexer = async (query: QueryOptions): Promise<Cell[]> => {
const _child = child
if (!_child) {
return []
Expand All @@ -102,7 +102,7 @@ export const queryIndexer = async (query: LumosCellQuery): Promise<LumosCell[]>
return registerRequest(_child, msg).catch(err => {
logger.error(`Sync:\tfailed to register query indexer task`, err)
return []
}) as Promise<LumosCell[]>
}) as Promise<Cell[]>
}

export const createBlockSyncTask = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -57,7 +29,7 @@ export abstract class Synchronizer {
protected processingBlockNumber?: string
protected addressesByWalletId: Map<string, AddressMeta[]> = new Map()
protected pollingIndexer: boolean = false
private indexerQueryQueue: QueueObject<LumosCellQuery> | undefined
private indexerQueryQueue: QueueObject<QueryOptions> | undefined
protected _needGenerateAddress: boolean = false

abstract connect(): Promise<void>
Expand Down Expand Up @@ -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) {
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/block-sync-renderer/task.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 4 additions & 2 deletions packages/neuron-wallet/src/models/chain/live-cell.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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<string, ScriptHashType> = {
type: ScriptHashType.Type,
data1: ScriptHashType.Data1,
data: ScriptHashType.Data,
}

export type CellWithOutPoint = Cell & { outPoint: IOutPoint }

export default class LiveCell {
public txHash: string
public outputIndex: string
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions packages/neuron-wallet/src/services/live-cell-service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,14 +20,14 @@ export default class LiveCellService {
lock: Script | null,
type: Script | null,
data: string | null
): Promise<LumosCell[]> {
): Promise<CellWithOutPoint[]> {
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
homura marked this conversation as resolved.
Show resolved Hide resolved
const liveCells = await queryIndexer(query)
return liveCells as CellWithOutPoint[]
}

public async getOneByLockScriptAndTypeScript(lock: Script | null, type: Script | null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
Expand All @@ -244,6 +246,7 @@ describe('unit tests for IndexerConnector', () => {
},
}
fakeCell2 = {
data: '0x',
blockHash: '0x',
outPoint: {
txHash: '0x',
Expand All @@ -257,7 +260,7 @@ describe('unit tests for IndexerConnector', () => {
args: '0x2',
},
type: {
hashType: 'lock',
hashType: 'data',
Keith-CY marked this conversation as resolved.
Show resolved Hide resolved
codeHash: '0xcode',
args: '0x2',
},
Expand All @@ -272,7 +275,7 @@ describe('unit tests for IndexerConnector', () => {
})

describe('when success', () => {
const query: LumosCellQuery = {
const query = {
lock: {
hashType: ScriptHashType.Data,
codeHash: '0xcode',
Expand All @@ -283,7 +286,6 @@ describe('unit tests for IndexerConnector', () => {
codeHash: '0xcode',
args: '0x',
},
data: null,
}

beforeEach(async () => {
Expand All @@ -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',
})
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -340,7 +341,6 @@ describe('unit tests for IndexerConnector', () => {
codeHash: '0xcode',
args: '0x2',
},
data: null,
}

const results: unknown[] = []
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions packages/neuron-wallet/tests/models/chain/live-cell.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(),
Expand All @@ -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'
Expand Down