Skip to content

Commit

Permalink
(update): bae
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid Ateir committed Sep 17, 2024
2 parents f3e4b56 + 3b51ccf commit 089894c
Show file tree
Hide file tree
Showing 44 changed files with 768 additions and 200 deletions.
2 changes: 1 addition & 1 deletion l1-contracts/contracts/bridge/L1ERC20Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ contract L1ERC20Bridge is IL1ERC20Bridge, ReentrancyGuard {
chainId: ERA_CHAIN_ID,
l2BatchNumber: _l2BatchNumber,
l2MessageIndex: _l2MessageIndex,
l2Sender: l2Bridge,
l2Sender: L1_NULLIFIER.l2BridgeAddress(ERA_CHAIN_ID),
l2TxNumberInBatch: _l2TxNumberInBatch,
message: _message,
merkleProof: _merkleProof
Expand Down
9 changes: 9 additions & 0 deletions l1-contracts/contracts/bridge/L1Nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ contract L1Nullifier is IL1Nullifier, ReentrancyGuard, Ownable2StepUpgradeable,
return __DEPRECATED_l2BridgeAddress[_chainId];
}

/// @notice Legacy function used for migration, do not use!
/// @param _chainId The chain id we want to get the balance for.
/// @param _token The address of the token.
// slither-disable-next-line uninitialized-state-variables
function chainBalance(uint256 _chainId, address _token) external view returns (uint256) {
// slither-disable-next-line uninitialized-state-variables
return __DEPRECATED_chainBalance[_chainId][_token];
}

