From 26434561e9bc1cd5780c326193b07bfe2647489e Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:39:42 +0200 Subject: [PATCH] fix updatedBetween condition + test shutdown --- packages/data-access/src/data-read.ts | 17 ++++++++------ .../test/getChannelsByTopic.test.ts | 23 ++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/data-access/src/data-read.ts b/packages/data-access/src/data-read.ts index 6271392c32..34d0698b76 100644 --- a/packages/data-access/src/data-read.ts +++ b/packages/data-access/src/data-read.ts @@ -59,6 +59,7 @@ export class DataAccessRead implements DataAccessTypes.IDataRead { ): Promise { 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, @@ -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 { diff --git a/packages/request-node/test/getChannelsByTopic.test.ts b/packages/request-node/test/getChannelsByTopic.test.ts index 39700942aa..78dfc67e71 100644 --- a/packages/request-node/test/getChannelsByTopic.test.ts +++ b/packages/request-node/test/getChannelsByTopic.test.ts @@ -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(); @@ -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((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)