Skip to content

Commit

Permalink
Refactor timeout and retry (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin authored Oct 16, 2023
1 parent 3e864ff commit 390a8de
Show file tree
Hide file tree
Showing 57 changed files with 913 additions and 859 deletions.
21 changes: 9 additions & 12 deletions connect/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ describe("Wormhole Tests", () => {
expect(vaa).toBeUndefined();
});

// TODO: Fix this test
//test("returns after first try fails", async () => {
// publicRpcMock.givenSignedVaaRequestWorksAfterRetry();
// const vaa = await wh.getVAABytes(
// "Base",
// testing.utils.makeChainAddress("Base").address,
// 1n,
// 5,
// );
// expect(vaa).toBeDefined();
//});
test("returns after first try fails", async () => {
publicRpcMock.givenSignedVaaRequestWorksAfterRetry();
const vaa = await wh.getVAABytes(
"Base",
testing.utils.makeChainAddress("Base").address,
1n,
);
expect(vaa).toBeDefined();
});
});
});

Expand Down Expand Up @@ -98,7 +96,6 @@ describe("Chain Tests", () => {
let c: ChainContext<PlatformName>;
beforeEach(() => {
const wh = new Wormhole(network, allPlatformCtrs);
const p = wh.getPlatform("Ethereum");
c = wh.getChain("Ethereum");
});

Expand Down
63 changes: 0 additions & 63 deletions connect/src/api.ts

This file was deleted.

14 changes: 6 additions & 8 deletions connect/src/circle-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ const mapCircleAttestation = (
export async function getCircleAttestation(
circleApi: string,
msgHash: string,
): Promise<Attestation | null> {
): Promise<string | null> {
const url = `${circleApi}/${msgHash}`;
try {
const response = await axios.get<CircleAttestationResponse>(url);
const attestation = mapCircleAttestation(response?.data);

// Found but still pending
if (attestation.message === "PENDING") return null;

return attestation;
return attestation.message === "PENDING" ? null : attestation.message;
} catch (error) {
// This is a 404 error, which means the attestation is not yet available
// since its not available yet, we return null signaling it can be tried again
if (!(axios.isAxiosError(error) && error?.response?.status === 404)) {
console.error(error);
return null;
}
return null;
throw error;
}
}
40 changes: 40 additions & 0 deletions connect/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
ChainContext,
Signer,
TransactionId,
TxHash,
UnsignedTransaction,
} from "@wormhole-foundation/sdk-definitions";
import { PlatformName } from "@wormhole-foundation/sdk-base";

export async function signSendWait(
chain: ChainContext<PlatformName>,
xfer: AsyncGenerator<UnsignedTransaction>,
signer: Signer,
): Promise<TransactionId[]> {
// buffer unsigned transactions as long as they are
// marked as parallelizable
let txbuff: UnsignedTransaction[] = [];

const txHashes: TxHash[] = [];
for await (const tx of xfer) {
txbuff.push(tx);

if (!tx.parallelizable) {
// sign/send
const signed = await signer.sign(txbuff);
const txids = await chain.sendWait(signed);
txHashes.push(...txids);
// reset buffer
txbuff = [];
}
}

if (txbuff.length > 0) {
const signed = await signer.sign(txbuff);
const txids = await chain.sendWait(signed);
txHashes.push(...txids);
}

return txHashes.map((txid) => ({ chain: chain.chain, txid }));
}
15 changes: 9 additions & 6 deletions connect/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
constMap,
circleAPI,
PlatformName,
blockTime,
} from "@wormhole-foundation/sdk-base";
import { WormholeConfig } from "./types";
import {
Expand All @@ -19,20 +20,22 @@ import {
ChainsConfig,
} from "@wormhole-foundation/sdk-definitions";

/*
TODO:
add missing chains for each config
Note: the false exclamation marks here are because not every chain
is represented in the consts we're pulling from yet
*/
export const DEFAULT_TASK_TIMEOUT = 60 * 1000; // 1 minute in milliseconds

export const CIRCLE_RETRY_INTERVAL = 2000;
export const WHSCAN_RETRY_INTERVAL = 2000;

// TODO: add missing chains for each config
function combineConfig(n: Network): ChainsConfig {
const cc: ChainsConfig = chains
.map((c: ChainName): ChainConfig => {
const platform = chainToPlatform(c);
return {
key: c,
platform,
network: n,
finalityThreshold: finalityThreshold.get(n, c) || 0,
blockTime: blockTime(c),
contracts: getContracts(n, c),
nativeTokenDecimals: nativeDecimals.get(platform)!, //TODO the exclamation mark is a lie
explorer: explorerConfigs(n, c)!, //TODO the exclamation mark is a lie
Expand Down
2 changes: 1 addition & 1 deletion connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from "./protocols/cctpTransfer";
export * from "./protocols/gatewayTransfer";

export * as circle from "./circle-api";
export * as api from "./api";
export * as api from "./whscan-api";

// Re-export from core packages
export {
Expand Down
Loading

0 comments on commit 390a8de

Please sign in to comment.