Skip to content

Commit

Permalink
Use Retry provider & Improve stability
Browse files Browse the repository at this point in the history
  • Loading branch information
wcgcyx committed Dec 19, 2023
1 parent 11359b3 commit 480d7da
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 27 deletions.
5 changes: 3 additions & 2 deletions scripts/bootstrap/0_pre_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, hasDuplicates } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

// The total supply of IMX
const TOTAL_SUPPLY = "2000000000";
Expand Down Expand Up @@ -66,8 +67,8 @@ async function run() {
requireEnv("RATE_LIMIT_GOG_REFILL_RATE");
requireEnv("RATE_LIMIT_GOG_LARGE_THRESHOLD");

const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
let deployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
3 changes: 2 additions & 1 deletion scripts/bootstrap/1_deployer_funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, waitForReceipt, getFee, hasDuplicates } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

async function run() {
console.log("=======Start Deployer Funding=======");
Expand All @@ -20,7 +21,7 @@ async function run() {
let passportDeployerFund = requireEnv("PASSPORT_NONCE_RESERVER_FUND");

// Get deployer address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
let childDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
5 changes: 3 additions & 2 deletions scripts/bootstrap/2_deployment_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as dotenv from "dotenv";
dotenv.config();
import { ethers } from "ethers";
import { requireEnv, hasDuplicates, requireNonEmptyCode } from "../helpers/helpers";
import { RetryProvider } from "../helpers/retry";

async function run() {
console.log("=======Start Deployment Validation=======");
Expand All @@ -26,8 +27,8 @@ async function run() {
throw("Duplicate address detected!");
}

const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));

// Check child chain.
console.log("Check contracts on child chain...");
Expand Down
3 changes: 2 additions & 1 deletion scripts/bootstrap/6_imx_burning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, hasDuplicates, waitForReceipt, getFee, getContract, getChildContracts } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

async function run() {
console.log("=======Start IMX Burning=======");
Expand All @@ -21,7 +22,7 @@ async function run() {
let childBridgeAddr = childContracts.CHILD_BRIDGE_ADDRESS;

// Get deployer address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
let childDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
5 changes: 3 additions & 2 deletions scripts/bootstrap/7_imx_rebalancing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, waitForReceipt, getFee, hasDuplicates, getChildContracts, getRootContracts } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

// The total supply of IMX
const TOTAL_SUPPLY = "2000000000";
Expand All @@ -30,8 +31,8 @@ async function run() {
let rootBridgeAddr = rootContracts.ROOT_BRIDGE_ADDRESS;

// Get deployer address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
let rootDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
3 changes: 2 additions & 1 deletion scripts/bootstrap/9_test_preparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers, utils } from "ethers";
import { deployRootContract, getContract, getRootContracts, requireEnv, saveRootContracts, waitForConfirmation, waitForReceipt } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

async function run() {
console.log("=======Start Test Preparation=======");
Expand All @@ -16,7 +17,7 @@ async function run() {
let rootPrivilegedMultisig = requireEnv("ROOT_PRIVILEGED_MULTISIG_ADDR");

// Get deployer address
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
let rootDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
3 changes: 2 additions & 1 deletion scripts/deploy/child_deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, deployChildContract, waitForReceipt, getFee, getChildContracts, getContract, saveChildContracts, verifyChildContract } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

export async function deployChildContracts() {
// Check environment variables
Expand All @@ -17,7 +18,7 @@ export async function deployChildContracts() {
// Read from contract file.
let childContracts = getChildContracts();

const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));

// Get deployer address
let childDeployerWallet;
Expand Down
3 changes: 2 additions & 1 deletion scripts/deploy/child_initialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, waitForReceipt, getFee, getContract, getChildContracts, getRootContracts } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

export async function initialiseChildContracts() {
let rootChainName = requireEnv("ROOT_CHAIN_NAME");
Expand Down Expand Up @@ -33,7 +34,7 @@ export async function initialiseChildContracts() {
}

// Get deployer address
const childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
const childProvider = new RetryProvider(childRPCURL, Number(childChainID));
let childDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
3 changes: 2 additions & 1 deletion scripts/deploy/root_deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers } from "ethers";
import { requireEnv, waitForConfirmation, deployRootContract, waitForReceipt, getRootContracts, getContract, saveRootContracts, verifyRootContract } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

export async function deployRootContracts() {
// Check environment variables
Expand All @@ -17,7 +18,7 @@ export async function deployRootContracts() {
// Read from contract file.
let rootContracts = getRootContracts();

const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));

// Get deployer address
let rootDeployerWallet;
Expand Down
3 changes: 2 additions & 1 deletion scripts/deploy/root_initialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers, utils } from "ethers";
import { requireEnv, waitForConfirmation, waitForReceipt, getContract, getChildContracts, getRootContracts } from "../helpers/helpers";
import { LedgerSigner } from "../helpers/ledger_signer";
import { RetryProvider } from "../helpers/retry";

