Skip to content

Commit

Permalink
(feat) Update storage layout.
Browse files Browse the repository at this point in the history
(feat) Start to adapt tests
  • Loading branch information
rrw-zilliqa committed Jan 7, 2025
1 parent bc83607 commit 0034e16
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ contract LockProxyTokenManagerUpgradeableV4 is TokenManagerUpgradeableV4, ILockP
_disableInitializers();
}

function reinitialize(uint fees) external reinitializer(2) {
_setFees(fees);
}

// Incoming currency - transfer into the lock proxy (directly!)
function _handleTransfer(address token, address from, uint amount) internal override {
address lockProxyAddress = getLockProxy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ abstract contract TokenManagerUpgradeableV4 is
address gateway;
// localTokenAddress => remoteChainId => RemoteToken
mapping(address => mapping(uint => RemoteToken)) remoteTokens;
// This stores the scale for remote tokens, as remote_token.decimals-local_token.decimals.
// So, when sending a token with a +ve scale, we shift left.
// When sending a token with a -ve scale, we shift right.
// When receiving a token, we do nothing.
// This allows us to validate that the tokens can be exactly converted to the remote
// and revert the sending txn if not.
mapping(address => mapping(uint => int8)) scaleForRemoteTokens;
}

// keccak256(abi.encode(uint256(keccak256("zilliqa.storage.TokenManager")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant Token_Manager_Storage_Location =
0x4a6c2e6a7e6518c249bdcd1d934ea16ea5325bbae105af814eb678f5f49f3400;


function _getTokenManagerStorage()
private
pure
Expand All @@ -106,27 +112,6 @@ abstract contract TokenManagerUpgradeableV4 is
}
}

struct TokenManagerV4Storage {
// This stores the scale for remote tokens, as remote_token.decimals-local_token.decimals.
// So, when sending a token with a +ve scale, we shift left.
// When sending a token with a -ve scale, we shift right.
// When receiving a token, we do nothing.
// This allows us to validate that the tokens can be exactly converted to the remote
// and revert the sending txn if not.
mapping(address => mapping(uint => int8)) scaleForRemoteTokens;
}

// keccack256(abi.encode(uint256(keccak256("zilliqa.storage.TokenManagerV4"))-1))& ~bytes32(uint256(0xff))
bytes32 private constant Token_ManagerV4_Storage_Location =
0xe8b1c929e9ac4c16aaeb9d6494126adb9c7e3b297332aeb86accee6d41c67500;

function _getTokenManagerV4Storage()
private pure returns (TokenManagerV4Storage storage $)
{
assembly {
$.slot := Token_ManagerV4_Storage_Location
}
}

function getGateway() public view returns (address) {
TokenManagerStorage storage $ = _getTokenManagerStorage();
Expand All @@ -142,7 +127,7 @@ abstract contract TokenManagerUpgradeableV4 is
}

function getRemoteTokenScale(address token, uint remoteChainId) public view returns (int8) {
TokenManagerV4Storage storage $ = _getTokenManagerV4Storage();
TokenManagerStorage storage $ = _getTokenManagerStorage();
return $.scaleForRemoteTokens[token][remoteChainId];
}

Expand Down Expand Up @@ -172,8 +157,7 @@ abstract contract TokenManagerUpgradeableV4 is
function _removeToken(address localToken, uint remoteChainId) internal {
TokenManagerStorage storage $ = _getTokenManagerStorage();
delete $.remoteTokens[localToken][remoteChainId];
TokenManagerV4Storage storage $$ = _getTokenManagerV4Storage();
delete $$.scaleForRemoteTokens[localToken][remoteChainId];
delete $.scaleForRemoteTokens[localToken][remoteChainId];
emit TokenRemoved(localToken, remoteChainId);
}

