Skip to content

Commit

Permalink
Add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Sep 16, 2024
1 parent 184f75d commit bef3572
Show file tree
Hide file tree
Showing 5 changed files with 2,225 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity ^0.8.20;
import "../../interfaces/IBasePolygonZkEVMGlobalExitRoot.sol";

interface IPolygonZkEVMBridgeL2SovereignChains {
interface IBridgeL2SovereignChains {
/**
* @dev Thrown when the destination network is invalid
*/
Expand Down Expand Up @@ -100,6 +100,16 @@ interface IPolygonZkEVMBridgeL2SovereignChains {
*/
error NotValidBridgeManager();

/**
* @dev Thrown when try to set a zero address to a non valid zero address field
*/
error InvalidZeroAddress();

/**
* @dev Thrown when the origin network is invalid
*/
error OriginNetworkInvalid();

function wrappedTokenToTokenInfo(
address destinationAddress
) external view returns (uint32, address);
Expand Down
35 changes: 21 additions & 14 deletions contracts/v2/sovereignChains/BridgeL2SovereignChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20Metadat
import "../../lib/TokenWrapped.sol";
import "../../interfaces/IBasePolygonZkEVMGlobalExitRoot.sol";
import "../../interfaces/IBridgeMessageReceiver.sol";
import "../interfaces/IPolygonZkEVMBridgeL2SovereignChains.sol";
import "../interfaces/IBridgeL2SovereignChains.sol";
import "../../lib/EmergencyManager.sol";
import "../../lib/GlobalExitRootLib.sol";

/**
* PolygonZkEVMBridge that will be deployed on Ethereum and all Polygon rollups
* Sovereign chains bridge that will be deployed on Ethereum and all Sovereign chains
* Contract responsible to manage the token interactions with other networks
*/
contract BridgeL2SovereignChain is
DepositContractV2,
EmergencyManager,
IPolygonZkEVMBridgeL2SovereignChains
IBridgeL2SovereignChains
{
using SafeERC20Upgradeable for IERC20Upgradeable;

Expand Down Expand Up @@ -150,7 +150,7 @@ contract BridgeL2SovereignChain is
* @param _gasTokenAddress gas token address
* @param _gasTokenNetwork gas token network
* @param _globalExitRootManager global exit root manager address
* @param _polygonRollupManager polygonZkEVM address
* @param _polygonRollupManager Rollup manager address
* @notice The value of `_polygonRollupManager` on the L2 deployment of the contract will be address(0), so
* emergency state is not possible for the L2 deployment of the bridge, intentionally
* @param _gasTokenMetadata Abi encoded gas token metadata
Expand Down Expand Up @@ -224,18 +224,18 @@ contract BridgeL2SovereignChain is
* @param permitData Raw data of the call `permit` of the token
*/
function bridgeAsset(
uint32 destinationNetwork, // 1
address destinationAddress, // whatever
uint256 amount, // 1
address token, // ETH
uint32 destinationNetwork,
address destinationAddress,
uint256 amount,
address token,
bool forceUpdateGlobalExitRoot,
bytes calldata permitData
) public payable virtual ifNotEmergencyState nonReentrant {
if (destinationNetwork == networkID) {
revert DestinationNetworkInvalid();
}

address originTokenAddress; // Address of the token at the origin network
address originTokenAddress;
uint32 originNetwork;
bytes memory metadata;
uint256 leafAmount = amount;
Expand Down Expand Up @@ -819,6 +819,7 @@ contract BridgeL2SovereignChain is
* origin token.
* @notice If this function is called multiple times for the same existingTokenAddress,
* this will override the previous calls and only keep the last sovereignTokenAddress.
* @notice The tokenInfoToWrappedToken mapping value is replaced by the new sovereign address but it's not the case for the wrappedTokenToTokenInfo map where the value is added, this way user will always be able to withdraw their tokens
* @param originNetwork Origin network
* @param originTokenAddress Origin token address, 0 address is reserved for ether
* @param sovereignTokenAddress Address of the sovereign wrapped token
Expand All @@ -830,15 +831,21 @@ contract BridgeL2SovereignChain is
address sovereignTokenAddress,
bool isNotMintable
) external onlyBridgeManager {
// TODO: Add checks
// - origin and sovereign token address are not 0
// - originnetwork != current network, wrapped always other networks
// Handle claimAsset on target chain
// origin and sovereign token address are not 0
if(originTokenAddress == address(0) || sovereignTokenAddress == address(0)) {
revert InvalidZeroAddress();
}
// originnetwork != current network, wrapped tokens are always from other networks
if (originNetwork == networkID) {
revert OriginNetworkInvalid();
}
// Compute token info hash
bytes32 tokenInfoHash = keccak256(
abi.encodePacked(originNetwork, originTokenAddress)
);
// Set the address of the wrapper
tokenInfoToWrappedToken[tokenInfoHash] = sovereignTokenAddress;
// todo: comment: you will always can withdraw (explain)
// Set the token info mapping
wrappedTokenToTokenInfo[sovereignTokenAddress] = TokenInformation(
originNetwork,
originTokenAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ contract GlobalExitRootManagerL2SovereignChain is
// Store every global exit root: Root --> timestamp
mapping(bytes32 => uint256) public globalExitRootMap;

// Rollup exit root will be updated for every PolygonZkEVMBridge call
// Rollup exit root will be updated for every Sovereign chain call
bytes32 public lastRollupExitRoot;

// PolygonZkEVM Bridge address
// Sovereign chain Bridge address
address public immutable bridgeAddress;

/**
* @dev Emitted when a new global exit root is inserted
*/
event InsertGlobalExitRoot(
bytes32 indexed newGlobalExitRoot
);
event InsertGlobalExitRoot(bytes32 indexed newGlobalExitRoot);

/**
* @param _bridgeAddress PolygonZkEVMBridge contract address
* @param _bridgeAddress BridgeL2SovereignChain contract address
*/
constructor(address _bridgeAddress) {
bridgeAddress = _bridgeAddress;
Expand All @@ -45,11 +43,21 @@ contract GlobalExitRootManagerL2SovereignChain is
_;
}

/**
* @notice Only allows a function to be callable by the bride contract
*/
modifier onlyBridgeAddress() {
if (msg.sender != bridgeAddress) {
revert OnlyAllowedContracts();
}
_;
}

/**
* @notice Update the exit root of one of the networks and the global exit root
* @param newRoot new exit tree root
*/
function updateExitRoot(bytes32 newRoot) external onlyTrustedSequencer {
function updateExitRoot(bytes32 newRoot) external onlyBridgeAddress() {
lastRollupExitRoot = newRoot;
}

Expand All @@ -59,9 +67,7 @@ contract GlobalExitRootManagerL2SovereignChain is
// do not update timestamp if already set
if (globalExitRootMap[_newRoot] == 0) {
globalExitRootMap[_newRoot] = block.timestamp;
emit InsertGlobalExitRoot(
_newRoot
);
emit InsertGlobalExitRoot(_newRoot);
}
}
}
Loading

0 comments on commit bef3572

Please sign in to comment.