Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koloz193 committed Jun 10, 2024
1 parent 65bef9c commit 4d745a5
Show file tree
Hide file tree
Showing 31 changed files with 295 additions and 216 deletions.
30 changes: 14 additions & 16 deletions l1-contracts/contracts/bridge/L1SharedBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {ETH_TOKEN_ADDRESS, TWO_BRIDGES_MAGIC_VALUE} from "../common/Config.sol";
import {IBridgehub, L2TransactionRequestTwoBridgesInner, L2TransactionRequestDirect} from "../bridgehub/IBridgehub.sol";
import {IGetters} from "../state-transition/chain-interfaces/IGetters.sol";
import {L2_BASE_TOKEN_SYSTEM_CONTRACT_ADDR} from "../common/L2ContractAddresses.sol";
import {Unauthorized, ZeroAddress, SharedBridgeValueAlreadySet, SharedBridgeKey, NoFundsTransferred, ZeroBalance, ValueMismatch, NonEmptyMsgValue, L2BridgeNotDeployed, TokenNotSupported, WithdrawIncorrectAmount, EmptyDeposit, DepositExists, AddressAlreadyUsed, InvalidProof, DepositDNE, InsufficientFunds, DepositFailed, ShareadBridgeValueNotSet, WithdrawalAlreadyFinalized, WithdrawFailed, MalformedMessage, InvalidSelector} from "../common/L1ContractErrors.sol";
import {Unauthorized, ZeroAddress, SharedBridgeValueAlreadySet, SharedBridgeKey, NoFundsTransferred, ZeroBalance, ValueMismatch, NonEmptyMsgValue, L2BridgeNotSet, TokenNotSupported, WithdrawIncorrectAmount, EmptyDeposit, DepositExists, AddressAlreadyUsed, InvalidProof, DepositDNE, InsufficientChainBalance, WithdrawalFailed, ShareadBridgeValueNotSet, WithdrawalAlreadyFinalized, WithdrawalFailed, L2WithdrawalMessageWrongLength, InvalidSelector, SharedBridgeBalanceMismatch} from "../common/L1ContractErrors.sol";

/// @author Matter Labs
/// @custom:security-contact [email protected]
Expand Down Expand Up @@ -116,7 +116,9 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade

/// @notice Checks that the message sender is the shared bridge itself.
modifier onlySelf() {
require(msg.sender == address(this), "ShB not shared bridge");
if (msg.sender != address(this)) {
revert Unauthorized(msg.sender);
}
_;
}

