diff --git a/watcher/src/watchers/SolanaWatcher.ts b/watcher/src/watchers/SolanaWatcher.ts index 3220303b..fbfe0934 100644 --- a/watcher/src/watchers/SolanaWatcher.ts +++ b/watcher/src/watchers/SolanaWatcher.ts @@ -7,6 +7,7 @@ import { PublicKey, SolanaJSONRPCError, VersionedBlockResponse, + VersionedTransactionResponse, } from '@solana/web3.js'; import { decode } from 'bs58'; import { z } from 'zod'; @@ -94,12 +95,19 @@ export class SolanaWatcher extends Watcher { // look for the (last block + 1) and (first block - 1) since the signature parameters in the search later // are _exclusive_ so we have to get the signatures immediate preceeding or following the ones we're interested in const retries = 5; - const toBlock: VersionedBlockResponse = await this.findNextValidBlock(toSlot + 1, -1, retries); - const fromBlock: VersionedBlockResponse = await this.findNextValidBlock( - fromSlot - 1, - 1, - retries - ); + let toBlock: VersionedBlockResponse + let fromBlock: VersionedBlockResponse + + try { + toBlock = await this.findNextValidBlock(toSlot + 1, -1, retries); + fromBlock = await this.findNextValidBlock( + fromSlot - 1, + 1, + retries + ); + } catch (e) { + throw new Error("solana: invalid block range: " + (e as Error).message) + } const fromSignature = toBlock.transactions[0].transaction.signatures[0]; const toSignature = @@ -143,8 +151,7 @@ export class SolanaWatcher extends Watcher { } if (!res || !res.blockTime) { throw new Error( - `solana: failed to fetch tx for signature ${ - res?.transaction.signatures[0] || 'unknown' + `solana: failed to fetch tx for signature ${res?.transaction.signatures[0] || 'unknown' }` ); } diff --git a/watcher/src/watchers/__tests__/SolanaWatcher.test.ts b/watcher/src/watchers/__tests__/SolanaWatcher.test.ts index 4698856a..7e503a6b 100644 --- a/watcher/src/watchers/__tests__/SolanaWatcher.test.ts +++ b/watcher/src/watchers/__tests__/SolanaWatcher.test.ts @@ -32,13 +32,13 @@ test('getMessagesForBlocks - fromSlot is skipped slot', async () => { const watcher = new SolanaWatcher(); const messages = await watcher.getMessagesForBlocks(171774030, 171774032); // 171774024 - 171774031 are skipped expect(Object.keys(messages).length).toBe(1); - expect(messages).toMatchObject({ '171774032/2023-01-10T13:36:38.000Z': [] }); + expect(messages).toMatchObject({ '171774032/2023-01-10T13:36:39.000Z': [] }); }); test('getMessagesForBlocks - toSlot is skipped slot', async () => { const watcher = new SolanaWatcher(); const messages = await watcher.getMessagesForBlocks(171774023, 171774025); - expect(messages).toMatchObject({ '171774023/2023-01-10T13:36:34.000Z': [] }); + expect(messages).toMatchObject({ '171774025/2023-01-10T13:36:34.000Z': [] }); }); test('getMessagesForBlocks - empty block', async () => { @@ -46,7 +46,7 @@ test('getMessagesForBlocks - empty block', async () => { const watcher = new SolanaWatcher(); const messages = await watcher.getMessagesForBlocks(170979766, 170979766); expect(Object.keys(messages).length).toBe(1); - expect(messages).toMatchObject({ '170979766/2023-01-05T18:40:24.000Z': [] }); + expect(messages).toMatchObject({ '170979766/2023-01-05T18:40:25.000Z': [] }); }); // temporary skip due to SolanaJSONRPCError: failed to get confirmed block: Block 174108865 cleaned up, does not exist on node. First available block: 176892532