@@ -53,14 +53,21 @@ import {IChainAssetHandlerBase} from "../../core/chain-asset-handler/IChainAsset
5353import {IL1MessageRoot} from "../../core/message-root/IL1MessageRoot.sol " ;
5454
5555contract L1AssetTracker is AssetTrackerBase , IL1AssetTracker {
56- /// @dev Per-(chainId, assetId) migration accounting stored on L1.
56+ /// @notice Per-(chainId, assetId) migration accounting stored on L1.
5757 /// @param preV31ChainBalance Chain balance right before the v31 migration.
5858 /// - For non-native tokens it is exactly equal to chainBalance before the *ecosystem* upgraded to v31 (0 for new tokens).
5959 /// - For tokens native to the chain, we imagine that it received 2^256-1 token deposit at the inception point and so
6060 /// all the balances that are not present on the chain are from claimed withdrawals, i.e. for a token that was bridged
6161 /// before v31 it is equal to `2^256-1 - <sum of other chainBalances, including l1>`. For new tokens it is exactly `2^256-1`.
62- /// @param totalDepositedFromL1 Total amount deposited from L1 to the chain since v31 accounting started.
63- /// @param totalClaimedOnL1 Total amount claimed on L1 (withdrawals and failed deposits) since v31 accounting started.
62+ /// @param totalDepositedFromL1 Total amount deposited from L1 to the chain since v31 accounting started. Note, that it is not
63+ /// just about any L1->L2 deposit, but only those that debited the chainBalance on L1 directly and it is assumed that every such
64+ /// deposit will be processed while the chain is still settling on L1. It is the responsbility of the chain admin to ensure that.
65+ /// @param totalClaimedOnL1 Total amount claimed on L1 (withdrawals and failed deposits) since v31 accounting started. Note, that it is not just
66+ /// about claim, but claims that affect `chainBalance` of the chain (i.e. the respective failed deposits or withdrawals were submitted
67+ /// while the chain was settling on L1)).
68+ /// @dev It is the responsibility of the *chain* and its admin to ensure that all deposits are processed before the migration to Gateway is complete
69+ /// and vice versa, i.e. all deposits are either fully processed on L1 or fully processed while it settles on ZK Gateway. In case the chain violates
70+ /// this rule, invalid migration amount can be migrated, but it must only affect the chain and its users.
6471 struct InteropL1Info {
6572 uint256 preV31ChainBalance;
6673 uint256 totalDepositedFromL1;
@@ -135,14 +142,13 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
135142 /// @dev Note, that this function performs O(number of chains) calls to NTV. It relies on the fact
136143 /// that the max number of chains is bound by 100, so this function should be always processable.
137144 /// @param _assetId The asset id of the token to migrate the token balance for.
138- function migrateTokenBalanceFromNTVV31 (bytes32 _assetId ) public {
145+ function registerLegacyToken (bytes32 _assetId ) public {
139146 IL1NativeTokenVault l1NTV = IL1NativeTokenVault (address (NATIVE_TOKEN_VAULT));
140147 uint256 originChainId = NATIVE_TOKEN_VAULT.originChainId (_assetId);
141148 require (originChainId != 0 , InvalidChainId ());
142149
143150 // This function is only intended to be used for legacy tokens that have not yet been registered.
144151 require (! isTokenRegistered[_assetId], "Max chain balance already assigned " );
145- isTokenRegistered[_assetId] = true ;
146152
147153 uint256 [] memory allZKChainIds = BRIDGE_HUB.getAllZKChainChainIDs ();
148154
@@ -154,7 +160,7 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
154160 // chainBalance inside the L1AT should never be incremented until the token is registered.
155161 require (chainBalance[chainId][_assetId] == 0 , "Chain balance already set " );
156162
157- // Origin chain id will be handled later.
163+ // Origin chain id will be handled later in this function .
158164 if (chainId == originChainId) {
159165 continue ;
160166 }
@@ -172,8 +178,10 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
172178 if (originChainId != block .chainid ) {
173179 address tokenAddress = NATIVE_TOKEN_VAULT.tokenAddress (_assetId);
174180 // Note, that here we have an implicit invariant that the token's total supply
175- // can never be changed before this migration happens.
176- // So until a token is registered, all withdrawals must fail.
181+ // can never be changed before this migration happens. So until a token is registered, all withdrawals must fail.
182+ // Note, that if a token is a bridged token native to L2, its representation on L1
183+ // is deployed by NativeTokenVault as `BridgedStandardERC20`, so we can safely assume the returned value
184+ // will be correct.
177185 uint256 migratedBalance = IERC20 (tokenAddress).totalSupply ();
178186 chainBalance[block .chainid ][_assetId] = migratedBalance;
179187 interopInfo[block .chainid ][_assetId].preV31ChainBalance = migratedBalance;
@@ -360,7 +368,7 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
360368
361369 // We check the assetId to make sure the chain is not lying about it.
362370 DataEncoding.assetIdCheck (data.tokenOriginChainId, data.assetId, data.originToken);
363- _requireMigratedFromNTV (data.chainId , data.assetId);
371+ _autoRegisterTokenFromMigration (data.tokenOriginChainId , data.assetId);
364372
365373 uint256 currentSettlementLayer = BRIDGE_HUB.settlementLayer (data.chainId);
366374 require (currentSettlementLayer != block .chainid , NotMigratedChain ());
@@ -389,7 +397,6 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
389397 require (fromChainBalance >= amountToKeep, InvalidMigrationAmount (fromChainBalance, amountToKeep));
390398 uint256 amountToMigrate = fromChainBalance - amountToKeep;
391399
392- _autoRegisterTokenFromMigration (data.tokenOriginChainId, data.assetId);
393400 _migrateFunds ({
394401 _fromChainId: data.chainId,
395402 _toChainId: currentSettlementLayer,
@@ -467,7 +474,7 @@ contract L1AssetTracker is AssetTrackerBase, IL1AssetTracker {
467474 /// @notice used to pause deposits on Gateway from L1 for migration back to L1.
468475 function requestPauseDepositsForChainOnGateway (uint256 _chainId ) external onlyChain (_chainId) {
469476 uint256 settlementLayer = BRIDGE_HUB.settlementLayer (_chainId);
470- require (settlementLayer != 0 , InvalidSettlementLayer ());
477+ require (settlementLayer != block . chainid , InvalidSettlementLayer ());
471478 _sendToChain (
472479 settlementLayer,
473480 GW_ASSET_TRACKER_ADDR,
0 commit comments