Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bz888 committed Apr 17, 2024
1 parent 491989d commit 72d0947
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
63 changes: 25 additions & 38 deletions packages/node/src/indexer/api.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ describe('ApiService', () => {
const prepareApiService = async (
endpoint: string,
chainId: string,
kyve: boolean,
fileCacheDir?: string,
) => {
const module = await Test.createTestingModule({
Expand All @@ -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(
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export class ApiService
);

if (this.nodeConfig.kyveEndpoint) {
console.log(this.project.fileCacheDir);
try {
this.kyveApi = await KyveApi.create(
network.chainId,
Expand Down
23 changes: 18 additions & 5 deletions packages/node/src/utils/kyve/kyve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions packages/node/src/utils/kyve/kyve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 72d0947

Please sign in to comment.