Skip to content

test: added bridge-tests for cases #79

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

Merged
merged 6 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ tags
.history
.ionide

# Ignore the .DS_Store file
.DS_Store

# Support for Project snippet scope
!.vscode/*.code-snippets

Expand Down
23 changes: 18 additions & 5 deletions contracts/deploy/01-foreign-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ const paramsByChainId = {
claimDeposit: parseEther("0.1"),
challengeDuration: 86400, // 1 day
homeChainId: 42161,
arbitrumInbox: "0x4Dbd4fc535Ac27206064B68FfCf827b0A60BAB3f",
},
4: {
claimDeposit: parseEther("0.1"),
challengeDuration: 120, // 2 min
homeChainId: 421611,
arbitrumInbox: "0x578BAde599406A8fE3d24Fd7f7211c0911F5B29e",
},
31337: {
claimDeposit: parseEther("0.1"),
challengeDuration: 120, // 2 min
homeChainId: 31337,
arbitrumInbox: "0x00",
},
};

Expand All @@ -50,26 +53,35 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme
let nonce;
if (chainId === ForeignChains.HARDHAT) {
nonce = await ethers.provider.getTransactionCount(deployer);
nonce += 4; // HomeGatewayToEthereum deploy tx will be the 6th after this, same network for both home/foreign.
nonce += 5; // HomeGatewayToEthereum deploy tx will be the 6th after this, same network for both home/foreign.
} else {
const homeChainProvider = new providers.JsonRpcProvider(homeNetworks[chainId].url);
nonce = await homeChainProvider.getTransactionCount(deployer);
nonce += 1; // HomeGatewayToEthereum deploy tx will the third tx after this on its home network, so we add two to the current nonce.
}
const { claimDeposit, challengeDuration, homeChainId } = paramsByChainId[chainId];
const { claimDeposit, challengeDuration, homeChainId, arbitrumInbox } = paramsByChainId[chainId];
const challengeDeposit = claimDeposit;
const bridgeAlpha = 5000;
const homeChainIdAsBytes32 = hexZeroPad(homeChainId, 32);

const homeGatewayAddress = getContractAddress(deployer, nonce);
console.log("calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);
nonce -= 1;

const fastBridgeSenderAddress = getContractAddress(deployer, nonce);
console.log("calculated future FastSender for nonce %d: %s", nonce, fastBridgeSenderAddress);

nonce += 5;

const inboxAddress = chainId === ForeignChains.HARDHAT ? getContractAddress(deployer, nonce) : arbitrumInbox;
console.log("calculated future inboxAddress for nonce %d: %s", nonce, inboxAddress);

const fastBridgeReceiver = await deploy("FastBridgeReceiverOnEthereum", {
from: deployer,
args: [
deployer,
ethers.constants.AddressZero, // should be safeBridgeSender
ethers.constants.AddressZero, // should be Arbitrum Inbox
fastBridgeSenderAddress,
inboxAddress,
claimDeposit,
challengeDeposit,
challengeDuration,
Expand All @@ -92,7 +104,8 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme

const metaEvidenceUri =
"https://raw.githubusercontent.com/kleros/kleros-v2/master/contracts/deployments/rinkeby/MetaEvidence_ArbitrableExample.json";
const arbitrable = await deploy("ArbitrableExample", {

await deploy("ArbitrableExample", {
from: deployer,
args: [foreignGateway.address, metaEvidenceUri],
log: true,
Expand Down
126 changes: 96 additions & 30 deletions contracts/deploy/02-home-chain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { Address } from "ethereumjs-util";
import { ethers } from "hardhat";

const HOME_CHAIN_IDS = [42161, 421611, 31337]; // ArbOne, ArbRinkeby, Hardhat

// TODO: use deterministic deployments

const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { deployments, getNamedAccounts, getChainId } = hre;
const { deploy, execute } = deployments;
Expand All @@ -14,35 +15,100 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
console.log("deployer: %s", deployer);

// The object below is not available when launching the hardhat node.
// TODO: use deterministic deployments
const fastBridgeReceiver =
chainId === 31337
? await deployments.get("FastBridgeReceiverOnEthereum")
: await hre.companionNetworks.foreign.deployments.get("FastBridgeReceiverOnEthereum");
const fastBridgeSender = await deploy("FastBridgeSenderToEthereum", {
from: deployer,
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero],
log: true,
}); // nonce+0

const klerosCore = await deployments.get("KlerosCore");
const foreignGateway =
chainId === 31337
? await deployments.get("ForeignGatewayOnEthereum")
: await hre.companionNetworks.foreign.deployments.get("ForeignGatewayOnEthereum");
const foreignChainId = chainId === 31337 ? 31337 : Number(await hre.companionNetworks.foreign.getChainId());
const homeGateway = await deploy("HomeGatewayToEthereum", {
from: deployer,
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
log: true,
}); // nonce+1

const fastSender = await hre.ethers
.getContractAt("FastBridgeSenderToEthereum", fastBridgeSender.address)
.then((contract) => contract.fastBridgeSender());
if (fastSender === ethers.constants.AddressZero) {
await execute("FastBridgeSenderToEthereum", { from: deployer, log: true }, "changeFastSender", homeGateway.address);
// ----------------------------------------------------------------------------------------------
const hardhatDeployer = async () => {
const fastBridgeReceiver = await deployments.get("FastBridgeReceiverOnEthereum");
const arbSysMock = await deploy("ArbSysMock", { from: deployer, log: true });

const fastBridgeSender = await deploy("FastBridgeSenderToEthereumMock", {
from: deployer,
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero, arbSysMock.address],
log: true,
}); // nonce+0

const klerosCore = await deployments.get("KlerosCore");
const foreignGateway = await deployments.get("ForeignGatewayOnEthereum");
const foreignChainId = 31337;

const homeGateway = await deploy("HomeGatewayToEthereum", {
from: deployer,
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
log: true,
}); // nonce+1

const fastSender = await hre.ethers
.getContractAt("FastBridgeSenderToEthereumMock", fastBridgeSender.address)
.then((contract) => contract.fastBridgeSender());

if (fastSender === ethers.constants.AddressZero) {
await execute(
"FastBridgeSenderToEthereumMock",
{
from: deployer,
log: true,
},
"changeFastSender",
homeGateway.address
);

const outbox = await deploy("OutboxMock", {
from: deployer,
args: [fastBridgeSender.address],
log: true,
});

const bridge = await deploy("BridgeMock", {
from: deployer,
args: [outbox.address],
log: true,
});

await deploy("InboxMock", {
from: deployer,
args: [bridge.address],
log: true,
});
}
};

// ----------------------------------------------------------------------------------------------
const liveDeployer = async () => {
const fastBridgeReceiver = await hre.companionNetworks.foreign.deployments.get("FastBridgeReceiverOnEthereum");

const fastBridgeSender = await deploy("FastBridgeSenderToEthereum", {
from: deployer,
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero],
log: true,
}); // nonce+0

const klerosCore = await deployments.get("KlerosCore");
const foreignGateway = await hre.companionNetworks.foreign.deployments.get("ForeignGatewayOnEthereum");
const foreignChainId = Number(await hre.companionNetworks.foreign.getChainId());
const homeGateway = await deploy("HomeGatewayToEthereum", {
from: deployer,
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
log: true,
}); // nonce+1

const fastSender = await hre.ethers
.getContractAt("FastBridgeSenderToEthereum", fastBridgeSender.address)
.then((contract) => contract.fastBridgeSender());

if (fastSender === ethers.constants.AddressZero) {
await execute(
"FastBridgeSenderToEthereum",
{ from: deployer, log: true },
"changeFastSender",
homeGateway.address
);
}
};

// ----------------------------------------------------------------------------------------------
if (chainId === 31337) {
await hardhatDeployer();
} else {
await liveDeployer();
}
};

Expand Down
25 changes: 25 additions & 0 deletions contracts/src/bridge/mock/ArbSysMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

/**
* @authors: [@hrishibhat]
* @reviewers: []
* @auditors: []
* @bounties: []
* @deployments: []
*/