export async function initialiseRootContracts() {
// Check environment variables
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function initialiseRootContracts() {
let rootTemplateAddr = rootContracts.ROOT_TOKEN_TEMPLATE;

// Get deployer address
const rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
const rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
let rootDeployerWallet;
if (deployerSecret == "ledger") {
let index = requireEnv("DEPLOYER_LEDGER_INDEX");
Expand Down
5 changes: 3 additions & 2 deletions scripts/e2e/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotenv.config();
import { ethers, providers } from "ethers";
import { requireEnv, waitForReceipt, getFee, getContract, delay, getChildContracts, getRootContracts, saveChildContracts, waitUntilSucceed } from "../helpers/helpers";
import { expect } from "chai";
import { RetryProvider } from "../helpers/retry";

// The contract ABI of IMX on L1.
const IMX_ABI = `[{"inputs":[{"internalType":"address","name":"minter","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MINTER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"cap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]`;
Expand Down Expand Up @@ -42,8 +43,8 @@ describe("Bridge e2e test", () => {
let rootBridgeAddr = rootContracts.ROOT_BRIDGE_ADDRESS;
let rootCustomTokenAddr = rootContracts.ROOT_TEST_CUSTOM_TOKEN;

rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
childProvider = new RetryProvider(childRPCURL, Number(childChainID));
rootTestWallet = new ethers.Wallet(testAccountKey, rootProvider);
childTestWallet = new ethers.Wallet(testAccountKey, childProvider);

Expand Down
24 changes: 16 additions & 8 deletions scripts/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ export async function verifyChildContract(contract: string, contractAddr: string
return;
}
let cmd = `forge verify-contract --verifier blockscout --verifier-url ${url} ${contractAddr} ${contract}`;
const { stdout, stderr } = await exec(cmd);
if (stderr != "") {
throw(stderr);
try {
const { stdout, stderr } = await exec(cmd);
if (stderr != "") {
console.log(stderr);
}
console.log(stdout);
} catch (e) {
console.log(e);
}
console.log(stdout);
}

export async function verifyRootContract(contract: string, contractAddr: string, args: string | null) {
Expand All @@ -215,9 +219,13 @@ export async function verifyRootContract(contract: string, contractAddr: string,
if (args != null) {
cmd += ` --constructor-args $(cast abi-encode ${args})`
}
const { stdout, stderr } = await exec(cmd);
if (stderr != "") {
throw(stderr);
try {
const { stdout, stderr } = await exec(cmd);
if (stderr != "") {
console.log(stderr);
}
console.log(stdout);
} catch (e) {
console.log(e);
}
console.log(stdout);
}
39 changes: 39 additions & 0 deletions scripts/helpers/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { providers, utils } from "ethers";

const MAX_ATTEMPT = 20;

export class RetryProvider extends providers.JsonRpcProvider {

constructor(
url?: utils.ConnectionInfo | string,
network?: providers.Networkish
) {
super(url, network);
}

public perform(method: string, params: any) {
let attempts = 0;
return utils.poll(() => {
if (attempts != 0) {
console.log("Retry RPC Request: " + attempts);
}
attempts++;
return super.perform(method, params)
.then(result => {
return result;
}, (error: any) => {
if (error.statusCode !== 429) {
return Promise.reject(error);
} else {
return Promise.resolve(undefined);
}
})
.catch(error => {
console.log(error);
return Promise.resolve(undefined);
})
}, {
retryLimit: MAX_ATTEMPT,
});
}
}
5 changes: 3 additions & 2 deletions scripts/localdev/axelar_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Network, networks, EvmRelayer, relay } from '@axelar-network/axelar-loc
import { requireEnv, waitForReceipt } from "../helpers/helpers";
import { ethers } from "ethers";
import * as fs from "fs";
import { RetryProvider } from "../helpers/retry";

let relaying = false;
const defaultEvmRelayer = new EvmRelayer();
Expand All @@ -20,7 +21,7 @@ async function main() {
let axelarDeployerKey = requireEnv("AXELAR_DEPLOYER_SECRET");

// Create root chain.
let rootProvider = new ethers.providers.JsonRpcProvider(rootRPCURL, Number(rootChainID));
let rootProvider = new RetryProvider(rootRPCURL, Number(rootChainID));
let rootChain = new Network();
rootChain.name = rootChainName;
rootChain.chainId = Number(rootChainID);
Expand All @@ -45,7 +46,7 @@ async function main() {
rootChain.lastExpressedBlock = rootChain.lastRelayedBlock;

// Create child chain.
let childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
let childProvider = new RetryProvider(childRPCURL, Number(childChainID));
let childChain = new Network();
childChain.name = childChainName;
childChain.chainId = Number(childChainID);
Expand Down
3 changes: 2 additions & 1 deletion scripts/localdev/childchain_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ dotenv.config();
import { ethers as hardhat } from "hardhat";
import { ethers } from "ethers";
import { requireEnv } from "../helpers/helpers";
import { RetryProvider } from "../helpers/retry";

async function main() {
let childRPCURL = requireEnv("CHILD_RPC_URL");
let childChainID = requireEnv("CHILD_CHAIN_ID");
let deployerAddr = requireEnv("DEPLOYER_ADDR");

// Get child provider.
let childProvider = new ethers.providers.JsonRpcProvider(childRPCURL, Number(childChainID));
let childProvider = new RetryProvider(childRPCURL, Number(childChainID));

// Give admin EOA account 2B IMX.
await hardhat.provider.send("hardhat_setBalance", [
Expand Down
Loading

0 comments on commit 480d7da

Please sign in to comment.