Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
test mainnet fork
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Aug 16, 2023
1 parent 29711da commit c6f5d99
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 223 deletions.
3 changes: 3 additions & 0 deletions onchain/rollups/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ libs = ['../node_modules', 'lib']
test = 'test/foundry'
cache_path = 'forge-cache'
solc_version = '0.8.19'
fs_permissions = [
{ access = "read", path = "./export/abi/mainnet.json"}
]

[invariant]
runs = 32
Expand Down
166 changes: 40 additions & 126 deletions onchain/rollups/test/foundry/consensus/authority/Authority.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ contract HistoryReverts is IHistory {
}

contract AuthorityTest is TestBase {
struct Claim {
bytes32 epochHash;
uint128 firstIndex;
uint128 lastIndex;
}

Authority authority;
address owner;
IHistory history;

// events
event OwnershipTransferred(
Expand All @@ -55,6 +63,12 @@ contract AuthorityTest is TestBase {
event NewHistory(IHistory history);
event ApplicationJoined(address application);

function setUp() public {
authority = Authority(getMainnetAddress("Authority"));
owner = authority.owner();
history = IHistory(getMainnetAddress("History"));
}

function testConstructor(address _owner) public {
vm.assume(_owner != address(0));
uint256 numOfEvents;
Expand Down Expand Up @@ -85,106 +99,56 @@ contract AuthorityTest is TestBase {
new Authority(address(0));
}

function testMigrateHistory(
address _owner,
IHistory _history,
address _newConsensus
) public isMockable(address(_history)) {
vm.assume(_owner != address(0));
vm.assume(_owner != address(this));
function testMigrateHistory(address _newConsensus) public {
vm.assume(_newConsensus != address(0));

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

vm.assume(address(_history) != address(authority));
vm.mockCall(
address(_history),
abi.encodeWithSelector(
IHistory.migrateToConsensus.selector,
_newConsensus
),
""
);

// will fail as not called from owner
vm.expectRevert("Ownable: caller is not the owner");
authority.migrateHistoryToConsensus(_newConsensus);

vm.expectCall(
address(_history),
address(history),
abi.encodeWithSelector(
IHistory.migrateToConsensus.selector,
_newConsensus
)
);

// can only be called by owner
vm.prank(_owner);
vm.prank(owner);
authority.migrateHistoryToConsensus(_newConsensus);
}

function testSubmitClaim(
address _owner,
IHistory _history,
bytes calldata _claim
) public isMockable(address(_history)) {
vm.assume(_owner != address(0));
vm.assume(_owner != address(this));

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

vm.assume(address(_history) != address(authority));
vm.mockCall(
address(_history),
abi.encodeWithSelector(IHistory.submitClaim.selector, _claim),
""
);
function testSubmitClaim(address _dapp, Claim memory _claim) public {
_claim.firstIndex = 0;
vm.assume(_claim.lastIndex < type(uint128).max);
bytes memory encodedData = abi.encode(_dapp, _claim);

// will fail as not called from owner
vm.expectRevert("Ownable: caller is not the owner");
authority.submitClaim(_claim);
authority.submitClaim(encodedData);

vm.expectCall(
address(_history),
abi.encodeWithSelector(IHistory.submitClaim.selector, _claim)
address(history),
abi.encodeWithSelector(IHistory.submitClaim.selector, encodedData)
);

// can only be called by owner
vm.prank(_owner);
authority.submitClaim(_claim);
vm.prank(owner);
authority.submitClaim(encodedData);
}

function testSetHistory(
address _owner,
IHistory _history,
IHistory _newHistory
) public {
vm.assume(_owner != address(0));
vm.assume(_owner != address(this));

authority = new Authority(_owner);

vm.prank(_owner);
vm.expectEmit(false, false, false, true);
emit NewHistory(_history);
authority.setHistory(_history);

function testSetHistory(IHistory _newHistory) public {
// before setting new history
assertEq(address(authority.getHistory()), address(_history));
assertEq(address(authority.getHistory()), address(history));

// set new history
// will fail as not called from owner
vm.expectRevert("Ownable: caller is not the owner");
authority.setHistory(_newHistory);

// can only be called by owner
vm.prank(_owner);
vm.prank(owner);
// expect event NewHistory
vm.expectEmit(false, false, false, true);
emit NewHistory(_newHistory);
Expand All @@ -195,26 +159,15 @@ contract AuthorityTest is TestBase {
}

function testGetClaim(
address _owner,
IHistory _history,
address _dapp,
bytes calldata _proofContext,
bytes32 _r0,
uint256 _r1,
uint256 _r2
) public isMockable(address(_history)) {
vm.assume(_owner != address(0));
vm.assume(_owner != address(this));

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

) public {
// mocking history
vm.assume(address(_history) != address(authority));
vm.mockCall(
address(_history),
address(history),
abi.encodeWithSelector(
IHistory.getClaim.selector,
_dapp,
Expand All @@ -224,7 +177,7 @@ contract AuthorityTest is TestBase {
);

vm.expectCall(
address(_history),
address(history),
abi.encodeWithSelector(
IHistory.getClaim.selector,
_dapp,
Expand All @@ -246,84 +199,61 @@ contract AuthorityTest is TestBase {

// test behaviors when history reverts
function testHistoryReverts(
address _owner,
IHistory _newHistory,
address _dapp,
bytes calldata _claim,
address _consensus,
bytes calldata _proofContext
) public {
vm.assume(_owner != address(0));

HistoryReverts historyR = new HistoryReverts();

authority = new Authority(_owner);

vm.prank(_owner);
vm.prank(owner);
authority.setHistory(historyR);
assertEq(address(authority.getHistory()), address(historyR));

vm.expectRevert();
vm.prank(_owner);
vm.prank(owner);
authority.submitClaim(_claim);

vm.expectRevert();
vm.prank(_owner);
vm.prank(owner);
authority.migrateHistoryToConsensus(_consensus);

vm.expectRevert();
authority.getClaim(_dapp, _proofContext);

vm.prank(_owner);
vm.prank(owner);
authority.setHistory(_newHistory);
assertEq(address(authority.getHistory()), address(_newHistory));
}

function testWithdrawERC20TokensNotOwner(
address _owner,
IHistory _history,
address _notOwner,
IERC20 _token,
address _recipient,
uint256 _amount
) public {
vm.assume(_owner != address(0));
vm.assume(_owner != _notOwner);

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

vm.prank(_notOwner);
vm.expectRevert("Ownable: caller is not the owner");
authority.withdrawERC20Tokens(_token, _recipient, _amount);
}

function testWithdrawERC20Tokens(
address _owner,
IHistory _history,
address _recipient,
uint256 _amount,
uint256 _balance
) public {
vm.assume(_owner != address(0));
vm.assume(_recipient != address(0));
vm.assume(_amount <= _balance);
vm.assume(_balance < type(uint256).max);

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

vm.assume(_recipient != address(authority));

// mint `_balance` ERC-20 tokens for authority contract
IERC20 token = new SimpleERC20(address(authority), _balance);

// try to transfer more than balance
vm.prank(_owner);
vm.prank(owner);
vm.expectRevert("ERC20: transfer amount exceeds balance");
authority.withdrawERC20Tokens(token, _recipient, _balance + 1);

Expand All @@ -332,7 +262,7 @@ contract AuthorityTest is TestBase {
assertEq(token.balanceOf(_recipient), 0);

// it would succeed if the transfer amount is within balance
vm.prank(_owner);
vm.prank(owner);
authority.withdrawERC20Tokens(token, _recipient, _amount);

// now check balance after a successful withdraw
Expand All @@ -341,22 +271,13 @@ contract AuthorityTest is TestBase {
}

function testWithdrawERC20TokensFailed(
address _owner,
IHistory _history,
address _recipient,
uint256 _amount,
uint256 _balance
) public {
vm.assume(_owner != address(0));
vm.assume(_recipient != address(0));
vm.assume(_amount <= _balance);
vm.assume(_balance < type(uint256).max);

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

vm.assume(_recipient != address(authority));

// mint `_balance` ERC-20 tokens for authority contract
Expand All @@ -370,7 +291,7 @@ contract AuthorityTest is TestBase {
assertEq(tokenFailed.balanceOf(_recipient), 0);

// withdrawal fails because `transfer` returns `false`
vm.prank(_owner);
vm.prank(owner);
vm.expectRevert(Authority.AuthorityWithdrawalFailed.selector);
authority.withdrawERC20Tokens(tokenFailed, _recipient, _amount);

Expand All @@ -379,14 +300,7 @@ contract AuthorityTest is TestBase {
assertEq(tokenFailed.balanceOf(_recipient), 0);
}

function testJoin(address _owner, IHistory _history, address _dapp) public {
vm.assume(_owner != address(0));

authority = new Authority(_owner);

vm.prank(_owner);
authority.setHistory(_history);

function testJoin(address _dapp) public {
vm.expectEmit(false, false, false, true);
emit ApplicationJoined(_dapp);

Expand Down
11 changes: 7 additions & 4 deletions onchain/rollups/test/foundry/dapp/CartesiDApp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pragma solidity ^0.8.8;
import {TestBase} from "../util/TestBase.sol";

import {CartesiDApp} from "contracts/dapp/CartesiDApp.sol";
import {CartesiDAppFactory} from "contracts/dapp/CartesiDAppFactory.sol";
import {Proof} from "contracts/dapp/ICartesiDApp.sol";
import {IConsensus} from "contracts/consensus/IConsensus.sol";
import {OutputValidityProof, LibOutputValidation} from "contracts/library/LibOutputValidation.sol";
Expand All @@ -16,6 +17,7 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

import {Authority} from "contracts/consensus/authority/Authority.sol";
import {SimpleConsensus} from "../util/SimpleConsensus.sol";
import {SimpleERC20} from "../util/SimpleERC20.sol";
import {SimpleERC721} from "../util/SimpleERC721.sol";
Expand Down Expand Up @@ -668,13 +670,14 @@ contract CartesiDAppTest is TestBase {
}

function deployDAppDeterministically() internal returns (CartesiDApp) {
vm.prank(dappOwner);
return new CartesiDApp{salt: salt}(consensus, dappOwner, templateHash);
CartesiDAppFactory factory = CartesiDAppFactory(
getMainnetAddress("CartesiDAppFactory")
);
return factory.newApplication(consensus, dappOwner, templateHash, salt);
}

function deployConsensusDeterministically() internal returns (IConsensus) {
vm.prank(dappOwner);
return new SimpleConsensus{salt: salt}();
return IConsensus(getMainnetAddress("Authority"));
}

function deployERC20Deterministically() internal returns (IERC20) {
Expand Down
5 changes: 3 additions & 2 deletions onchain/rollups/test/foundry/dapp/CartesiDAppFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {CartesiDAppFactory} from "contracts/dapp/CartesiDAppFactory.sol";
import {CartesiDApp} from "contracts/dapp/CartesiDApp.sol";
import {IConsensus} from "contracts/consensus/IConsensus.sol";
import {Vm} from "forge-std/Vm.sol";
import {Authority} from "contracts/consensus/authority/Authority.sol";

contract CartesiDAppFactoryTest is TestBase {
CartesiDAppFactory factory;
IConsensus consensus;

function setUp() public {
factory = new CartesiDAppFactory();
consensus = new SimpleConsensus();
factory = CartesiDAppFactory(getMainnetAddress("CartesiDAppFactory"));
consensus = Authority(getMainnetAddress("Authority"));
}

event ApplicationCreated(
Expand Down
Loading

0 comments on commit c6f5d99

Please sign in to comment.