Skip to content

Commit

Permalink
interactions loader - previous sk instead of offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Dec 21, 2023
1 parent fdb7f29 commit 3399026
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/unit/gateway-interactions.loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const sorter = new LexicographicalInteractionsSorter(Arweave.init({}));
const contractId = 'SJ3l7474UHh3Dw6dWVT1bzsJ-8JvOewtGoDdOecWIZo';
const fromBlockHeight = sorter.generateLastSortKey(600000);
const toBlockHeight = sorter.generateLastSortKey(655393);
const baseUrl = `http://baseUrl/gateway/v2/interactions-sort-key?contractId=SJ3l7474UHh3Dw6dWVT1bzsJ-8JvOewtGoDdOecWIZo&from=000000600000%2C9999999999999%2Czzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz&to=000000655393%2C9999999999999%2Czzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz`;
const baseUrl = `http://baseUrl/gateway/v3/interactions-sort-key?contractId=SJ3l7474UHh3Dw6dWVT1bzsJ-8JvOewtGoDdOecWIZo&from=000000600000%2C9999999999999%2Czzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz&to=000000655393%2C9999999999999%2Czzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz`;
const fetchMock = jest
.spyOn(global, 'fetch')
.mockImplementation(
Expand All @@ -97,7 +97,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with correct params', async () => {
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
});
it('should be called accordingly to the amount of pages', async () => {
const fetchMock = jest.spyOn(global, 'fetch').mockImplementation(
Expand All @@ -110,7 +110,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
);
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
/*expect(fetchMock).toBeCalledWith(`${baseUrl}&page=2&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=3&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=4&fromSdk=true`);
Expand All @@ -120,12 +120,12 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with confirmationStatus set to "confirmed"', async () => {
const loader = getLoader({ confirmed: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true&confirmationStatus=confirmed`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=confirmed`);
});
it('should be called with confirmationStatus set to "not_corrupted"', async () => {
const loader = getLoader({ notCorrupted: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true&confirmationStatus=not_corrupted`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=not_corrupted`);
});
it('should throw an error in case of timeout', async () => {
jest.spyOn(global, 'fetch').mockImplementation(() => Promise.reject({ status: 504, ok: false }));
Expand Down
8 changes: 5 additions & 3 deletions src/core/modules/impl/WarpGatewayInteractionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
signal?: AbortSignal
): Promise<GQLNodeInterface[]> {
this.logger.debug('Loading interactions: for ', { contractId, fromSortKey, toSortKey });
const originalFromSortKey = fromSortKey;

const interactions: GQLNodeInterface[] = [];
let page = 0;
Expand All @@ -88,16 +89,16 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
throw new AbortError(`Abort signal in ${WarpGatewayInteractionsLoader.name}`);
}

const url = `${baseUrl}/gateway/v2/interactions-sort-key`;
const url = `${baseUrl}/gateway/v3/interactions-sort-key`;

page++;
const response = await getJsonResponse<InteractionsResult>(
fetch(
`${url}?${new URLSearchParams({
contractId: contractId,
...(this._warp.whoAmI ? { client: this._warp.whoAmI } : ''),
...(fromSortKey ? { from: fromSortKey } : ''),
...(toSortKey ? { to: toSortKey } : ''),
page: (++page).toString(),
fromSdk: 'true',
...(this.confirmationStatus && this.confirmationStatus.confirmed
? { confirmationStatus: 'confirmed' }
Expand All @@ -114,12 +115,13 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
interactions.push(...response.interactions);
limit = response.paging.limit;
items = response.paging.items;
fromSortKey = interactions[interactions.length - 1]?.sortKey;

this.logger.debug(`Loaded interactions length: ${interactions.length}, from: ${fromSortKey}, to: ${toSortKey}`);
} while (items == limit && page < pagesPerBatch); // note: items < limit means that we're on the last page

this.logger.debug('All loaded interactions:', {
from: fromSortKey,
from: originalFromSortKey,
to: toSortKey,
loaded: interactions.length,
time: benchmarkTotalTime.elapsed()
Expand Down

0 comments on commit 3399026

Please sign in to comment.