Unverified Commit c7705712 by Nicolás Venturo Committed by GitHub

Remove in-constructor requirements (#2195)

* Remove isConstructor requirement from _setupRole

* Remove isConstructor requirement from _setupDecimals

* Update contracts/access/AccessControl.sol

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
parent 13e113df
...@@ -164,12 +164,16 @@ abstract contract AccessControl is Context { ...@@ -164,12 +164,16 @@ abstract contract AccessControl is Context {
* event. Note that unlike {grantRole}, this function doesn't perform any * event. Note that unlike {grantRole}, this function doesn't perform any
* checks on the calling account. * checks on the calling account.
* *
* Requirements: * [WARNING]
* * ====
* - this function can only be called from a constructor. * This function should only be called from the constructor when setting
* up the initial roles for the system.
*
* Using this function in any other way is effectively circumventing the admin
* system imposed by {AccessControl}.
* ====
*/ */
function _setupRole(bytes32 role, address account) internal virtual { function _setupRole(bytes32 role, address account) internal virtual {
require(!address(this).isContract(), "AccessControl: roles cannot be setup after construction");
_grantRole(role, account); _grantRole(role, account);
} }
......
...@@ -10,8 +10,4 @@ contract AccessControlMock is AccessControl { ...@@ -10,8 +10,4 @@ contract AccessControlMock is AccessControl {
function setRoleAdmin(bytes32 roleId, bytes32 adminRoleId) public { function setRoleAdmin(bytes32 roleId, bytes32 adminRoleId) public {
_setRoleAdmin(roleId, adminRoleId); _setRoleAdmin(roleId, adminRoleId);
} }
function setupRole(bytes32 roleId, address account) public {
_setupRole(roleId, account);
}
} }
...@@ -6,8 +6,4 @@ contract ERC20DecimalsMock is ERC20 { ...@@ -6,8 +6,4 @@ contract ERC20DecimalsMock is ERC20 {
constructor (string memory name, string memory symbol, uint8 decimals) public ERC20(name, symbol) { constructor (string memory name, string memory symbol, uint8 decimals) public ERC20(name, symbol) {
_setupDecimals(decimals); _setupDecimals(decimals);
} }
function setupDecimals(uint8 decimals) public {
_setupDecimals(decimals);
}
} }
...@@ -279,12 +279,11 @@ contract ERC20 is Context, IERC20 { ...@@ -279,12 +279,11 @@ contract ERC20 is Context, IERC20 {
/** /**
* @dev Sets {decimals} to a value other than the default one of 18. * @dev Sets {decimals} to a value other than the default one of 18.
* *
* Requirements: * WARNING: This function should only be called from the constructor. Most
* * applications that interact with token contracts will not expect
* - this function can only be called from a constructor. * {decimals} to ever change, and may work incorrectly if it does.
*/ */
function _setupDecimals(uint8 decimals_) internal { function _setupDecimals(uint8 decimals_) internal {
require(!address(this).isContract(), "ERC20: decimals cannot be changed after construction");
_decimals = decimals_; _decimals = decimals_;
} }
......
...@@ -17,15 +17,6 @@ describe('AccessControl', function () { ...@@ -17,15 +17,6 @@ describe('AccessControl', function () {
this.accessControl = await AccessControlMock.new({ from: admin }); this.accessControl = await AccessControlMock.new({ from: admin });
}); });
describe('_setupRole', function () {
it('cannot be called outside the constructor', async function () {
await expectRevert(
this.accessControl.setupRole(OTHER_ROLE, other),
'AccessControl: roles cannot be setup after construction'
);
});
});
describe('default admin', function () { describe('default admin', function () {
it('deployer has default admin role', async function () { it('deployer has default admin role', async function () {
expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true); expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true);
......
...@@ -44,12 +44,6 @@ describe('ERC20', function () { ...@@ -44,12 +44,6 @@ describe('ERC20', function () {
const token = await ERC20DecimalsMock.new(name, symbol, decimals); const token = await ERC20DecimalsMock.new(name, symbol, decimals);
expect(await token.decimals()).to.be.bignumber.equal(decimals); expect(await token.decimals()).to.be.bignumber.equal(decimals);
}); });
it('reverts if setting decimals after construction', async function () {
const token = await ERC20DecimalsMock.new(name, symbol, decimals);
await expectRevert(token.setupDecimals(decimals.addn(1)), 'ERC20: decimals cannot be changed after construction');
});
}); });
shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount); shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment