Skip to content

Commit

Permalink
recfactor contract test files
Browse files Browse the repository at this point in the history
  • Loading branch information
jackchuma committed Dec 31, 2024
1 parent ea1f755 commit c1a3ef0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 145 deletions.
39 changes: 4 additions & 35 deletions contracts/test/ArbitrumProver.t.sol
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {Test} from "forge-std/Test.sol";
import {ERC20Mock} from "openzeppelin-contracts/contracts/mocks/token/ERC20Mock.sol";
import {stdJson} from "forge-std/StdJson.sol";

import {CAIP10} from "../src/libraries/CAIP10.sol";
import {GlobalTypes} from "../src/libraries/GlobalTypes.sol";
import {ArbitrumProver} from "../src/libraries/provers/ArbitrumProver.sol";
import {StateValidator} from "../src/libraries/StateValidator.sol";
import {ERC7786Base} from "../src/ERC7786Base.sol";
import {Call} from "../src/RIP7755Structs.sol";
import {RIP7755OutboxToArbitrum} from "../src/outboxes/RIP7755OutboxToArbitrum.sol";

import {MockArbitrumProver} from "./mocks/MockArbitrumProver.sol";
import {MockBeaconOracle} from "./mocks/MockBeaconOracle.sol";
import {BaseTest} from "./BaseTest.t.sol";

