Skip to content

Commit c25ceb9

Browse files
authored
PPOM instance to stay when user switch the network (#101)
1 parent 7d91def commit c25ceb9

File tree

2 files changed

+15
-181
lines changed

2 files changed

+15
-181
lines changed

src/ppom-controller.test.ts

Lines changed: 9 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
PPOMClass,
32
VERSION_INFO,
43
buildDummyResponse,
54
buildFetchSpy,
@@ -369,59 +368,6 @@ describe('PPOMController', () => {
369368
'Aborting validation as not all files could not be downloaded for the network with chainId: 0x1',
370369
);
371370
});
372-
373-
it('should reset PPOM when network is switched', async () => {
374-
buildFetchSpy();
375-
let callBack: any;
376-
const freeMock = jest.fn();
377-
ppomController = buildPPOMController({
378-
storageBackend: buildStorageBackend({
379-
read: async (): Promise<any> => {
380-
throw new Error('not found');
381-
},
382-
}),
383-
onNetworkChange: (func: any) => {
384-
callBack = func;
385-
},
386-
chainId: '0x2',
387-
ppomProvider: {
388-
ppomInit: () => undefined,
389-
PPOM: new PPOMClass(undefined, freeMock),
390-
},
391-
});
392-
callBack({ providerConfig: { chainId: '0x1' } });
393-
await flushPromises();
394-
jest.runOnlyPendingTimers();
395-
await flushPromises();
396-
callBack({ providerConfig: { chainId: '0x2' } });
397-
await flushPromises();
398-
expect(freeMock).toHaveBeenCalledTimes(1);
399-
});
400-
401-
it('should not throw error if PPOM init on network changs fails', async () => {
402-
buildFetchSpy();
403-
let callBack: any;
404-
const newMock = jest.fn().mockImplementation(() => {
405-
throw Error('test');
406-
});
407-
ppomController = buildPPOMController({
408-
onNetworkChange: (func: any) => {
409-
callBack = func;
410-
},
411-
chainId: '0x1',
412-
ppomProvider: {
413-
ppomInit: () => undefined,
414-
PPOM: new PPOMClass(newMock),
415-
},
416-
});
417-
callBack({ providerConfig: { chainId: '0x2' } });
418-
await flushPromises();
419-
jest.runOnlyPendingTimers();
420-
await flushPromises();
421-
callBack({ providerConfig: { chainId: '0x1' } });
422-
await flushPromises();
423-
expect(newMock).toHaveBeenCalledTimes(1);
424-
});
425371
});
426372

