Skip to content

Commit

Permalink
Allow CCIP-read to continue during low-level fetch failures (#4842).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 1, 2024
1 parent 5aba496 commit 1c31f95
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
17 changes: 16 additions & 1 deletion src.ts/_tests/test-providers-ccip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Test CCIP execution", function() {
assert.equal(result, keccak256(check), "response is equal");
}

const address = "0xaeaa06a37e6421ac63120d6daddee0ffa04b43e8";
const address = "0xb66e9b20258712bfb9fd40acb13d7712ef149d6e";
const networkName = "sepolia";

const calldata = "0x1234";
Expand Down Expand Up @@ -176,6 +176,21 @@ describe("Test CCIP execution", function() {
verify(address, calldata, result);
});

it("testGetDeadHostFallback passes if any URL returns correctly", async function() {
this.timeout(60000);

const provider = connect(networkName);

// testGetDeadHostFallback(bytes callData = "0x1234")
const tx = {
to: address, enableCcipRead: true,
data: "0x0324be5a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000"
}

const result = await provider.call(tx);
verify(address, calldata, result);
});

it("testPost passes under normal operation", async function() {
this.timeout(60000);

Expand Down
29 changes: 20 additions & 9 deletions src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {

import type { Addressable, AddressLike } from "../address/index.js";
import type { BigNumberish, BytesLike } from "../utils/index.js";
import type { Listener } from "../utils/index.js";
import type { FetchResponse, Listener } from "../utils/index.js";

import type { Networkish } from "./network.js";
import type { FetchUrlFeeDataNetworkPlugin } from "./plugins-network.js";
Expand Down Expand Up @@ -604,15 +604,26 @@ export class AbstractProvider implements Provider {

let errorMessage = "unknown error";

const resp = await request.send();
// Fetch the resource...
let resp: FetchResponse;
try {
const result = resp.bodyJson;
if (result.data) {
this.emit("debug", { action: "receiveCcipReadFetchResult", request, result });
return result.data;
}
if (result.message) { errorMessage = result.message; }
this.emit("debug", { action: "receiveCcipReadFetchError", request, result });
resp = await request.send();
} catch (error: any) {
// ...low-level fetch error (missing host, bad SSL, etc.),
// so try next URL
errorMessages.push(error.message);
this.emit("debug", { action: "receiveCcipReadFetchError", request, result: { error } });
continue;
}

try {
const result = resp.bodyJson;
if (result.data) {
this.emit("debug", { action: "receiveCcipReadFetchResult", request, result });
return result.data;
}
if (result.message) { errorMessage = result.message; }
this.emit("debug", { action: "receiveCcipReadFetchError", request, result });
} catch (error) { }

// 4xx indicates the result is not present; stop
Expand Down

0 comments on commit 1c31f95

Please sign in to comment.