Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve relayer e2e test #1058

Merged
merged 15 commits into from
Jun 11, 2024
24 changes: 23 additions & 1 deletion e2e-contracts/integration/test/helpers/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,26 @@ export async function sendAndGetFullResponse(method: string, params: any[] = [])
export async function send(method: string, params: any[] = []): Promise<any> {
const response = await sendAndGetFullResponse(method, params);
return response.data.result;
}
}

// Sends an RPC request to the blockchain with retry logic, returning its result field.
export async function sendWithRetry(methodName: string, params: any[], maxAttempts = 5, delay = 2000) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const result = await send(methodName, params);
if (result !== null) {
return result;
}
if (attempt < maxAttempts) {
await new Promise(resolve => setTimeout(resolve, delay));
}
} catch (error) {
if (attempt < maxAttempts) {
await new Promise(resolve => setTimeout(resolve, delay));
} else {
throw error;
}
}
}
throw new Error(`Failed to get a non-null response from ${methodName} after ${maxAttempts} attempts.`);
}
32 changes: 24 additions & 8 deletions e2e-contracts/integration/test/relayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
setDeployer,
send,
deployer,
updateProviderUrl
updateProviderUrl,
sendWithRetry
} from "./helpers/rpc";

describe("Relayer integration test", function () {
Expand Down Expand Up @@ -81,23 +82,22 @@ describe("Relayer integration test", function () {

await new Promise(resolve => setTimeout(resolve, transactionInterval));
}
await new Promise(resolve => setTimeout(resolve, 2000));
});

it(`${params.name}: Validate transaction mined delay between Stratus and Hardhat`, async function () {
// Get Stratus timestamps
updateProviderUrl("stratus");
const stratusTimestamps = await Promise.all(txHashList.map(async (txHash) => {
const receipt = await send("eth_getTransactionReceipt", [txHash]);
const block = await send("eth_getBlockByNumber", [receipt.blockNumber, false]);
const receipt = await sendWithRetry("eth_getTransactionReceipt", [txHash]);
const block = await sendWithRetry("eth_getBlockByNumber", [receipt.blockNumber, false]);
return parseInt(block.timestamp, 16);
}));

// Get Hardhat timestamps
updateProviderUrl("hardhat");
const hardhatTimestamps = await Promise.all(txHashList.map(async (txHash) => {
const receipt = await send("eth_getTransactionReceipt", [txHash]);
const block = await send("eth_getBlockByNumber", [receipt.blockNumber, false]);
const receipt = await sendWithRetry("eth_getTransactionReceipt", [txHash]);
const block = await sendWithRetry("eth_getBlockByNumber", [receipt.blockNumber, false]);
return parseInt(block.timestamp, 16);
}));

Expand Down Expand Up @@ -165,6 +165,7 @@ describe("Relayer integration test", function () {
});

describe("Edge case transaction test", function () {
let txHashList: string[] = []
it("Back and forth transfer with minimum funds should order successfully", async function () {
const alice = ethers.Wallet.createRandom().connect(ethers.provider);
const bob = ethers.Wallet.createRandom().connect(ethers.provider);
Expand All @@ -181,11 +182,26 @@ describe("Relayer integration test", function () {
let sender = wallets[i % 2];
let receiver = wallets[(i + 1) % 2];

await brlcToken.connect(sender).transfer(receiver.address, 10, { gasPrice: 0, gasLimit: GAS_LIMIT_OVERRIDE, type: 0, nonce: nonces[i % 2] });
const tx = await brlcToken.connect(sender).transfer(receiver.address, 10, { gasPrice: 0, gasLimit: GAS_LIMIT_OVERRIDE, type: 0, nonce: nonces[i % 2] });

txHashList.push(tx.transactionHash);

nonces[i % 2]++;
}
await new Promise(resolve => setTimeout(resolve, 2000));

// Get Stratus transaction receipts
updateProviderUrl("stratus");
await Promise.all(txHashList.map(async (txHash) => {
const receipt = await sendWithRetry("eth_getTransactionReceipt", [txHash]);
return receipt;
}));

// Get Hardhat transaction receipts
updateProviderUrl("hardhat");
await Promise.all(txHashList.map(async (txHash) => {
const receipt = await sendWithRetry("eth_getTransactionReceipt", [txHash]);
return receipt;
}));
});

it("Validate no mismatched transactions were generated", async function () {
Expand Down