Expand Down Expand Up @@ -201,13 +203,9 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
}
IL1ERC20Bridge(_target).transferTokenToSharedBridge(_token);
uint256 balanceAfter = IERC20(_token).balanceOf(address(this));
<<<<<<< HEAD
if (balanceAfter - balanceBefore != legacyBridgeBalance) {
revert ValueMismatch(balanceAfter - balanceBefore, legacyBridgeBalance);
if (balanceAfter - balanceBefore < legacyBridgeBalance) {
revert SharedBridgeBalanceMismatch();
}
=======
require(balanceAfter - balanceBefore >= legacyBridgeBalance, "ShB: wrong amount transferred");
>>>>>>> protocol-defense
chainBalance[_targetChainId][_token] = chainBalance[_targetChainId][_token] + legacyBridgeBalance;
}
}
Expand Down Expand Up @@ -296,7 +294,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
returns (L2TransactionRequestTwoBridgesInner memory request)
{
if (l2BridgeAddress[_chainId] == address(0)) {
revert L2BridgeNotDeployed(_chainId);
revert L2BridgeNotSet(_chainId);
}

(address _l1Token, uint256 _depositAmount, address _l2Receiver) = abi.decode(
Expand Down Expand Up @@ -499,7 +497,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
if (!hyperbridgingEnabled[_chainId]) {
// check that the chain has sufficient balance
if (chainBalance[_chainId][_l1Token] < _amount) {
revert InsufficientFunds();
revert InsufficientChainBalance();
}
chainBalance[_chainId][_l1Token] -= _amount;
}
Expand All @@ -512,7 +510,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
callSuccess := call(gas(), _depositSender, _amount, 0, 0, 0, 0)
}
if (!callSuccess) {
revert DepositFailed();
revert WithdrawalFailed();
}
} else {
IERC20(_l1Token).safeTransfer(_depositSender, _amount);
Expand Down Expand Up @@ -641,7 +639,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
// Check that the chain has sufficient balance
if (chainBalance[_chainId][l1Token] < amount) {
// not enough funds
revert InsufficientFunds();
revert InsufficientChainBalance();
}
chainBalance[_chainId][l1Token] -= amount;
}
Expand All @@ -653,7 +651,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
callSuccess := call(gas(), l1Receiver, amount, 0, 0, 0, 0)
}
if (!callSuccess) {
revert WithdrawFailed();
revert WithdrawalFailed();
}
} else {
// Withdraw funds
Expand Down Expand Up @@ -711,7 +709,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
// So the data is expected to be at least 56 bytes long.
// wrong message length
if (_l2ToL1message.length < 56) {
revert MalformedMessage();
revert L2WithdrawalMessageWrongLength(_l2ToL1message.length);
}

(uint32 functionSignature, uint256 offset) = UnsafeBytes.readUint32(_l2ToL1message, 0);
Expand All @@ -729,7 +727,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
// It should be equal to the length of the function signature + address + address + uint256 = 4 + 20 + 20 + 32 =
// 76 (bytes).
if (_l2ToL1message.length != 76) {
revert MalformedMessage();
revert L2WithdrawalMessageWrongLength(_l2ToL1message.length);
}
(l1Receiver, offset) = UnsafeBytes.readAddress(_l2ToL1message, offset);
(l1Token, offset) = UnsafeBytes.readAddress(_l2ToL1message, offset);
Expand Down Expand Up @@ -777,7 +775,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
address _refundRecipient
) external payable override onlyLegacyBridge nonReentrant whenNotPaused returns (bytes32 l2TxHash) {
if (l2BridgeAddress[ERA_CHAIN_ID] == address(0)) {
revert L2BridgeNotDeployed(ERA_CHAIN_ID);
revert L2BridgeNotSet(ERA_CHAIN_ID);
}
if (_l1Token == L1_WETH_TOKEN) {
revert TokenNotSupported(L1_WETH_TOKEN);
Expand Down
25 changes: 9 additions & 16 deletions l1-contracts/contracts/bridgehub/Bridgehub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

pragma solidity 0.8.24;

<<<<<<< HEAD
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
=======
// solhint-disable reason-string, gas-custom-errors

import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable-v4/access/Ownable2StepUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable-v4/security/PausableUpgradeable.sol";
>>>>>>> protocol-defense

import {L2TransactionRequestDirect, L2TransactionRequestTwoBridgesOuter, L2TransactionRequestTwoBridgesInner} from "./IBridgehub.sol";
import {IBridgehub, IL1SharedBridge} from "../bridge/interfaces/IL1SharedBridge.sol";
Expand All @@ -20,7 +13,7 @@ import {IZkSyncHyperchain} from "../state-transition/chain-interfaces/IZkSyncHyp
import {ETH_TOKEN_ADDRESS, TWO_BRIDGES_MAGIC_VALUE, BRIDGEHUB_MIN_SECOND_BRIDGE_ADDRESS} from "../common/Config.sol";
import {BridgehubL2TransactionRequest, L2Message, L2Log, TxStatus} from "../common/Messaging.sol";
import {AddressAliasHelper} from "../vendor/AddressAliasHelper.sol";
import {Unauthorized, STMAlreadyRegistered, STMNotRegistered, TokenAlreadyRegistered, TokenNotRegistered, InvalidChainId, WethBridgeNotSet, BridgeHubAlreadyRegistered, ValueMismatch, InsufficientFunds, NonEmptyMsgValue, AddressTooLow} from "../common/L1ContractErrors.sol";
import {Unauthorized, STMAlreadyRegistered, STMNotRegistered, TokenAlreadyRegistered, TokenNotRegistered, ZeroChainId, ChainIdTooBig, BridgeNotSet, BridgeHubAlreadyRegistered, AddressTooLow, MsgValueMismatch, WrongMagicValue} from "../common/L1ContractErrors.sol";

contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, PausableUpgradeable {
/// @notice all the ether is held by the weth bridge
Expand Down Expand Up @@ -137,10 +130,10 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
bytes calldata _initData
) external onlyOwnerOrAdmin nonReentrant whenNotPaused returns (uint256) {
if (_chainId == 0) {
revert InvalidChainId();
revert ZeroChainId();
}
if (_chainId > type(uint48).max) {
revert InvalidChainId();
revert ChainIdTooBig();
}

if (!stateTransitionManagerIsRegistered[_stateTransitionManager]) {
Expand All @@ -150,7 +143,7 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
revert TokenNotRegistered(_baseToken);
}
if (address(sharedBridge) == address(0)) {
revert WethBridgeNotSet();
revert BridgeNotSet();
}

if (stateTransitionManager[_chainId] != address(0)) {
Expand Down Expand Up @@ -242,11 +235,11 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
address token = baseToken[_request.chainId];
if (token == ETH_TOKEN_ADDRESS) {
if (msg.value != _request.mintValue) {
revert InsufficientFunds();
revert MsgValueMismatch(_request.mintValue, msg.value);
}
} else {
if (msg.value != 0) {
revert NonEmptyMsgValue();
revert MsgValueMismatch(0, msg.value);
}
}

Expand Down Expand Up @@ -293,12 +286,12 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
uint256 baseTokenMsgValue;
if (token == ETH_TOKEN_ADDRESS) {
if (msg.value != _request.mintValue + _request.secondBridgeValue) {
revert InsufficientFunds();
revert MsgValueMismatch(_request.mintValue + _request.secondBridgeValue, msg.value);
}
baseTokenMsgValue = _request.mintValue;
} else {
if (msg.value != _request.secondBridgeValue) {
revert NonEmptyMsgValue();
revert MsgValueMismatch(_request.secondBridgeValue, msg.value);
}
baseTokenMsgValue = 0;
}
Expand All @@ -323,7 +316,7 @@ contract Bridgehub is IBridgehub, ReentrancyGuard, Ownable2StepUpgradeable, Paus
);

if (outputRequest.magicValue != TWO_BRIDGES_MAGIC_VALUE) {
revert ValueMismatch(uint256(TWO_BRIDGES_MAGIC_VALUE), uint256(outputRequest.magicValue));
revert WrongMagicValue(uint256(TWO_BRIDGES_MAGIC_VALUE), uint256(outputRequest.magicValue));
}

address refundRecipient = AddressAliasHelper.actualRefundRecipient(_request.refundRecipient, msg.sender);
Expand Down
108 changes: 100 additions & 8 deletions l1-contracts/contracts/common/L1ContractErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ error TokenAlreadyRegistered(address token);
error TokenNotRegistered(address token);
// 0x7a47c9a2
error InvalidChainId();
// 0xe4ed5fcc
error WethBridgeNotSet();
// 0x6cf12312
error BridgeHubAlreadyRegistered();
// 0x1eee5481
Expand All @@ -79,8 +77,6 @@ error PreviousOperationNotExecuted();
error HashMismatch(bytes32 expected, bytes32 actual);
// 0xb615c2b1
error HyperchainLimitReached();
// 0x71dcf049
error TimeNotReached();
// 0xf0b4e88f
error TooMuchGas();
// 0x59170bf0
Expand Down Expand Up @@ -127,8 +123,6 @@ error UpgradeBatchNumberIsNotZero();
error NonSequentialBatch();
// 0x00c6ead2
error CantExecuteUnprovenBatches();
// 0x213eb372
error SystemLogsSizeOverflow();
// 0xd8e9405c
error InvalidNumberOfBlobs(uint256 expected, uint256 numCommitments, uint256 numHashes);
// 0x2dbdba00
Expand Down Expand Up @@ -193,14 +187,112 @@ error InvalidUpgradeTxn(UpgradeTxVerifyParam);
error NotEnoughGas();
// 0x5cb29523
error InvalidTxType(uint256 txType);
// 0x4ee1b0ed
error NewProtocolVersionNotInUpgradeTxn();
// 0x07218375
error UnexpectedNumberOfFactoryDeps();
// 0x101ba748
error PreviousUpgradeNotFinalized(bytes32 txHash);
// 0xa0f47245
error PreviousUpgradeNotCleaned();
// 0x826fb11e
error InsufficientChainBalance();
// 0x27fcd9d1
error WithdrawalFailed();
// 0x97e1359e
error L2WithdrawalMessageWrongLength(uint256 messageLen);
// 0xff8811ff
error L2BridgeNotSet(uint256 chainId);
// 0xc84885d4
error ZeroChainId();
// 0x8f620a06
error ChainIdTooBig();
// 0x733fa4c3
error BridgeNotSet();
// 0x4a094431
error MsgValueMismatch(uint256 expectedMsgValue, uint256 providedMsgValue);
// 0x15e8e429
error WrongMagicValue(uint256 expectedMagicValue, uint256 providedMagicValue);
// 0xdd7e3621
error NotInitializedReentrancyGuard();
// 0xab143c06
error Reentrancy();
// 0xe37d2c02
error LengthIsNotDivisibleBy32(uint256 length);
// 0x0a8ed92c
error DenominatorIsZero();
// 0xa461f651
error ProtocolIdMismatch(uint256 expectedProtocolVersion, uint256 providedProtocolId);
// 0x64f94ec2
error ProtocolIdNotGreater();
// 0x0e7ee319
error DiamondAlreadyFrozen();
// 0xbd4455ff
error BatchNumberMismatch(uint256 expectedBatchNumber, uint256 providedBatchNumber);
// 0xec0b4118
error CalldataLenghtTooBig();

Check warning on line 231 in l1-contracts/contracts/common/L1ContractErrors.sol

View workflow job for this annotation

GitHub Actions / typos

"Lenght" should be "Length".
// 0x5513177c
error InvalidPubdataHash(bytes32 expectedHash, bytes32 provided);
// 0xfb5c22e6
error L2TimestampTooBig();
// 0xf640f0e5
error TooManyBlobs();
// 0xd5a99014
error PriorityOperationsRollingHashMismatch();
// 0xae43b424
error SystemLogsSizeTooBig();
// 0x8d5851de
error PointEvalCallFailed(bytes);
// 0x53dee67b
error PubdataCommitmentsEmpty();
// 0x7734c31a
error PubdataCommitmentsTooBig();
// 0x53e6d04d
error InvalidPubdataCommitmentsSize();
// 0x32eb8b2f
error LegacyMethodIsSupportedOnlyForEra();
// 0x3580370c
error ReplaceFunctionFacetAddressZero();
// 0x667d17de
error RemoveFunctionFacetAddressNotZero(address facet);
// 0xf7a01e4d
error DelegateCallFailed(bytes returnData);
// 0x959f26fb
error PubdataGreaterThanLimit(uint256 limit, uint256 length);
// 0x2e311df8
error TxnBodyGasLimitNotEnoughGas();
// 0x47b3b145
error ValidateTxnNotEnoughGas();
// 0x08753982
error TimeNotReached(uint256 expectedTimestamp, uint256 actualTimestamp);
// 0xd7f50a9d
error PatchCantSetUpgradeTxn();
// 0xd2c011d6
error L2UpgradeNonceNotEqualToNewProtocolVersion(uint256 nonce, uint256 procotolVersion);

Check warning on line 269 in l1-contracts/contracts/common/L1ContractErrors.sol

View workflow job for this annotation

GitHub Actions / typos

"procotol" should be "protocol".
// 0xcb5e4247
error L2BytecodeHashMismatch(bytes32 expected, bytes32 provided);
// 0x88d7b498
error ProtocolVersionTooSmall();
// 0x56d45b12
error ProtocolVersionTooBig();
// 0x5c598b60
error PreviousProtocolMajorVersionNotZero();
// 0x72ea85ad
error NewProtocolMajorVersionNotZero();
// 0xd328c12a
error ProtocolVersionMinorDeltaTooBig(uint256 limit, uint256 proposed);
// 0x559cc34e
error PatchUpgradeCantSetDefaultAccount();
// 0x962fd7d0
error PatchUpgradeCantSetBootloader();
// 0xc1d9246c
error SharedBridgeBalanceMismatch();
// 0x3a1a8589
error GenesisUpgradeZero();
// 0x7940c83f
error GenesisBatchHashZero();
// 0xb4fc6835
error GenesisIndexStorageZero();
// 0x6d4a7df8
error GenesisBatchCommitmentZero();

enum SharedBridgeKey {
PostUpgradeFirstBatch,
Expand Down
7 changes: 5 additions & 2 deletions l1-contracts/contracts/common/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.20;

import {SlotOccupied} from "./L1ContractErrors.sol";
import {SlotOccupied, NotInitializedReentrancyGuard, Reentrancy} from "./L1ContractErrors.sol";

/**
* @custom:security-contact [email protected]
Expand Down Expand Up @@ -76,8 +76,11 @@ abstract contract ReentrancyGuard {
}

// On the first call to nonReentrant, _notEntered will be true
if (_status == 0) {
revert NotInitializedReentrancyGuard();
}
if (_status != _NOT_ENTERED) {
revert SlotOccupied();
revert Reentrancy();
}

// Any calls to nonReentrant after this point will fail
Expand Down
4 changes: 2 additions & 2 deletions l1-contracts/contracts/common/libraries/L2ContractHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.20;

import {BytecodeError, MalformedBytecode} from "../L1ContractErrors.sol";
import {BytecodeError, MalformedBytecode, LengthIsNotDivisibleBy32} from "../L1ContractErrors.sol";

/**
* @author Matter Labs
Expand All @@ -23,7 +23,7 @@ library L2ContractHelper {
function hashL2Bytecode(bytes memory _bytecode) internal pure returns (bytes32 hashedBytecode) {
// Note that the length of the bytecode must be provided in 32-byte words.
if (_bytecode.length % 32 != 0) {
revert MalformedBytecode(BytecodeError.Length);
revert LengthIsNotDivisibleBy32(_bytecode.length);
}

uint256 bytecodeLenInWords = _bytecode.length / 32;
Expand Down
Loading

0 comments on commit 4d745a5

Please sign in to comment.