Skip to content

Commit

Permalink
Add the ability to test with two local chains
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Mar 26, 2024
1 parent 8f36b9b commit e7bcd71
Show file tree
Hide file tree
Showing 31 changed files with 1,852 additions and 300 deletions.
3 changes: 3 additions & 0 deletions packages/validator/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,6 @@ typechain-types
cache
artifacts

/**/*/chain1/node/*
/**/*/chain2/node/*
.openzeppelin/
12 changes: 12 additions & 0 deletions packages/validator/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@ DATABASE_PORT=5432

# 0x6F8CF905906dDe9E440F0DF5B26146cf1f195F12
DEPLOYER=0x0d451ab5bd459f59fda0cf8018cfbfa4df5c454c5c25a844b70a5afcaf246c98
# 0x6629E54deC62B88b34d6fF7439B290cC0f1bfaEC
FEE=0xcfefe53d3c74cda9aa7b0af55d0b615cdcf7d4ed3b8653fb87fb2259f083f007
# 0xF99D11fa9E430005b2cEE0f671d4dA79868D9570
BRIDGE_VALIDATOR1=0xba07977ad7f97189551f9f119a9f92413cec54d456921f249c71ec39a0c04925
# 0x6F8CF905906dDe9E440F0DF5B26146cf1f195F12
BRIDGE_VALIDATOR2=0x0d451ab5bd459f59fda0cf8018cfbfa4df5c454c5c25a844b70a5afcaf246c98
# 0x414E20AfA67289f119d3Edf05F27a7832cEc738b
BRIDGE_VALIDATOR3=0x643600c786cff4ba0c5e0d8d251d2c83062b043d324b0241bcff3bddacf92399
# 0x98F74031045b809a9078f43A51601c51Acf7B042
BRIDGE_VALIDATOR4=0x0ff87ad864e2ad510f1a09fdef0b087f67e0b19e1b4ef66f81acdafde0dcbd2c
# 0xe3231Cda6B5be01f7C52B39cF05637A9c5Aee98A
BRIDGE_VALIDATOR5=0x6b56aac382c8b18d57e3451f11077e260cf0d242c4d44251a3b01bfc65fc08f9

REPORT_GAS=true
9 changes: 9 additions & 0 deletions packages/validator/hardhat-change-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { EthereumProvider } from "hardhat/types/provider";
// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
import "hardhat/types/runtime";
import { EthersProviderWrapper } from "@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper";
declare module "hardhat/types/runtime" {
interface HardhatRuntimeEnvironment {
changeNetwork(newNetwork: string): Promise<void>;
getProvider(newNetwork: string): Promise<EthereumProvider>;
getChainId(newNetwork: string): number;
}
}

Expand All @@ -19,6 +21,7 @@ extendEnvironment((hre) => {
// We use lazyObject to avoid initializing things until they are actually
// needed.
const providers: { [name: string]: EthereumProvider } = {};
const chainIds: { [name: string]: number } = {};

hre.getProvider = async function getProvider(name: string): Promise<EthereumProvider> {
if (!providers[name]) {
Expand All @@ -44,14 +47,20 @@ extendEnvironment((hre) => {
if (newNetwork === "hardhat") {
const { EthersProviderWrapper } = require("@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper");
(this as any).ethers.provider = new EthersProviderWrapper(this.network.provider);
chainIds[newNetwork] = (await (this as any).ethers.provider.getNetwork()).chainId;
} else {
const httpNetConfig = this.config.networks[newNetwork] as HttpNetworkConfig;
const chainId = httpNetConfig.chainId || 0;
(this as any).ethers.provider = new JsonRpcProvider(httpNetConfig.url, {
name: newNetwork,
chainId,
}) as JsonRpcProvider;
chainIds[newNetwork] = chainId;
}
}
};

hre.getChainId = function getChainId(name: string): number {
return chainIds[name];
};
});
150 changes: 150 additions & 0 deletions packages/validator/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,146 @@ function getAccounts() {
accounts.push(process.env.BRIDGE_VALIDATOR5);
}

if (process.env.KEY01 !== undefined && process.env.KEY01.trim() !== "" && reg_bytes64.test(process.env.KEY01)) {
accounts.push(process.env.KEY01);
} else {
process.env.KEY01 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY01);
}

if (process.env.KEY02 !== undefined && process.env.KEY02.trim() !== "" && reg_bytes64.test(process.env.KEY02)) {
accounts.push(process.env.KEY02);
} else {
process.env.KEY02 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY02);
}

if (process.env.KEY03 !== undefined && process.env.KEY03.trim() !== "" && reg_bytes64.test(process.env.KEY03)) {
accounts.push(process.env.KEY03);
} else {
process.env.KEY03 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY03);
}

if (process.env.KEY04 !== undefined && process.env.KEY04.trim() !== "" && reg_bytes64.test(process.env.KEY04)) {
accounts.push(process.env.KEY04);
} else {
process.env.KEY04 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY04);
}

if (process.env.KEY05 !== undefined && process.env.KEY05.trim() !== "" && reg_bytes64.test(process.env.KEY05)) {
accounts.push(process.env.KEY05);
} else {
process.env.KEY05 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY05);
}

if (process.env.KEY06 !== undefined && process.env.KEY06.trim() !== "" && reg_bytes64.test(process.env.KEY06)) {
accounts.push(process.env.KEY06);
} else {
process.env.KEY06 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY06);
}

if (process.env.KEY07 !== undefined && process.env.KEY07.trim() !== "" && reg_bytes64.test(process.env.KEY07)) {
accounts.push(process.env.KEY07);
} else {
process.env.KEY07 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY07);
}

if (process.env.KEY08 !== undefined && process.env.KEY08.trim() !== "" && reg_bytes64.test(process.env.KEY08)) {
accounts.push(process.env.KEY08);
} else {
process.env.KEY08 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY08);
}

if (process.env.KEY09 !== undefined && process.env.KEY09.trim() !== "" && reg_bytes64.test(process.env.KEY09)) {
accounts.push(process.env.KEY09);
} else {
process.env.KEY09 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY09);
}

if (process.env.KEY10 !== undefined && process.env.KEY10.trim() !== "" && reg_bytes64.test(process.env.KEY10)) {
accounts.push(process.env.KEY10);
} else {
process.env.KEY10 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY10);
}

if (process.env.KEY11 !== undefined && process.env.KEY11.trim() !== "" && reg_bytes64.test(process.env.KEY11)) {
accounts.push(process.env.KEY11);
} else {
process.env.KEY11 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY11);
}

if (process.env.KEY12 !== undefined && process.env.KEY12.trim() !== "" && reg_bytes64.test(process.env.KEY12)) {
accounts.push(process.env.KEY12);
} else {
process.env.KEY12 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY12);
}

if (process.env.KEY13 !== undefined && process.env.KEY13.trim() !== "" && reg_bytes64.test(process.env.KEY13)) {
accounts.push(process.env.KEY13);
} else {
process.env.KEY13 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY13);
}

if (process.env.KEY14 !== undefined && process.env.KEY14.trim() !== "" && reg_bytes64.test(process.env.KEY14)) {
accounts.push(process.env.KEY14);
} else {
process.env.KEY14 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY14);
}

if (process.env.KEY15 !== undefined && process.env.KEY15.trim() !== "" && reg_bytes64.test(process.env.KEY15)) {
accounts.push(process.env.KEY15);
} else {
process.env.KEY15 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY15);
}

if (process.env.KEY16 !== undefined && process.env.KEY16.trim() !== "" && reg_bytes64.test(process.env.KEY16)) {
accounts.push(process.env.KEY16);
} else {
process.env.KEY16 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY16);
}

if (process.env.KEY17 !== undefined && process.env.KEY17.trim() !== "" && reg_bytes64.test(process.env.KEY17)) {
accounts.push(process.env.KEY17);
} else {
process.env.KEY17 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY17);
}

if (process.env.KEY18 !== undefined && process.env.KEY18.trim() !== "" && reg_bytes64.test(process.env.KEY18)) {
accounts.push(process.env.KEY18);
} else {
process.env.KEY18 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY18);
}

if (process.env.KEY19 !== undefined && process.env.KEY19.trim() !== "" && reg_bytes64.test(process.env.KEY19)) {
accounts.push(process.env.KEY19);
} else {
process.env.KEY19 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY19);
}

if (process.env.KEY20 !== undefined && process.env.KEY20.trim() !== "" && reg_bytes64.test(process.env.KEY20)) {
accounts.push(process.env.KEY20);
} else {
process.env.KEY20 = Wallet.createRandom().privateKey;
accounts.push(process.env.KEY20);
}

while (accounts.length < 100) {
accounts.push(Wallet.createRandom().privateKey);
}
Expand Down Expand Up @@ -154,6 +294,16 @@ const config = {
chainId: 24680,
accounts: getAccounts(),
},
chain1: {
url: "http://localhost:8541",
chainId: 215191,
accounts: getAccounts(),
},
chain2: {
url: "http://localhost:8542",
chainId: 215192,
accounts: getAccounts(),
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"start": "TESTING=false NODE_ENV=production hardhat run src/main.ts --network production_net",
"formatting:check": "prettier '**/*.{json,sol,ts,js,yaml}' -c",
"formatting:write": "prettier '**/*.{json,sol,ts,js,yaml}' --write",
"test:local": "test-local/start-test.sh",
"test:01-Collector": "TESTING=true hardhat test test/01-Collector.test.ts",
"test:02-Bridge": "TESTING=true hardhat test test/02-Bridge.test.ts"
},
Expand Down
49 changes: 0 additions & 49 deletions packages/validator/scripts/exchange.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/validator/scripts/sync.ts

