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 26, 2023
1 parent 1af2668 commit 0323968
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
35 changes: 19 additions & 16 deletions watcher/src/watchers/SolanaWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ export class SolanaWatcher extends Watcher {

connection: Connection | undefined;

constructor(rpc: string = RPCS_BY_CHAIN.solana!, contract: string = WORMHOLE_PROGRAM_ID) {
constructor() {
super('solana');
this.rpc = rpc;
this.programId = contract;
this.rpc = RPCS_BY_CHAIN.solana!;
this.programId = WORMHOLE_PROGRAM_ID;
}

getConnnection(): Connection {
getConnection(): Connection {
this.connection = this.connection ?? new Connection(this.rpc, COMMITMENT);
return this.connection;
}

async getFinalizedBlockNumber(): Promise<number> {
return this.getConnnection().getSlot();
return this.getConnection().getSlot();
}

private async findNextValidBlock(
Expand All @@ -60,7 +60,7 @@ export class SolanaWatcher extends Watcher {

let block: VersionedBlockResponse | null = null;
try {
block = await this.getConnnection().getBlock(slot, { maxSupportedTransactionVersion: 0 });
block = await this.getConnection().getBlock(slot, { maxSupportedTransactionVersion: 0 });
} catch (e) {
if (e instanceof SolanaJSONRPCError && (e.code === -32007 || e.code === -32009)) {
// failed to get confirmed block: slot was skipped or missing in long-term storage
Expand Down Expand Up @@ -94,12 +94,15 @@ 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 All @@ -110,7 +113,7 @@ export class SolanaWatcher extends Watcher {
let currSignature: string | undefined = fromSignature;
while (numSignatures === this.getSignaturesLimit) {
const signatures: ConfirmedSignatureInfo[] =
await this.getConnnection().getSignaturesForAddress(new PublicKey(this.programId), {
await this.getConnection().getSignaturesForAddress(new PublicKey(this.programId), {
before: currSignature,
until: toSignature,
limit: this.getSignaturesLimit,
Expand All @@ -125,7 +128,7 @@ export class SolanaWatcher extends Watcher {
// reused, overwriting previous data). Then, the message account is the account given by
// the second index in the instruction's account key indices. From here, we can fetch the
// message data from the account and parse out the emitter and sequence.
const results = await this.getConnnection().getTransactions(
const results = await this.getConnection().getTransactions(
signatures.map((s) => s.signature),
{
maxSupportedTransactionVersion: 0,
Expand Down Expand Up @@ -158,7 +161,7 @@ export class SolanaWatcher extends Watcher {
// before looking for the programIdIndex
if (message.addressTableLookups.length > 0) {
const lookupPromises = message.addressTableLookups.map(async (atl) => {
const lookupTableAccount = await this.getConnnection()
const lookupTableAccount = await this.getConnection()
.getAddressLookupTable(atl.accountKey)
.then((res) => res.value);

Expand Down Expand Up @@ -196,7 +199,7 @@ export class SolanaWatcher extends Watcher {
const accountId = accountKeys[instruction.accountKeyIndexes[1]];
const {
message: { emitterAddress, sequence },
} = await getPostedMessage(this.getConnnection(), accountId.toBase58(), COMMITMENT);
} = await getPostedMessage(this.getConnection(), accountId.toBase58(), COMMITMENT);

const blockKey = makeBlockKey(
res.slot.toString(),
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 0323968

Please sign in to comment.