// Copyright (C) 2020 Energi Core // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . pragma solidity = 0.5.16; import { NonReentrant } from "../NonReentrant.sol"; import { IEnergiswapFactory } from './IEnergiswapFactory.sol'; import { IGovernedContract } from "../interfaces/IGovernedContract.sol"; import { IGovernedProxy } from "../interfaces/IGovernedProxy.sol"; import { IEnergiswapFactoryGovernedProxy } from "../energiswapFactory/IEnergiswapFactoryGovernedProxy.sol"; import { IUpgradeProposal } from "../interfaces/IUpgradeProposal.sol"; import { ISporkRegistry } from "../interfaces/ISporkRegistry.sol"; /** * SC-9: This contract has no chance of being updated. It must be stupid simple. * * If another upgrade logic is required in the future - it can be done as proxy stage II. */ contract EnergiswapFactoryGovernedProxy is IEnergiswapFactoryGovernedProxy, NonReentrant { IGovernedContract public impl; IGovernedProxy public spork_proxy; address public multicall; address public router; address public pairsManager; address public pairsERC20; bool public initialized = false; mapping(address => IGovernedContract) public upgrade_proposals; IUpgradeProposal[] public upgrade_proposal_list; event PairCreated(address indexed token0, address indexed token1, address pair, uint allPairsLength); modifier senderOrigin { // Internal calls are expected to use impl directly. // That's due to use of call() instead of delegatecall() on purpose. // solium-disable-next-line security/no-tx-origin require( tx.origin == msg.sender || msg.sender == multicall || msg.sender == router || msg.sender == pairsManager, "EnergiswapFactoryGovernedProxy: FORBIDDEN, not a direct call or a call from router or pairsManager"); _; } modifier requireImpl { require(msg.sender == address(impl), "EnergiswapFactoryGovernedProxy: FORBIDDEN, not impl"); _; } modifier requireRouter { require(msg.sender == router, "EnergiswapFactoryGovernedProxy: FORBIDDEN, not router"); _; } modifier requirePairsManager { require(msg.sender == pairsManager, "EnergiswapFactoryGovernedProxy: FORBIDDEN, not pairsManager"); _; } modifier requirePairsERC20 { require(msg.sender == pairsERC20, "EnergiswapFactoryGovernedProxy: FORBIDDEN, not pairsERC20"); _; } constructor(address _impl, address payable _sporkProxy) public { impl = IGovernedContract(_impl); spork_proxy = IGovernedProxy(_sporkProxy); } // Initialize contract. This function can only be called once function initialize(address _multicall, address _router, address _pairsManager, address _pairsERC20) external requireImpl { require(initialized == false, 'EnergiswapFactoryGovernedProxy: already initialized'); multicall = _multicall; router = _router; pairsManager = _pairsManager; pairsERC20 = _pairsERC20; initialized = true; } // Update router implementation address function setRouter(address _router) external requireRouter { router = _router; // Updates EnergiswapFactory implementation with new router implementation address IEnergiswapFactory(address(impl)).setRouter(_router); } // Update pairs manager implementation address function setPairsManager(address _pairsManager) external requirePairsManager { pairsManager = _pairsManager; // Updates EnergiswapFactory implementation with new pairs manager implementation address IEnergiswapFactory(address(impl)).setPairsManager(_pairsManager); } // Update pairs ERC20 implementation address function setPairsERC20(address _pairsERC20) external requirePairsERC20 { pairsERC20 = _pairsERC20; // Updates EnergiswapFactory implementation with new pairs ERC20 implementation address IEnergiswapFactory(address(impl)).setPairsERC20(_pairsERC20); } // Emit PairCreated event function emitPairCreated(address token0, address token1, address pair, uint allPairsLength) external requireImpl { emit PairCreated(token0, token1, pair, allPairsLength); } /** * Pre-create a new contract first. * Then propose upgrade based on that. */ function proposeUpgrade(IGovernedContract _newImpl, uint _period) external payable senderOrigin noReentry returns(IUpgradeProposal) { require(_newImpl != impl, "Already active!"); require(_newImpl.proxy() == address(this), "Wrong proxy!"); ISporkRegistry spork_reg = ISporkRegistry(address(spork_proxy.impl())); IUpgradeProposal proposal = spork_reg.createUpgradeProposal.value(msg.value)(_newImpl, _period, msg.sender); upgrade_proposals[address(proposal)] = _newImpl; upgrade_proposal_list.push(proposal); emit UpgradeProposal(_newImpl, proposal); return proposal; } /** * Once proposal is accepted, anyone can activate that. */ function upgrade(IUpgradeProposal _proposal) external noReentry { IGovernedContract new_impl = upgrade_proposals[address(_proposal)]; require(new_impl != impl, "Already active!"); // in case it changes in the flight require(address(new_impl) != address(0), "Not registered!"); require(_proposal.isAccepted(), "Not accepted!"); IGovernedContract old_impl = impl; new_impl.migrate(old_impl); impl = new_impl; old_impl.destroy(new_impl); // SECURITY: prevent downgrade attack _cleanupProposal(_proposal); // Return fee ASAP _proposal.destroy(); emit Upgraded(new_impl, _proposal); } /** * Map proposal to implementation */ function upgradeProposalImpl(IUpgradeProposal _proposal) external view returns(IGovernedContract new_impl) { new_impl = upgrade_proposals[address(_proposal)]; } /** * Lists all available upgrades */ function listUpgradeProposals() external view returns(IUpgradeProposal[] memory proposals) { uint len = upgrade_proposal_list.length; proposals = new IUpgradeProposal[](len); for (uint i = 0; i < len; ++i) { proposals[i] = upgrade_proposal_list[i]; } return proposals; } /** * Once proposal is reject, anyone can start collect procedure. */ function collectUpgradeProposal(IUpgradeProposal _proposal) external noReentry { IGovernedContract new_impl = upgrade_proposals[address(_proposal)]; require(address(new_impl) != address(0), "Not registered!"); _proposal.collect(); delete upgrade_proposals[address(_proposal)]; _cleanupProposal(_proposal); } function _cleanupProposal(IUpgradeProposal _proposal) internal { delete upgrade_proposals[address(_proposal)]; uint len = upgrade_proposal_list.length; for (uint i = 0; i < len; ++i) { if (upgrade_proposal_list[i] == _proposal) { upgrade_proposal_list[i] = upgrade_proposal_list[len - 1]; upgrade_proposal_list.pop(); break; } } } /** * Related to above */ function proxy() external view returns (address) { return address(this); } /** * SECURITY: prevent on-behalf-of calls */ function migrate(IGovernedContract) external pure { revert("Good try"); } /** * SECURITY: prevent on-behalf-of calls */ function destroy(IGovernedContract) external pure { revert("Good try"); } /** * Proxy all other calls to implementation. */ function () external payable senderOrigin { // SECURITY: senderOrigin() modifier is mandatory IGovernedContract impl_m = impl; // solium-disable-next-line security/no-inline-assembly assembly { let ptr := mload(0x40) calldatacopy(ptr, 0, calldatasize()) let res := call(sub(gas(), 10000), impl_m, callvalue(), ptr, calldatasize(), 0, 0) // NOTE: returndatasize should allow repeatable calls // what should save one opcode. returndatacopy(ptr, 0, returndatasize()) switch res case 0 { revert(ptr, returndatasize()) } default { return(ptr, returndatasize()) } } } }