Skip to content

Commit

Permalink
Merge branch 'kl-factory' into bh-evm-192-unit-tests
Browse files Browse the repository at this point in the history
# Conflicts:
#	ethereum/test/unit_tests/diamond_cut_test.spec.ts
#	ethereum/test/unit_tests/executor_test.spec.ts
  • Loading branch information
benceharomi committed Oct 4, 2023
2 parents 8c276fc + d79960a commit d5618f0
Show file tree
Hide file tree
Showing 58 changed files with 2,594 additions and 556 deletions.
2 changes: 2 additions & 0 deletions docs/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function applyL1ToL2Alias(address l1Address) internal pure returns (address l2Ad
l2Address = address(uint160(l1Address) + offset);
}
}
```

For most of the rollups the address aliasing needs to prevent cross-chain exploits that would otherwise be possible if
Expand Down Expand Up @@ -285,6 +286,7 @@ struct Deposit {
bool depositLimitation;
uint256 depositCap;
}
```

Currently, the limit is used only for blocking deposits of the specific token (turning on the limitation and setting the
Expand Down
4 changes: 1 addition & 3 deletions ethereum/contracts/bridge/L1ERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,7 @@ contract L1ERC20Bridge is IL1Bridge, IL1BridgeLegacy, AllowListed, ReentrancyGua
bytes calldata _message,
bytes32[] calldata _merkleProof
) external nonReentrant senderCanCallFunction(allowList) {
{
require(!isWithdrawalFinalized[_chainId][_l2BlockNumber][_l2MessageIndex], "pw");
}
require(!isWithdrawalFinalized[_chainId][_l2BlockNumber][_l2MessageIndex], "pw");

L2Message memory l2ToL1Message = L2Message({
txNumberInBlock: _l2TxNumberInBlock,
Expand Down
6 changes: 5 additions & 1 deletion ethereum/contracts/bridge/interfaces/IL2Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ interface IL2Bridge {
bytes calldata _data
) external payable;

function withdraw(address _l1Receiver, address _l2Token, uint256 _amount) external;
function withdraw(
address _l1Receiver,
address _l2Token,
uint256 _amount
) external;

function l1TokenAddress(address _l2Token) external view returns (address);

Expand Down
6 changes: 5 additions & 1 deletion ethereum/contracts/bridge/interfaces/IL2ERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ pragma solidity ^0.8.13;

/// @author Matter Labs
interface IL2ERC20Bridge {
function initialize(address _l1Bridge, bytes32 _l2TokenProxyBytecodeHash, address _governor) external;
function initialize(
address _l1Bridge,
bytes32 _l2TokenProxyBytecodeHash,
address _governor
) external;
}
6 changes: 5 additions & 1 deletion ethereum/contracts/bridge/interfaces/IL2WethBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
pragma solidity ^0.8.13;

interface IL2WethBridge {
function initialize(address _l1Bridge, address _l1WethAddress, address _l2WethAddress) external;
function initialize(
address _l1Bridge,
address _l1WethAddress,
address _l2WethAddress
) external;
}
2 changes: 1 addition & 1 deletion ethereum/contracts/bridgehead/Bridgehead.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract Bridgehead is BridgeheadGetters, BridgeheadMailbox, Registry {
IAllowList _allowList,
uint256 _priorityTxMaxGasLimit
) public {
require(bridgeheadStorage.chainImplementation == address(0), "r1");
require(bridgeheadStorage.chainImplementation == address(0), "bridgehead1");
bridgeheadStorage.governor = _governor;
bridgeheadStorage.chainImplementation = _chainImplementation;
bridgeheadStorage.chainProxyAdmin = _chainProxyAdmin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
uint256 _l2TxNumberInBlock
) external view override returns (bool) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return IBridgeheadChain(chainContract).isEthWithdrawalFinalized(_l2MessageIndex, _l2TxNumberInBlock);
}

Expand All @@ -26,7 +25,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
bytes32[] calldata _proof
) external view override returns (bool) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return IBridgeheadChain(chainContract).proveL2MessageInclusion(_blockNumber, _index, _message, _proof);
}