/// @notice Sets the L1ERC20Bridge contract address.
/// @dev Should be called only once by the owner.
/// @param _legacyBridge The address of the legacy bridge.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ contract L1AssetRouter is AssetRouterBase, IL1AssetRouter, ReentrancyGuard {
) external override {
/// @dev We use a deprecated field to support L2->L1 legacy withdrawals, which were started
/// by the legacy bridge.
address legacyL2Bridge = L1_NULLIFIER.__DEPRECATED_l2BridgeAddress(_chainId);
address legacyL2Bridge = L1_NULLIFIER.l2BridgeAddress(_chainId);
FinalizeL1DepositParams memory finalizeWithdrawalParams = FinalizeL1DepositParams({
chainId: _chainId,
l2BatchNumber: _l2BatchNumber,
Expand Down
12 changes: 9 additions & 3 deletions l1-contracts/contracts/bridge/asset-router/L2AssetRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ contract L2AssetRouter is AssetRouterBase, IL2AssetRouter {
uint256 _eraChainId,
address _l1AssetRouter,
address _legacySharedBridge,
bytes32 _baseTokenAssetId
bytes32 _baseTokenAssetId,
address _aliasedOwner
) AssetRouterBase(_l1ChainId, _eraChainId, IBridgehub(L2_BRIDGEHUB_ADDR)) {
L2_LEGACY_SHARED_BRIDGE = _legacySharedBridge;
if (_l1AssetRouter == address(0)) {
Expand All @@ -83,6 +84,7 @@ contract L2AssetRouter is AssetRouterBase, IL2AssetRouter {
assetHandlerAddress[_baseTokenAssetId] = L2_NATIVE_TOKEN_VAULT_ADDR;
BASE_TOKEN_ASSET_ID = _baseTokenAssetId;
_disableInitializers();
_transferOwnership(_aliasedOwner);
}

/// @inheritdoc IL2AssetRouter
Expand Down Expand Up @@ -164,8 +166,12 @@ contract L2AssetRouter is AssetRouterBase, IL2AssetRouter {
// slither-disable-next-line unused-return
L2ContractHelper.sendMessageToL1(message);
} else {
address l1Token = IL2NativeTokenVault(L2_NATIVE_TOKEN_VAULT_ADDR).tokenAddress(_assetId);
require(l1Token != address(0), "Unsupported asset Id by NTV");
address l1Token = IBridgedStandardToken(
IL2NativeTokenVault(L2_NATIVE_TOKEN_VAULT_ADDR).tokenAddress(_assetId)
).originToken();
if (l1Token == address(0)) {
revert AssetIdNotSupported(_assetId);
}
(uint256 amount, address l1Receiver) = abi.decode(_assetData, (uint256, address));
message = _getSharedBridgeWithdrawMessage(l1Receiver, l1Token, amount);
IL2SharedBridgeLegacy(L2_LEGACY_SHARED_BRIDGE).sendMessageToL1(message);
Expand Down
4 changes: 2 additions & 2 deletions l1-contracts/contracts/bridge/interfaces/IL1Nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ interface IL1Nullifier {

function setL1AssetRouter(address _l1AssetRouter) external;

function __DEPRECATED_chainBalance(uint256 _chainId, address _token) external view returns (uint256);
function chainBalance(uint256 _chainId, address _token) external view returns (uint256);

function __DEPRECATED_l2BridgeAddress(uint256 _chainId) external view returns (address);
function l2BridgeAddress(uint256 _chainId) external view returns (address);

function transferTokenToNTV(address _token) external;

Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/contracts/bridge/ntv/L1NativeTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ contract L1NativeTokenVault is IL1NativeTokenVault, IL1AssetHandler, NativeToken
/// @param _token The address of token to be transferred (address(1) for ether and contract address for ERC20).
/// @param _targetChainId The chain ID of the corresponding ZK chain.
function updateChainBalancesFromSharedBridge(address _token, uint256 _targetChainId) external {
uint256 nullifierChainBalance = L1_NULLIFIER.__DEPRECATED_chainBalance(_targetChainId, _token);
uint256 nullifierChainBalance = L1_NULLIFIER.chainBalance(_targetChainId, _token);
bytes32 assetId = DataEncoding.encodeNTVAssetId(block.chainid, _token);
chainBalance[_targetChainId][assetId] = chainBalance[_targetChainId][assetId] + nullifierChainBalance;
originChainId[assetId] = block.chainid;
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/contracts/bridge/ntv/NativeTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ abstract contract NativeTokenVault is INativeTokenVault, IAssetHandler, Ownable2
if (msg.value != 0) {
revert NonEmptyMsgValue();
}
_handleChainBalanceIncrease(_chainId, _assetId, amount, true);
amount = _depositAmount;
_handleChainBalanceIncrease(_chainId, _assetId, amount, true);
if (!_depositChecked) {
uint256 expectedDepositAmount = _depositFunds(_originalCaller, IERC20(nativeToken), _depositAmount); // note if _originalCaller is this contract, this will return 0. This does not happen.
// The token has non-standard transfer logic
Expand Down
10 changes: 5 additions & 5 deletions l1-contracts/contracts/common/L1ContractErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ error RemovingPermanentRestriction();
error UnallowedImplementation(bytes32 implementationHash);
// 0x1ff9d522
error AddressAlreadyUsed(address addr);
//
// 0x0dfb42bf
error AddressAlreadySet(address addr);
// 0x86bb51b8
error AddressHasNoCode(address);
//
// 0x1f73225f
error AddressMismatch(address expected, address supplied);
// 0x1eee5481
error AddressTooLow(address);
//
// 0x5e85ae73
error AmountMustBeGreaterThanZero();
//
// 0xfde974f4
error AssetHandlerDoesNotExist(bytes32 assetId);
//
// 0x1294e9e1
error AssetIdMismatch(bytes32 expected, bytes32 supplied);
//
error AssetIdAlreadyRegistered();
Expand Down
20 changes: 20 additions & 0 deletions l1-contracts/contracts/dev-contracts/L1NullifierDev.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.24;

import {L1Nullifier, IBridgehub} from "../bridge/L1Nullifier.sol";

contract L1NullifierDev is L1Nullifier {
constructor(
IBridgehub _bridgehub,
uint256 _eraChainId,
address _eraDiamondProxy
) L1Nullifier(_bridgehub, _eraChainId, _eraDiamondProxy) {}

function setL2LegacySharedBridge(uint256 _chainId, address _l2Bridge) external {
__DEPRECATED_l2BridgeAddress[_chainId] = _l2Bridge;
}

// add this to be excluded from coverage report
function test() internal virtual {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ struct ForceDeployment {
bytes input;
}

struct ZKChainSpecificForceDeploymentsData {
bytes32 baseTokenAssetId;
address l2LegacySharedBridge;
address l2Weth;
}

/// @author Matter Labs
/// @custom:security-contact [email protected]
interface IL2GenesisUpgrade {
Expand All @@ -23,6 +29,7 @@ interface IL2GenesisUpgrade {
function genesisUpgrade(
uint256 _chainId,
address _ctmDeployer,
bytes calldata _forceDeploymentsData
bytes calldata _fixedForceDeploymentsData,
bytes calldata _additionalForceDeploymentsData
) external payable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ library BatchDecoder {
uint256[] memory proof
)
{
if (_proofData.length == 0) {
revert EmptyData();
}

uint8 encodingVersion = uint8(_proofData[0]);
if (encodingVersion == SUPPORTED_ENCODING_VERSION) {
(prevBatch, provedBatches, proof) = abi.decode(
Expand Down
4 changes: 3 additions & 1 deletion l1-contracts/contracts/upgrades/GatewayUpgrade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ contract GatewayUpgrade is BaseZkSyncUpgrade, Initializable {

s.baseTokenAssetId = DataEncoding.encodeNTVAssetId(block.chainid, s.__DEPRECATED_baseToken);
s.priorityTree.setup(s.priorityQueue.getTotalPriorityTxs());
IBridgehub(s.bridgehub).setLegacyBaseTokenAssetId(s.chainId);
IBridgehub bridgehub = IBridgehub(s.bridgehub);
s.baseTokenBridge = bridgehub.sharedBridge(); // we change the assetRouter
bridgehub.setLegacyBaseTokenAssetId(s.chainId);
ProposedUpgrade memory proposedUpgrade = _proposedUpgrade;
address l2LegacyBridge = IL1SharedBridgeLegacy(s.baseTokenBridge).l2BridgeAddress(s.chainId);
proposedUpgrade.l2ProtocolUpgradeTx.data = bytes.concat(
Expand Down
21 changes: 18 additions & 3 deletions l1-contracts/contracts/upgrades/L1GenesisUpgrade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {Diamond} from "../state-transition/libraries/Diamond.sol";
import {BaseZkSyncUpgradeGenesis} from "./BaseZkSyncUpgradeGenesis.sol";
import {ProposedUpgrade} from "./IDefaultUpgrade.sol";
import {L2CanonicalTransaction} from "../common/Messaging.sol";
import {IL2GenesisUpgrade} from "../state-transition/l2-deps/IL2GenesisUpgrade.sol";
import {IL2GenesisUpgrade, ZKChainSpecificForceDeploymentsData} from "../state-transition/l2-deps/IL2GenesisUpgrade.sol";
import {IL1GenesisUpgrade} from "./IL1GenesisUpgrade.sol";
import {IL1Nullifier} from "../bridge/interfaces/IL1Nullifier.sol";
import {IL1AssetRouter} from "../bridge/asset-router/IL1AssetRouter.sol";
import {IComplexUpgrader} from "../state-transition/l2-deps/IComplexUpgrader.sol";
import {L2_FORCE_DEPLOYER_ADDR, L2_COMPLEX_UPGRADER_ADDR, L2_GENESIS_UPGRADE_ADDR} from "../common/L2ContractAddresses.sol"; //, COMPLEX_UPGRADER_ADDR, GENESIS_UPGRADE_ADDR
import {REQUIRED_L2_GAS_PRICE_PER_PUBDATA, SYSTEM_UPGRADE_L2_TX_TYPE, PRIORITY_TX_MAX_GAS_LIMIT} from "../common/Config.sol";
Expand All @@ -27,17 +29,18 @@ contract L1GenesisUpgrade is IL1GenesisUpgrade, BaseZkSyncUpgradeGenesis {
uint256 _chainId,
uint256 _protocolVersion,
address _l1CtmDeployerAddress,
bytes calldata _forceDeploymentsData,
bytes calldata _fixedForceDeploymentsData,
bytes[] calldata _factoryDeps
) public override returns (bytes32) {
L2CanonicalTransaction memory l2ProtocolUpgradeTx;

{
bytes memory complexUpgraderCalldata;
{
bytes memory additionalForceDeploymentsData = _getZKChainSpecificForceDeploymentsData();
bytes memory l2GenesisUpgradeCalldata = abi.encodeCall(
IL2GenesisUpgrade.genesisUpgrade,
(_chainId, _l1CtmDeployerAddress, _forceDeploymentsData)
(_chainId, _l1CtmDeployerAddress, _fixedForceDeploymentsData, additionalForceDeploymentsData)
);
complexUpgraderCalldata = abi.encodeCall(
IComplexUpgrader.upgrade,
Expand Down Expand Up @@ -101,4 +104,16 @@ contract L1GenesisUpgrade is IL1GenesisUpgrade, BaseZkSyncUpgradeGenesis {
super.upgrade(_proposedUpgrade);
return Diamond.DIAMOND_INIT_SUCCESS_RETURN_VALUE;
}

function _getZKChainSpecificForceDeploymentsData() internal view returns (bytes memory) {
IL1Nullifier l1Nullifier = IL1AssetRouter(s.baseTokenBridge).L1_NULLIFIER();
address legacySharedBridge = l1Nullifier.l2BridgeAddress(s.chainId);
ZKChainSpecificForceDeploymentsData
memory additionalForceDeploymentsData = ZKChainSpecificForceDeploymentsData({
baseTokenAssetId: s.baseTokenAssetId,
l2LegacySharedBridge: legacySharedBridge,
l2Weth: address(0) // kl todo
});
return abi.encode(additionalForceDeploymentsData);
}
}
3 changes: 2 additions & 1 deletion l1-contracts/deploy-scripts/DeployL1.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ contract DeployL1Script is Script {
}

function deployGenesisUpgrade() internal {
address contractAddress = deployViaCreate2(type(L1GenesisUpgrade).creationCode);
bytes memory bytecode = abi.encodePacked(type(L1GenesisUpgrade).creationCode);
address contractAddress = deployViaCreate2(bytecode);
console.log("GenesisUpgrade deployed at:", contractAddress);
addresses.stateTransition.genesisUpgrade = contractAddress;
}
Expand Down
42 changes: 0 additions & 42 deletions l1-contracts/deploy-scripts/GenerateForceDeploymentsData.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,48 +95,6 @@ contract GenerateForceDeploymentsData is Script {
address aliasedGovernance = AddressAliasHelper.applyL1ToL2Alias(config.governance);
ForceDeployment[] memory forceDeployments = new ForceDeployment[](4);

forceDeployments[0] = ForceDeployment({
bytecodeHash: keccak256(contracts.bridgehubBytecode),
newAddress: L2_BRIDGEHUB_ADDR,
callConstructor: true,
value: 0,
input: abi.encode(config.chainId, aliasedGovernance)
});

forceDeployments[1] = ForceDeployment({
bytecodeHash: keccak256(contracts.l2AssetRouterBytecode),
newAddress: L2_ASSET_ROUTER_ADDR,
callConstructor: true,
value: 0,
// solhint-disable-next-line func-named-parameters
input: abi.encode(config.chainId, config.eraChainId, config.l1AssetRouterProxy, address(1))
});

forceDeployments[2] = ForceDeployment({
bytecodeHash: keccak256(contracts.l2NtvBytecode),
newAddress: L2_NATIVE_TOKEN_VAULT_ADDR,
callConstructor: true,
value: 0,
// solhint-disable-next-line func-named-parameters
input: abi.encode(
config.chainId,
aliasedGovernance,
keccak256(contracts.l2TokenProxyBytecode),
config.l2LegacySharedBridge,
config.l2TokenBeacon,
config.contractsDeployedAlready
)
});

forceDeployments[3] = ForceDeployment({
bytecodeHash: keccak256(contracts.messageRootBytecode),
newAddress: L2_MESSAGE_ROOT_ADDR,
callConstructor: true,
value: 0,
// solhint-disable-next-line func-named-parameters
input: abi.encode(L2_BRIDGEHUB_ADDR)
});

config.forceDeploymentsData = abi.encode(forceDeployments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as fs from "fs";
import * as path from "path";
import { Deployer } from "../src.ts/deploy";
import { GAS_MULTIPLIER, web3Provider } from "./utils";
import { ADDRESS_ONE, encodeNTVAssetId } from "../src.ts/utils";
import { ADDRESS_ONE, encodeNTVAssetId, isCurrentNetworkLocal } from "../src.ts/utils";
import { getTokens } from "../src.ts/deploy-token";

const ETH_TOKEN_ADDRESS = ADDRESS_ONE;
Expand Down Expand Up @@ -103,7 +103,9 @@ async function main() {
if (!(await deployer.bridgehubContract(deployWallet).assetIdIsRegistered(baseTokenAssetId))) {
await deployer.registerTokenBridgehub(baseTokenAddress, cmd.useGovernance);
}
await deployer.registerTokenInNativeTokenVault(baseTokenAddress);
if (baseTokenAddress != ETH_TOKEN_ADDRESS) {
await deployer.registerTokenInNativeTokenVault(baseTokenAddress);
}
await deployer.registerZKChain(
baseTokenAssetId,
cmd.validiumMode,
Expand All @@ -112,7 +114,8 @@ async function main() {
true,
null,
null,
cmd.useGovernance
cmd.useGovernance,
isCurrentNetworkLocal() || cmd.localLegacyBridgeTesting
);

const tokenMultiplierSetterAddress = cmd.tokenMultiplierSetterAddress || "";
Expand Down
Loading

0 comments on commit 089894c

Please sign in to comment.