Skip to content

Commit

Permalink
throw specific error
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 23, 2023
1 parent ea33c0c commit f4bc9f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
23 changes: 15 additions & 8 deletions watcher/src/watchers/SolanaWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PublicKey,
SolanaJSONRPCError,
VersionedBlockResponse,
VersionedTransactionResponse,
} from '@solana/web3.js';
import { decode } from 'bs58';
import { z } from 'zod';
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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'
}`
);
}
Expand Down
6 changes: 3 additions & 3 deletions watcher/src/watchers/__tests__/SolanaWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ 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 () => {
// Even if there are no messages, last block should still be returned
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
Expand Down

0 comments on commit f4bc9f9

Please sign in to comment.