From 1e8cf86dd8ae65b4e6033bcbe0579e295cb53a89 Mon Sep 17 00:00:00 2001 From: Artem Chystiakov Date: Wed, 19 Apr 2023 11:58:45 +0300 Subject: [PATCH] paginator & EIP-6224 fix --- .../AbstractContractsRegistry.sol | 20 +++++++++++++------ .../proxy/ProxyUpgrader.sol | 13 ++++++------ contracts/libs/arrays/Paginator.sol | 19 +++++++++--------- contracts/package.json | 2 +- package.json | 2 +- test/contracts-registry/ProxyUpgrader.test.js | 2 +- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/contracts/contracts-registry/AbstractContractsRegistry.sol b/contracts/contracts-registry/AbstractContractsRegistry.sol index 3795a376..e66155e6 100644 --- a/contracts/contracts-registry/AbstractContractsRegistry.sol +++ b/contracts/contracts-registry/AbstractContractsRegistry.sol @@ -41,8 +41,10 @@ abstract contract AbstractContractsRegistry is Initializable { mapping(string => address) private _contracts; mapping(address => bool) private _isProxy; - event AddedContract(string name, address contractAddress, bool isProxy); - event RemovedContract(string name); + event ContractAdded(string name, address contractAddress); + event ProxyContractAdded(string name, address contractAddress, address implementation); + event ProxyContractUpgraded(string name, address newImplementation); + event ContractRemoved(string name); /** * @notice The proxy initializer function @@ -147,6 +149,8 @@ abstract contract AbstractContractsRegistry is Initializable { require(_isProxy[contractToUpgrade_], "ContractsRegistry: not a proxy contract"); _proxyUpgrader.upgrade(contractToUpgrade_, newImplementation_, data_); + + emit ProxyContractUpgraded(name_, newImplementation_); } /** @@ -160,7 +164,7 @@ abstract contract AbstractContractsRegistry is Initializable { _contracts[name_] = contractAddress_; - emit AddedContract(name_, contractAddress_, false); + emit ContractAdded(name_, contractAddress_); } /** @@ -179,7 +183,7 @@ abstract contract AbstractContractsRegistry is Initializable { _contracts[name_] = proxyAddr_; _isProxy[proxyAddr_] = true; - emit AddedContract(name_, proxyAddr_, true); + emit ProxyContractAdded(name_, proxyAddr_, contractAddress_); } /** @@ -195,7 +199,11 @@ abstract contract AbstractContractsRegistry is Initializable { _contracts[name_] = contractAddress_; _isProxy[contractAddress_] = true; - emit AddedContract(name_, contractAddress_, true); + emit ProxyContractAdded( + name_, + contractAddress_, + _proxyUpgrader.getImplementation(contractAddress_) + ); } /** @@ -210,6 +218,6 @@ abstract contract AbstractContractsRegistry is Initializable { delete _isProxy[contractAddress_]; delete _contracts[name_]; - emit RemovedContract(name_); + emit ContractRemoved(name_); } } diff --git a/contracts/contracts-registry/proxy/ProxyUpgrader.sol b/contracts/contracts-registry/proxy/ProxyUpgrader.sol index f79a8b1c..1c6e64ab 100644 --- a/contracts/contracts-registry/proxy/ProxyUpgrader.sol +++ b/contracts/contracts-registry/proxy/ProxyUpgrader.sol @@ -15,10 +15,8 @@ contract ProxyUpgrader { address private immutable _OWNER; - event Upgraded(address proxy, address implementation); - modifier onlyOwner() { - require(_OWNER == msg.sender, "ProxyUpgrader: not an owner"); + _onlyOwner(); _; } @@ -32,15 +30,18 @@ contract ProxyUpgrader { } else { TransparentUpgradeableProxy(payable(what_)).upgradeTo(to_); } - - emit Upgraded(what_, to_); } function getImplementation(address what_) external view onlyOwner returns (address) { // bytes4(keccak256("implementation()")) == 0x5c60da1b (bool success_, bytes memory returndata_) = address(what_).staticcall(hex"5c60da1b"); - require(success_); + + require(success_, "ProxyUpgrader: not a proxy"); return abi.decode(returndata_, (address)); } + + function _onlyOwner() internal view { + require(_OWNER == msg.sender, "ProxyUpgrader: not an owner"); + } } diff --git a/contracts/libs/arrays/Paginator.sol b/contracts/libs/arrays/Paginator.sol index aa0c28d9..7a69f82a 100644 --- a/contracts/libs/arrays/Paginator.sol +++ b/contracts/libs/arrays/Paginator.sol @@ -10,7 +10,6 @@ import {StringSet} from "../data-structures/StringSet.sol"; * * Supports the following data types `uin256[]`, `address[]`, `bytes32[]`, `UintSet`, * `AddressSet`, `BytesSet`, `StringSet`. - * */ library Paginator { using EnumerableSet for *; @@ -34,7 +33,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (uint256[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_); + uint256 to_ = getTo(arr.length, offset_, limit_); list_ = new uint256[](to_ - offset_); @@ -48,7 +47,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (address[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_); + uint256 to_ = getTo(arr.length, offset_, limit_); list_ = new address[](to_ - offset_); @@ -62,7 +61,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (bytes32[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_); + uint256 to_ = getTo(arr.length, offset_, limit_); list_ = new bytes32[](to_ - offset_); @@ -76,7 +75,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (uint256[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_); + uint256 to_ = getTo(set.length(), offset_, limit_); list_ = new uint256[](to_ - offset_); @@ -90,7 +89,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (address[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_); + uint256 to_ = getTo(set.length(), offset_, limit_); list_ = new address[](to_ - offset_); @@ -104,7 +103,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (bytes32[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_); + uint256 to_ = getTo(set.length(), offset_, limit_); list_ = new bytes32[](to_ - offset_); @@ -118,7 +117,7 @@ library Paginator { uint256 offset_, uint256 limit_ ) internal view returns (string[] memory list_) { - uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_); + uint256 to_ = getTo(set.length(), offset_, limit_); list_ = new string[](to_ - offset_); @@ -127,11 +126,11 @@ library Paginator { } } - function _handleIncomingParametersForPart( + function getTo( uint256 length_, uint256 offset_, uint256 limit_ - ) private pure returns (uint256 to_) { + ) internal pure returns (uint256 to_) { to_ = offset_ + limit_; if (to_ > length_) { diff --git a/contracts/package.json b/contracts/package.json index 6f2ea133..5e5f874e 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,6 +1,6 @@ { "name": "@dlsl/dev-modules", - "version": "2.3.4", + "version": "2.4.0", "license": "MIT", "author": "Distributed Lab", "readme": "README.md", diff --git a/package.json b/package.json index cd6ffb40..eef48589 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dlsl", - "version": "2.3.4", + "version": "2.4.0", "license": "MIT", "author": "Distributed Lab", "description": "Solidity Development Modules by Distributed Lab", diff --git a/test/contracts-registry/ProxyUpgrader.test.js b/test/contracts-registry/ProxyUpgrader.test.js index 485e0997..374c1f28 100644 --- a/test/contracts-registry/ProxyUpgrader.test.js +++ b/test/contracts-registry/ProxyUpgrader.test.js @@ -44,7 +44,7 @@ describe("ProxyUpgrader", () => { }); it("should not get implementation", async () => { - await truffleAssert.reverts(proxyUpgrader.getImplementation(token.address)); + await truffleAssert.reverts(proxyUpgrader.getImplementation(token.address), "ProxyUpgrader: not a proxy"); }); it("only owner should get implementation", async () => {