Skip to content

Commit 3dff5a7

Browse files
fix: pagination returns less than the page size when decryption fails (#1547)
1 parent 59d5257 commit 3dff5a7

File tree

19 files changed

+676
-364
lines changed

19 files changed

+676
-364
lines changed

packages/data-access/src/combined-data-access.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ export abstract class CombinedDataAccess implements DataAccessTypes.IDataAccess
3131
async getChannelsByTopic(
3232
topic: string,
3333
updatedBetween?: DataAccessTypes.ITimestampBoundaries | undefined,
34-
page?: number,
35-
pageSize?: number,
3634
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
37-
return await this.reader.getChannelsByTopic(topic, updatedBetween, page, pageSize);
35+
return await this.reader.getChannelsByTopic(topic, updatedBetween);
3836
}
3937
async getChannelsByMultipleTopics(
4038
topics: string[],
4139
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
42-
page?: number | undefined,
43-
pageSize?: number | undefined,
4440
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
45-
return await this.reader.getChannelsByMultipleTopics(topics, updatedBetween, page, pageSize);
41+
return await this.reader.getChannelsByMultipleTopics(topics, updatedBetween);
4642
}
4743
async persistTransaction(
4844
transactionData: DataAccessTypes.ITransaction,

packages/data-access/src/data-read.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,15 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
4949
async getChannelsByTopic(
5050
topic: string,
5151
updatedBetween?: DataAccessTypes.ITimestampBoundaries | undefined,
52-
page?: number | undefined,
53-
pageSize?: number | undefined,
5452
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
55-
return this.getChannelsByMultipleTopics([topic], updatedBetween, page, pageSize);
53+
return this.getChannelsByMultipleTopics([topic], updatedBetween);
5654
}
5755

5856
async getChannelsByMultipleTopics(
5957
topics: string[],
6058
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
61-
page?: number,
62-
pageSize?: number,
6359
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
64-
// Validate pagination parameters
65-
if (page !== undefined && page < 1) {
66-
throw new Error(`Page number must be greater than or equal to 1, but it is ${page}`);
67-
}
68-
if (pageSize !== undefined && pageSize < 1) {
69-
throw new Error(`Page size must be greater than 0, but it is ${pageSize}`);
70-
}
71-
60+
const result = await this.storage.getTransactionsByTopics(topics);
7261
const pending = this.pendingStore?.findByTopics(topics) || [];
7362

7463
const pendingItems = pending.map((item) => ({
@@ -84,33 +73,6 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
8473
topics: item.topics || [],
8574
}));
8675

87-
// Calculate adjusted pagination
88-
let adjustedPage = page;
89-
let adjustedPageSize = pageSize;
90-
let pendingItemsOnCurrentPage = 0;
91-
if (page !== undefined && pageSize !== undefined) {
92-
const totalPending = pendingItems.length;
93-
const itemsPerPage = (page - 1) * pageSize;
94-
95-
if (totalPending > itemsPerPage) {
96-
pendingItemsOnCurrentPage = Math.min(totalPending - itemsPerPage, pageSize);
97-
adjustedPageSize = pageSize - pendingItemsOnCurrentPage;
98-
adjustedPage = 1;
99-
if (adjustedPageSize === 0) {
100-
adjustedPageSize = 1;
101-
pendingItemsOnCurrentPage--;
102-
}
103-
} else {
104-
adjustedPage = page - Math.floor(totalPending / pageSize);
105-
}
106-
}
107-
108-
const result = await this.storage.getTransactionsByTopics(
109-
topics,
110-
adjustedPage,
111-
adjustedPageSize,
112-
);
113-
11476
const transactions = result.transactions.concat(...pendingItems);
11577

11678
// list of channels having at least one tx updated during the updatedBetween boundaries
@@ -138,17 +100,6 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
138100
prev[curr.channelId].push(curr.hash);
139101
return prev;
140102
}, {} as Record<string, string[]>),
141-
pagination:
142-
page && pageSize
143-
? {
144-
total: result.transactions.length + pendingItems.length,
145-
page,
146-
pageSize,
147-
hasMore:
148-
(page - 1) * pageSize + filteredTxs.length - pendingItemsOnCurrentPage <
149-
result.transactions.length,
150-
}
151-
: undefined,
152103
},
153104
result: {
154105
transactions: filteredTxs.reduce((prev, curr) => {

packages/data-access/src/in-memory-indexer.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,11 @@ export class InMemoryIndexer implements StorageTypes.IIndexer {
5555
};
5656
}
5757

58-
async getTransactionsByTopics(
59-
topics: string[],
60-
page?: number,
61-
pageSize?: number,
62-
): Promise<StorageTypes.IGetTransactionsResponse> {
63-
if (page !== undefined && page < 1) {
64-
throw new Error('Page must be greater than or equal to 1');
65-
}
66-
if (pageSize !== undefined && pageSize <= 0) {
67-
throw new Error('Page size must be greater than 0');
68-
}
69-
58+
async getTransactionsByTopics(topics: string[]): Promise<StorageTypes.IGetTransactionsResponse> {
7059
// Efficiently get total count without creating intermediate array
7160
const channelIdsSet = new Set(topics.flatMap((topic) => this.#topicToChannelsIndex.get(topic)));
72-
const total = channelIdsSet.size;
73-
let channelIds = Array.from(channelIdsSet);
61+
const channelIds = Array.from(channelIdsSet);
7462

75-
if (page && pageSize) {
76-
const start = (page - 1) * pageSize;
77-
// Return empty result if page exceeds available data
78-
if (start >= total) {
79-
return {
80-
blockNumber: 0,
81-
transactions: [],
82-
pagination:
83-
page && pageSize
84-
? { total, page, pageSize, hasMore: page * pageSize < total }
85-
: undefined,
86-
};
87-
}
88-
channelIds = channelIds.slice(start, start + pageSize);
89-
}
9063
const locations = channelIds
9164
.map((channel) => this.#channelToLocationsIndex.get(channel))
9265
.flat();

packages/integration-test/test/node-client.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ describe('Request client using a request node', () => {
273273
await waitForConfirmation(requestDataCancel);
274274

275275
// get requests without boundaries
276-
let requests = await requestNetwork.fromTopic(topicsRequest1and2[0]);
276+
const response = await requestNetwork.fromTopic(topicsRequest1and2[0]);
277+
let requests = Array.isArray(response) ? response : response.requests;
277278
expect(requests.length).toBe(2);
278279
expect(requests[0].requestId).toBe(request1.requestId);
279280
expect(requests[1].requestId).toBe(request2.requestId);
@@ -286,9 +287,10 @@ describe('Request client using a request node', () => {
286287
expect(requestData2.state).toBe(Types.RequestLogic.STATE.CREATED);
287288

288289
// get requests with boundaries
289-
requests = await requestNetwork.fromTopic(topicsRequest1and2[0], {
290+
const result = await requestNetwork.fromTopic(topicsRequest1and2[0], {
290291
to: timestampBeforeReduce,
291292
});
293+
requests = Array.isArray(result) ? result : result.requests;
292294
expect(requests.length).toBe(1);
293295
expect(requests[0].requestId).toBe(request1.requestId);
294296

@@ -341,9 +343,10 @@ describe('Request client using a request node', () => {
341343
await new Promise((r) => setTimeout(r, 1500));
342344

343345
// get requests with boundaries
344-
const requests = await requestNetwork.fromIdentity(payerSmartContract, {
346+
const result = await requestNetwork.fromIdentity(payerSmartContract, {
345347
from: timestampCreation,
346348
});
349+
const requests = Array.isArray(result) ? result : result.requests;
347350
expect(requests.length).toBe(1);
348351
expect(requests[0].requestId).toBe(request1.requestId);
349352
});

packages/request-client.js/src/api/request-network.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
SignatureProviderTypes,
1818
TransactionTypes,
1919
} from '@requestnetwork/types';
20-
import { deepCopy, supportedIdentities, validatePaginationParams } from '@requestnetwork/utils';
20+
import { deepCopy, supportedIdentities } from '@requestnetwork/utils';
2121
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
2222
import * as Types from '../types';
2323
import ContentDataExtension from './content-data-extension';
@@ -294,7 +294,9 @@ export default class RequestNetwork {
294294
page?: number;
295295
pageSize?: number;
296296
},
297-
): Promise<Request[]> {
297+
): Promise<
298+
Request[] | { meta: RequestLogicTypes.IReturnGetRequestsByTopic['meta']; requests: Request[] }
299+
> {
298300
if (!this.supportedIdentities.includes(identity.type)) {
299301
throw new Error(`${identity.type} is not supported`);
300302
}
@@ -317,7 +319,9 @@ export default class RequestNetwork {
317319
page?: number;
318320
pageSize?: number;
319321
},
320-
): Promise<Request[]> {
322+
): Promise<
323+
Request[] | { meta: RequestLogicTypes.IReturnGetRequestsByTopic['meta']; requests: Request[] }
324+
> {
321325
const identityNotSupported = identities.find(
322326
(identity) => !this.supportedIdentities.includes(identity.type),
323327
);
@@ -345,9 +349,9 @@ export default class RequestNetwork {
345349
page?: number;
346350
pageSize?: number;
347351
},
348-
): Promise<Request[]> {
349-
validatePaginationParams(options?.page, options?.pageSize);
350-
352+
): Promise<
353+
Request[] | { meta: RequestLogicTypes.IReturnGetRequestsByTopic['meta']; requests: Request[] }
354+
> {
351355
// Gets all the requests indexed by the value of the identity
352356
const requestsAndMeta: RequestLogicTypes.IReturnGetRequestsByTopic =
353357
await this.requestLogic.getRequestsByTopic(
@@ -389,8 +393,16 @@ export default class RequestNetwork {
389393
return request;
390394
},
391395
);
392-
393-
return Promise.all(requestPromises);
396+
const requests = await Promise.all(requestPromises);
397+
398+
if (options?.page && options?.pageSize) {
399+
return {
400+
requests,
401+
meta: requestsAndMeta.meta,
402+
};
403+
} else {
404+
return requests;
405+
}
394406
}
395407

396408
/**
@@ -409,9 +421,9 @@ export default class RequestNetwork {
409421
page?: number;
410422
pageSize?: number;
411423
},
412-
): Promise<Request[]> {
413-
validatePaginationParams(options?.page, options?.pageSize);
414-
424+
): Promise<
425+
Request[] | { meta: RequestLogicTypes.IReturnGetRequestsByTopic['meta']; requests: Request[] }
426+
> {
415427
// Gets all the requests indexed by the value of the identity
416428
const requestsAndMeta: RequestLogicTypes.IReturnGetRequestsByTopic =
417429
await this.requestLogic.getRequestsByMultipleTopics(
@@ -454,8 +466,15 @@ export default class RequestNetwork {
454466
return request;
455467
},
456468
);
457-
458-
return Promise.all(requestPromises);
469+
const requests = await Promise.all(requestPromises);
470+
if (options?.page && options?.pageSize) {
471+
return {
472+
requests,
473+
meta: requestsAndMeta.meta,
474+
};
475+
} else {
476+
return requests;
477+
}
459478
}
460479

461480
/*

packages/request-client.js/src/http-data-access.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ export default class HttpDataAccess extends CombinedDataAccess {
110110
public async getChannelsByTopic(
111111
topic: string,
112112
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
113-
page?: number,
114-
pageSize?: number,
115113
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
116-
return await this.reader.getChannelsByTopic(topic, updatedBetween, page, pageSize);
114+
return await this.reader.getChannelsByTopic(topic, updatedBetween);
117115
}
118116

119117
/**
@@ -125,10 +123,8 @@ export default class HttpDataAccess extends CombinedDataAccess {
125123
public async getChannelsByMultipleTopics(
126124
topics: string[],
127125
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
128-
page?: number,
129-
pageSize?: number,
130126
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
131-
return await this.reader.getChannelsByMultipleTopics(topics, updatedBetween, page, pageSize);
127+
return await this.reader.getChannelsByMultipleTopics(topics, updatedBetween);
132128
}
133129

134130
/**

packages/request-client.js/src/http-data-read.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { DataAccessTypes } from '@requestnetwork/types';
2-
import { validatePaginationParams } from '@requestnetwork/utils';
32
import { HttpDataAccessConfig } from './http-data-access-config';
43

54
export class HttpDataRead implements DataAccessTypes.IDataRead {
@@ -53,16 +52,10 @@ export class HttpDataRead implements DataAccessTypes.IDataRead {
5352
public async getChannelsByTopic(
5453
topic: string,
5554
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
56-
page?: number,
57-
pageSize?: number,
5855
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
59-
validatePaginationParams(page, pageSize);
60-
6156
const params = {
6257
topic,
6358
updatedBetween,
64-
...(page !== undefined && { page }),
65-
...(pageSize !== undefined && { pageSize }),
6659
};
6760

6861
return await this.dataAccessConfig.fetchAndRetry('/getChannelsByTopic', params);
@@ -77,16 +70,10 @@ export class HttpDataRead implements DataAccessTypes.IDataRead {
7770
public async getChannelsByMultipleTopics(
7871
topics: string[],
7972
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
80-
page?: number,
81-
pageSize?: number,
8273
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
83-
validatePaginationParams(page, pageSize);
84-
8574
return await this.dataAccessConfig.fetchAndRetry('/getChannelsByMultipleTopics', {
8675
topics,
8776
updatedBetween,
88-
page,
89-
pageSize,
9077
});
9178
}
9279
}

0 commit comments

Comments
 (0)