From 08561c19ab0e2f73ed52fc6f590444fd5b9a3613 Mon Sep 17 00:00:00 2001 From: Sam Jeston Date: Thu, 27 Jun 2024 15:22:19 +1000 Subject: [PATCH] fix: workaround multi-job flake in functional test suite (#1948) --- .../zkevm/step-definitions/shared.ts | 65 ++++++++++++++----- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/tests/func-tests/zkevm/step-definitions/shared.ts b/tests/func-tests/zkevm/step-definitions/shared.ts index 9380e4c31c..6a02582541 100644 --- a/tests/func-tests/zkevm/step-definitions/shared.ts +++ b/tests/func-tests/zkevm/step-definitions/shared.ts @@ -15,18 +15,42 @@ const imxForApproval = 0.03 * 1e18; const imxForFulfillment = 0.08 * 1e18; const listingPrice = 0.0001 * 1e18; +// Workaround to retry banker on-chain actions which can race with test runs on other PRs +// eslint-disable-next-line consistent-return +async function withBankerRetry(func: () => Promise, attempt = 1): Promise { + try { + await func(); + } catch (e) { + if (attempt > 5) { + throw e; + } + + // 1 block baseline wait + const waitTime = 2_000; + + // jitter between block * retry count + const jitter = Math.random() * waitTime * attempt; + + // eslint-disable-next-line no-promise-executor-return + await new Promise((resolve) => setTimeout(resolve, waitTime + jitter)); + return withBankerRetry(func, attempt + 1); + } +} + export const givenIHaveAFundedOffererAccount = ( given: DefineStepFunction, banker: Wallet, offerer: Wallet, ) => { given(/^I have a funded offerer account$/, async () => { - const fundingTx = await banker.sendTransaction({ - to: offerer.address, - value: `${imxForApproval}`, - ...GAS_OVERRIDES, + await withBankerRetry(async () => { + const fundingTx = await banker.sendTransaction({ + to: offerer.address, + value: `${imxForApproval}`, + ...GAS_OVERRIDES, + }); + await fundingTx.wait(1); }); - await fundingTx.wait(1); }); }; @@ -41,9 +65,12 @@ export const andTheOffererAccountHasERC721Token = ( const testToken = await connectToTestERC721Token(banker, contractAddress); for (const tokenId of tokenIds) { // eslint-disable-next-line no-await-in-loop - const mintTx = await testToken.mint(offerer.address, tokenId, GAS_OVERRIDES); - // eslint-disable-next-line no-await-in-loop - await mintTx.wait(1); + await withBankerRetry(async () => { + // eslint-disable-next-line no-await-in-loop + const mintTx = await testToken.mint(offerer.address, tokenId, GAS_OVERRIDES); + // eslint-disable-next-line no-await-in-loop + await mintTx.wait(1); + }); } }); }; @@ -56,9 +83,11 @@ export const andTheOffererAccountHasERC1155Tokens = ( tokenId: string, ) => { and(/^the offerer account has (\d+) ERC1155 tokens$/, async (amount) => { - const testToken = await connectToTestERC1155Token(banker, contractAddress); - const mintTx = await testToken.safeMint(offerer.address, tokenId, amount, '0x', GAS_OVERRIDES); - await mintTx.wait(1); + await withBankerRetry(async () => { + const testToken = await connectToTestERC1155Token(banker, contractAddress); + const mintTx = await testToken.safeMint(offerer.address, tokenId, amount, '0x', GAS_OVERRIDES); + await mintTx.wait(1); + }); }); }; @@ -68,13 +97,15 @@ export const andIHaveAFundedFulfillerAccount = ( fulfiller: Wallet, ) => { and(/^I have a funded fulfiller account$/, async () => { - const fundingTx = await banker.sendTransaction({ - to: fulfiller.address, - value: `${(listingPrice + imxForFulfillment)}`, - ...GAS_OVERRIDES, - }); + await withBankerRetry(async () => { + const fundingTx = await banker.sendTransaction({ + to: fulfiller.address, + value: `${(listingPrice + imxForFulfillment)}`, + ...GAS_OVERRIDES, + }); - await fundingTx.wait(1); + await fundingTx.wait(1); + }); }); };