From 1bc25ebda787f6a3ae49f49bcea2e170ed042df2 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Mon, 11 Nov 2019 15:50:13 +0800 Subject: [PATCH] feat: remove SkipDataAndType module --- .../neuron-wallet/src/database/address/dao.ts | 10 ++-- packages/neuron-wallet/src/services/cells.ts | 42 +++++--------- .../services/settings/skip-data-and-type.ts | 39 ------------- .../tests/services/cells.test.ts | 51 ++--------------- .../settings/skip-data-and-type.test.ts | 57 ------------------- .../services/tx/transaction-generator.test.ts | 2 - 6 files changed, 21 insertions(+), 180 deletions(-) delete mode 100644 packages/neuron-wallet/src/services/settings/skip-data-and-type.ts delete mode 100644 packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts diff --git a/packages/neuron-wallet/src/database/address/dao.ts b/packages/neuron-wallet/src/database/address/dao.ts index 1bada2fd3f..4f1b1b1f6d 100644 --- a/packages/neuron-wallet/src/database/address/dao.ts +++ b/packages/neuron-wallet/src/database/address/dao.ts @@ -75,12 +75,10 @@ export default class AddressDao { const addressEntity = entity addressEntity.txCount = txCount const lockHashes: string[] = lockUtils.addressToAllLockHashes(addressEntity.address) - addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, true) - addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true) - addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending, true) - const totalLiveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, false) - const totalSentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, false) - addressEntity.totalBalance = (BigInt(totalLiveBalance) - BigInt(totalSentBalance)).toString() + addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live) + addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent) + addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending) + addressEntity.totalBalance = '0' return addressEntity }) ) diff --git a/packages/neuron-wallet/src/services/cells.ts b/packages/neuron-wallet/src/services/cells.ts index 2a16c9dd1d..86038e038d 100644 --- a/packages/neuron-wallet/src/services/cells.ts +++ b/packages/neuron-wallet/src/services/cells.ts @@ -3,7 +3,6 @@ import OutputEntity from 'database/chain/entities/output' import { Cell, OutPoint, Input } from 'types/cell-types' import { CapacityNotEnough, CapacityNotEnoughForChange } from 'exceptions' import { OutputStatus } from './tx/params' -import SkipDataAndType from './settings/skip-data-and-type' export const MIN_CELL_CAPACITY = '6100000000' @@ -11,21 +10,8 @@ export default class CellsService { // exclude hasData = true and typeScript != null public static getBalance = async ( lockHashes: string[], - status: OutputStatus, - skipDataAndType: boolean + status: OutputStatus ): Promise => { - const queryParams = { - lockHash: In(lockHashes), - status, - } - - if (skipDataAndType) { - Object.assign(queryParams, { - hasData: false, - typeScript: null, - }) - } - const cells: OutputEntity[] = await getConnection() .getRepository(OutputEntity) .createQueryBuilder('output') @@ -36,7 +22,12 @@ export default class CellsService { "output.typeScript", "output.capacity" ]) - .where(queryParams) + .where({ + lockHash: In(lockHashes), + status, + hasData: false, + typeScript: null, + }) .getMany() const capacity: bigint = cells.map(c => BigInt(c.capacity)).reduce((result, c) => result + c, BigInt(0)) @@ -95,23 +86,16 @@ export default class CellsService { throw new Error(`capacity can't be less than ${MIN_CELL_CAPACITY}`) } - const queryParams = { - lockHash: In(lockHashes), - status: OutputStatus.Live, - } - const skipDataAndType = SkipDataAndType.getInstance().get() - if (skipDataAndType) { - Object.assign(queryParams, { - hasData: false, - typeScript: null, - }) - } - // only live cells, skip which has data or type const cellEntities: OutputEntity[] = await getConnection() .getRepository(OutputEntity) .find({ - where: queryParams, + where: { + lockHash: In(lockHashes), + status: OutputStatus.Live, + hasData: false, + typeScript: null, + }, }) cellEntities.sort((a, b) => { const result = BigInt(a.capacity) - BigInt(b.capacity) diff --git a/packages/neuron-wallet/src/services/settings/skip-data-and-type.ts b/packages/neuron-wallet/src/services/settings/skip-data-and-type.ts deleted file mode 100644 index c4b0e40a50..0000000000 --- a/packages/neuron-wallet/src/services/settings/skip-data-and-type.ts +++ /dev/null @@ -1,39 +0,0 @@ -import BaseSettings from './base' - -export default class SkipDataAndType { - private skip: boolean | undefined = undefined - private keyName = 'skip' - - private static instance: SkipDataAndType - - static getInstance(): SkipDataAndType { - if (!SkipDataAndType.instance) { - SkipDataAndType.instance = new SkipDataAndType() - } - - return SkipDataAndType.instance - } - - // skip means can use cells with data and type - public update(skip: boolean) { - BaseSettings.getInstance().updateSetting(this.keyName, skip) - // cache this variable - this.skip = skip - } - - public get(): boolean { - // if cached, don't read file - if (this.skip !== undefined) { - return this.skip - } - - const skip = BaseSettings.getInstance().getSetting(this.keyName) - - if (skip === false) { - return false - } - - // default is true - return true - } -} diff --git a/packages/neuron-wallet/tests/services/cells.test.ts b/packages/neuron-wallet/tests/services/cells.test.ts index ef5aaff4d7..e0dd9049a3 100644 --- a/packages/neuron-wallet/tests/services/cells.test.ts +++ b/packages/neuron-wallet/tests/services/cells.test.ts @@ -5,7 +5,6 @@ import { OutputStatus } from '../../src/services/tx/params' import { ScriptHashType, Script } from '../../src/types/cell-types' import CellsService from '../../src/services/cells' import { CapacityNotEnough, CapacityNotEnoughForChange } from '../../src/exceptions/wallet' -import SkipDataAndType from '../../src/services/settings/skip-data-and-type' const randomHex = (length: number = 64): string => { const str: string = Array.from({ length }) @@ -136,36 +135,22 @@ describe('CellsService', () => { it('getBalance, Live, skip', async () => { await createCells() - const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, true) + const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live) expect(balance).toEqual('100') }) it('getBalance, Sent, skip', async () => { await createCells() - const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true) + const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent) expect(balance).toEqual('200') }) - it('getBalance, Live, not skip', async () => { - await createCells() - - const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, false) - expect(balance).toEqual('11100') - }) - - it('getBalance, Pending, not skip', async () => { - await createCells() - - const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Pending, false) - expect(balance).toEqual('33300') - }) - it('getBalance with alice', async () => { await createCells() await createCell('2222', OutputStatus.Live, false, null, alice) - const balance: string = await CellsService.getBalance([alice.lockHash, bob.lockHash], OutputStatus.Live, true) + const balance: string = await CellsService.getBalance([alice.lockHash, bob.lockHash], OutputStatus.Live) expect(balance).toEqual((100 + 2222).toString()) }) @@ -173,7 +158,7 @@ describe('CellsService', () => { await createCells() await createCell('2222', OutputStatus.Live, false, null, alice) - const balance: string = await CellsService.getBalance([alice.lockHash], OutputStatus.Live, true) + const balance: string = await CellsService.getBalance([alice.lockHash], OutputStatus.Live) expect(balance).toEqual('2222') }) }) @@ -191,7 +176,6 @@ describe('CellsService', () => { } it('1000, skip', async () => { - SkipDataAndType.getInstance().update(true) await createCells() const result = await CellsService.gatherInputs(toShannon('1000'), lockHashes) @@ -200,7 +184,6 @@ describe('CellsService', () => { }) it('1001, skip', async () => { - SkipDataAndType.getInstance().update(true) await createCells() let error @@ -212,31 +195,7 @@ describe('CellsService', () => { expect(error).toBeInstanceOf(CapacityNotEnough) }) - it('6000, not skip', async () => { - SkipDataAndType.getInstance().update(false) - await createCells() - - const ckb = toShannon('6000') - const result = await CellsService.gatherInputs(ckb, lockHashes) - - expect(result.capacities).toEqual(ckb) - }) - - it('6001, not skip', async () => { - SkipDataAndType.getInstance().update(false) - await createCells() - - let error - try { - await CellsService.gatherInputs(toShannon('6001'), lockHashes) - } catch (e) { - error = e - } - expect(error).toBeInstanceOf(CapacityNotEnough) - }) - it(`bob's and alice's cells`, async () => { - SkipDataAndType.getInstance().update(true) await createCells() await createCell(toShannon('5000'), OutputStatus.Live, false, null, alice) @@ -246,7 +205,6 @@ describe('CellsService', () => { }) it(`only bob's cells`, async () => { - SkipDataAndType.getInstance().update(true) await createCells() await createCell(toShannon('5000'), OutputStatus.Live, false, null, alice) @@ -274,7 +232,6 @@ describe('CellsService', () => { describe('skip, by feeRate 1000', () => { beforeEach(async done => { - SkipDataAndType.getInstance().update(true) const cells: OutputEntity[] = [ generateCell(toShannon('1000'), OutputStatus.Live, false, null), generateCell(toShannon('2000'), OutputStatus.Live, false, null), diff --git a/packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts b/packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts deleted file mode 100644 index 99b7851e06..0000000000 --- a/packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import SkipDataAndType from '../../../src/services/settings/skip-data-and-type' -import FileService from '../../../src/services/file' -import BaseSettings from '../../../src/services/settings/base' - -describe(`SkipDataAndType`, () => { - let skipDataAndType: SkipDataAndType | undefined - - beforeEach(() => { - const fileService = FileService.getInstance() - // @ts-ignore: Private method - const { moduleName, fileName } = BaseSettings - if (fileService.hasFile(moduleName, fileName)) { - fileService.deleteFileSync(moduleName, fileName) - } - - skipDataAndType = new SkipDataAndType() - }) - - it('getInstance', () => { - const skip = SkipDataAndType.getInstance() - expect(skip).toBeInstanceOf(SkipDataAndType) - }) - - it('update', () => { - expect(() => { - skipDataAndType!.update(true) - }).not.toThrowError() - }) - - describe('with instance cache', () => { - it('get true', () => { - skipDataAndType!.update(true) - const skip = skipDataAndType!.get() - expect(skip).toBe(true) - }) - - it('get false', () => { - skipDataAndType!.update(false) - const skip = skipDataAndType!.get() - expect(skip).toBe(false) - }) - }) - - describe('without cache', () => { - it('first time open', () => { - const skip = skipDataAndType!.get() - expect(skip).toBe(true) - }) - - it('new instance', () => { - skipDataAndType!.update(false) - const newInstance = new SkipDataAndType() - const skip = newInstance.get() - expect(skip).toBe(false) - }) - }) -}) 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 d2abd9f481..5edcb86a6a 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -3,7 +3,6 @@ import { initConnection } from '../../../src/database/chain/ormconfig' import { ScriptHashType, Script, TransactionWithoutHash } from '../../../src/types/cell-types' import { OutputStatus } from '../../../src/services/tx/params' import OutputEntity from '../../../src/database/chain/entities/output' -import SkipDataAndType from '../../../src/services/settings/skip-data-and-type' import TransactionGenerator from '../../../src/services/tx/transaction-generator' import LockUtils from '../../../src/models/lock-utils' import CellsService from '../../../src/services/cells' @@ -110,7 +109,6 @@ describe('TransactionGenerator', () => { describe('generateTx', () => { beforeEach(async done => { - SkipDataAndType.getInstance().update(true) const cells: OutputEntity[] = [ generateCell(toShannon('1000'), OutputStatus.Live, false, null), generateCell(toShannon('2000'), OutputStatus.Live, false, null),