contract ArbitrumProverTest is Test, ERC7786Base {
contract ArbitrumProverTest is BaseTest {
using stdJson for string;
using GlobalTypes for address;
using CAIP10 for address;

MockArbitrumProver prover;
ERC20Mock mockErc20;
MockBeaconOracle mockBeaconOracle;

Call[] calls;
address ALICE = makeAddr("alice");
address FILLER = makeAddr("filler");
string validProof;
string invalidL1State;
string invalidBlockHeaders;
string invalidL2Storage;
uint256 private _REWARD_AMOUNT = 1 ether;
bytes32 private constant _VERIFIER_STORAGE_LOCATION =
0x43f1016e17bdb0194ec37b77cf476d255de00011d02616ab831d2e2ce63d9ee2;

address private constant _INBOX_CONTRACT = 0x49E2cDC9e81825B6C718ae8244fe0D5b062F4874;

function setUp() external {
prover = new MockArbitrumProver();
mockErc20 = new ERC20Mock();
deployCodeTo("MockBeaconOracle.sol", abi.encode(), 0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);
mockBeaconOracle = MockBeaconOracle(0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);
approveAddr = address(prover);
_setUp();

string memory rootPath = vm.projectRoot();
string memory path = string.concat(rootPath, "/test/data/ArbitrumSepoliaProof.json");
string memory invalidPath = string.concat(rootPath, "/test/data/invalids/ArbitrumInvalidL1State.json");
string memory invalidBlockHeadersPath =
Expand All @@ -58,14 +39,6 @@ contract ArbitrumProverTest is Test, ERC7786Base {
invalidL2Storage = vm.readFile(invalidL2StoragePath);
}

modifier fundAlice(uint256 amount) {
mockErc20.mint(ALICE, amount);
vm.deal(ALICE, amount);
vm.prank(ALICE);
mockErc20.approve(address(prover), amount);
_;
}

function test_reverts_ifFinalityDelaySecondsStillInProgress() external fundAlice(_REWARD_AMOUNT) {
(string memory sender, string memory receiver, bytes memory payload, bytes[] memory attributes) =
_initMessage(_REWARD_AMOUNT);
Expand Down Expand Up @@ -200,8 +173,4 @@ contract ArbitrumProverTest is Test, ERC7786Base {

return (sender, receiver, payload, attributes);
}

function _deriveStorageKey(bytes32 messageId) private pure returns (bytes memory) {
return abi.encode(keccak256(abi.encodePacked(messageId, _VERIFIER_STORAGE_LOCATION)));
}
}
51 changes: 51 additions & 0 deletions contracts/test/BaseTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {Test} from "forge-std/Test.sol";
import {ERC20Mock} from "openzeppelin-contracts/contracts/mocks/token/ERC20Mock.sol";

import {ERC7786Base} from "../src/ERC7786Base.sol";
import {Call} from "../src/RIP7755Structs.sol";

import {MockBeaconOracle} from "./mocks/MockBeaconOracle.sol";

contract BaseTest is Test, ERC7786Base {
ERC20Mock mockErc20;
MockBeaconOracle mockBeaconOracle;

address approveAddr;

Call[] calls;
address ALICE = makeAddr("alice");
address FILLER = makeAddr("filler");
string rootPath;
string validProof;
string invalidL1State;
string invalidBlockHeaders;
string invalidL2StateRootProof;
string invalidL2Storage;

uint256 constant _REWARD_AMOUNT = 1 ether;
bytes32 constant _VERIFIER_STORAGE_LOCATION = 0x43f1016e17bdb0194ec37b77cf476d255de00011d02616ab831d2e2ce63d9ee2;

function _setUp() internal {
mockErc20 = new ERC20Mock();

deployCodeTo("MockBeaconOracle.sol", abi.encode(), 0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);
mockBeaconOracle = MockBeaconOracle(0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);

rootPath = vm.projectRoot();
}

modifier fundAlice(uint256 amount) {
mockErc20.mint(ALICE, amount);
vm.deal(ALICE, amount);
vm.prank(ALICE);
mockErc20.approve(approveAddr, amount);
_;
}

function _deriveStorageKey(bytes32 messageId) internal pure returns (bytes memory) {
return abi.encode(keccak256(abi.encodePacked(messageId, _VERIFIER_STORAGE_LOCATION)));
}
}
32 changes: 4 additions & 28 deletions contracts/test/HashiProver.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {Test} from "forge-std/Test.sol";
import {ERC20Mock} from "openzeppelin-contracts/contracts/mocks/token/ERC20Mock.sol";
import {stdJson} from "forge-std/StdJson.sol";

import {HashiProver} from "../src/libraries/provers/HashiProver.sol";
Expand All @@ -11,13 +9,12 @@ import {CAIP10} from "../src/libraries/CAIP10.sol";
import {GlobalTypes} from "../src/libraries/GlobalTypes.sol";
import {StateValidator} from "../src/libraries/StateValidator.sol";
import {RIP7755OutboxToHashi} from "../src/outboxes/RIP7755OutboxToHashi.sol";
import {ERC7786Base} from "../src/ERC7786Base.sol";
import {Call} from "../src/RIP7755Structs.sol";

import {MockShoyuBashi} from "./mocks/MockShoyuBashi.sol";
import {MockHashiProver} from "./mocks/MockHashiProver.sol";
import {BaseTest} from "./BaseTest.t.sol";

contract HashiProverTest is Test, ERC7786Base {
contract HashiProverTest is BaseTest {
using stdJson for string;
using GlobalTypes for address;
using BlockHeaders for bytes;
Expand All @@ -27,35 +24,18 @@ contract HashiProverTest is Test, ERC7786Base {
address private constant _INBOX_CONTRACT = 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512;

MockHashiProver prover;
ERC20Mock mockErc20;
MockShoyuBashi shoyuBashi;

Call[] calls;
address ALICE = makeAddr("alice");
address FILLER = makeAddr("filler");
string validProof;
uint256 private constant _REWARD_AMOUNT = 1 ether;
bytes32 private constant _VERIFIER_STORAGE_LOCATION =
0x43f1016e17bdb0194ec37b77cf476d255de00011d02616ab831d2e2ce63d9ee2;

function setUp() external {
shoyuBashi = new MockShoyuBashi();
prover = new MockHashiProver();
mockErc20 = new ERC20Mock();
approveAddr = address(prover);
_setUp();

string memory rootPath = vm.projectRoot();
string memory path = string.concat(rootPath, "/test/data/HashiProverProof.json");
validProof = vm.readFile(path);
}

modifier fundAlice(uint256 amount) {
mockErc20.mint(ALICE, amount);
vm.deal(ALICE, amount);
vm.prank(ALICE);
mockErc20.approve(address(prover), amount);
_;
}

function test_reverts_ifFinalityDelaySecondsStillInProgress() external fundAlice(_REWARD_AMOUNT) {
(string memory sender, string memory receiver, bytes memory payload, bytes[] memory attributes) =
_initMessage(_REWARD_AMOUNT);
Expand Down Expand Up @@ -154,8 +134,4 @@ contract HashiProverTest is Test, ERC7786Base {

return (sender, receiver, payload, attributes);
}

function _deriveStorageKey(bytes32 messageId) private pure returns (bytes memory) {
return abi.encode(keccak256(abi.encodePacked(messageId, _VERIFIER_STORAGE_LOCATION)));
}
}
47 changes: 8 additions & 39 deletions contracts/test/OPStackProver.t.sol
Original file line number Diff line number Diff line change
@@ -1,66 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {Test} from "forge-std/Test.sol";
import {ERC20Mock} from "openzeppelin-contracts/contracts/mocks/token/ERC20Mock.sol";
import {stdJson} from "forge-std/StdJson.sol";

import {OPStackProver} from "../src/libraries/provers/OPStackProver.sol";
import {CAIP10} from "../src/libraries/CAIP10.sol";
import {GlobalTypes} from "../src/libraries/GlobalTypes.sol";
import {StateValidator} from "../src/libraries/StateValidator.sol";
import {ERC7786Base} from "../src/ERC7786Base.sol";
import {Call} from "../src/RIP7755Structs.sol";
import {RIP7755OutboxToOPStack} from "../src/outboxes/RIP7755OutboxToOPStack.sol";

import {MockBeaconOracle} from "./mocks/MockBeaconOracle.sol";
import {MockOPStackProver} from "./mocks/MockOPStackProver.sol";
import {BaseTest} from "./BaseTest.t.sol";

contract RIP7755OutboxOPStackValidatorTest is Test, ERC7786Base {
contract OPStackProverTest is BaseTest {
using stdJson for string;
using GlobalTypes for address;
using CAIP10 for address;

MockOPStackProver prover;
ERC20Mock mockErc20;
MockBeaconOracle mockBeaconOracle;

Call[] calls;
address ALICE = makeAddr("alice");
address FILLER = makeAddr("filler");
string validProof;
string invalidL1StorageProof;
string invalidL2StateRootProof;
string invalidL2StorageProof;
uint256 private _REWARD_AMOUNT = 1 ether;
bytes32 private constant _VERIFIER_STORAGE_LOCATION =
0x43f1016e17bdb0194ec37b77cf476d255de00011d02616ab831d2e2ce63d9ee2;

address private constant _INBOX_CONTRACT = 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512;

function setUp() external {
prover = new MockOPStackProver();
mockErc20 = new ERC20Mock();
deployCodeTo("MockBeaconOracle.sol", abi.encode(), 0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);
mockBeaconOracle = MockBeaconOracle(0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02);
approveAddr = address(prover);
_setUp();

string memory rootPath = vm.projectRoot();
string memory path = string.concat(rootPath, "/test/data/OPSepoliaProof.json");
string memory invalidL1StoragePath = string.concat(rootPath, "/test/data/invalids/OPInvalidL1Storage.json");
string memory invalidL2StateRootPath = string.concat(rootPath, "/test/data/invalids/OPInvalidL2StateRoot.json");
string memory invalidL2StoragePath = string.concat(rootPath, "/test/data/invalids/OPInvalidL2Storage.json");
validProof = vm.readFile(path);
invalidL1StorageProof = vm.readFile(invalidL1StoragePath);
invalidL1State = vm.readFile(invalidL1StoragePath);
invalidL2StateRootProof = vm.readFile(invalidL2StateRootPath);
invalidL2StorageProof = vm.readFile(invalidL2StoragePath);
}

modifier fundAlice(uint256 amount) {
mockErc20.mint(ALICE, amount);
vm.deal(ALICE, amount);
vm.prank(ALICE);
mockErc20.approve(address(prover), amount);
_;
invalidL2Storage = vm.readFile(invalidL2StoragePath);
}

function test_validate_reverts_ifFinalityDelaySecondsInProgress() external fundAlice(_REWARD_AMOUNT) {
Expand Down Expand Up @@ -127,7 +100,7 @@ contract RIP7755OutboxOPStackValidatorTest is Test, ERC7786Base {
_initMessage(_REWARD_AMOUNT);
bytes32 messageId = keccak256(abi.encode(sender, receiver, payload, attributes));

bytes memory storageProofData = _buildProofAndEncodeProof(invalidL1StorageProof);
bytes memory storageProofData = _buildProofAndEncodeProof(invalidL1State);
bytes memory inboxStorageKey = _deriveStorageKey(messageId);

vm.prank(FILLER);
Expand All @@ -153,7 +126,7 @@ contract RIP7755OutboxOPStackValidatorTest is Test, ERC7786Base {
_initMessage(_REWARD_AMOUNT);
bytes32 messageId = keccak256(abi.encode(sender, receiver, payload, attributes));

bytes memory storageProofData = _buildProofAndEncodeProof(invalidL2StorageProof);
bytes memory storageProofData = _buildProofAndEncodeProof(invalidL2Storage);
bytes memory inboxStorageKey = _deriveStorageKey(messageId);

vm.prank(FILLER);
Expand Down Expand Up @@ -233,8 +206,4 @@ contract RIP7755OutboxOPStackValidatorTest is Test, ERC7786Base {

return (sender, receiver, payload, attributes);
}

function _deriveStorageKey(bytes32 messageId) private pure returns (bytes memory) {
return abi.encode(keccak256(abi.encodePacked(messageId, _VERIFIER_STORAGE_LOCATION)));
}
}
Loading

0 comments on commit c1a3ef0

Please sign in to comment.