This file was deleted.

46 changes: 30 additions & 16 deletions packages/validator/src/scheduler/EventCollector.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { IBridge__factory } from "../../typechain-types";
import { IBridgeInterface } from "../../typechain-types/dms-bridge-contracts/contracts/interfaces/IBridge";
import { ValidatorStorage } from "../storage/ValidatorStorage";

import { BigNumber, Wallet } from "ethers";
import * as hre from "hardhat";
import { logger } from "../common/Logger";
import { ValidatorType } from "../types";
import { ValidatorType, WithdrawStatus } from "../types";

import { Provider } from "@ethersproject/providers";

export class EventCollector {
private wallet: Wallet;
private readonly type: ValidatorType;
private readonly network: string;
private readonly contractAddress: string;
private readonly startNumber: bigint;
private contract: any;
private storage: ValidatorStorage;
private provider: Provider | undefined;
private interfaceOfBridge: IBridgeInterface | undefined;

constructor(
storage: ValidatorStorage,
Expand All @@ -31,15 +35,18 @@ export class EventCollector {
this.wallet = wallet;
}

private async getLatestBlockNumber(): Promise<bigint> {
const block = await hre.ethers.provider.getBlock("latest");
return BigInt(block.number);
}

public async work() {
await hre.changeNetwork(this.network);
if (this.provider === undefined) {
await hre.changeNetwork(this.network);
this.provider = hre.ethers.provider;
}

const latestBlockNumber = await this.getLatestBlockNumber();
if (this.interfaceOfBridge === undefined) {
this.interfaceOfBridge = IBridge__factory.createInterface();
}

const block = await this.provider.getBlock("latest");
const latestBlockNumber = BigInt(block.number);

let from: BigInt;
const latestCollectedNumber = await this.storage.getLatestNumber(this.wallet.address, this.type, this.network);
Expand All @@ -50,21 +57,28 @@ export class EventCollector {
if (from > latestBlockNumber) from = this.startNumber;
}

this.contract = new hre.web3.eth.Contract(IBridge__factory.abi as any, this.contractAddress);
const events = await this.contract.getPastEvents("BridgeDeposited", {
const filters = [this.interfaceOfBridge.getEventTopic("BridgeDeposited")];

const logs = await this.provider.getLogs({
fromBlock: Number(from),
toBlock: Number(latestBlockNumber),
address: this.contractAddress,
topics: filters,
});

const depositEvents = events.map((m: any) => {
const iface = this.interfaceOfBridge;
const depositEvents = logs.map((m: any) => {
const event = iface.parseLog(m);
return {
network: this.network,
tokenId: m.returnValues.tokenId,
depositId: m.returnValues.depositId,
account: m.returnValues.account,
amount: BigNumber.from(m.returnValues.amount),
tokenId: event.args.tokenId,
depositId: event.args.depositId,
account: event.args.account,
amount: BigNumber.from(event.args.amount),
blockNumber: BigInt(m.blockNumber),
transactionHash: m.transactionHash,
withdrawStatus: WithdrawStatus.None,
withdrawTimestamp: 0n,
};
});

Expand Down
Loading

0 comments on commit e7bcd71

Please sign in to comment.