From 606bae5c66d95468f5a9bd43c9ad0c0b9f1ffc12 Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 27 Aug 2024 13:56:04 -0500 Subject: [PATCH] fix: ensure retrieval of unknown txns return a warning --- .../account/src/providers/provider.test.ts | 71 +++++++++++++++++++ packages/account/src/providers/provider.ts | 17 ++++- .../test/fixtures/transaction-summary.ts | 2 + .../transactions/src/coders/transaction.ts | 3 +- 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/packages/account/src/providers/provider.test.ts b/packages/account/src/providers/provider.test.ts index 57ae650b050..4721aae735b 100644 --- a/packages/account/src/providers/provider.test.ts +++ b/packages/account/src/providers/provider.test.ts @@ -16,6 +16,10 @@ import { MESSAGE_PROOF_RAW_RESPONSE, MESSAGE_PROOF, } from '../../test/fixtures'; +import { + MOCK_TX_UNKNOWN_RAW_PAYLOAD, + MOCK_TX_SCRIPT_RAW_PAYLOAD, +} from '../../test/fixtures/transaction-summary'; import { setupTestProviderAndWallets, launchNode, TestMessage } from '../test-utils'; import type { Coin } from './coin'; @@ -56,6 +60,73 @@ const getCustomFetch = * @group node */ describe('Provider', () => { + it('should throw an error when retrieving a transaction with an unknown transaction type', async () => { + using launched = await setupTestProviderAndWallets(); + const { provider } = launched; + + const mockProvider = await Provider.create(provider.url, { + fetch: getCustomFetch('getTransaction', { + transaction: { + id: '0x1234567890abcdef', + rawPayload: MOCK_TX_UNKNOWN_RAW_PAYLOAD, // Unknown transaction type + }, + }), + }); + + await expectToThrowFuelError( + () => mockProvider.getTransaction('0x1234567890abcdef'), + new FuelError(ErrorCode.UNSUPPORTED_TRANSACTION_TYPE, 'Unsupported transaction type: 6') + ); + }); + + it('should log a warning when retrieving batch transactions with an unknown transaction type', async () => { + using launched = await setupTestProviderAndWallets(); + const { provider: nodeProvider } = launched; + + // Create a mock provider with custom getTransactions operation + const mockProvider = await Provider.create(nodeProvider.url, { + fetch: getCustomFetch('getTransactions', { + transactions: { + edges: [ + { + node: { + id: '0x1234567890abcdef', + rawPayload: MOCK_TX_UNKNOWN_RAW_PAYLOAD, + }, + }, + { + node: { + id: '0xabcdef1234567890', + rawPayload: MOCK_TX_SCRIPT_RAW_PAYLOAD, + }, + }, + ], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + startCursor: null, + endCursor: null, + }, + }, + }), + }); + + // Spy on console.warn + const consoleWarnSpy = vi.spyOn(console, 'warn'); + + // Verify that only one transaction was returned (the known type) + const { transactions } = await mockProvider.getTransactions(); + expect(transactions.length).toBe(1); + + // Check if warning was logged + expect(consoleWarnSpy).toHaveBeenCalledWith( + expect.stringContaining('Unsupported transaction type encountered:') + ); + + // Clean up + consoleWarnSpy.mockRestore(); + }); + it('can getVersion()', async () => { using launched = await setupTestProviderAndWallets(); const { provider } = launched; diff --git a/packages/account/src/providers/provider.ts b/packages/account/src/providers/provider.ts index 9684ee9c1d2..1362989848c 100644 --- a/packages/account/src/providers/provider.ts +++ b/packages/account/src/providers/provider.ts @@ -1458,9 +1458,20 @@ Supported fuel-core version: ${supportedVersion}.` } = await this.operations.getTransactions(paginationArgs); const coder = new TransactionCoder(); - const transactions = edges.map( - ({ node: { rawPayload } }) => coder.decode(arrayify(rawPayload), 0)[0] - ); + const transactions = edges + .map(({ node: { rawPayload } }) => { + try { + return coder.decode(arrayify(rawPayload), 0)[0]; + } catch (error) { + if (error instanceof FuelError && error.code === ErrorCode.UNSUPPORTED_TRANSACTION_TYPE) { + // eslint-disable-next-line no-console + console.warn('Unsupported transaction type encountered:'); + return null; + } + throw error; + } + }) + .filter((tx): tx is Transaction => tx !== null); return { transactions, pageInfo }; } diff --git a/packages/account/test/fixtures/transaction-summary.ts b/packages/account/test/fixtures/transaction-summary.ts index 04660b3b57e..9e954446590 100644 --- a/packages/account/test/fixtures/transaction-summary.ts +++ b/packages/account/test/fixtures/transaction-summary.ts @@ -325,3 +325,5 @@ export const MOCK_TX_CREATE_RAW_PAYLOAD = '0x00000000000000010000000000000000b100016b3e4e6c6ec572832e5cd9b5bd9162d1371f932ee28c5a61f5a8607f7e0000000000000000000000000000000900000000000000010000000000000002000000000000000200000000000000000000000000000754000000000000000002cc837ec4516621729d615acb83b4871b34b59772c9ad42674f24cbf232f25b0000000000000000a1bfd8f997bb7654af676f6d8a9ebda7eb1ab63426d7f3e5745fdc1672f0031000000000004c4b4000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000e00000000000000010000000000000000000000000000000000000000000000000000000000000004181c794f94f71f983a1cb57b18ee43be3d1d3a74aa2e3ed4c9e50687a18f015600000000000000000000000000000000000000000000000000000000000000000000000000000002a1bfd8f997bb7654af676f6d8a9ebda7eb1ab63426d7f3e5745fdc1672f0031000000000004c43ec00000000000000000000000000000000000000000000000000000000000000000000000000000594740000034700000000000000000003945dfcc00110fff3005d4060495d47f03213490440764800085d47f033134904407648007c5d47f03413490440764800bf72f0007b36f000001aec5000910001405d43f035104103005fec00005047b00f5e4410005d47f03610451300504bb010724c0020284914c05053b0505fec000a5045400f5e4410005057b03072440020285504405043b0605fec100c5045000f5c4bf0905e4520005047b0705e440000504910085c4ff0985e493000504910105c4ff0a05e4930005d4bf0155fed201150491020724c0010284944c050491030724c0020284954c05045105072480010284504805d43f0371041030072440010340004117240001034001ed05d43f038104103005d47f01672480010340114125043b0105d47f01772480020340114125d43f039104103005d47f01872480008340114125043b0705d47f01972480060340114125d43f03a104103005d47f01a72480010340114125043b0705c4100005d47f01b334110005d43f03b104103005d47f01c72480010340114125043b0105047b0d05d4bf0165fed201a5049100f5c4ff0905e493000504bb0e05c4ff0e85e493000504d20085c53f0f05e4d4000504d20105c53f0f85e4d40005d4ff0205fed301f504d202072500010284fb500504d203072500020284d05005041205072480010284114805043b0e05d47f02172480060340114125d43f03c104103005d47f02272480008340114125c43f090244000001aec5000910001305d40604a5d4900005d4d00015d43f035104103005fec00005047b00f5e4410005047b120725000102847b5005047b0305fec00065051100f5e5010005053b01072540020285105405057b0405fec10085041500f5c5bf0905e416000505bb0505e580000504160085c5ff0985e417000504160105c5ff0a05e4170005d43f0155fed000d50416020725c0010284115c0504160307244002028414440504160507244001028415440134124c05047b050134100007640000e5d43f03d104103005d4bf025724c0010340124135043b120504bb110724c0010284904c05d43f026724c0010340104935c43f090244000005043b0b072480060284114805d47f02772480060340114125d43f028364000001aec5000910000305d40604a5c450000504100085d4bf03e104923005d4ff03072500018340134947248002028ed04805d43f0315fed00045fed10055d43f03f104103001a44a0002dec04115c43f0902440000047000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00fffffffffffffffffffffffffffff47616d65205374617465000000000000436f6e7472616374204964000000000047616d652052656647616d65205265662053636f726500004469726563742047616d6500000000005761732054727565010000000000000064000000000000000a000000000000000000000000018af8000000000000000200000000000000030000000000000004000000000000000500000000000000060000000000000007000000000000000865000000000000000c00000000000000030000000000000000000000000201570000000000000009000000000000000a48656c6c6f2054657374657200000000000000000000000d000000000000000e000000000000000cffffffffffff000048656c6c6f2066726f6d206d61696e20436f6e74726163740000000000000000000000000000000000000000000000000000000000000000000000000000000f00000000b1abb86f000000002151bd4b00000000fdbf0f6a0000000045b1551100000000000003b4000000000000039400000000000003d400000000000003e400000000000003f400000000000003fc000000000000040c000000000000041c00000000000004ac00000000000004dc00000000000004f400000000000000000000004041836759e99b4bd0b1a8d9a622e091bf15cbfe9f975dacc38334dfb084ced1c55d58b4e5b4072d22fd3279bf90b1f3bf6429ce4096626905037cccbc05bec7e4'; export const MOCK_TX_MINT_RAW_PAYLOAD = '0x0000000000000002000000000000000500000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'; +export const MOCK_TX_UNKNOWN_RAW_PAYLOAD = + '0x0000000000000006000000000000000500000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'; diff --git a/packages/transactions/src/coders/transaction.ts b/packages/transactions/src/coders/transaction.ts index 65142eb294e..7515df19f20 100644 --- a/packages/transactions/src/coders/transaction.ts +++ b/packages/transactions/src/coders/transaction.ts @@ -28,7 +28,7 @@ export enum TransactionType /* u8 */ { Upgrade = 3, Upload = 4, Blob = 5, - Unknown = -1, + Unknown = 6, } /** @hidden */ @@ -696,6 +696,7 @@ export class TransactionCoder extends Coder { [decoded, o] = new TransactionBlobCoder().decode(data, o); return [decoded, o]; } + case TransactionType.Unknown: default: { throw new FuelError( ErrorCode.UNSUPPORTED_TRANSACTION_TYPE,