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

[SC-97] Moving Files from dss-test #1

Merged
merged 7 commits into from
Aug 31, 2023
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
67 changes: 2 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,3 @@
## Foundry
# xchain-helpers

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
This repository contains tooling for multichain testing such as cross-chain e2e testing support.
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

189 changes: 189 additions & 0 deletions src/ArbitrumDomain.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// SPDX-FileCopyrightText: © 2022 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";
import { StdChains } from "forge-std/StdChains.sol";

import { Domain, BridgedDomain } from "./BridgedDomain.sol";
import { RecordedLogs } from "./RecordedLogs.sol";

interface InboxLike {
function createRetryableTicket(
address destAddr,
uint256 arbTxCallValue,
uint256 maxSubmissionCost,
address submissionRefundAddress,
address valueRefundAddress,
uint256 maxGas,
uint256 gasPriceBid,
bytes calldata data
) external payable returns (uint256);
function bridge() external view returns (address);
}

interface BridgeLike {
function rollup() external view returns (address);
function executeCall(
address,
uint256,
bytes calldata
) external returns (bool, bytes memory);
function setOutbox(address, bool) external;
}

contract ArbSysOverride {

event SendTxToL1(address sender, address target, bytes data);

function sendTxToL1(address target, bytes calldata message) external payable returns (uint256) {
emit SendTxToL1(msg.sender, target, message);
return 0;
}

}