Expand All @@ -38,7 +36,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
bytes32[] calldata _proof
) external view override returns (bool) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return IBridgeheadChain(chainContract).proveL2LogInclusion(_blockNumber, _index, _log, _proof);
}

Expand All @@ -52,7 +49,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
TxStatus _status
) external view override returns (bool) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return
IBridgeheadChain(chainContract).proveL1ToL2TransactionStatus(
_l2TxHash,
Expand All @@ -75,7 +71,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
address _refundRecipient
) public payable override returns (bytes32 canonicalTxHash) {
address chainContract = bridgeheadStorage.chainContract[_chainId];

canonicalTxHash = IBridgeheadChain(chainContract).requestL2TransactionBridgehead{value: msg.value}(
msg.sender,
_contractL2,
Expand All @@ -97,7 +92,6 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
bytes32[] calldata _merkleProof
) external override {
address chainContract = bridgeheadStorage.chainContract[_chainId];

return
IBridgeheadChain(chainContract).finalizeEthWithdrawalBridgehead(
msg.sender,
Expand All @@ -113,7 +107,11 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {

/// @notice Transfer ether from the contract to the receiver
/// @dev Reverts only if the transfer call failed
function withdrawFunds(uint256 _chainId, address _to, uint256 _amount) external onlyChainContract(_chainId) {
function withdrawFunds(
uint256 _chainId,
address _to,
uint256 _amount
) external onlyChainContract(_chainId) {
bool callSuccess;
// Low-level assembly call, to avoid any memory copying (save gas)
assembly {
Expand All @@ -122,26 +120,13 @@ contract BridgeheadMailbox is BridgeheadBase, IBridgeheadMailbox {
require(callSuccess, "pz");
}

function requestL2TransactionProof(
uint256 _chainId,
WritePriorityOpParams memory _params,
bytes calldata _calldata,
bytes[] calldata _factoryDeps,
bool _isFree
) external override returns (bytes32 canonicalTxHash) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return IBridgeheadChain(chainContract).requestL2TransactionProof(_params, _calldata, _factoryDeps, _isFree);
}

function l2TransactionBaseCost(
uint256 _chainId,
uint256 _gasPrice,
uint256 _l2GasLimit,
uint256 _l2GasPerPubdataByteLimit
) external view returns (uint256) {
address chainContract = bridgeheadStorage.chainContract[_chainId];
require(chainContract != address(0), "r1");
return IBridgeheadChain(chainContract).l2TransactionBaseCost(_gasPrice, _l2GasLimit, _l2GasPerPubdataByteLimit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ interface IBridgeheadForProof is IBridgeheadBase {
function collectOperationsFromPriorityQueue(uint256 _chainId, uint256 _index) external returns (bytes32 concatHash);

/// @notice Adding txs to the priority queue
function addL2Logs(uint256 _chainId, uint256 _index, bytes32 _l2LogsRootHashes) external;
function addL2Logs(
uint256 _chainId,
uint256 _index,
bytes32 _l2LogsRootHashes
) external;

function requestL2TransactionProof(
uint256 _chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import "../chain-interfaces/IMailboxEvents.sol";
interface IBridgeheadMailbox is IMailboxEvents {
function deposit(uint256 _chainId) external payable;

function withdrawFunds(uint256 _chainId, address _to, uint256 _amount) external;
function withdrawFunds(
uint256 _chainId,
address _to,
uint256 _amount
) external;

function isEthWithdrawalFinalized(
uint256 _chainId,
Expand Down Expand Up @@ -63,14 +67,6 @@ interface IBridgeheadMailbox is IMailboxEvents {
address _refundRecipient
) external payable returns (bytes32 canonicalTxHash);

function requestL2TransactionProof(
uint256 _chainId,
WritePriorityOpParams memory _params,
bytes calldata _calldata,
bytes[] calldata _factoryDeps,
bool _isFree
) external returns (bytes32 canonicalTxHash);

function l2TransactionBaseCost(
uint256 _chainId,
uint256 _gasPrice,
Expand Down
10 changes: 7 additions & 3 deletions ethereum/contracts/bridgehead/chain-deps/ChainExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ contract ChainExecutor is IChainExecutor, ChainBase {
function executeBlocks() external override nonReentrant {}

/// @return concatHash , Returns the concatenated Hash of operations from the priority queue
function collectOperationsFromPriorityQueue(
uint256 _nPriorityOps
) external override nonReentrant onlyProofChain returns (bytes32 concatHash) {
function collectOperationsFromPriorityQueue(uint256 _nPriorityOps)
external
override
nonReentrant
onlyProofChain
returns (bytes32 concatHash)
{
concatHash = EMPTY_STRING_KECCAK;
require(_nPriorityOps <= chainStorage.priorityQueue.getSize(), "g1");

Expand Down
58 changes: 31 additions & 27 deletions ethereum/contracts/bridgehead/chain-deps/Mailbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ contract Mailbox is IMailbox, ChainBase {
uint16 _l2TxNumberInBlock,
bytes calldata _message,
bytes32[] calldata _merkleProof
) external onlyBridgehead {
return
_finalizeEthWithdrawalSender(
_sender,
_l2BlockNumber,
_l2MessageIndex,
_l2TxNumberInBlock,
_message,
_merkleProof
);
) external onlyBridgehead knownSenderCanCallFunction(_sender, chainStorage.allowList) {
_finalizeEthWithdrawalSender(
_sender,
_l2BlockNumber,
_l2MessageIndex,
_l2TxNumberInBlock,
_message,
_merkleProof
);
}

// this is implemented in the bridghead, does not go through the router.
Expand All @@ -51,7 +50,13 @@ contract Mailbox is IMailbox, ChainBase {
uint256 _l2GasPerPubdataByteLimit,
bytes[] calldata _factoryDeps,
address _refundRecipient
) external payable onlyBridgehead returns (bytes32 canonicalTxHash) {
)
external
payable
onlyBridgehead
knownSenderCanCallFunction(_sender, chainStorage.allowList)
returns (bytes32 canonicalTxHash)
{
canonicalTxHash = _requestL2TransactionSender(
_sender,
_contractL2,
Expand Down Expand Up @@ -143,7 +148,7 @@ contract Mailbox is IMailbox, ChainBase {
) internal view returns (bool) {
// kl todo is this even needed? as we only add logs in executeblocks.
// But if it is needed we need to update totalBlocksExecuted
// require(_blockNumber <= chainStorage.totalBlocksExecuted, "xx");
require(_blockNumber <= chainStorage.totalBlocksExecuted, "xx");

bytes32 hashedLog = keccak256(
abi.encodePacked(_log.l2ShardId, _log.isService, _log.txNumberInBlock, _log.sender, _log.key, _log.value)
Expand Down Expand Up @@ -211,7 +216,7 @@ contract Mailbox is IMailbox, ChainBase {
uint16 _l2TxNumberInBlock,
bytes calldata _message,
bytes32[] calldata _merkleProof
) public override {
) public override knownSenderCanCallFunction(msg.sender, chainStorage.allowList) {
_finalizeEthWithdrawalSender(
msg.sender,
_l2BlockNumber,
Expand All @@ -235,7 +240,7 @@ contract Mailbox is IMailbox, ChainBase {
uint16 _l2TxNumberInBlock,
bytes calldata _message,
bytes32[] calldata _merkleProof
) public nonReentrant knownSenderCanCallFunction(_sender, chainStorage.allowList) {
) internal nonReentrant {
require(!chainStorage.isEthWithdrawalFinalized[_l2BlockNumber][_l2MessageIndex], "jj");

L2Message memory l2ToL1Message = L2Message({
Expand Down Expand Up @@ -267,7 +272,7 @@ contract Mailbox is IMailbox, ChainBase {
uint256 _l2GasPerPubdataByteLimit,
bytes[] calldata _factoryDeps,
address _refundRecipient
) public payable returns (bytes32 canonicalTxHash) {
) public payable knownSenderCanCallFunction(msg.sender, chainStorage.allowList) returns (bytes32 canonicalTxHash) {
canonicalTxHash = _requestL2TransactionSender(
msg.sender,
_contractL2,
Expand Down Expand Up @@ -307,12 +312,7 @@ contract Mailbox is IMailbox, ChainBase {
uint256 _l2GasPerPubdataByteLimit,
bytes[] calldata _factoryDeps,
address _refundRecipient
)
internal
nonReentrant
knownSenderCanCallFunction(_sender, chainStorage.allowList)
returns (bytes32 canonicalTxHash)
{
) internal nonReentrant returns (bytes32 canonicalTxHash) {
// Change the sender address if it is a smart contract to prevent address collision between L1 and L2.
// Please note, currently zkSync address derivation is different from Ethereum one, but it may be changed in the future.
address sender = _sender;
Expand Down Expand Up @@ -457,9 +457,11 @@ contract Mailbox is IMailbox, ChainBase {
}

/// @notice Hashes the L2 bytecodes and returns them in the format in which they are processed by the bootloader
function _hashFactoryDeps(
bytes[] calldata _factoryDeps
) internal pure returns (uint256[] memory hashedFactoryDeps) {
function _hashFactoryDeps(bytes[] calldata _factoryDeps)
internal
pure
returns (uint256[] memory hashedFactoryDeps)
{
uint256 factoryDepsLen = _factoryDeps.length;
hashedFactoryDeps = new uint256[](factoryDepsLen);
for (uint256 i = 0; i < factoryDepsLen; i = i.uncheckedInc()) {
Expand All @@ -473,9 +475,11 @@ contract Mailbox is IMailbox, ChainBase {
}

/// @dev Decode the withdraw message that came from L2
function _parseL2WithdrawalMessage(
bytes memory _message
) internal pure returns (address l1Receiver, uint256 amount) {
function _parseL2WithdrawalMessage(bytes memory _message)
internal
pure
returns (address l1Receiver, uint256 amount)
{
// We check that the message is long enough to read the data.
// Please note that there are two versions of the message:
// 1. The message that is sent by `withdraw(address _l1Receiver)`
Expand Down
2 changes: 1 addition & 1 deletion ethereum/contracts/common/AllowListed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract contract AllowListed {
modifier knownSenderCanCallFunction(address _sender, IAllowList _allowList) {
// Preventing the stack too deep error
{
require(_allowList.canCall(_sender, address(this), msg.sig), "nr");
require(_allowList.canCall(_sender, address(this), msg.sig), "nr2");
}
_;
}
Expand Down
27 changes: 22 additions & 5 deletions ethereum/contracts/common/interfaces/IAllowList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ interface IAllowList {

function getAccessMode(address _target) external view returns (AccessMode);

function hasSpecialAccessToCall(address _caller, address _target, bytes4 _functionSig) external view returns (bool);

function canCall(address _caller, address _target, bytes4 _functionSig) external view returns (bool);
function hasSpecialAccessToCall(
address _caller,
address _target,
bytes4 _functionSig
) external view returns (bool);

function canCall(
address _caller,
address _target,
bytes4 _functionSig
) external view returns (bool);

function getTokenDepositLimitData(address _l1Token) external view returns (Deposit memory);

Expand All @@ -58,11 +66,20 @@ interface IAllowList {
bool[] calldata _enables
) external;

function setPermissionToCall(address _caller, address _target, bytes4 _functionSig, bool _enable) external;
function setPermissionToCall(
address _caller,
address _target,
bytes4 _functionSig,
bool _enable
) external;

/*//////////////////////////////////////////////////////////////
DEPOSIT LIMIT LOGIC
//////////////////////////////////////////////////////////////*/

function setDepositLimit(address _l1Token, bool _depositLimitation, uint256 _depositCap) external;
function setDepositLimit(
address _l1Token,
bool _depositLimitation,
uint256 _depositCap
) external;
}
Loading

0 comments on commit d5618f0

Please sign in to comment.