Skip to content

Commit

Permalink
fix buuug
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Jan 25, 2024
1 parent 00d6ea8 commit d8eff2b
Show file tree
Hide file tree
Showing 7 changed files with 709 additions and 380 deletions.
35 changes: 22 additions & 13 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always"
}
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"trailingComma": "es5",
"bracketSpacing": false,
"arrowParens": "always",
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always"
}
]
}
}
]
}
45 changes: 24 additions & 21 deletions contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../interfaces/IPolygonZkEVMBridge.sol";
import "./interfaces/IPolygonRollupBase.sol";
import "../lib/EmergencyManager.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "./lib/PolygonTransparentProxy.sol";
import "./lib/PolygonAccessControlUpgradeable.sol";
import "../interfaces/IVerifierRollup.sol";
import "./consensus/zkEVM/PolygonZkEVMV2Upgraded.sol";
Expand Down Expand Up @@ -330,6 +330,7 @@ contract PolygonRollupManager is
address verifier,
uint64 forkID,
bytes32 genesis,
uint8 rollupCompatibilityID,
string description
);

Expand Down Expand Up @@ -597,6 +598,7 @@ contract PolygonRollupManager is
address(verifier),
forkID,
genesis,
rollupCompatibilityID,
description
);
}
Expand Down Expand Up @@ -665,21 +667,10 @@ contract PolygonRollupManager is
// Consensus will be the implementation, and this contract the admin
uint32 rollupID = ++rollupCount;
address rollupAddress = address(
new TransparentUpgradeableProxy(
new PolygonTransparentProxy(
rollupType.consensusImplementation,
address(this),
abi.encodeCall(
IPolygonRollupBase.initialize,
(
admin,
trustedSequencer,
rollupID,
gasTokenAddress,
gasTokenNetwork,
trustedSequencerURL,
networkName
)
)
new bytes(0)
)
);

Expand All @@ -700,6 +691,17 @@ contract PolygonRollupManager is
rollup.rollupCompatibilityID = rollupType.rollupCompatibilityID;

emit CreateNewRollup(rollupID, rollupTypeID, rollupAddress, chainID);

// Initialize new rollup
IPolygonRollupBase(rollupAddress).initialize(
admin,
trustedSequencer,
rollupID,
gasTokenAddress,
gasTokenNetwork,
trustedSequencerURL,
networkName
);
}

/**
Expand Down Expand Up @@ -778,15 +780,16 @@ contract PolygonRollupManager is

//review, should check that there are not sequenced batches pending to be verified?¿?
//this way no one can break the virtual state, ( but maybe is worth to break it)

// Also upgrade address can send `upgradeData` to the proxy, being able to call `onVerifyBathes` maliciously
// To avoid attacks, should data be empty, or should check that does not call `onVerifyBatches`
/**
* @notice Upgrade an existing rollup
* @param rollupContract Rollup consensus proxy address
* @param newRollupTypeID New rolluptypeID to upgrade to
* @param upgradeData Upgrade data
*/
function updateRollup(
TransparentUpgradeableProxy rollupContract,
ITransparentUpgradeableProxy rollupContract,
uint32 newRollupTypeID,
bytes calldata upgradeData
) external onlyRole(_UPDATE_ROLLUP_ROLE) {
Expand Down Expand Up @@ -828,11 +831,11 @@ contract PolygonRollupManager is
rollup.rollupTypeID = newRollupTypeID;
rollup.lastVerifiedBatchBeforeUpgrade = getLastVerifiedBatch(rollupID);

// Upgrade rollup if the consensus implementation it's different
address newConsensusAddress = newRollupType.consensusImplementation;
if (rollupContract.implementation() != newConsensusAddress) {
rollupContract.upgradeToAndCall(newConsensusAddress, upgradeData);
}
// Upgrade rollup
rollupContract.upgradeToAndCall(
newRollupType.consensusImplementation,
upgradeData
);

emit UpdateRollup(
rollupID,
Expand Down
2 changes: 1 addition & 1 deletion contracts/v2/lib/PolygonAccessControlUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeabl
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

/**
* @dev Contrac AccessControlUpgradeable from Openzeppelin with the following modifications:
* @dev Contract AccessControlUpgradeable from Openzeppelin with the following modifications:
* - Delete ERC165Upgradeable dependencies, which is not important to our contract and save us the "gap"
* variables and let us have consistent storage
* - Add the legacy Owner variable, to be consistent with the previous one
Expand Down
80 changes: 80 additions & 0 deletions contracts/v2/lib/PolygonTransparentProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (proxy/transparent/TransparentUpgradeableProxy.sol)

pragma solidity ^0.8.20;

import {ERC1967Utils} from "@openzeppelin/contracts5/proxy/ERC1967/ERC1967Utils.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts5/proxy/ERC1967/ERC1967Proxy.sol";
import {IERC1967} from "@openzeppelin/contracts5/interfaces/IERC1967.sol";
import {ProxyAdmin} from "@openzeppelin/contracts5/proxy/transparent/ProxyAdmin.sol";
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts5/proxy/transparent/TransparentUpgradeableProxy.sol";

/**
* @dev Contrac TransparentUpgradeableProxy from Openzeppelin v5 with the following modifications:
* - Admin is a parameter in the constructor ( like previous versions) isntead of being deployed
* - Let the admin get access to the proxy
* - Add custom errors
* - Replace _msgSender() with msg.sender
*/
contract PolygonTransparentProxy is ERC1967Proxy {
// An immutable address for the admin to avoid unnecessary SLOADs before each call
// at the expense of removing the ability to change the admin once it's set.
// This is acceptable if the admin is always a ProxyAdmin instance or similar contract
// with its own ability to transfer the permissions to another account.
address private immutable _admin;

/**
* @dev Initializes an upgradeable proxy managed by an instance of a {ProxyAdmin} with an `initialOwner`,
* backed by the implementation at `_logic`, and optionally initialized with `_data` as explained in
* {ERC1967Proxy-constructor}.
*/
constructor(
address _logic,
address admin,
bytes memory _data
) payable ERC1967Proxy(_logic, _data) {
_admin = admin;
// Set the storage value and emit an event for ERC-1967 compatibility
ERC1967Utils.changeAdmin(_proxyAdmin());
}

/**
* @dev Returns the admin of this proxy.
*/
function _proxyAdmin() internal virtual returns (address) {
return _admin;
}

/**
* @dev If caller is the admin process the call internally, otherwise transparently fallback to the proxy behavior.
*/
function _fallback() internal virtual override {
if (msg.sender == _proxyAdmin()) {
if (
msg.sig !=
ITransparentUpgradeableProxy.upgradeToAndCall.selector
) {
super._fallback();
} else {
_dispatchUpgradeToAndCall();
}
} else {
super._fallback();
}
}

/**
* @dev Upgrade the implementation of the proxy. See {ERC1967Utils-upgradeToAndCall}.
*
* Requirements:
*
* - If `data` is empty, `msg.value` must be zero.
*/
function _dispatchUpgradeToAndCall() private {
(address newImplementation, bytes memory data) = abi.decode(
msg.data[4:],
(address, bytes)
);
ERC1967Utils.upgradeToAndCall(newImplementation, data);
}
}
Loading

0 comments on commit d8eff2b

Please sign in to comment.