Skip to content

Commit

Permalink
Merge branch 'develop' into fix-history-table
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored May 15, 2024
2 parents 0cb98d7 + 9060738 commit b550565
Show file tree
Hide file tree
Showing 32 changed files with 166 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .ckb-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.115.0
v0.116.1
5 changes: 5 additions & 0 deletions compatible.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"fullVersions": [
"0.116",
"0.115",
"0.114",
"0.113",
Expand All @@ -21,6 +22,7 @@
"compatible": {
"0.111": {
"full": [
"0.116",
"0.115",
"0.114",
"0.113",
Expand All @@ -36,6 +38,7 @@
},
"0.110": {
"full": [
"0.116",
"0.115",
"0.114",
"0.113",
Expand Down Expand Up @@ -67,6 +70,7 @@
},
"0.112": {
"full": [
"0.116",
"0.115",
"0.114",
"0.113",
Expand All @@ -82,6 +86,7 @@
},
"0.114": {
"full": [
"0.116",
"0.115",
"0.114",
"0.113",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"resolutions": {
"@types/react": "18.2.45",
"react-i18next": ">=11.16.4",
"react-refresh": "0.14.0"
"react-refresh": "0.14.0",
"node-fetch": "2.6.13"
},
"volta": {
"node": "20.10.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"sqlite3": "5.1.6",
"subleveldown": "4.1.4",
"tslib": "2.6.2",
"typeorm": "0.2.45",
"typeorm": "0.3.17",
"undici": "5.28.4",
"uuid": "8.3.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class IndexerCacheService {
cacheBlockNumberEntity =
(await getConnection()
.getRepository(SyncInfoEntity)
.findOne({ name: SyncInfoEntity.getLastCachedKey(blake160) })) ??
.findOneBy({ name: SyncInfoEntity.getLastCachedKey(blake160) })) ??
SyncInfoEntity.fromObject({
name: SyncInfoEntity.getLastCachedKey(blake160),
value: '0x0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class TxAddressFinder {
let shouldSync = false
for (const input of inputs) {
const outPoint: OutPoint = input.previousOutput!
const output = await getConnection().getRepository(OutputEntity).findOne({
const output = await getConnection().getRepository(OutputEntity).findOneBy({
outPointTxHash: outPoint.txHash,
outPointIndex: outPoint.index,
})
Expand Down
6 changes: 2 additions & 4 deletions packages/neuron-wallet/src/database/chain/connection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { getConnection as originGetConnection } from 'typeorm'
import { ConnectionName, dataSource } from './ormconfig'
import { NetworkType } from '../../models/network'
import NetworksService from '../../services/networks'

export type ConnectionName = 'light' | 'full'

export function getCurrentConnectionName(): ConnectionName {
return NetworksService.getInstance().getCurrent()?.type === NetworkType.Light ? 'light' : 'full'
}

export function getConnection(connectionName: ConnectionName = getCurrentConnectionName()) {
const connection = originGetConnection(connectionName)
const connection = dataSource[connectionName]
if (!connection) {
throw new Error(`The connection ${connectionName} should be initialized before use`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default class MultisigConfig {
blake160s!: string[]

@Column()
alias!: string
alias: string = ''

@Column()
lastestBlockNumber!: string
lastestBlockNumber: string = ''

public static fromModel(model: MultisigConfigModel): MultisigConfig {
const multisigConfig = new MultisigConfig()
Expand Down
79 changes: 59 additions & 20 deletions packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, getConnectionOptions, getConnection } from 'typeorm'
import { AbstractLogger, DataSource, LogLevel, LogMessage } from 'typeorm'
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'
import path from 'path'
import fs from 'fs'
Expand Down Expand Up @@ -63,32 +63,65 @@ import { CreateCellLocalInfo1701234043432 } from './migrations/1701234043432-Cre
import { RenameSyncProgress1702781527414 } from './migrations/1702781527414-RenameSyncProgress'
import { RemoveAddressInIndexerCache1704357651876 } from './migrations/1704357651876-RemoveAddressInIndexerCache'
import { AmendTransaction1709008125088 } from './migrations/1709008125088-AmendTransaction'
import { ConnectionName } from './connection'
import AddressSubscribe from './subscriber/address-subscriber'
import MultisigConfigSubscribe from './subscriber/multisig-config-subscriber'
import TxDescriptionSubscribe from './subscriber/tx-description-subscriber'
import SudtTokenInfoSubscribe from './subscriber/sudt-token-info-subscriber'
import AssetAccountSubscribe from './subscriber/asset-account-subscriber'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'
export type ConnectionName = 'light' | 'full'

const dbPath = (name: string, connectionName: string): string => {
const filename = `${connectionName}-${name}.sqlite`
return path.join(env.fileBasePath, 'cells', filename)
}

const connectOptions = async (
genesisBlockHash: string,
connectionName: ConnectionName
): Promise<SqliteConnectionOptions> => {
const connectionOptions = await getConnectionOptions()
class TypeormLogger extends AbstractLogger {
/**
* Write log to specific output.
*/
protected writeLog(level: LogLevel, logMessage: LogMessage | LogMessage[]) {
const messages = this.prepareLogMessages(logMessage, {
highlightSql: false,
})

for (let message of messages) {
switch (message.type ?? level) {
case 'log':
case 'schema-build':
case 'migration':
case 'info':
case 'query':
case 'warn':
case 'query-slow':
if (message.prefix) {
console.info(message.prefix, message.message)
} else {
console.info(message.message)
}
break

case 'error':
case 'query-error':
if (message.prefix) {
console.error(message.prefix, message.message)
} else {
console.error(message.message)
}
break
}
}
}
}

const getConnectionOptions = (genesisBlockHash: string, connectionName: ConnectionName): SqliteConnectionOptions => {
const database = env.isTestMode ? ':memory:' : dbPath(genesisBlockHash, connectionName)

const logging: boolean | ('query' | 'schema' | 'error' | 'warn' | 'info' | 'log' | 'migration')[] = ['warn', 'error']
// (env.isDevMode) ? ['warn', 'error', 'log', 'info', 'schema', 'migration'] : ['warn', 'error']

return {
...connectionOptions,
name: connectionName,
type: 'sqlite',
database,
Expand Down Expand Up @@ -159,35 +192,38 @@ const connectOptions = async (
SudtTokenInfoSubscribe,
TxDescriptionSubscribe,
],
logger: 'simple-console',
logger: new TypeormLogger(),
logging,
migrationsRun: true,
maxQueryExecutionTime: 30,
}
}

export const dataSource: Record<ConnectionName, DataSource | null> = {
light: null,
full: null,
}

const initConnectionWithType = async (genesisBlockHash: string, connectionName: ConnectionName) => {
// try to close connection, if not exist, will throw ConnectionNotFoundError when call getConnection()
try {
await getConnection(connectionName).close()
await dataSource[connectionName]?.destroy()
} catch (err) {
dataSource[connectionName] = null
// do nothing
}
const connectionOptions = await connectOptions(genesisBlockHash, connectionName)
const connectionOptions = getConnectionOptions(genesisBlockHash, connectionName)
dataSource[connectionName] = new DataSource(connectionOptions)

try {
await createConnection(connectionOptions)
await getConnection(connectionName).manager.query(`PRAGMA busy_timeout = 3000;`)
await getConnection(connectionName).manager.query(`PRAGMA temp_store = MEMORY;`)
await dataSource[connectionName]?.initialize()
await dataSource[connectionName]?.manager.query(`PRAGMA busy_timeout = 3000;`)
await dataSource[connectionName]?.manager.query(`PRAGMA temp_store = MEMORY;`)
} catch (err) {
logger.error(err.message)
}
}

export async function initConnection(genesisBlockHash: string) {
await initConnectionWithType(genesisBlockHash, 'full')
await initConnectionWithType(genesisBlockHash, 'light')
}

export function migrateDBFile(genesisBlockHash: string) {
const originDBFile = dbPath(genesisBlockHash, 'cell')
const currentFullDBFile = dbPath(genesisBlockHash, 'full')
Expand All @@ -203,4 +239,7 @@ export function migrateDBFile(genesisBlockHash: string) {
}
}

export default initConnection
export default async function initConnection(genesisBlockHash: string) {
await initConnectionWithType(genesisBlockHash, 'full')
await initConnectionWithType(genesisBlockHash, 'light')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default class AssetAccountSubscribe extends UserSettingSubscriber<AssetAc
async afterInsert(event: InsertEvent<AssetAccount>): Promise<AssetAccount | void> {
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository(AssetAccount)
if (repo && event.entity) {
const exist = await repo.findOne({ tokenID: event.entity.tokenID, blake160: event.entity.blake160 })
const exist = await repo.findOneBy({ tokenID: event.entity.tokenID, blake160: event.entity.blake160 })
if (exist) {
await repo.upsert(AssetAccount.fromModel(event.entity.toModel()), this.unionKeys)
await repo.update(exist.id, AssetAccount.fromModel(event.entity.toModel()))
} else {
await repo.save(event.entity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default class SudtTokenInfoSubscribe extends UserSettingSubscriber<SudtTo
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository(SudtTokenInfo)
if (repo && event.entity) {
let mergeEntity: SudtTokenInfo | undefined = undefined
const existEntity = await event.connection.getRepository(SudtTokenInfo).findOne(event.entity.tokenID)
const existEntity = await event.connection
.getRepository(SudtTokenInfo)
.findOneBy({ tokenID: event.entity.tokenID })
if (existEntity) {
mergeEntity = new SudtTokenInfo()
mergeEntity.tokenID = event.entity.tokenID || existEntity.tokenID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EntitySubscriberInterface, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm'
import { ConnectionName, getConnection, getCurrentConnectionName } from '../connection'
import { getConnection, getCurrentConnectionName } from '../connection'
import { ConnectionName } from '../ormconfig'

type Constructor<T> = new (...args: unknown[]) => T

Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/models/synced-block-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class SyncedBlockNumber {
if (!this.#blockNumberEntity) {
let blockNumber = await getConnection()
.getRepository(SyncInfoEntity)
.findOne({ name: SyncInfoEntity.CURRENT_BLOCK_NUMBER })
.findOneBy({ name: SyncInfoEntity.CURRENT_BLOCK_NUMBER })

if (!blockNumber) {
blockNumber = new SyncInfoEntity()
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/amend-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AmendTransactionEntity from '../database/chain/entities/amend-transaction

export default class AmendTransactionService {
static async save(hash: string, amendHash: string) {
const exist = await getConnection().getRepository(AmendTransactionEntity).findOne({
const exist = await getConnection().getRepository(AmendTransactionEntity).findOneBy({
hash,
amendHash,
})
Expand Down
12 changes: 3 additions & 9 deletions packages/neuron-wallet/src/services/asset-account-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { In } from 'typeorm'
import { In, IsNull } from 'typeorm'
import { getConnection } from '../database/chain/connection'
import BufferUtils from '../utils/buffer'
import OutputEntity from '../database/chain/entities/output'
Expand All @@ -22,19 +22,13 @@ export default class AssetAccountService {
private static async getACPCells(publicKeyHash: string, tokenId: string = 'CKBytes') {
const assetAccountInfo = new AssetAccountInfo()
const anyoneCanPayLockHash = assetAccountInfo.generateAnyoneCanPayScript(publicKeyHash).computeHash()
let typeHash = null
if (tokenId !== 'CKBytes') {
typeHash = assetAccountInfo.generateSudtScript(tokenId).computeHash()
}
const outputs = await getConnection()
.getRepository(OutputEntity)
.createQueryBuilder('output')
.where({
.findBy({
status: In([OutputStatus.Live, OutputStatus.Sent]),
lockHash: anyoneCanPayLockHash,
typeHash,
typeHash: tokenId !== 'CKBytes' ? assetAccountInfo.generateSudtScript(tokenId).computeHash() : IsNull(),
})
.getMany()

return outputs
}
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ export default class CellsService {
}

public static getLiveCell = async (outPoint: OutPoint): Promise<Cell | undefined> => {
const cellEntity: OutputEntity | undefined = await CellsService.getLiveCellEntity(outPoint)
const cellEntity = await CellsService.getLiveCellEntity(outPoint)

if (!cellEntity) {
return undefined
Expand All @@ -515,8 +515,8 @@ export default class CellsService {
return cellEntity.toModel()
}

private static getLiveCellEntity = async (outPoint: OutPoint): Promise<OutputEntity | undefined> => {
const cellEntity: OutputEntity | undefined = await getConnection().getRepository(OutputEntity).findOne({
private static getLiveCellEntity = async (outPoint: OutPoint): Promise<OutputEntity | null> => {
const cellEntity = await getConnection().getRepository(OutputEntity).findOneBy({
outPointTxHash: outPoint.txHash,
outPointIndex: outPoint.index,
status: 'live',
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/sudt-token-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class SudtTokenInfoService {
.execute()
}

static getSudtTokenInfo(typeArgs: string): Promise<SudtTokenInfoEntity | undefined> {
static getSudtTokenInfo(typeArgs: string): Promise<SudtTokenInfoEntity | null> {
return getConnection()
.getRepository(SudtTokenInfoEntity)
.createQueryBuilder('info')
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class SyncProgressService {
}

static async getOtherTypeSyncBlockNumber() {
const items = await getConnection().getRepository(SyncProgress).find({
const items = await getConnection().getRepository(SyncProgress).findBy({
addressType: SyncAddressType.Multisig,
})
return items.reduce<Record<string, number>>((pre, cur) => ({ ...pre, [cur.hash]: cur.localSavedBlockNumber }), {})
Expand Down
Loading

0 comments on commit b550565

Please sign in to comment.