From 899321ffb6e0612b9d62a8247a02f6e174eccafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Thu, 20 Jun 2024 21:58:10 -0300 Subject: [PATCH 1/5] tests: added tests for the voidTransaction method --- packages/daemon/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/daemon/package.json b/packages/daemon/package.json index d950264f..5a32364d 100644 --- a/packages/daemon/package.json +++ b/packages/daemon/package.json @@ -22,7 +22,7 @@ "test_images_wait_for_ws": "yarn dlx ts-node ./__tests__/integration/scripts/wait-for-ws-up.ts", "test_images_setup_database": "yarn dlx ts-node ./__tests__/integration/scripts/setup-database.ts", "test": "jest --coverage", - "test_integration": "yarn run test_images_up && yarn run test_images_wait_for_db && yarn run test_images_wait_for_ws && yarn run test_images_setup_database && yarn run test_images_migrate && yarn run test_images_integration && yarn run test_images_down" + "test_integration": "yarn run test_images_up && yarn run test_images_wait_for_db && yarn run test_images_wait_for_ws && yarn run test_images_setup_database && yarn run test_images_migrate && yarn run test_images_integration" }, "name": "sync-daemon", "author": "André Abadesso", From 946e9c026c64a00bc6a2b05adc0ca9f81c5ea00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Tue, 25 Jun 2024 11:11:31 -0300 Subject: [PATCH 2/5] chore: re-add test_images_down --- packages/daemon/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/daemon/package.json b/packages/daemon/package.json index 5a32364d..d950264f 100644 --- a/packages/daemon/package.json +++ b/packages/daemon/package.json @@ -22,7 +22,7 @@ "test_images_wait_for_ws": "yarn dlx ts-node ./__tests__/integration/scripts/wait-for-ws-up.ts", "test_images_setup_database": "yarn dlx ts-node ./__tests__/integration/scripts/setup-database.ts", "test": "jest --coverage", - "test_integration": "yarn run test_images_up && yarn run test_images_wait_for_db && yarn run test_images_wait_for_ws && yarn run test_images_setup_database && yarn run test_images_migrate && yarn run test_images_integration" + "test_integration": "yarn run test_images_up && yarn run test_images_wait_for_db && yarn run test_images_wait_for_ws && yarn run test_images_setup_database && yarn run test_images_migrate && yarn run test_images_integration && yarn run test_images_down" }, "name": "sync-daemon", "author": "André Abadesso", From b67edfc7d7c9033c75d4e36a430d6b93aa79d20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Tue, 30 Jul 2024 10:51:23 -0300 Subject: [PATCH 3/5] fix: markUtxosAsVoided should not throw when inputs are empty --- packages/daemon/__tests__/db/index.test.ts | 6 ++++++ packages/daemon/src/db/index.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/packages/daemon/__tests__/db/index.test.ts b/packages/daemon/__tests__/db/index.test.ts index a399a85d..de037f41 100644 --- a/packages/daemon/__tests__/db/index.test.ts +++ b/packages/daemon/__tests__/db/index.test.ts @@ -289,6 +289,12 @@ describe('tx output methods', () => { expect(countAfterDelete).toStrictEqual(0); }); + test('markUtxosAsVoided should not throw when utxos list is empty', async () => { + expect.hasAssertions(); + + await expect(markUtxosAsVoided(mysql, [])).resolves.not.toThrow(); + }); + test('getTxOutputsFromTx, getTxOutputs, getTxOutput', async () => { expect.hasAssertions(); diff --git a/packages/daemon/src/db/index.ts b/packages/daemon/src/db/index.ts index 68e7c143..d0ec6f7a 100644 --- a/packages/daemon/src/db/index.ts +++ b/packages/daemon/src/db/index.ts @@ -1344,6 +1344,10 @@ export const markUtxosAsVoided = async ( ): Promise => { const txIds = utxos.map((tx) => tx.txId); + if (txIds.length === 0) { + return; + } + await mysql.query(` UPDATE \`tx_output\` SET \`voided\` = TRUE From 402ee17e11d28d2fc5db5f2eec5414e1d4508204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Tue, 30 Jul 2024 11:39:06 -0300 Subject: [PATCH 4/5] fix: returning empty object on db utils and added test for services including db --- .../services/services_with_db.test.ts | 64 +++++++++++++++++++ packages/daemon/src/db/index.ts | 8 +++ packages/daemon/src/utils/wallet.ts | 4 ++ 3 files changed, 76 insertions(+) create mode 100644 packages/daemon/__tests__/services/services_with_db.test.ts diff --git a/packages/daemon/__tests__/services/services_with_db.test.ts b/packages/daemon/__tests__/services/services_with_db.test.ts new file mode 100644 index 00000000..76c11a5b --- /dev/null +++ b/packages/daemon/__tests__/services/services_with_db.test.ts @@ -0,0 +1,64 @@ +/** + * Copyright (c) Hathor Labs and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import * as db from '../../src/db'; +import { handleVoidedTx } from '../../src/services'; +import { LRU } from '../../src/utils'; + +/** + * @jest-environment node + */ + +describe('handleVoidedTx (db)', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should handle transactions with an empty list of inputs', async () => { + const voidTxSpy = jest.spyOn(db, 'voidTransaction'); + voidTxSpy.mockResolvedValue(); + + const context = { + socket: expect.any(Object), + healthcheck: expect.any(Object), + retryAttempt: expect.any(Number), + initialEventId: expect.any(Number), + txCache: expect.any(LRU), + event: { + stream_id: 'stream-id', + peer_id: 'peer_id', + network: 'testnet', + type: 'FULLNODE_EVENT', + latest_event_id: 4, + event: { + id: 5, + data: { + hash: 'random-hash', + outputs: [], + inputs: [], + tokens: [], + }, + }, + }, + }; + + const mysql = await db.getDbConnection(); + const lastEvent = await db.getLastSyncedEvent(mysql); + + await expect(handleVoidedTx(context as any)).resolves.not.toThrow(); + expect(db.voidTransaction).toHaveBeenCalledWith( + expect.any(Object), + 'random-hash', + expect.any(Object), + ); + expect(lastEvent).toStrictEqual({ + id: expect.any(Number), + last_event_id: 5, + updated_at: expect.any(String), + }); + }); +}); diff --git a/packages/daemon/src/db/index.ts b/packages/daemon/src/db/index.ts index d0ec6f7a..1367514d 100644 --- a/packages/daemon/src/db/index.ts +++ b/packages/daemon/src/db/index.ts @@ -1413,6 +1413,10 @@ export const fetchAddressBalance = async ( mysql: MysqlConnection, addresses: string[], ): Promise => { + if (addresses.length === 0) { + return []; + } + const [results] = await mysql.query( `SELECT * FROM \`address_balance\` @@ -1443,6 +1447,10 @@ export const fetchAddressTxHistorySum = async ( mysql: MysqlConnection, addresses: string[], ): Promise => { + if (addresses.length === 0) { + return []; + } + const [results] = await mysql.query( `SELECT address, token_id, diff --git a/packages/daemon/src/utils/wallet.ts b/packages/daemon/src/utils/wallet.ts index 1ff43b1a..d9b6a34c 100644 --- a/packages/daemon/src/utils/wallet.ts +++ b/packages/daemon/src/utils/wallet.ts @@ -58,6 +58,10 @@ import { stringMapIterator } from './helpers'; * metadata. */ export const prepareOutputs = (outputs: EventTxOutput[], tokens: string[]): TxOutputWithIndex[] => { + if (outputs.length === 0) { + return []; + } + const preparedOutputs: [number, TxOutputWithIndex[]] = outputs.reduce( ([currIndex, newOutputs]: [number, TxOutputWithIndex[]], _output: EventTxOutput): [number, TxOutputWithIndex[]] => { const output = new Output(_output.value, Buffer.from(_output.script, 'base64'), { From 786a4f9b95ac1bc10847d7de3aba90a5c082ded6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Abadesso?= Date: Tue, 30 Jul 2024 11:53:19 -0300 Subject: [PATCH 5/5] tests: fix invalid order of calls --- packages/daemon/__tests__/services/services_with_db.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/daemon/__tests__/services/services_with_db.test.ts b/packages/daemon/__tests__/services/services_with_db.test.ts index 76c11a5b..33b7809e 100644 --- a/packages/daemon/__tests__/services/services_with_db.test.ts +++ b/packages/daemon/__tests__/services/services_with_db.test.ts @@ -47,9 +47,9 @@ describe('handleVoidedTx (db)', () => { }; const mysql = await db.getDbConnection(); - const lastEvent = await db.getLastSyncedEvent(mysql); - await expect(handleVoidedTx(context as any)).resolves.not.toThrow(); + + const lastEvent = await db.getLastSyncedEvent(mysql); expect(db.voidTransaction).toHaveBeenCalledWith( expect.any(Object), 'random-hash',