Skip to content

Commit 2683e51

Browse files
add retries to submitTransaction
1 parent 519e56b commit 2683e51

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

sdk/src/network-client.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ class AleoNetworkClient {
126126
}
127127
}
128128

129+
130+
/**
131+
* Wrapper around the POST helper to allow mocking in tests.
132+
*
133+
* @param url The URL to POST to.
134+
* @param options The RequestInit options for the POST request.
135+
* @returns The Response object from the POST request.
136+
*/
137+
private async sendPost(url: string, options: RequestInit) {
138+
return post(url, options);
139+
}
140+
129141
/**
130142
* Attempt to find records in the Aleo blockchain.
131143
*
@@ -1347,12 +1359,13 @@ class AleoNetworkClient {
13471359
? transaction.toString()
13481360
: transaction;
13491361
try {
1350-
const response = await post(this.host + "/transaction/broadcast", {
1351-
body: transaction_string,
1352-
headers: Object.assign({}, this.headers, {
1353-
"Content-Type": "application/json",
1354-
}),
1355-
});
1362+
const response = await retryWithBackoff(() =>
1363+
this.sendPost(this.host + "/transaction/broadcast", {
1364+
body: transaction_string,
1365+
headers: Object.assign({}, this.headers, {
1366+
"Content-Type": "application/json",
1367+
}),
1368+
}));
13561369

13571370
try {
13581371
const text = await response.text();

sdk/tests/network-client.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +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";
2122

2223
async function catchError(f: () => Promise<any>): Promise<Error | null> {
2324
try {
@@ -234,6 +235,27 @@ describe("NodeConnection", () => {
234235
expect(attemptCount).to.be.greaterThan(1);
235236
}
236237
});
238+
239+
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 () => {
246+
attemptCount++;
247+
console.warn(`fake sendPost attempt ${attemptCount}`);
248+
throw new Error("503 Service Unavailable");
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");
256+
expect(attemptCount).to.be.greaterThan(1);
257+
}
258+
});
237259
});
238260

239261

0 commit comments

Comments
 (0)