Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle tx with no inputs #181

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/daemon/__tests__/db/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
64 changes: 64 additions & 0 deletions packages/daemon/__tests__/services/services_with_db.test.ts
Original file line number Diff line number Diff line change
@@ -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();
await expect(handleVoidedTx(context as any)).resolves.not.toThrow();

const lastEvent = await db.getLastSyncedEvent(mysql);
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),
});
});
});
12 changes: 12 additions & 0 deletions packages/daemon/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,10 @@ export const markUtxosAsVoided = async (
): Promise<void> => {
const txIds = utxos.map((tx) => tx.txId);

if (txIds.length === 0) {
return;
}

await mysql.query(`
UPDATE \`tx_output\`
SET \`voided\` = TRUE
Expand Down Expand Up @@ -1409,6 +1413,10 @@ export const fetchAddressBalance = async (
mysql: MysqlConnection,
addresses: string[],
): Promise<AddressBalance[]> => {
if (addresses.length === 0) {
return [];
}

const [results] = await mysql.query<AddressBalanceRow[]>(
`SELECT *
FROM \`address_balance\`
Expand Down Expand Up @@ -1439,6 +1447,10 @@ export const fetchAddressTxHistorySum = async (
mysql: MysqlConnection,
addresses: string[],
): Promise<AddressTotalBalance[]> => {
if (addresses.length === 0) {
return [];
}

const [results] = await mysql.query<AddressTxHistorySumRow[]>(
`SELECT address,
token_id,
Expand Down
4 changes: 4 additions & 0 deletions packages/daemon/src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'), {
Expand Down
Loading