Skip to content

Commit 097baa7

Browse files
authored
Merge pull request #79 from kleros/feat/bridge-tests
test: added bridge-tests for cases
2 parents 98d903c + 5544d12 commit 097baa7

File tree

9 files changed

+770
-58
lines changed

9 files changed

+770
-58
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ tags
173173
.history
174174
.ionide
175175

176+
# Ignore the .DS_Store file
177+
.DS_Store
178+
176179
# Support for Project snippet scope
177180
!.vscode/*.code-snippets
178181

contracts/deploy/01-foreign-chain.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ const paramsByChainId = {
1515
claimDeposit: parseEther("0.1"),
1616
challengeDuration: 86400, // 1 day
1717
homeChainId: 42161,
18+
arbitrumInbox: "0x4Dbd4fc535Ac27206064B68FfCf827b0A60BAB3f",
1819
},
1920
4: {
2021
claimDeposit: parseEther("0.1"),
2122
challengeDuration: 120, // 2 min
2223
homeChainId: 421611,
24+
arbitrumInbox: "0x578BAde599406A8fE3d24Fd7f7211c0911F5B29e",
2325
},
2426
31337: {
2527
claimDeposit: parseEther("0.1"),
2628
challengeDuration: 120, // 2 min
2729
homeChainId: 31337,
30+
arbitrumInbox: "0x00",
2831
},
2932
};
3033

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

6467
const homeGatewayAddress = getContractAddress(deployer, nonce);
6568
console.log("calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);
69+
nonce -= 1;
70+
71+
const fastBridgeSenderAddress = getContractAddress(deployer, nonce);
72+
console.log("calculated future FastSender for nonce %d: %s", nonce, fastBridgeSenderAddress);
73+
74+
nonce += 5;
75+
76+
const inboxAddress = chainId === ForeignChains.HARDHAT ? getContractAddress(deployer, nonce) : arbitrumInbox;
77+
console.log("calculated future inboxAddress for nonce %d: %s", nonce, inboxAddress);
6678

6779
const fastBridgeReceiver = await deploy("FastBridgeReceiverOnEthereum", {
6880
from: deployer,
6981
args: [
7082
deployer,
71-
ethers.constants.AddressZero, // should be safeBridgeSender
72-
ethers.constants.AddressZero, // should be Arbitrum Inbox
83+
fastBridgeSenderAddress,
84+
inboxAddress,
7385
claimDeposit,
7486
challengeDeposit,
7587
challengeDuration,
@@ -92,7 +104,8 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme
92104

93105
const metaEvidenceUri =
94106
"https://raw.githubusercontent.com/kleros/kleros-v2/master/contracts/deployments/rinkeby/MetaEvidence_ArbitrableExample.json";
95-
const arbitrable = await deploy("ArbitrableExample", {
107+
108+
await deploy("ArbitrableExample", {
96109
from: deployer,
97110
args: [foreignGateway.address, metaEvidenceUri],
98111
log: true,

contracts/deploy/02-home-chain.ts

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { HardhatRuntimeEnvironment } from "hardhat/types";
22
import { DeployFunction } from "hardhat-deploy/types";
3-
import { Address } from "ethereumjs-util";
43
import { ethers } from "hardhat";
54

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

7+
// TODO: use deterministic deployments
8+
89
const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
910
const { deployments, getNamedAccounts, getChainId } = hre;
1011
const { deploy, execute } = deployments;
@@ -14,35 +15,100 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
1415
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
1516
console.log("deployer: %s", deployer);
1617

17-
// The object below is not available when launching the hardhat node.
18-
// TODO: use deterministic deployments
19-
const fastBridgeReceiver =
20-
chainId === 31337
21-
? await deployments.get("FastBridgeReceiverOnEthereum")
22-
: await hre.companionNetworks.foreign.deployments.get("FastBridgeReceiverOnEthereum");
23-
const fastBridgeSender = await deploy("FastBridgeSenderToEthereum", {
24-
from: deployer,
25-
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero],
26-
log: true,
27-
}); // nonce+0
28-
29-
const klerosCore = await deployments.get("KlerosCore");
30-
const foreignGateway =
31-
chainId === 31337
32-
? await deployments.get("ForeignGatewayOnEthereum")
33-
: await hre.companionNetworks.foreign.deployments.get("ForeignGatewayOnEthereum");
34-
const foreignChainId = chainId === 31337 ? 31337 : Number(await hre.companionNetworks.foreign.getChainId());
35-
const homeGateway = await deploy("HomeGatewayToEthereum", {
36-
from: deployer,
37-
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
38-
log: true,
39-
}); // nonce+1
40-
41-
const fastSender = await hre.ethers
42-
.getContractAt("FastBridgeSenderToEthereum", fastBridgeSender.address)
43-
.then((contract) => contract.fastBridgeSender());
44-
if (fastSender === ethers.constants.AddressZero) {
45-
await execute("FastBridgeSenderToEthereum", { from: deployer, log: true }, "changeFastSender", homeGateway.address);
18+
// ----------------------------------------------------------------------------------------------
19+
const hardhatDeployer = async () => {
20+
const fastBridgeReceiver = await deployments.get("FastBridgeReceiverOnEthereum");
21+
const arbSysMock = await deploy("ArbSysMock", { from: deployer, log: true });
22+
23+
const fastBridgeSender = await deploy("FastBridgeSenderToEthereumMock", {
24+
from: deployer,
25+
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero, arbSysMock.address],
26+
log: true,
27+
}); // nonce+0
28+
29+
const klerosCore = await deployments.get("KlerosCore");
30+
const foreignGateway = await deployments.get("ForeignGatewayOnEthereum");
31+
const foreignChainId = 31337;
32+
33+
const homeGateway = await deploy("HomeGatewayToEthereum", {
34+
from: deployer,
35+
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
36+
log: true,
37+
}); // nonce+1
38+
39+
const fastSender = await hre.ethers
40+
.getContractAt("FastBridgeSenderToEthereumMock", fastBridgeSender.address)
41+
.then((contract) => contract.fastBridgeSender());
42+
43+
if (fastSender === ethers.constants.AddressZero) {
44+
await execute(
45+
"FastBridgeSenderToEthereumMock",
46+
{
47+
from: deployer,
48+
log: true,
49+
},
50+
"changeFastSender",
51+
homeGateway.address
52+
);
53+
54+
const outbox = await deploy("OutboxMock", {
55+
from: deployer,
56+
args: [fastBridgeSender.address],
57+
log: true,
58+
});
59+
60+
const bridge = await deploy("BridgeMock", {
61+
from: deployer,
62+
args: [outbox.address],
63+
log: true,
64+
});
65+
66+
await deploy("InboxMock", {
67+
from: deployer,
68+
args: [bridge.address],
69+
log: true,
70+
});
71+
}
72+
};
73+
74+
// ----------------------------------------------------------------------------------------------
75+
const liveDeployer = async () => {
76+
const fastBridgeReceiver = await hre.companionNetworks.foreign.deployments.get("FastBridgeReceiverOnEthereum");
77+
78+
const fastBridgeSender = await deploy("FastBridgeSenderToEthereum", {
79+
from: deployer,
80+
args: [deployer, fastBridgeReceiver.address, ethers.constants.AddressZero],
81+
log: true,
82+
}); // nonce+0
83+
84+
const klerosCore = await deployments.get("KlerosCore");
85+
const foreignGateway = await hre.companionNetworks.foreign.deployments.get("ForeignGatewayOnEthereum");
86+
const foreignChainId = Number(await hre.companionNetworks.foreign.getChainId());
87+
const homeGateway = await deploy("HomeGatewayToEthereum", {
88+
from: deployer,
89+
args: [klerosCore.address, fastBridgeSender.address, foreignGateway.address, foreignChainId],
90+
log: true,
91+
}); // nonce+1
92+
93+
const fastSender = await hre.ethers
94+
.getContractAt("FastBridgeSenderToEthereum", fastBridgeSender.address)
95+
.then((contract) => contract.fastBridgeSender());
96+
97+
if (fastSender === ethers.constants.AddressZero) {
98+
await execute(
99+
"FastBridgeSenderToEthereum",
100+
{ from: deployer, log: true },
101+
"changeFastSender",
102+
homeGateway.address
103+
);
104+
}
105+
};
106+
107+
// ----------------------------------------------------------------------------------------------
108+
if (chainId === 31337) {
109+
await hardhatDeployer();
110+
} else {
111+
await liveDeployer();
46112
}
47113
};
48114

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
/**
4+
* @authors: [@hrishibhat]
5+
* @reviewers: []
6+
* @auditors: []
7+
* @bounties: []
8+
* @deployments: []
9+
*/
10+
11+
pragma solidity ^0.8.0;
12+
13+
import "../interfaces/arbitrum/IArbSys.sol";
14+
15+
contract ArbSysMock {
16+
function sendTxToL1(address destination, bytes calldata calldataForL1)
17+
external
18+
payable
19+
returns (uint256 _withdrawal_ID)
20+
{
21+
(bool success, ) = address(destination).call(calldataForL1);
22+
require(success, "Failed TxToL1");
23+
return _withdrawal_ID;
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
/**
4+
* @authors: [@hrishibhat]
5+
* @reviewers: []
6+
* @auditors: []
7+
* @bounties: []
8+
* @deployments: []
9+
*/
10+
11+
pragma solidity ^0.8.0;
12+
13+
import "../interfaces/arbitrum/IInbox.sol";
14+
15+
contract BridgeMock is IBridge {
16+
address public outbox;
17+
18+
constructor(address _outbox) {
19+
outbox = _outbox;
20+
}
21+
22+
function activeOutbox() external view returns (address _outbox) {
23+
return address(outbox);
24+
}
25+
26+
function deliverMessageToInbox(
27+
uint8 kind,
28+
address sender,
29+
bytes32 messageDataHash
30+
) external payable returns (uint256) {}
31+
32+
function executeCall(
33+
address destAddr,
34+
uint256 amount,
35+
bytes calldata data
36+
) external returns (bool success, bytes memory returnData) {}
37+
38+
// These are only callable by the admin
39+
function setInbox(address inbox, bool enabled) external {}
40+
41+
function setOutbox(address inbox, bool enabled) external {}
42+
43+
// View functions
44+
45+
function allowedInboxes(address inbox) external view returns (bool) {}
46+
47+
function allowedOutboxes(address outbox) external view returns (bool) {}
48+
49+
function inboxAccs(uint256 index) external view returns (bytes32) {}
50+
51+
function messageCount() external view returns (uint256) {}
52+
}

0 commit comments

Comments
 (0)