Skip to content

Commit

Permalink
fix updatedBetween condition + test shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Oct 4, 2023
1 parent 3fdbcea commit 2643456
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 10 additions & 7 deletions packages/data-access/src/data-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
): Promise<DataAccessTypes.IReturnGetChannelsByTopic> {
const result = await this.storage.getTransactionsByTopics(topics);
const pending = this.pendingStore?.findByTopics(topics) || [];

const pendingItems = pending.map((item) => ({
hash: item.storageResult.id,
channelId: item.channelId,
Expand All @@ -75,13 +76,15 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
const transactions = result.transactions.concat(...pendingItems);

// list of channels having at least one tx updated during the updatedBetween boundaries
const channels = transactions
.filter(
(tx) =>
tx.blockTimestamp >= (updatedBetween?.from || 0) &&
tx.blockTimestamp <= (updatedBetween?.to || Number.MAX_SAFE_INTEGER),
)
.map((x) => x.channelId);
const channels = (
updatedBetween
? transactions.filter(
(tx) =>
tx.blockTimestamp >= (updatedBetween.from || 0) &&
tx.blockTimestamp <= (updatedBetween.to || Number.MAX_SAFE_INTEGER),
)
: transactions
).map((x) => x.channelId);

const filteredTxs = transactions.filter((tx) => channels.includes(tx.channelId));
return {
Expand Down
23 changes: 22 additions & 1 deletion packages/request-node/test/getChannelsByTopic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { StatusCodes } from 'http-status-codes';
import { getRequestNode } from '../src/server';
import request from 'supertest';
import { RequestNode } from '../src/requestNode';
import { normalizeKeccak256Hash } from '@requestnetwork/utils';
import { providers } from 'ethers';

// enable re-running these tests on local environment by having a different channel ID each time.
const time = Date.now();
Expand Down Expand Up @@ -96,7 +98,26 @@ describe('getChannelsByTopic', () => {
[anotherChannelId]: [expect.objectContaining({ transaction: otherTransactionData })],
}),
);
});

// confirm the transactions for clean shutdown
const provider = new providers.JsonRpcProvider();
const confirm = (txData: unknown) => {
const transactionHash = normalizeKeccak256Hash(txData).value;
return new Promise<void>((r) => {
const i = setInterval(async () => {
await provider.send('evm_mine', []);
const res = await request(server)
.get('/getConfirmedTransaction')
.query({ transactionHash });
if (res.status === 200) {
clearInterval(i);
return r();
}
}, 200);
});
};
await Promise.all([confirm(transactionData), confirm(otherTransactionData)]);
}, 10000);

it('responds with no transaction to requests with a non-existent topic', async () => {
const serverResponse = await request(server)
Expand Down

0 comments on commit 2643456

Please sign in to comment.