Expand All @@ -194,7 +178,7 @@ abstract contract TokenManagerUpgradeableV4 is
function _setScaleForToken(address localToken,
uint remoteChainId,
int8 scale) internal {
TokenManagerV4Storage storage $ = _getTokenManagerV4Storage();
TokenManagerStorage storage $ = _getTokenManagerStorage();
$.scaleForRemoteTokens[localToken][remoteChainId] = scale;
emit TokenScaleChanged( localToken,
remoteChainId,
Expand All @@ -203,8 +187,8 @@ abstract contract TokenManagerUpgradeableV4 is


function _getScaleForToken(address localToken,
uint remoteChainId) internal returns (int8) {
TokenManagerV4Storage storage $ = _getTokenManagerV4Storage();
uint remoteChainId) internal view returns (int8) {
TokenManagerStorage storage $ = _getTokenManagerStorage();
return $.scaleForRemoteTokens[localToken][remoteChainId];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract deployZilBridgeTokenManagers is Script, LockProxyTokenManagerDeployer,
allowedTokens[1] = bscBridgedZRC2Address;
allowedTokens[2] = bscBridgedZILAddress;
LockProxyProxy lockProxyProxy = new LockProxyProxy(allowedTokens, vm.addr(validatorPrivateKey), address(lockProxy));
LockProxyTokenManagerUpgradeableV3 tokenManager = deployLatestLockProxyTokenManager(address(chainGateway), address(lockProxy), address(lockProxyProxy), fees);
LockProxyTokenManagerUpgradeableV3 tokenManager = deployLockProxyTokenManagerV3(address(chainGateway), address(lockProxy), address(lockProxyProxy), fees);
lockProxyProxy.addCaller(address(tokenManager));
console.log(
" address public constant bscLockProxyTokenManagerAddress = %s", address(tokenManager));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract Deployment is Script, LockAndReleaseOrNativeTokenManagerDeployer,Testne

vm.startBroadcast(deployerPrivateKey);
LockAndReleaseOrNativeTokenManagerUpgradeableV3 tokenManager =
deployLatestLockAndReleaseOrNativeTokenManager(chainGatewayAddress, fees);
deployLockAndReleaseOrNativeTokenManagerV3(chainGatewayAddress, fees);
console.log(
"LockAndReleaseOrNativeTokenManager Proxy deployed to %s, with owner %s and gateway %s",
address(tokenManager),
Expand Down
14 changes: 7 additions & 7 deletions smart-contracts/test/periphery/TokenBridge.integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ pragma solidity 0.8.20;

import {Tester, Vm} from "test/Tester.sol";
import {ITokenManagerStructs, TokenManagerUpgradeable} from "contracts/periphery/TokenManagerUpgradeable.sol";
import {LockAndReleaseTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockAndReleaseTokenManagerUpgradeableV3.sol";
import {MintAndBurnTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/MintAndBurnTokenManagerUpgradeableV3.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockAndReleaseOrNativeTokenManagerUpgradeableV4.sol";
import {MintAndBurnTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/MintAndBurnTokenManagerUpgradeableV4.sol";
import {BridgedToken} from "contracts/periphery/BridgedToken.sol";
import {CallMetadata, IRelayerEvents} from "contracts/core/Relayer.sol";
import {ValidatorManager} from "contracts/core/ValidatorManager.sol";
import {ChainGateway} from "contracts/core/ChainGateway.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {TestToken} from "test/Helpers.sol";
import {LockAndReleaseTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/LockAndReleaseTokenManagerDeployer.sol";
import {LockAndReleaseOrNativeTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/LockAndReleaseOrNativeTokenManagerDeployer.sol";
import {MintAndBurnTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/MintAndBurnTokenManagerDeployer.sol";

// Integration Tests combining the TokenManagers and ChainGateway
contract TokenBridgeIntegrationTests is
Tester,
IRelayerEvents,
LockAndReleaseTokenManagerDeployer,
LockAndReleaseOrNativeTokenManagerDeployer,
MintAndBurnTokenManagerDeployer
{
using MessageHashUtils for bytes;
Expand All @@ -32,12 +32,12 @@ contract TokenBridgeIntegrationTests is
uint originalTokenSupply = 1000 ether;
uint fees = 0.1 ether;

LockAndReleaseTokenManagerUpgradeableV3 sourceTokenManager;
LockAndReleaseOrNativeTokenManagerUpgradeableV4 sourceTokenManager;
TestToken originalToken;
ChainGateway sourceChainGateway;
ValidatorManager sourceValidatorManager;

MintAndBurnTokenManagerUpgradeableV3 remoteTokenManager;
MintAndBurnTokenManagerUpgradeableV4 remoteTokenManager;
BridgedToken bridgedToken;
ChainGateway remoteChainGateway;
ValidatorManager remoteValidatorManager;
Expand All @@ -61,7 +61,7 @@ contract TokenBridgeIntegrationTests is
);

// Deploy LockAndReleaseTokenManagerUpgradeable
sourceTokenManager = deployLatestLockAndReleaseTokenManager(
sourceTokenManager = deployLatestLockAndReleaseOrNativeTokenManager(
address(sourceChainGateway),
fees
);
Expand Down
6 changes: 4 additions & 2 deletions smart-contracts/test/periphery/TokenBridge.native.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pragma solidity 0.8.20;
import {Tester, Vm} from "test/Tester.sol";
import {ITokenManagerStructs, TokenManagerUpgradeable} from "contracts/periphery/TokenManagerUpgradeable.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockAndReleaseOrNativeTokenManagerUpgradeableV3.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockAndReleaseOrNativeTokenManagerUpgradeableV4.sol";
import {MintAndBurnTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/MintAndBurnTokenManagerUpgradeableV3.sol";
import {MintAndBurnTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/MintAndBurnTokenManagerUpgradeableV4.sol";
import {BridgedToken} from "contracts/periphery/BridgedToken.sol";
import {CallMetadata, IRelayerEvents} from "contracts/core/Relayer.sol";
import {ValidatorManager} from "contracts/core/ValidatorManager.sol";
Expand Down Expand Up @@ -32,12 +34,12 @@ contract TokenBridgeNativeTests is
uint originalTokenSupply = 1000 ether;
uint fees = 0.1 ether;

LockAndReleaseOrNativeTokenManagerUpgradeableV3 sourceTokenManager;
LockAndReleaseOrNativeTokenManagerUpgradeableV4 sourceTokenManager;
TestToken originalToken;
ChainGateway sourceChainGateway;
ValidatorManager sourceValidatorManager;

MintAndBurnTokenManagerUpgradeableV3 remoteTokenManager;
MintAndBurnTokenManagerUpgradeableV4 remoteTokenManager;
BridgedToken bridgedToken;
ChainGateway remoteChainGateway;
ValidatorManager remoteValidatorManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.s
import {LockAndReleaseTokenManagerUpgradeable} from "contracts/periphery/LockAndReleaseTokenManagerUpgradeable.sol";
import {LockAndReleaseTokenManagerUpgradeableV2} from "contracts/periphery/TokenManagerV2/LockAndReleaseTokenManagerUpgradeableV2.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockAndReleaseOrNativeTokenManagerUpgradeableV3.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockAndReleaseOrNativeTokenManagerUpgradeableV4.sol";
import {LockAndReleaseTokenManagerDeployer} from "./LockAndReleaseTokenManagerDeployer.sol";

abstract contract LockAndReleaseOrNativeTokenManagerDeployer is
Expand All @@ -27,10 +28,22 @@ abstract contract LockAndReleaseOrNativeTokenManagerDeployer is
return LockAndReleaseOrNativeTokenManagerUpgradeableV3(payable(address(proxy)));
}

function deployLockAndReleaseOrNativeTokenManagerV4(
address chainGateway,
uint fees
) public returns (LockAndReleaseOrNativeTokenManagerUpgradeableV4) {
LockAndReleaseOrNativeTokenManagerUpgradeableV3 proxy = deployLockAndReleaseOrNativeTokenManagerV3(
chainGateway,
fees);
address newImplementation = address(new LockAndReleaseOrNativeTokenManagerUpgradeableV4());
proxy.upgradeToAndCall(newImplementation, "");
return LockAndReleaseOrNativeTokenManagerUpgradeableV4(payable(address(proxy)));
}

function deployLatestLockAndReleaseOrNativeTokenManager(
address chainGateway,
uint fees
) public returns (LockAndReleaseOrNativeTokenManagerUpgradeableV3) {
return deployLockAndReleaseOrNativeTokenManagerV3(chainGateway, fees);
) public returns (LockAndReleaseOrNativeTokenManagerUpgradeableV4) {
return deployLockAndReleaseOrNativeTokenManagerV4(chainGateway, fees);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ abstract contract LockAndReleaseTokenManagerDeployer {
return LockAndReleaseTokenManagerUpgradeableV3(address(proxy));
}


function deployLatestLockAndReleaseTokenManager(
address chainGateway,
uint fees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.s
import {MintAndBurnTokenManagerUpgradeable} from "contracts/periphery/MintAndBurnTokenManagerUpgradeable.sol";
import {MintAndBurnTokenManagerUpgradeableV2} from "contracts/periphery/TokenManagerV2/MintAndBurnTokenManagerUpgradeableV2.sol";
import {MintAndBurnTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/MintAndBurnTokenManagerUpgradeableV3.sol";
import {MintAndBurnTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/MintAndBurnTokenManagerUpgradeableV4.sol";

abstract contract MintAndBurnTokenManagerDeployer {
function deployMintAndBurnTokenManagerV1(
Expand Down Expand Up @@ -66,10 +67,23 @@ abstract contract MintAndBurnTokenManagerDeployer {
return MintAndBurnTokenManagerUpgradeableV3(address(proxy));
}

function deployMintAndBurnTokenManagerV4(
address chainGateway,
uint fees
) public returns (MintAndBurnTokenManagerUpgradeableV4) {
MintAndBurnTokenManagerUpgradeableV3 proxy = deployMintAndBurnTokenManagerV3(
chainGateway,
fees
);
address newImplementation = address(
new MintAndBurnTokenManagerUpgradeableV4()
);
return MintAndBurnTokenManagerUpgradeableV4(address(proxy));
}
function deployLatestMintAndBurnTokenManager(
address chainGateway,
uint fees
) public returns (MintAndBurnTokenManagerUpgradeableV3) {
return deployMintAndBurnTokenManagerV3(chainGateway, fees);
) public returns (MintAndBurnTokenManagerUpgradeableV4) {
return deployMintAndBurnTokenManagerV4(chainGateway, fees);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.20;

import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {LockProxyTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockProxyTokenManagerUpgradeableV3.sol";
import {LockProxyTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockProxyTokenManagerUpgradeableV4.sol";
import {LockProxyTokenManagerUpgradeable} from "contracts/periphery/LockProxyTokenManagerUpgradeable.sol";

abstract contract LockProxyTokenManagerDeployer {
Expand All @@ -29,7 +30,18 @@ abstract contract LockProxyTokenManagerDeployer {
return LockProxyTokenManagerUpgradeableV3(address(proxy));
}

function deployLatestLockProxyTokenManager(address chainGateway, address lockProxy, address lockProxyProxy, uint fees) public returns (LockProxyTokenManagerUpgradeableV3) {
return deployLockProxyTokenManagerV3(chainGateway, lockProxy, lockProxyProxy, fees);
function deployLockProxyTokenManagerV4(
address chainGateway,
address lockProxyAddress,
address lockProxyProxyAddress,
uint fees) public returns (LockProxyTokenManagerUpgradeableV4) {
LockProxyTokenManagerUpgradeableV3 proxy = deployLockProxyTokenManagerV3(chainGateway, lockProxyAddress, lockProxyProxyAddress, fees);
address newImplementation = address(new LockProxyTokenManagerUpgradeableV4());
proxy.upgradeToAndCall(newImplementation, "");
return LockProxyTokenManagerUpgradeableV4(address(proxy));
}

function deployLatestLockProxyTokenManager(address chainGateway, address lockProxy, address lockProxyProxy, uint fees) public returns (LockProxyTokenManagerUpgradeableV4) {
return deployLockProxyTokenManagerV4(chainGateway, lockProxy, lockProxyProxy, fees);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ pragma solidity 0.8.20;
import "forge-std/console.sol";
import {Tester, Vm} from "test/Tester.sol";
import {ITokenManagerStructs, TokenManagerUpgradeable} from "contracts/periphery/TokenManagerUpgradeable.sol";
import {LockAndReleaseTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockAndReleaseTokenManagerUpgradeableV3.sol";
import {MintAndBurnTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/MintAndBurnTokenManagerUpgradeableV3.sol";
import {LockAndReleaseOrNativeTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockAndReleaseOrNativeTokenManagerUpgradeableV4.sol";
import {MintAndBurnTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/MintAndBurnTokenManagerUpgradeableV4.sol";
import {BridgedToken} from "contracts/periphery/BridgedToken.sol";
import { LockProxyProxy } from "contracts/periphery/LockProxyProxy.sol";
import {CallMetadata, IRelayerEvents} from "contracts/core/Relayer.sol";
import {ValidatorManager} from "contracts/core/ValidatorManager.sol";
import {ChainGateway} from "contracts/core/ChainGateway.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {TestToken} from "test/Helpers.sol";
import {LockProxyTokenManagerUpgradeableV3} from "contracts/periphery/TokenManagerV3/LockProxyTokenManagerUpgradeableV3.sol";
import {LockProxyTokenManagerUpgradeableV4} from "contracts/periphery/TokenManagerV4/LockProxyTokenManagerUpgradeableV4.sol";
import {LockProxyTokenManagerDeployer} from "test/zilbridge/TokenManagerDeployers/LockProxyTokenManagerDeployer.sol";
import {MintAndBurnTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/MintAndBurnTokenManagerDeployer.sol";
import {LockAndReleaseTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/LockAndReleaseTokenManagerDeployer.sol";
import {LockAndReleaseOrNativeTokenManagerDeployer} from "test/periphery/TokenManagerDeployers/LockAndReleaseOrNativeTokenManagerDeployer.sol";
import { SwitcheoToken } from "test/zilbridge/tokens/switcheo/tokens/SwitcheoTokenETH.sol";
import { ZilBridgeFixture } from "test/zilbridge/DeployZilBridge.t.sol";
import { MockLockProxy } from "./MockLockProxy.sol";
Expand All @@ -29,7 +29,7 @@ import { MockLockProxy } from "./MockLockProxy.sol";
* Since the word "native" is quite heavily overloaded, we use "gas" to describe the gas token - ETH on ethereum, for example.
*/
contract ZilBridgeTokenBridgeIntegrationFixture is
Tester, IRelayerEvents, LockAndReleaseTokenManagerDeployer, LockProxyTokenManagerDeployer, ZilBridgeFixture {
Tester, IRelayerEvents, LockAndReleaseOrNativeTokenManagerDeployer, LockProxyTokenManagerDeployer, ZilBridgeFixture {
using MessageHashUtils for bytes;

// Gateway shared between the two chains
Expand All @@ -41,7 +41,7 @@ Tester, IRelayerEvents, LockAndReleaseTokenManagerDeployer, LockProxyTokenManage
uint originalTokenSupply = 1000 ether;
uint fees = 0.1 ether;

LockProxyTokenManagerUpgradeableV3 sourceTokenManager;
LockProxyTokenManagerUpgradeableV4 sourceTokenManager;
LockProxyProxy lockProxyProxy;

// There are "actually" three of these - native (which is lock/release), a mint/burn token and a conventional token.
Expand All @@ -54,7 +54,7 @@ Tester, IRelayerEvents, LockAndReleaseTokenManagerDeployer, LockProxyTokenManage
// see doc/zilbridge.md
MockLockProxy mockRemoteLockProxy;
LockProxyProxy remoteLockProxyProxy;
LockProxyTokenManagerUpgradeableV3 remoteTokenManager;
LockProxyTokenManagerUpgradeableV4 remoteTokenManager;
SwitcheoToken remoteNativelyOnSource;
SwitcheoToken remoteBridgedGasToken;
TestToken nativelyOnRemote;
Expand Down

0 comments on commit 0034e16

Please sign in to comment.