-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Split full node and light client db file.
- Loading branch information
Showing
53 changed files
with
350 additions
and
97 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
packages/neuron-wallet/src/block-sync-renderer/sync/indexer-cache-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/neuron-wallet/src/block-sync-renderer/sync/tx-address-finder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/neuron-wallet/src/block-sync-renderer/tx-status-listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getConnection as originGetConnection } from 'typeorm' | ||
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) | ||
if (!connection) { | ||
throw new Error(`The connection ${connectionName} should be initialized before use`) | ||
} | ||
return connection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/neuron-wallet/src/database/chain/subscriber/address-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EventSubscriber } from 'typeorm' | ||
import UserSettingSubscriber from './user-setting-subscriber' | ||
import AddressDescription from '../entities/address-description' | ||
|
||
@EventSubscriber() | ||
export default class AddressSubscribe extends UserSettingSubscriber<AddressDescription> { | ||
listenTo() { | ||
return AddressDescription | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/neuron-wallet/src/database/chain/subscriber/asset-account-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { EventSubscriber, InsertEvent } from 'typeorm' | ||
import UserSettingSubscriber from './user-setting-subscriber' | ||
import AssetAccount from '../entities/asset-account' | ||
|
||
@EventSubscriber() | ||
export default class AssetAccountSubscribe extends UserSettingSubscriber<AssetAccount> { | ||
unionKeys: string[] = ['tokenID', 'blake160'] | ||
|
||
ignoreUpdateKeys: string[] = ['sudtTokenInfo'] | ||
|
||
listenTo() { | ||
return AssetAccount | ||
} | ||
|
||
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 }) | ||
if (exist) { | ||
await repo.upsert(AssetAccount.fromModel(event.entity.toModel()), this.unionKeys) | ||
} else { | ||
await repo.save(event.entity) | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/neuron-wallet/src/database/chain/subscriber/multisig-config-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EventSubscriber, InsertEvent } from 'typeorm' | ||
import UserSettingSubscriber from './user-setting-subscriber' | ||
import MultisigConfig from '../entities/multisig-config' | ||
|
||
@EventSubscriber() | ||
export default class MultisigConfigSubscribe extends UserSettingSubscriber<MultisigConfig> { | ||
ignoreUpdateKeys = ['lastestBlockNumber'] | ||
|
||
listenTo() { | ||
return MultisigConfig | ||
} | ||
|
||
async afterInsert(event: InsertEvent<MultisigConfig>): Promise<MultisigConfig | void> { | ||
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository(MultisigConfig) | ||
if (repo && event.entity) { | ||
await repo.upsert(event.entity.cloneIgnoreBlockNumber(), this.unionKeys) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/neuron-wallet/src/database/chain/subscriber/sudt-token-info-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { EventSubscriber, InsertEvent } from 'typeorm' | ||
import UserSettingSubscriber from './user-setting-subscriber' | ||
import SudtTokenInfo from '../entities/sudt-token-info' | ||
|
||
@EventSubscriber() | ||
export default class SudtTokenInfoSubscribe extends UserSettingSubscriber<SudtTokenInfo> { | ||
unionKeys: string[] = ['tokenID'] | ||
|
||
entityKeyName: string = 'tokenID' | ||
|
||
ignoreUpdateKeys: string[] = ['assetAccounts'] | ||
|
||
listenTo() { | ||
return SudtTokenInfo | ||
} | ||
|
||
async afterInsert(event: InsertEvent<SudtTokenInfo>): Promise<SudtTokenInfo | void> { | ||
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) | ||
if (existEntity) { | ||
mergeEntity = new SudtTokenInfo() | ||
mergeEntity.tokenID = event.entity.tokenID || existEntity.tokenID | ||
mergeEntity.symbol = event.entity.symbol || existEntity.symbol | ||
mergeEntity.tokenName = event.entity.tokenName || existEntity.tokenName | ||
mergeEntity.decimal = event.entity.decimal || existEntity.decimal | ||
} | ||
await repo.upsert(mergeEntity ?? event.entity, this.unionKeys) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/neuron-wallet/src/database/chain/subscriber/tx-description-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EventSubscriber } from 'typeorm' | ||
import UserSettingSubscriber from './user-setting-subscriber' | ||
import TxDescription from '../entities/tx-description' | ||
|
||
@EventSubscriber() | ||
export default class TxDescriptionSubscribe extends UserSettingSubscriber<TxDescription> { | ||
listenTo() { | ||
return TxDescription | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/neuron-wallet/src/database/chain/subscriber/user-setting-subscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { EntitySubscriberInterface, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm' | ||
import { ConnectionName, getConnection, getCurrentConnectionName } from '../connection' | ||
|
||
type Constructor<T> = new (...args: unknown[]) => T | ||
|
||
// Trigger relative updating through subscribing the changes from corresponding entities | ||
export default abstract class UserSettingSubscriber<Entity extends object> | ||
implements EntitySubscriberInterface<Entity> | ||
{ | ||
abstract listenTo(): string | Constructor<Entity> | ||
|
||
unionKeys: string[] = ['id'] | ||
|
||
entityKeyName: string = 'id' | ||
|
||
ignoreUpdateKeys: string[] = [] | ||
|
||
getNeedSyncConnection(connectionName: string) { | ||
const currentConnectionName = getCurrentConnectionName() | ||
if (connectionName === currentConnectionName) { | ||
const otherConnectionName: ConnectionName = currentConnectionName === 'full' ? 'light' : 'full' | ||
return getConnection(otherConnectionName) | ||
} | ||
return | ||
} | ||
|
||
async afterInsert(event: InsertEvent<Entity>): Promise<Entity | void> { | ||
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository<Entity>(this.listenTo()) | ||
if (repo && event.entity) { | ||
await repo.upsert(event.entity, this.unionKeys) | ||
} | ||
} | ||
|
||
async afterUpdate(event: UpdateEvent<Entity>): Promise<Entity | void> { | ||
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository<Entity>(this.listenTo()) | ||
const updatedColumns = event.updatedColumns.filter(v => !this.ignoreUpdateKeys.includes(v.propertyName)) | ||
if (repo && event.entity && event.databaseEntity && updatedColumns.length) { | ||
const updateEntity = updatedColumns.reduce( | ||
(pre, cur) => ({ | ||
...pre, | ||
[cur.propertyName]: event.entity![cur.propertyName], | ||
}), | ||
{} | ||
) | ||
const key = (event.databaseEntity as any)[this.entityKeyName] | ||
if (key !== undefined && key !== null) { | ||
await repo.update(key, updateEntity) | ||
} | ||
} | ||
} | ||
|
||
async afterRemove(event: RemoveEvent<Entity>): Promise<Entity | void> { | ||
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository<Entity>(this.listenTo()) | ||
if (repo && event.databaseEntity) { | ||
await repo.remove(event.databaseEntity) | ||
} | ||
} | ||
} |
Oops, something went wrong.