pragma solidity ^0.8.0;

import "../interfaces/arbitrum/IArbSys.sol";

contract ArbSysMock {
function sendTxToL1(address destination, bytes calldata calldataForL1)
external
payable
returns (uint256 _withdrawal_ID)
{
(bool success, ) = address(destination).call(calldataForL1);
require(success, "Failed TxToL1");
return _withdrawal_ID;
}
}
52 changes: 52 additions & 0 deletions contracts/src/bridge/mock/BridgeMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT

/**
* @authors: [@hrishibhat]
* @reviewers: []
* @auditors: []
* @bounties: []
* @deployments: []
*/

pragma solidity ^0.8.0;

import "../interfaces/arbitrum/IInbox.sol";

contract BridgeMock is IBridge {
address public outbox;

constructor(address _outbox) {
outbox = _outbox;
}

function activeOutbox() external view returns (address _outbox) {
return address(outbox);
}

function deliverMessageToInbox(
uint8 kind,
address sender,
bytes32 messageDataHash
) external payable returns (uint256) {}

function executeCall(
address destAddr,
uint256 amount,
bytes calldata data
) external returns (bool success, bytes memory returnData) {}

// These are only callable by the admin
function setInbox(address inbox, bool enabled) external {}

function setOutbox(address inbox, bool enabled) external {}

// View functions

function allowedInboxes(address inbox) external view returns (bool) {}

function allowedOutboxes(address outbox) external view returns (bool) {}

function inboxAccs(uint256 index) external view returns (bytes32) {}

function messageCount() external view returns (uint256) {}
}
Loading