Skip to content

Commit 865c83e

Browse files
address comments and add tests
1 parent 527b01b commit 865c83e

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

sdk/src/network-client.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,18 @@ class AleoNetworkClient {
138138
*/
139139
async fetchData<Type>(url = "/"): Promise<Type> {
140140
try {
141-
return await retryWithBackoff(() => this.fetchRaw(url).then(parseJSON), {
142-
retryOnStatus: [500, 502, 503, 504],
143-
shouldRetry: (err) => {
144-
const msg = err?.message?.toLowerCase?.() || "";
145-
return msg.includes("network") || msg.includes("timeout") || msg.includes("503");
146-
}
147-
});
141+
const raw = await retryWithBackoff(() => this.fetchRaw(url), {
142+
retryOnStatus: [500, 502, 503, 504],
143+
shouldRetry: (err) => {
144+
const msg = err?.message?.toLowerCase?.() || "";
145+
return msg.includes("network") || msg.includes("timeout") || msg.includes("503");
146+
}
147+
});
148+
return parseJSON(raw);
148149
} catch (error) {
149-
throw new Error(`Error fetching data: ${error}`);
150+
throw new Error(`Error fetching data: ${error}`);
150151
}
151-
}
152+
}
152153

153154

154155
/**
@@ -400,7 +401,7 @@ class AleoNetworkClient {
400401
// Attempt to see if the serial number is spent
401402
try {
402403
await retryWithBackoff(() => this.getTransitionId(serialNumber), {
403-
retryOnStatus: [500, 502, 503],
404+
retryOnStatus: [500, 502, 503, 504],
404405
shouldRetry: (err) => {
405406
const msg = err?.message?.toLowerCase?.() || "";
406407
return msg.includes("timeout") || msg.includes("503") || msg.includes("network");

sdk/tests/network-client.test.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
TransitionObject,
1919
} from "@provablehq/sdk/%%NETWORK%%.js";
2020
import { beaconPrivateKeyString } from "./data/account-data";
21-
import * as utils from "../src/utils";
21+
import { retryWithBackoff } from "../src/utils";
2222

2323
async function catchError(f: () => Promise<any>): Promise<Error | null> {
2424
try {
@@ -237,24 +237,52 @@ describe("NodeConnection", () => {
237237
});
238238

239239
it("should retry failed transaction submissions and eventually give up", async () => {
240-
const client = new AleoNetworkClient("http://localhost:1234");
241-
242-
let attemptCount = 0;
243-
244-
// @ts-ignore override for testing
245-
client["sendPost"] = async () => {
240+
const client = new AleoNetworkClient("http://localhost:1234");
241+
242+
let attemptCount = 0;
243+
244+
client["sendPost"] = async () => {
245+
attemptCount++;
246+
console.warn(`fake sendPost attempt ${attemptCount}`);
247+
const error = new Error("503 Service Unavailable") as any;
248+
error.status = 503;
249+
throw error;
250+
};
251+
252+
try {
253+
await retryWithBackoff(() =>
254+
client["sendPost"]("http://fakeurl", { method: "POST" }), {
255+
retryOnStatus: [503],
256+
}
257+
);
258+
throw new Error("Expected retryWithBackoff to fail");
259+
} catch (err: any) {
260+
expect(err.message).to.include("503");
261+
expect(attemptCount).to.be.greaterThan(1);
262+
}
263+
});
264+
265+
it("should retry solution submission and eventually throw", async () => {
266+
const client = new AleoNetworkClient("http://localhost:1234");
267+
268+
let attemptCount = 0;
269+
270+
client["sendPost"] = async function () {
246271
attemptCount++;
247-
console.warn(`fake sendPost attempt ${attemptCount}`);
248-
throw Object.assign(new Error("503 Service Unavailable"), { status: 503 });
249-
};
250-
251-
try {
252-
await client.submitTransaction("dummy_tx_string");
253-
throw new Error("Expected submitTransaction to fail");
254-
} catch (err: any) {
255-
expect(err.message).to.include("503");
272+
throw Object.assign(new Error("Network error"), { status: 503 });
273+
};
274+
275+
try {
276+
await retryWithBackoff(() =>
277+
client["sendPost"]("http://fakeurl", { method: "POST" }), {
278+
retryOnStatus: [503],
279+
}
280+
);
281+
throw new Error("Expected sendPost to fail");
282+
} catch (err: any) {
283+
expect(err.message).to.include("Network error");
256284
expect(attemptCount).to.be.greaterThan(1);
257-
}
285+
}
258286
});
259287
});
260288

0 commit comments

Comments
 (0)