Skip to content

Commit

Permalink
First round of revert string removals (#139)
Browse files Browse the repository at this point in the history
* First round of revert string removements

* add revert errors in forkable bridge
  • Loading branch information
josojo authored Jan 1, 2024
1 parent afbfdd2 commit 0de67bd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 35 deletions.
8 changes: 6 additions & 2 deletions contracts/ForkableBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ contract ForkableBridge is
uint256 amount,
address to
) external onlyAfterForking {
require(_hardAssetManager == msg.sender, "Not authorized");
require(to == children[0] || to == children[1], "Invalid to");
if (_hardAssetManager != msg.sender) {
revert NotAuthorized();
}
if (to != children[0] && to != children[1]) {
revert InvalidDestinationForHardAsset();
}
IERC20(token).transfer(to, amount);
}

Expand Down
5 changes: 5 additions & 0 deletions contracts/interfaces/IForkableBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {IBasePolygonZkEVMGlobalExitRoot} from "@RealityETH/zkevm-contracts/contr
import {IForkableStructure} from "./IForkableStructure.sol";

interface IForkableBridge is IForkableStructure, IPolygonZkEVMBridge {
/// @dev Error thrown when activity is started by a non-authorized actor
error NotAuthorized();
/// @dev Error thrown when trying to send bridged tokens to a child contract
error InvalidDestinationForHardAsset();

/**
* @dev Function to initialize the contract
* @param _forkmanager: address of the forkmanager contract
Expand Down
12 changes: 12 additions & 0 deletions contracts/interfaces/IForkableStructure.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
pragma solidity ^0.8.20;

interface IForkableStructure {
/// @dev Error thrown when trying to call a function that can only be called after forking
error NoChangesAfterForking();
/// @dev Error thrown when trying to call a function that can only be called before forking
error OnlyAfterForking();
/// @dev Error thrown when trying to call a function that can only be called by the parent contract
error OnlyParentIsAllowed();
/// @dev Error thrown when trying to call a function that can only be called by the forkmanager
error OnlyForkManagerIsAllowed();

/// @dev Returns the address of the forkmanager
function forkmanager() external view returns (address);

/// @dev Returns the address of the parent contract
function parentContract() external view returns (address);

/// @dev Returns the addresses of the two children
function getChildren() external view returns (address, address);
}
25 changes: 15 additions & 10 deletions contracts/mixin/ForkableStructure.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@ contract ForkableStructure is IForkableStructure, Initializable {
mapping(uint256 => address) public children;

modifier onlyBeforeForking() {
require(children[0] == address(0x0), "No changes after forking");
_;
}

modifier onlyBeforeCreatingChild2() {
require(children[2] == address(0x0), "No changes after forking");
if (children[0] != address(0x0)) {
revert NoChangesAfterForking();
}
_;
}

modifier onlyAfterForking() {
require(children[0] != address(0x0), "onlyAfterForking");
require(children[1] != address(0x0), "onlyAfterForking");
if (children[0] == address(0x0)) {
revert OnlyAfterForking();
}
if (children[1] == address(0x0)) {
revert OnlyAfterForking();
}
_;
}

modifier onlyParent() {
require(msg.sender == parentContract, "Not parent");
if (msg.sender != parentContract) {
revert OnlyParentIsAllowed();
}
_;
}

modifier onlyForkManger() {
require(msg.sender == forkmanager, "Not forkManager");
if (msg.sender != forkmanager) {
revert OnlyForkManagerIsAllowed();
}
_;
}

Expand Down
36 changes: 22 additions & 14 deletions test/ForkableBridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {Test} from "forge-std/Test.sol";
import {ForkableBridge} from "../contracts/ForkableBridge.sol";
import {ForkableBridgeWrapper} from "./testcontract/ForkableBridgeWrapper.sol";
import {ForkonomicToken} from "../contracts/ForkonomicToken.sol";
import {IForkableBridge} from "../contracts/interfaces/IForkableBridge.sol";
import {IForkableStructure} from "../contracts/interfaces/IForkableStructure.sol";
import {IForkonomicToken} from "../contracts/interfaces/IForkonomicToken.sol";
import {ForkableGlobalExitRoot} from "../contracts/ForkableGlobalExitRoot.sol";
import {IERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/interfaces/IERC20Upgradeable.sol";
Expand Down Expand Up @@ -69,7 +71,7 @@ contract ForkableBridgeTest is Test {
}

function testCreateChildren() public {
vm.expectRevert(bytes("Not forkManager"));
vm.expectRevert(IForkableStructure.OnlyForkManagerIsAllowed.selector);
forkableBridge.createChildren();
ForkableGlobalExitRoot exitRoot = new ForkableGlobalExitRoot();
vm.mockCall(
Expand All @@ -96,7 +98,7 @@ contract ForkableBridgeTest is Test {
abi.encodePacked(originNetwork, token)
);

vm.expectRevert(bytes("Not parent"));
vm.expectRevert(IForkableStructure.OnlyParentIsAllowed.selector);
forkableBridge.mintForkableToken(
address(token),
originNetwork,
Expand Down Expand Up @@ -173,7 +175,7 @@ contract ForkableBridgeTest is Test {
amount
);

vm.expectRevert(bytes("Not parent"));
vm.expectRevert(IForkableStructure.OnlyParentIsAllowed.selector);
forkableBridge.burnForkableTokens(
destinationAddress,
originTokenAddress,
Expand Down Expand Up @@ -213,7 +215,8 @@ contract ForkableBridgeTest is Test {
);

// Testing revert if children are not yet created
vm.expectRevert(bytes("onlyAfterForking"));
vm.expectRevert(IForkableStructure.OnlyAfterForking.selector);

forkableBridge.splitTokenIntoChildToken(address(token), amount, true);

vm.prank(forkableBridge.forkmanager());
Expand Down Expand Up @@ -345,7 +348,8 @@ contract ForkableBridgeTest is Test {
forkableBridge.splitTokenIntoChildToken(forkableToken, amount, true);

// Only parent can merge
vm.expectRevert(bytes("onlyAfterForking"));
vm.expectRevert(IForkableStructure.OnlyAfterForking.selector);

ForkableBridge(child1).mergeChildTokens(forkableToken, amount + 1);

// Merge the token
Expand Down Expand Up @@ -387,7 +391,7 @@ contract ForkableBridgeTest is Test {
vm.prank(forkableBridge.forkmanager());
forkableBridge.createChildren();

vm.expectRevert(bytes("No changes after forking"));
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableBridge.bridgeAsset(
12,
address(0x3),
Expand All @@ -397,11 +401,11 @@ contract ForkableBridgeTest is Test {
bytes("0x3453")
);

vm.expectRevert(bytes("No changes after forking"));
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableBridge.bridgeMessage(2, address(0x45), false, bytes("0x3453"));

bytes32[32] memory smtProof;
vm.expectRevert(bytes("No changes after forking"));
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableBridge.claimMessage(
smtProof,
2,
Expand All @@ -415,7 +419,7 @@ contract ForkableBridgeTest is Test {
bytes("0x")
);

vm.expectRevert(bytes("No changes after forking"));
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableBridge.claimAsset(
smtProof,
32,
Expand Down Expand Up @@ -447,7 +451,7 @@ contract ForkableBridgeTest is Test {
false,
""
);
vm.expectRevert("onlyAfterForking");
vm.expectRevert(IForkableStructure.OnlyAfterForking.selector);
vm.prank(hardAssetManger);
forkableBridge.transferHardAssetsToChild(
address(erc20Token),
Expand Down Expand Up @@ -493,14 +497,16 @@ contract ForkableBridgeTest is Test {
depositTreeHashes
);

vm.expectRevert("Not authorized");
vm.expectRevert(IForkableBridge.NotAuthorized.selector);
forkableBridge.transferHardAssetsToChild(
address(erc20Token),
amount,
to
);

vm.expectRevert("Invalid to");
vm.expectRevert(
IForkableBridge.InvalidDestinationForHardAsset.selector
);
vm.prank(hardAssetManger);
forkableBridge.transferHardAssetsToChild(
address(erc20Token),
Expand Down Expand Up @@ -561,10 +567,12 @@ contract ForkableBridgeTest is Test {
erc20GasToken.mint(address(forkableBridge2), mintAmount);

// Now, call the function as the forkmanager
vm.expectRevert(bytes("onlyAfterForking"));
vm.expectRevert(IForkableStructure.OnlyAfterForking.selector);

vm.prank(forkmanager);
forkableBridge2.sendForkonomicTokensToChild(10, true, false);
vm.expectRevert(bytes("onlyAfterForking"));
vm.expectRevert(IForkableStructure.OnlyAfterForking.selector);

vm.prank(forkmanager);
forkableBridge2.sendForkonomicTokensToChild(10, true, false);

Expand Down
3 changes: 2 additions & 1 deletion test/ForkableGlobalExitRoot.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {IForkableStructure} from "../contracts/interfaces/IForkableStructure.sol";
import {ForkableGlobalExitRoot} from "../contracts/ForkableGlobalExitRoot.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {Util} from "./utils/Util.sol";
Expand Down Expand Up @@ -69,7 +70,7 @@ contract ForkableGlobalExitRootTest is Test {
}

function testCreateChildrenOnlyByForkManager() public {
vm.expectRevert("Not forkManager");
vm.expectRevert(IForkableStructure.OnlyForkManagerIsAllowed.selector);
forkableGlobalExitRoot.createChildren();
vm.prank(forkableGlobalExitRoot.forkmanager());
forkableGlobalExitRoot.createChildren();
Expand Down
11 changes: 6 additions & 5 deletions test/ForkableZkEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {PolygonZkEVM} from "@RealityETH/zkevm-contracts/contracts/inheritedMainContracts/PolygonZkEVM.sol";
import {ForkableZkEVM} from "../contracts/ForkableZkEVM.sol";
import {IForkableStructure} from "../contracts/interfaces/IForkableStructure.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IPolygonZkEVMGlobalExitRoot} from "@RealityETH/zkevm-contracts/contracts/interfaces/IPolygonZkEVMGlobalExitRoot.sol";
import {IVerifierRollup} from "@RealityETH/zkevm-contracts/contracts/interfaces/IVerifierRollup.sol";
Expand Down Expand Up @@ -93,7 +94,7 @@ contract ForkableZkEVMTest is Test {
}

function testCreateChildren() public {
vm.expectRevert("Not forkManager");
vm.expectRevert(IForkableStructure.OnlyForkManagerIsAllowed.selector);
forkableZkEVM.createChildren();

vm.prank(forkableZkEVM.forkmanager());
Expand Down Expand Up @@ -130,7 +131,7 @@ contract ForkableZkEVMTest is Test {
vm.prank(forkableZkEVM.forkmanager());
forkableZkEVM.createChildren();

vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableZkEVM.verifyBatches(
pendingStateNum,
initNumBatch,
Expand All @@ -145,15 +146,15 @@ contract ForkableZkEVMTest is Test {
vm.prank(forkableZkEVM.forkmanager());
forkableZkEVM.createChildren();

vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableZkEVM.consolidatePendingState(10);

bytes32[24] memory proof;
for (uint i = 0; i < 24; i++) {
proof[i] = bytes32("0x"); // Whatever initialization value you want
}

vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkableZkEVM.overridePendingState(
10,
10,
Expand All @@ -164,7 +165,7 @@ contract ForkableZkEVMTest is Test {
proof
);

vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
PolygonZkEVM.BatchData[] memory batches = new PolygonZkEVM.BatchData[](
1
);
Expand Down
3 changes: 2 additions & 1 deletion test/ForkingManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ForkableBridge} from "../contracts/ForkableBridge.sol";
import {ForkableZkEVM} from "../contracts/ForkableZkEVM.sol";
import {ForkonomicToken} from "../contracts/ForkonomicToken.sol";
import {ForkableGlobalExitRoot} from "../contracts/ForkableGlobalExitRoot.sol";
import {IForkableStructure} from "../contracts/interfaces/IForkableStructure.sol";
import {IPolygonZkEVMGlobalExitRoot} from "@RealityETH/zkevm-contracts/contracts/interfaces/IPolygonZkEVMGlobalExitRoot.sol";
import {IForkingManager} from "../contracts/interfaces/IForkingManager.sol";
import {IVerifierRollup} from "@RealityETH/zkevm-contracts/contracts/interfaces/IVerifierRollup.sol";
Expand Down Expand Up @@ -641,7 +642,7 @@ contract ForkingManagerTest is Test {
forkmanager.initiateFork(disputeData);
skip(forkmanager.forkPreparationTime() + 1);
forkmanager.executeFork();
vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
forkmanager.executeFork();
}

Expand Down
5 changes: 3 additions & 2 deletions test/L1GlobalChainInfoPublisher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {ForkableRealityETH_ERC20} from "../contracts/ForkableRealityETH_ERC20.so
import {RealityETH_v3_0} from "../contracts/lib/reality-eth/RealityETH-3.0.sol";
import {AdjudicationFramework} from "../contracts/AdjudicationFramework.sol";

import {IForkableStructure} from "../contracts/interfaces/IForkableStructure.sol";
import {L2ForkArbitrator} from "../contracts/L2ForkArbitrator.sol";
import {L1GlobalChainInfoPublisher} from "../contracts/L1GlobalChainInfoPublisher.sol";
import {L1GlobalForkRequester} from "../contracts/L1GlobalForkRequester.sol";
Expand Down Expand Up @@ -290,7 +291,7 @@ contract L1GlobalChainInfoPublisherTest is Test {
forkmanager.executeFork();

// The current bridge should no longer work
vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
l1GlobalChainInfoPublisher.updateL2ChainInfo(
address(bridge),
address(l2ChainInfo),
Expand Down Expand Up @@ -352,7 +353,7 @@ contract L1GlobalChainInfoPublisherTest is Test {
skip(forkmanager.forkPreparationTime() + 1);
forkmanager2.executeFork();

vm.expectRevert("No changes after forking");
vm.expectRevert(IForkableStructure.NoChangesAfterForking.selector);
l1GlobalChainInfoPublisher.updateL2ChainInfo(
bridge2,
address(l2ChainInfo),
Expand Down

0 comments on commit 0de67bd

Please sign in to comment.