From 72d09477a5491c894501e86190d63f12b63ee83f Mon Sep 17 00:00:00 2001 From: bz888 Date: Wed, 17 Apr 2024 16:31:39 +0800 Subject: [PATCH] update tests --- packages/node/src/indexer/api.service.test.ts | 63 ++++++++----------- packages/node/src/indexer/api.service.ts | 1 - packages/node/src/utils/kyve/kyve.spec.ts | 23 +++++-- packages/node/src/utils/kyve/kyve.ts | 4 +- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/packages/node/src/indexer/api.service.test.ts b/packages/node/src/indexer/api.service.test.ts index 210ab93a6..069bf9ccb 100644 --- a/packages/node/src/indexer/api.service.test.ts +++ b/packages/node/src/indexer/api.service.test.ts @@ -56,7 +56,6 @@ describe('ApiService', () => { const prepareApiService = async ( endpoint: string, chainId: string, - kyve: boolean, fileCacheDir?: string, ) => { const module = await Test.createTestingModule({ @@ -80,55 +79,41 @@ describe('ApiService', () => { app = module.createNestApplication(); await app.init(); apiService = app.get(ApiService); - if (kyve) { - (apiService as any).nodeConfig._config.kyveEndpoint = - 'https://api-us-1.kyve.network'; - (apiService as any).nodeConfig._config.kyveStorageUrl = - 'https://arweave.net'; - } + (apiService as any).nodeConfig._config.kyveEndpoint = + 'https://api-us-1.kyve.network'; + (apiService as any).nodeConfig._config.kyveStorageUrl = + 'https://arweave.net'; await apiService.init(); }; - describe('KYVE Api service', () => { - beforeAll(async () => { - const ENDPOINT = 'https://rpc.mainnet.archway.io:443'; - const CHAINID = 'archway-1'; + const ENDPOINT = 'https://rpc-juno.itastakers.com/'; + const CHAINID = 'juno-1'; + describe('RPC api service', () => { + beforeAll(async () => { tmpPath = await makeTempDir(); - await prepareApiService(ENDPOINT, CHAINID, true, tmpPath); }); + it('Falls back on rpc if kyve fails', async () => { + const endpoint = 'https://rpc.mainnet.archway.io:443'; + const chainId = 'archway-1'; - it('Able to fetch with cached promises and remove cached bundle files', async () => { - const rpcFetchSpy = jest.spyOn(apiService as any, 'retryFetch'); + await prepareApiService(endpoint, chainId, tmpPath); - const heights_1 = [150, 300, 1, 301, 450, 550]; - const heights_2 = [498, 600, 801, 1100]; - const blockArr = await Promise.all([ - apiService.fetchBlocks(heights_1), - apiService.fetchBlocks(heights_2), - ]); + jest + .spyOn((apiService as any).kyveApi, 'retrieveBundleData') + .mockRejectedValueOnce( + 'Error: Client network socket disconnected before secure TLS connection was established', + ); - blockArr.forEach((blockContent) => { - blockContent.forEach((b) => { - expect(b.block instanceof LazyBlockContent).toBe(true); - }); - }); + const rpcFetchSpy = jest.spyOn(apiService as any, 'retryFetch'); - const files = await fs.promises.readdir(tmpPath); + await apiService.fetchBlocks([1]); - expect(rpcFetchSpy).toHaveBeenCalledTimes(0); - expect(files).not.toContain('bundle_0.json'); - expect(files).not.toContain('bundle_1.json'); + expect(rpcFetchSpy).toHaveBeenCalledTimes(1); }); - }); - describe.skip('RPC api service', () => { - beforeAll(async () => { - const ENDPOINT = 'https://rpc-juno.itastakers.com/'; - const CHAINID = 'juno-1'; + it.skip('query block info', async () => { + await prepareApiService(ENDPOINT, CHAINID, tmpPath); - await prepareApiService(ENDPOINT, CHAINID, false); - }); - it('query block info', async () => { const api = apiService.api; const blockInfo = await api.blockInfo(TEST_BLOCKNUMBER); const doc: any = loadFromJsonOrYaml( @@ -150,7 +135,9 @@ describe('ApiService', () => { expect(blockInfo).toMatchObject(realBlockInfo); }); - it('query tx info by height', async () => { + it.skip('query tx info by height', async () => { + await prepareApiService(ENDPOINT, CHAINID, tmpPath); + const api = apiService.api; const txInfos = await api.txInfoByHeight(TEST_BLOCKNUMBER); expect(txInfos.length).toEqual(4); diff --git a/packages/node/src/indexer/api.service.ts b/packages/node/src/indexer/api.service.ts index bc705219e..102c01e0a 100644 --- a/packages/node/src/indexer/api.service.ts +++ b/packages/node/src/indexer/api.service.ts @@ -111,7 +111,6 @@ export class ApiService ); if (this.nodeConfig.kyveEndpoint) { - console.log(this.project.fileCacheDir); try { this.kyveApi = await KyveApi.create( network.chainId, diff --git a/packages/node/src/utils/kyve/kyve.spec.ts b/packages/node/src/utils/kyve/kyve.spec.ts index b8ea1f05f..df06ea8d8 100644 --- a/packages/node/src/utils/kyve/kyve.spec.ts +++ b/packages/node/src/utils/kyve/kyve.spec.ts @@ -258,11 +258,24 @@ describe('KyveApi', () => { expect(permissions).toBe('444'); } }); - it('retrieve and unzip storage data', async () => { - const bundle = await (kyveApi as any).getBundleById(8); - (kyveApi as any).cachedBundleDetails.push(bundle); - const block = await kyveApi.getBlockByHeight(1338); - expect(block.length).toBe(2); + it('able to fetch/write/read blocks using Kyve api', async () => { + const heights_1 = [150, 300, 1, 301, 450, 550]; + const heights_2 = [498, 600, 801, 1100]; + const blockArr = await Promise.all([ + kyveApi.fetchBlocksBatches(registry, heights_1, 300), + kyveApi.fetchBlocksBatches(registry, heights_2, 300), + ]); + + blockArr.forEach((blockContent) => { + blockContent.forEach((b) => { + expect(b.block instanceof LazyBlockContent).toBe(true); + }); + }); + + const files = await fs.promises.readdir(tmpPath); + + expect(files).not.toContain('bundle_0.json'); + expect(files).not.toContain('bundle_1.json'); }); it('Should increment bundleId when height exceeds cache', async () => { const bundle = await (kyveApi as any).getBundleById(0); diff --git a/packages/node/src/utils/kyve/kyve.ts b/packages/node/src/utils/kyve/kyve.ts index 03a7edd33..f66427ff5 100644 --- a/packages/node/src/utils/kyve/kyve.ts +++ b/packages/node/src/utils/kyve/kyve.ts @@ -67,9 +67,7 @@ export class KyveApi { const poolId = await KyveApi.fetchPoolId(chainId, lcdClient); - const kyve = new KyveApi(storageUrl, tmpCacheDir, poolId, lcdClient); - - return kyve; + return new KyveApi(storageUrl, tmpCacheDir, poolId, lcdClient); } private static async fetchPoolId(