-
Notifications
You must be signed in to change notification settings - Fork 521
fix: purge delisted networks from aggregate token cache and refresh stale wallet config OK-58112 #12533
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
Merged
Merged
fix: purge delisted networks from aggregate token cache and refresh stale wallet config OK-58112 #12533
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3676e84
fix: purge delisted networks from aggregate token cache and refresh s…
weatherstar e11a7f5
Merge branch 'x' into fix/aggregate-token-stale-network-cache
weatherstar 64c901e
fix: drop empty aggregate groups and harden background wallet-config …
weatherstar eb882b0
fix: degrade stale aggregate token groups with <=1 surviving member
weatherstar 24c58c6
fix: filter aggregate token members by listed preset networks in Toke…
weatherstar 70b4dd3
Merge remote-tracking branch 'origin/x' into fix/aggregate-token-stal…
weatherstar bcb2374
fix: address PR review feedback
weatherstar c2c9170
Merge branch 'x' into fix/aggregate-token-stale-network-cache
weatherstar 7023148
Merge branch 'x' into fix/aggregate-token-stale-network-cache
sidmorizon 73c49d1
Merge branch 'x' into fix/aggregate-token-stale-network-cache
weatherstar 02d697d
Merge branch 'x' into fix/aggregate-token-stale-network-cache
weatherstar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
packages/kit-bg/src/services/ServiceSetting.syncWalletConfigIfNeeded.test.ts
This file contains hidden or 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,108 @@ | ||
| /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return */ | ||
|
|
||
| import platformEnv from '@onekeyhq/shared/src/platformEnv'; | ||
| import timerUtils from '@onekeyhq/shared/src/utils/timerUtils'; | ||
|
|
||
| import ServiceSetting from './ServiceSetting'; | ||
|
|
||
| import type { ISimpleDBAggregateToken } from '../dbs/simple/entity/SimpleDbEntityAggregateToken'; | ||
|
|
||
| jest.mock('@onekeyhq/shared/src/background/backgroundDecorators', () => ({ | ||
| backgroundClass: () => (target: any) => target, | ||
| backgroundMethod: () => (_t: any, _k: string, desc: any) => desc, | ||
| backgroundMethodForDev: () => (_t: any, _k: string, desc: any) => desc, | ||
| toastIfError: () => (_t: any, _k: string, desc: any) => desc, | ||
| checkDevOnlyPassword: jest.fn(), | ||
| })); | ||
|
|
||
| const currentAppVersion = platformEnv.version ?? ''; | ||
| const oneDayMs = timerUtils.getTimeDurationMs({ day: 1 }); | ||
|
|
||
| function buildService(rawData: ISimpleDBAggregateToken | null) { | ||
| const service = new ServiceSetting({ | ||
| backgroundApi: { | ||
| simpleDb: { | ||
| aggregateToken: { | ||
| getRawData: jest.fn(async () => rawData), | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| const syncSpy = jest | ||
| .spyOn(service, 'syncWalletConfig') | ||
| .mockResolvedValue({} as any); | ||
| return { service, syncSpy }; | ||
| } | ||
|
|
||
| describe('ServiceSetting.syncWalletConfigIfNeeded', () => { | ||
| it('syncs when the config has never been synced', async () => { | ||
| const { service, syncSpy } = buildService(null); | ||
|
|
||
| await service.syncWalletConfigIfNeeded(); | ||
|
|
||
| expect(syncSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('syncs when the cached config has no sync meta', async () => { | ||
| const { service, syncSpy } = buildService({ | ||
| aggregateTokenConfigMap: {}, | ||
| }); | ||
|
|
||
| await service.syncWalletConfigIfNeeded(); | ||
|
|
||
| expect(syncSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('syncs when the app version changed since the last sync', async () => { | ||
| const { service, syncSpy } = buildService({ | ||
| aggregateTokenConfigMap: {}, | ||
| configSyncMeta: { | ||
| appVersion: 'stale-app-version', | ||
| syncedAt: Date.now(), | ||
| }, | ||
| }); | ||
|
|
||
| await service.syncWalletConfigIfNeeded(); | ||
|
|
||
| expect(syncSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('syncs when the cached config is older than the TTL', async () => { | ||
| const { service, syncSpy } = buildService({ | ||
| aggregateTokenConfigMap: {}, | ||
| configSyncMeta: { | ||
| appVersion: currentAppVersion, | ||
| syncedAt: Date.now() - oneDayMs - 1, | ||
| }, | ||
| }); | ||
|
|
||
| await service.syncWalletConfigIfNeeded(); | ||
|
|
||
| expect(syncSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('skips syncing when the cached config is fresh', async () => { | ||
| const { service, syncSpy } = buildService({ | ||
| aggregateTokenConfigMap: {}, | ||
| configSyncMeta: { | ||
| appVersion: currentAppVersion, | ||
| syncedAt: Date.now(), | ||
| }, | ||
| }); | ||
|
|
||
| await service.syncWalletConfigIfNeeded(); | ||
|
|
||
| expect(syncSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('dedupes concurrent calls into a single sync', async () => { | ||
| const { service, syncSpy } = buildService(null); | ||
|
|
||
| await Promise.all([ | ||
| service.syncWalletConfigIfNeeded(), | ||
| service.syncWalletConfigIfNeeded(), | ||
| ]); | ||
|
|
||
| expect(syncSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or 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
133 changes: 133 additions & 0 deletions
133
packages/kit-bg/src/services/ServiceToken.getAllAggregateTokenInfo.test.ts
This file contains hidden or 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,133 @@ | ||
| /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return */ | ||
|
|
||
| import ServiceToken from './ServiceToken'; | ||
|
|
||
| import type { ISimpleDBAggregateToken } from '../dbs/simple/entity/SimpleDbEntityAggregateToken'; | ||
|
|
||
| jest.mock('@onekeyhq/shared/src/background/backgroundDecorators', () => ({ | ||
| backgroundClass: () => (target: any) => target, | ||
| backgroundMethod: () => (_t: any, _k: string, desc: any) => desc, | ||
| backgroundMethodForDev: () => (_t: any, _k: string, desc: any) => desc, | ||
| toastIfError: () => (_t: any, _k: string, desc: any) => desc, | ||
| checkDevOnlyPassword: jest.fn(), | ||
| })); | ||
|
|
||
| function buildService(rawData: ISimpleDBAggregateToken | null) { | ||
| return new ServiceToken({ | ||
| backgroundApi: { | ||
| simpleDb: { | ||
| aggregateToken: { | ||
| getRawData: jest.fn(async () => rawData), | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function buildToken(networkId: string) { | ||
| return { | ||
| $key: `sameSymbol_${networkId}`, | ||
| networkId, | ||
| name: 'Tether', | ||
| symbol: 'USDT', | ||
| address: '0xusdt', | ||
| decimals: 6, | ||
| isNative: false, | ||
| }; | ||
| } | ||
|
|
||
| // Mirrors the aggregate descriptor shape built by | ||
| // ServiceSetting.syncWalletConfig from the map keys. | ||
| function buildAggregateToken($key: string) { | ||
| return { | ||
| $key, | ||
| isAggregateToken: true, | ||
| name: 'Tether', | ||
| symbol: 'USDT', | ||
| commonSymbol: 'USDT', | ||
| networkId: '', | ||
| address: $key, | ||
| decimals: 0, | ||
| isNative: false, | ||
| }; | ||
| } | ||
|
|
||
| describe('ServiceToken.getAllAggregateTokenInfo', () => { | ||
| it('drops tokens on networks missing from the bundled preset list', async () => { | ||
| // evm--810180 (zkLink Nova) was removed from presetNetworks and | ||
| // evm--321 (KCC) never existed there; both may still linger in an | ||
| // aggregate-token cache persisted by an older app version. | ||
| const service = buildService({ | ||
| allAggregateTokenMap: { | ||
| sameSymbol_USDT: { | ||
| tokens: [ | ||
| buildToken('evm--1'), | ||
| buildToken('evm--810180'), | ||
| buildToken('evm--321'), | ||
| ], | ||
| }, | ||
| }, | ||
| allAggregateTokens: [], | ||
| }); | ||
|
|
||
| const { allAggregateTokenMap } = await service.getAllAggregateTokenInfo(); | ||
|
|
||
| expect( | ||
| allAggregateTokenMap.sameSymbol_USDT.tokens.map((t) => t.networkId), | ||
| ).toEqual(['evm--1']); | ||
| }); | ||
|
|
||
| it('keeps tokens on bundled listed networks intact', async () => { | ||
| const service = buildService({ | ||
| allAggregateTokenMap: { | ||
| sameSymbol_USDT: { | ||
| tokens: [buildToken('evm--1'), buildToken('tron--0x2b6653dc')], | ||
| }, | ||
| }, | ||
| allAggregateTokens: [], | ||
| }); | ||
|
|
||
| const { allAggregateTokenMap } = await service.getAllAggregateTokenInfo(); | ||
|
|
||
| expect( | ||
| allAggregateTokenMap.sameSymbol_USDT.tokens.map((t) => t.networkId), | ||
| ).toEqual(['evm--1', 'tron--0x2b6653dc']); | ||
| }); | ||
|
|
||
| it('drops aggregate groups whose networks were all delisted', async () => { | ||
| // evm--100 (Gnosis) was removed from presetNetworks, so the whole | ||
| // XDAI aggregate group becomes empty after filtering and must be | ||
| // dropped from both the map and the flat descriptor list. | ||
| const service = buildService({ | ||
| allAggregateTokenMap: { | ||
| sameSymbol_USDT: { | ||
| tokens: [buildToken('evm--1'), buildToken('evm--810180')], | ||
| }, | ||
| sameSymbol_XDAI: { | ||
| tokens: [buildToken('evm--100')], | ||
| }, | ||
| }, | ||
| allAggregateTokens: [ | ||
| buildAggregateToken('sameSymbol_USDT'), | ||
| buildAggregateToken('sameSymbol_XDAI'), | ||
| ], | ||
| }); | ||
|
|
||
| const { allAggregateTokenMap, allAggregateTokens } = | ||
| await service.getAllAggregateTokenInfo(); | ||
|
|
||
| expect(Object.keys(allAggregateTokenMap)).toEqual(['sameSymbol_USDT']); | ||
| expect(allAggregateTokens.map((t) => t.$key)).toEqual(['sameSymbol_USDT']); | ||
| }); | ||
|
|
||
| it('returns empty structures when nothing is cached', async () => { | ||
| const service = buildService(null); | ||
|
|
||
| const result = await service.getAllAggregateTokenInfo(); | ||
|
|
||
| expect(result).toEqual({ | ||
| allAggregateTokenMap: {}, | ||
| allAggregateTokens: [], | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.