427373
describe('updatePPOM', () => {
@@ -479,25 +425,6 @@ describe('PPOMController', () => {
479425
expect(spy).toHaveBeenCalledTimes(9);
480426
});
481427

482-
it('should set dataFetched to true for supported chainIds in chainStatus', async () => {
483-
buildFetchSpy();
484-
let callBack: any;
485-
ppomController = buildPPOMController({
486-
onNetworkChange: (func: any) => {
487-
callBack = func;
488-
},
489-
});
490-
jest.runOnlyPendingTimers();
491-
callBack({ providerConfig: { chainId: '0x2' } });
492-
await ppomController.updatePPOM();
493-
jest.runOnlyPendingTimers();
494-
await flushPromises();
495-
const chainIdData1 = ppomController.state.chainStatus['0x1'];
496-
const chainIdData2 = ppomController.state.chainStatus['0x2'];
497-
expect(chainIdData1.dataFetched).toBe(true);
498-
expect(chainIdData2.dataFetched).toBe(false);
499-
});
500-
501428
it('should get files for only supported chains in chainStatus', async () => {
502429
const spy = buildFetchSpy({
503430
status: 200,
@@ -566,20 +493,21 @@ describe('PPOMController', () => {
566493
buildFetchSpy();
567494
let callBack: any;
568495
ppomController = buildPPOMController({
496+
chainId: '0x2',
569497
onNetworkChange: (func: any) => {
570498
callBack = func;
571499
},
572500
});
573501
jest.runOnlyPendingTimers();
574502
await flushPromises();
575-
const chainIdData1 = ppomController.state.chainStatus['0x1'];
503+
const chainIdData1 = ppomController.state.chainStatus['0x2'];
576504
expect(chainIdData1).toBeDefined();
577-
callBack({ providerConfig: { chainId: '0x2' } });
578505
callBack({ providerConfig: { chainId: '0x3' } });
506+
callBack({ providerConfig: { chainId: '0x4' } });
579507
jest.advanceTimersByTime(NETWORK_CACHE_DURATION);
580508
jest.runOnlyPendingTimers();
581509
await flushPromises();
582-
const chainIdData2 = ppomController.state.chainStatus['0x1'];
510+
const chainIdData2 = ppomController.state.chainStatus['0x2'];
583511
expect(chainIdData2).toBeUndefined();
584512
});
585513

@@ -599,48 +527,6 @@ describe('PPOMController', () => {
599527
await flushPromises();
600528
expect(spy).toHaveBeenCalledTimes(6);
601529
});
602-
603-
it('should re-init ppom when new set of files are fetched', async () => {
604-
buildFetchSpy();
605-
const newMock = jest.fn();
606-
ppomController = buildPPOMController({
607-
ppomProvider: {
608-
ppomInit: () => undefined,
609-
PPOM: new PPOMClass(newMock),
610-
},
611-
fileFetchScheduleDuration: 0,
612-
});
613-
await flushPromises();
614-
jest.advanceTimersByTime(REFRESH_TIME_INTERVAL);
615-
await flushPromises();
616-
expect(newMock).toHaveBeenCalledTimes(1);
617-
618-
buildFetchSpy(
619-
{
620-
status: 200,
621-
json: () => [
622-
...VERSION_INFO,
623-
{
624-
name: 'data2',
625-
chainId: '0x1',
626-
version: '1.0.3',
627-
checksum:
628-
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
629-
filePath: 'data2',
630-
},
631-
],
632-
},
633-
undefined,
634-
2,
635-
);
636-
await flushPromises();
637-
638-
jest.advanceTimersByTime(REFRESH_TIME_INTERVAL);
639-
await flushPromises();
640-
jest.advanceTimersByTime(1);
641-
await flushPromises();
642-
expect(newMock).toHaveBeenCalledTimes(3);
643-
});
644530
});
645531

646532
describe('onNetworkChange', () => {
@@ -687,6 +573,7 @@ describe('PPOMController', () => {
687573
buildFetchSpy();
688574
let callBack: any;
689575
ppomController = buildPPOMController({
576+
chainId: '0x2',
690577
onNetworkChange: (func: any) => {
691578
callBack = func;
692579
},
@@ -695,50 +582,25 @@ describe('PPOMController', () => {
695582
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(1);
696583

697584
jest.useFakeTimers().setSystemTime(new Date('2023-01-02'));
698-
callBack({ providerConfig: { chainId: '0x2' } });
585+
callBack({ providerConfig: { chainId: '0x3' } });
699586

700587
jest.useFakeTimers().setSystemTime(new Date('2023-01-05'));
701588
callBack({ providerConfig: { chainId: '0x5' } });
702589

703590
jest.useFakeTimers().setSystemTime(new Date('2023-01-03'));
704-
callBack({ providerConfig: { chainId: '0x3' } });
591+
callBack({ providerConfig: { chainId: '0x4' } });
705592

706593
jest.useFakeTimers().setSystemTime(new Date('2023-01-04'));
707-
callBack({ providerConfig: { chainId: '0x4' } });
594+
callBack({ providerConfig: { chainId: '0x6' } });
708595

709596
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);
710597

711598
jest.useFakeTimers().setSystemTime(new Date('2023-01-06'));
712-
callBack({ providerConfig: { chainId: '0x6' } });
599+
callBack({ providerConfig: { chainId: '0x7' } });
713600
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);
714601

715602
expect(ppomController.state.chainStatus['0x1']).toBeUndefined();
716603
});
717-
718-
it('should reset PPOM when network is changed', async () => {
719-
buildFetchSpy();
720-
const freeMock = jest.fn().mockReturnValue('abc');
721-
let callBack: any;
722-
ppomController = buildPPOMController({
723-
onNetworkChange: (func: any) => {
724-
callBack = func;
725-
},
726-
chainId: '0x1',
727-
ppomProvider: {
728-
ppomInit: () => undefined,
729-
PPOM: new PPOMClass(undefined, freeMock),
730-
},
731-
});
732-
733-
await flushPromises();
734-
jest.advanceTimersByTime(REFRESH_TIME_INTERVAL);
735-
await flushPromises();
736-
737-
expect(freeMock).toHaveBeenCalledTimes(0);
738-
callBack({ providerConfig: { chainId: '0x2' } });
739-
callBack({ providerConfig: { chainId: '0x1' } });
740-
expect(freeMock).toHaveBeenCalledTimes(1);
741-
});
742604
});
743605

744606
describe('onPreferencesChange', () => {

src/ppom-controller.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,6 @@ export class PPOMController extends BaseControllerV2<
327327
}
328328
// delete chains more than a week old
329329
this.#deleteOldChainIds();
330-
// If none of the networks in chainStatus are supported we stop fetching data files
331-
// and inactivate functionality by reseting PPOM
332-
if (!this.#chainStatusIncludeSupportedNetworks()) {
333-
this.#resetToInactiveState();
334-
return;
335-
}
336-
337330
await this.#updatePPOM();
338331
}
339332

@@ -398,17 +391,6 @@ export class PPOMController extends BaseControllerV2<
398391
}
399392
}
400393

401-
/*
402-
* The function check if ethereum mainnet is in list of recent networks
403-
*/
404-
#chainStatusIncludeSupportedNetworks() {
405-
const networkIsSupported = this.#networkIsSupported.bind(this);
406-
return (
407-
this.state?.chainStatus &&
408-
Object.keys(this.state?.chainStatus)?.some(networkIsSupported)
409-
);
410-
}
411-
412394
/*
413395
* The function check if ethereum chainId is supported for validation
414396
* Currently it checks for only Ethereum Mainnet but it will include more networks in future.
@@ -467,7 +449,6 @@ export class PPOMController extends BaseControllerV2<
467449
const id = addHexPrefix(networkControllerState.providerConfig.chainId);
468450
let chainStatus = { ...this.state.chainStatus };
469451
const existingNetworkObject = chainStatus[id];
470-
const oldChainId = this.#chainId;
471452
this.#chainId = id;
472453
chainStatus = {
473454
...chainStatus,
@@ -483,15 +464,6 @@ export class PPOMController extends BaseControllerV2<
483464
});
484465
this.#deleteOldChainIds();
485466
this.#checkScheduleFileDownloadForAllChains();
486-
if (oldChainId !== id) {
487-
if (chainStatus[id]?.dataFetched) {
488-
this.#reinitPPOM().catch(() => {
489-
console.error('Error in re-init of PPOM');
490-
});
491-
} else {
492-
this.#resetPPOM();
493-
}
494-
}
495467
}
496468

497469
/*
@@ -765,7 +737,9 @@ export class PPOMController extends BaseControllerV2<
765737
}
766738
const currentTimestamp = new Date().getTime();
767739

768-
const chainIds = Object.keys(this.state.chainStatus);
740+
const chainIds = Object.keys(this.state.chainStatus).filter(
741+
(id) => id !== ETHEREUM_CHAIN_ID,
742+
);
769743
const oldChaninIds: any[] = chainIds.filter(
770744
(chainId) =>
771745
(this.state.chainStatus[chainId] as any).lastVisited <
@@ -833,7 +807,8 @@ export class PPOMController extends BaseControllerV2<
833807
if (isLastFileOfNetwork) {
834808
// if this was last file for the chainId set dataFetched for chainId to true
835809
await this.#setChainIdDataFetched(fileVersionInfo.chainId);
836-
if (fileVersionInfo.chainId === this.#chainId) {
810+
// if (fileVersionInfo.chainId === this.#chainId) {
811+
if (fileVersionInfo.chainId === ETHEREUM_CHAIN_ID) {
837812
await this.#reinitPPOM();
838813
}
839814
}
@@ -1055,10 +1030,7 @@ export class PPOMController extends BaseControllerV2<
10551030
* starts the scheduled periodic task to fetch files for all the chains.
10561031
*/
10571032
#checkScheduleFileDownloadForAllChains(): void {
1058-
if (
1059-
this.#securityAlertsEnabled &&
1060-
this.#chainStatusIncludeSupportedNetworks()
1061-
) {
1033+
if (this.#securityAlertsEnabled) {
10621034
if (!this.#refreshDataInterval) {
10631035
this.#onDataUpdateDuration();
10641036
this.#refreshDataInterval = setInterval(

0 commit comments

Comments
 (0)