contract ArbitrumDomain is BridgedDomain {

bytes32 private constant MESSAGE_DELIVERED_TOPIC = keccak256("MessageDelivered(uint256,bytes32,address,uint8,address,bytes32,uint256,uint64)");
bytes32 private constant SEND_TO_L1_TOPIC = keccak256("SendTxToL1(address,address,bytes)");

address public constant ARB_SYS = 0x0000000000000000000000000000000000000064;
InboxLike public INBOX;
lucas-manuel marked this conversation as resolved.
Show resolved Hide resolved
BridgeLike public immutable BRIDGE;

address public l2ToL1Sender;

uint256 internal lastFromHostLogIndex;
uint256 internal lastToHostLogIndex;

constructor(StdChains.Chain memory _chain, Domain _hostDomain) Domain(_chain) BridgedDomain(_hostDomain) {
bytes32 name = keccak256(bytes(_chain.chainAlias));
if (name == keccak256("arbitrum_one")) {
INBOX = InboxLike(0x4Dbd4fc535Ac27206064B68FfCf827b0A60BAB3f);
} else if (name == keccak256("arbitrum_one_goerli")) {
INBOX = InboxLike(0x6BEbC4925716945D46F0Ec336D5C2564F419682C);
} else if (name == keccak256("arbitrum_nova")) {
INBOX = InboxLike(0xc4448b71118c9071Bcb9734A0EAc55D18A153949);
} else {
revert("Unsupported chain");
}

_hostDomain.selectFork();
BRIDGE = BridgeLike(INBOX.bridge());
vm.recordLogs();

// Make this contract a valid outbox
address _rollup = BRIDGE.rollup();
vm.store(
address(BRIDGE),
bytes32(uint256(8)),
bytes32(uint256(uint160(address(this))))
);
BRIDGE.setOutbox(address(this), true);
vm.store(
address(BRIDGE),
bytes32(uint256(8)),
bytes32(uint256(uint160(_rollup)))
);

// Need to replace ArbSys contract with custom code to make it compatible with revm
selectFork();
bytes memory bytecode = vm.getCode("ArbitrumDomain.sol:ArbSysOverride");
address deployed;
assembly {
deployed := create(0, add(bytecode, 0x20), mload(bytecode))
}
vm.etch(ARB_SYS, deployed.code);

_hostDomain.selectFork();
}

function parseData(bytes memory orig) private pure returns (address target, bytes memory message) {
// FIXME - this is not robust enough, only handling messages of a specific format
uint256 mlen;
(,,target ,,,,,,,, mlen) = abi.decode(orig, (uint256, uint256, address, uint256, uint256, uint256, address, address, uint256, uint256, uint256));
message = new bytes(mlen);
for (uint256 i = 0; i < mlen; i++) {
message[i] = orig[i + 352];
}
}

function relayFromHost(bool switchToGuest) external override {
selectFork();

// Read all L1 -> L2 messages and relay them under Arbitrum fork
Vm.Log[] memory logs = RecordedLogs.getLogs();
for (; lastFromHostLogIndex < logs.length; lastFromHostLogIndex++) {
Vm.Log memory log = logs[lastFromHostLogIndex];
if (log.topics[0] == MESSAGE_DELIVERED_TOPIC) {
// We need both the current event and the one that follows for all the relevant data
Vm.Log memory logWithData = logs[lastFromHostLogIndex + 1];
(,, address sender,,,) = abi.decode(log.data, (address, uint8, address, bytes32, uint256, uint64));
(address target, bytes memory message) = parseData(logWithData.data);
vm.startPrank(sender);
(bool success, bytes memory response) = target.call(message);
vm.stopPrank();
if (!success) {
string memory rmessage;
assembly {
let size := mload(add(response, 0x44))
rmessage := mload(0x40)
mstore(rmessage, size)
mstore(0x40, add(rmessage, and(add(add(size, 0x20), 0x1f), not(0x1f))))
returndatacopy(add(rmessage, 0x20), 0x44, size)
}
revert(rmessage);
}
}
}

if (!switchToGuest) {
hostDomain.selectFork();
}
}

function relayToHost(bool switchToHost) external override {
hostDomain.selectFork();

// Read all L2 -> L1 messages and relay them under host fork
Vm.Log[] memory logs = RecordedLogs.getLogs();
for (; lastToHostLogIndex < logs.length; lastToHostLogIndex++) {
Vm.Log memory log = logs[lastToHostLogIndex];
if (log.topics[0] == SEND_TO_L1_TOPIC) {
(address sender, address target, bytes memory message) = abi.decode(log.data, (address, address, bytes));
l2ToL1Sender = sender;
(bool success, bytes memory response) = BRIDGE.executeCall(target, 0, message);
if (!success) {
string memory rmessage;
assembly {
let size := mload(add(response, 0x44))
rmessage := mload(0x40)
mstore(rmessage, size)
mstore(0x40, add(rmessage, and(add(add(size, 0x20), 0x1f), not(0x1f))))
returndatacopy(add(rmessage, 0x20), 0x44, size)
}
revert(rmessage);
}
}
}

if (!switchToHost) {
selectFork();
}
}

}
30 changes: 30 additions & 0 deletions src/BridgedDomain.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: © 2022 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.8.0;

import { Domain } from "./Domain.sol";

abstract contract BridgedDomain is Domain {

Domain public immutable hostDomain;

constructor(Domain _hostDomain) {
hostDomain = _hostDomain;
}

function relayFromHost(bool switchToGuest) external virtual;
function relayToHost(bool switchToHost) external virtual;
}
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

47 changes: 47 additions & 0 deletions src/Domain.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: © 2022 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.8.0;

import { StdChains } from "forge-std/StdChains.sol";
import { Vm } from "forge-std/Vm.sol";

contract Domain {

Vm internal constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

StdChains.Chain private _details;
uint256 public forkId;

constructor(StdChains.Chain memory _chain) {
_details = _chain;
forkId = vm.createFork(_chain.rpcUrl);
vm.makePersistent(address(this));
}

function details() public view returns (StdChains.Chain memory) {
return _details;
}

function selectFork() public {
vm.selectFork(forkId);
require(block.chainid == _details.chainId, string(abi.encodePacked(_details.chainAlias, " is pointing to the wrong RPC endpoint '", _details.rpcUrl, "'")));
}

function rollFork(uint256 blocknum) public {
vm.rollFork(forkId, blocknum);
}

}
Loading