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

test: shared bridge unit tests #62

Merged
merged 10 commits into from
Oct 8, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

/* solhint-disable max-line-length */

import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol";

/* solhint-enable max-line-length */

contract DepositTest is BridgeheadMailboxTest {
function test_RevertWhen_CalledByNonChainContract() public {
address nonChainContract = makeAddr("nonChainContract");

vm.expectRevert(abi.encodePacked("12c"));
vm.startPrank(nonChainContract);
bridgehead.deposit(chainId);
}

function test_SuccessfullIfCalledByChainContract() public {
address chainContract = bridgehead.getChainContract(chainId);

vm.startPrank(chainContract);
bridgehead.deposit(chainId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

/* solhint-disable max-line-length */

import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol";
import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol";

/* solhint-enable max-line-length */

contract FinalizeEthWithdrawalTest is BridgeheadMailboxTest {
uint256 internal l2BlockNumber;
uint256 internal l2MessageIndex;
uint16 internal l2TxNumberInBlock;
bytes internal message;
bytes32[] internal merkleProof;
address internal msgSender;

function setUp() public {
l2BlockNumber = 3456789;
l2MessageIndex = 234567890;
l2TxNumberInBlock = 12345;
message = "message";
merkleProof = new bytes32[](1);
msgSender = makeAddr("msgSender");
}

function test_RevertWhen_InternalCallReverts() public {
bytes memory revertMessage = "random revert";

vm.mockCallRevert(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.finalizeEthWithdrawalBridgehead.selector,
msgSender,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
),
revertMessage
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.finalizeEthWithdrawalBridgehead.selector,
msgSender,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
)
);

vm.expectRevert(revertMessage);
vm.startPrank(msgSender);
bridgehead.finalizeEthWithdrawal(
chainId,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
);
}

function test_ShouldReturnReceivedCanonicalTxHash() public {
vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.finalizeEthWithdrawalBridgehead.selector,
msgSender,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
),
""
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.finalizeEthWithdrawalBridgehead.selector,
msgSender,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
)
);

vm.startPrank(msgSender);
bridgehead.finalizeEthWithdrawal(
chainId,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
merkleProof
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

/* solhint-disable max-line-length */

import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol";
import {IChainGetters} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IChainGetters.sol";

/* solhint-enable max-line-length */

contract IsEthWithdrawalFinalizedTest is BridgeheadMailboxTest {
uint256 internal l2MessageIndex;
uint256 internal l2TxNumberInBlock;

function setUp() public {
l2MessageIndex = 123456789;
l2TxNumberInBlock = 23456;
}

function test_WhenChainContractReturnsTrue() public {
vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock),
abi.encode(true)
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock)
);

bool res = bridgehead.isEthWithdrawalFinalized(chainId, l2MessageIndex, l2TxNumberInBlock);
assertEq(res, true, "ETH withdrawal should be finalized");
}

function test_WhenChainContractReturnsFalse() public {
vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock),
abi.encode(false)
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(IChainGetters.isEthWithdrawalFinalized.selector, l2MessageIndex, l2TxNumberInBlock)
);

bool res = bridgehead.isEthWithdrawalFinalized(chainId, l2MessageIndex, l2TxNumberInBlock);
assertEq(res, false, "ETH withdrawal should not be finalized");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

/* solhint-disable max-line-length */

import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol";
import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol";

/* solhint-enable max-line-length */

contract L2TransactionBaseCostTest is BridgeheadMailboxTest {
uint256 internal gasPrice;
uint256 internal l2GasLimit;
uint256 internal l2GasPerPubdataByteLimit;

function setUp() public {
gasPrice = 123456789;
l2GasLimit = 234567890;
l2GasPerPubdataByteLimit = 345678901;
}

function test_RevertWhen_InternalCallReverts() public {
bytes memory revertMessage = "random revert";

vm.mockCallRevert(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.l2TransactionBaseCost.selector,
gasPrice,
l2GasLimit,
l2GasPerPubdataByteLimit
),
revertMessage
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.l2TransactionBaseCost.selector,
gasPrice,
l2GasLimit,
l2GasPerPubdataByteLimit
)
);

vm.expectRevert(revertMessage);
bridgehead.l2TransactionBaseCost(chainId, gasPrice, l2GasLimit, l2GasPerPubdataByteLimit);
}

function test_ShouldReturnReceivedCanonicalTxHash() public {
uint256 expectedBaseCost = 123456789;

vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.l2TransactionBaseCost.selector,
gasPrice,
l2GasLimit,
l2GasPerPubdataByteLimit
),
abi.encode(expectedBaseCost)
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.l2TransactionBaseCost.selector,
gasPrice,
l2GasLimit,
l2GasPerPubdataByteLimit
)
);

uint256 baseCost = bridgehead.l2TransactionBaseCost(chainId, gasPrice, l2GasLimit, l2GasPerPubdataByteLimit);
assertEq(baseCost, expectedBaseCost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

/* solhint-disable max-line-length */

import {BridgeheadMailboxTest} from "./_BridgeheadMailbox_Shared.t.sol";
import {TxStatus} from "../../../../../../cache/solpp-generated-contracts/common/Messaging.sol";
import {IMailbox} from "../../../../../../cache/solpp-generated-contracts/bridgehead/chain-interfaces/IMailbox.sol";

/* solhint-enable max-line-length */

contract ProveL1ToL2TransactionStatusTest is BridgeheadMailboxTest {
uint256 internal blockNumber;
bytes32 internal l2TxHash;
uint256 internal l2BlockNumber;
uint256 internal l2MessageIndex;
uint16 internal l2TxNumberInBlock;
bytes32[] internal merkleProof;
TxStatus internal status;

function setUp() public {
l2TxHash = bytes32(uint256(123456789));
l2BlockNumber = 3456789;
l2MessageIndex = 234567890;
l2TxNumberInBlock = 12345;
merkleProof = new bytes32[](1);
status = TxStatus.Success;
}

function test_WhenChainContractReturnsTrue() public {
vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.proveL1ToL2TransactionStatus.selector,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
),
abi.encode(true)
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.proveL1ToL2TransactionStatus.selector,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
)
);

bool res = bridgehead.proveL1ToL2TransactionStatus(
chainId,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
);
assertEq(res, true, "L1 to L2 transaction status should be proven");
}

function test_WhenChainContractReturnsFalse() public {
vm.mockCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.proveL1ToL2TransactionStatus.selector,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
),
abi.encode(false)
);

vm.expectCall(
bridgehead.getChainContract(chainId),
abi.encodeWithSelector(
IMailbox.proveL1ToL2TransactionStatus.selector,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
)
);

bool res = bridgehead.proveL1ToL2TransactionStatus(
chainId,
l2TxHash,
l2BlockNumber,
l2MessageIndex,
l2TxNumberInBlock,
merkleProof,
status
);
assertEq(res, false, "L1 to L2 transaction status should not be proven");
}
}
Loading