Cleanup of Ownership directory (#2120)
* Remove Ownable.isOwner. * Remove ownable behavior from tests * Make Escrow use Ownable instead of Secondary * Migrate GSNRecipientERC20Fee to Ownable * Remove Secondary * Move Ownable to access directory * Remove mentions of Secondary * Add changelog entry * Move Ownable tests to access * Remove unused mockpull/2122/head^2
parent
baaadde3c5
commit
8176a901a9
@ -1,7 +1,9 @@ |
||||
= Access |
||||
|
||||
NOTE: This page is incomplete. We're working to improve it for the next release. Stay tuned! |
||||
Contract modules for authorization and access control mechanisms. |
||||
|
||||
== Library |
||||
== Contracts |
||||
|
||||
{{Ownable}} |
||||
|
||||
{{Roles}} |
||||
|
@ -1,15 +0,0 @@ |
||||
pragma solidity ^0.6.0; |
||||
|
||||
import "../ownership/Ownable.sol"; |
||||
|
||||
/** |
||||
* @title Ownable interface id calculator. |
||||
* @dev See the EIP165 specification for more information: |
||||
* https://eips.ethereum.org/EIPS/eip-165#specification |
||||
*/ |
||||
contract OwnableInterfaceId { |
||||
function getInterfaceId() public pure returns (bytes4) { |
||||
Ownable i; |
||||
return i.owner.selector ^ i.isOwner.selector ^ i.renounceOwnership.selector ^ i.transferOwnership.selector; |
||||
} |
||||
} |
@ -1,5 +1,5 @@ |
||||
pragma solidity ^0.6.0; |
||||
|
||||
import "../ownership/Ownable.sol"; |
||||
import "../access/Ownable.sol"; |
||||
|
||||
contract OwnableMock is Ownable { } |
||||
|
@ -1,7 +0,0 @@ |
||||
pragma solidity ^0.6.0; |
||||
|
||||
import "../ownership/Secondary.sol"; |
||||
|
||||
contract SecondaryMock is Secondary { |
||||
function onlyPrimaryMock() public view onlyPrimary { } |
||||
} |
@ -1,11 +0,0 @@ |
||||
= Ownership |
||||
|
||||
Contract modules for simple authorization and access control mechanisms. |
||||
|
||||
TIP: For more complex needs see xref:access.adoc[Access]. |
||||
|
||||
== Contracts |
||||
|
||||
{{Ownable}} |
||||
|
||||
{{Secondary}} |
@ -1,50 +0,0 @@ |
||||
pragma solidity ^0.6.0; |
||||
|
||||
import "../GSN/Context.sol"; |
||||
/** |
||||
* @dev A Secondary contract can only be used by its primary account (the one that created it). |
||||
*/ |
||||
contract Secondary is Context { |
||||
address private _primary; |
||||
|
||||
/** |
||||
* @dev Emitted when the primary contract changes. |
||||
*/ |
||||
event PrimaryTransferred( |
||||
address recipient |
||||
); |
||||
|
||||
/** |
||||
* @dev Sets the primary account to the one that is creating the Secondary contract. |
||||
*/ |
||||
constructor () internal { |
||||
address msgSender = _msgSender(); |
||||
_primary = msgSender; |
||||
emit PrimaryTransferred(msgSender); |
||||
} |
||||
|
||||
/** |
||||
* @dev Reverts if called from any account other than the primary. |
||||
*/ |
||||
modifier onlyPrimary() { |
||||
require(_msgSender() == _primary, "Secondary: caller is not the primary account"); |
||||
_; |
||||
} |
||||
|
||||
/** |
||||
* @return the address of the primary. |
||||
*/ |
||||
function primary() public view returns (address) { |
||||
return _primary; |
||||
} |
||||
|
||||
/** |
||||
* @dev Transfers contract to a new primary. |
||||
* @param recipient The address of new primary. |
||||
*/ |
||||
function transferPrimary(address recipient) public virtual onlyPrimary { |
||||
require(recipient != address(0), "Secondary: new primary is the zero address"); |
||||
_primary = recipient; |
||||
emit PrimaryTransferred(recipient); |
||||
} |
||||
} |
@ -0,0 +1,54 @@ |
||||
const { accounts, contract } = require('@openzeppelin/test-environment'); |
||||
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); |
||||
const { ZERO_ADDRESS } = constants; |
||||
|
||||
const { expect } = require('chai'); |
||||
|
||||
const Ownable = contract.fromArtifact('OwnableMock'); |
||||
|
||||
describe('Ownable', function () { |
||||
const [ owner, other ] = accounts; |
||||
|
||||
beforeEach(async function () { |
||||
this.ownable = await Ownable.new({ from: owner }); |
||||
}); |
||||
|
||||
it('should have an owner', async function () { |
||||
expect(await this.ownable.owner()).to.equal(owner); |
||||
}); |
||||
|
||||
it('changes owner after transfer', async function () { |
||||
const receipt = await this.ownable.transferOwnership(other, { from: owner }); |
||||
expectEvent(receipt, 'OwnershipTransferred'); |
||||
|
||||
expect(await this.ownable.owner()).to.equal(other); |
||||
}); |
||||
|
||||
it('should prevent non-owners from transferring', async function () { |
||||
await expectRevert( |
||||
this.ownable.transferOwnership(other, { from: other }), |
||||
'Ownable: caller is not the owner' |
||||
); |
||||
}); |
||||
|
||||
it('should guard ownership against stuck state', async function () { |
||||
await expectRevert( |
||||
this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }), |
||||
'Ownable: new owner is the zero address' |
||||
); |
||||
}); |
||||
|
||||
it('loses owner after renouncement', async function () { |
||||
const receipt = await this.ownable.renounceOwnership({ from: owner }); |
||||
expectEvent(receipt, 'OwnershipTransferred'); |
||||
|
||||
expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('should prevent non-owners from renouncement', async function () { |
||||
await expectRevert( |
||||
this.ownable.renounceOwnership({ from: other }), |
||||
'Ownable: caller is not the owner' |
||||
); |
||||
}); |
||||
}); |
@ -1,53 +0,0 @@ |
||||
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); |
||||
const { ZERO_ADDRESS } = constants; |
||||
|
||||
const { expect } = require('chai'); |
||||
|
||||
function shouldBehaveLikeOwnable (owner, [other]) { |
||||
describe('as an ownable', function () { |
||||
it('should have an owner', async function () { |
||||
expect(await this.ownable.owner()).to.equal(owner); |
||||
}); |
||||
|
||||
it('changes owner after transfer', async function () { |
||||
expect(await this.ownable.isOwner({ from: other })).to.equal(false); |
||||
const receipt = await this.ownable.transferOwnership(other, { from: owner }); |
||||
expectEvent(receipt, 'OwnershipTransferred'); |
||||
|
||||
expect(await this.ownable.owner()).to.equal(other); |
||||
expect(await this.ownable.isOwner({ from: other })).to.equal(true); |
||||
}); |
||||
|
||||
it('should prevent non-owners from transferring', async function () { |
||||
await expectRevert( |
||||
this.ownable.transferOwnership(other, { from: other }), |
||||
'Ownable: caller is not the owner' |
||||
); |
||||
}); |
||||
|
||||
it('should guard ownership against stuck state', async function () { |
||||
await expectRevert( |
||||
this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }), |
||||
'Ownable: new owner is the zero address' |
||||
); |
||||
}); |
||||
|
||||
it('loses owner after renouncement', async function () { |
||||
const receipt = await this.ownable.renounceOwnership({ from: owner }); |
||||
expectEvent(receipt, 'OwnershipTransferred'); |
||||
|
||||
expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('should prevent non-owners from renouncement', async function () { |
||||
await expectRevert( |
||||
this.ownable.renounceOwnership({ from: other }), |
||||
'Ownable: caller is not the owner' |
||||
); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
module.exports = { |
||||
shouldBehaveLikeOwnable, |
||||
}; |
@ -1,16 +0,0 @@ |
||||
const { accounts, contract } = require('@openzeppelin/test-environment'); |
||||
|
||||
require('@openzeppelin/test-helpers'); |
||||
const { shouldBehaveLikeOwnable } = require('./Ownable.behavior'); |
||||
|
||||
const Ownable = contract.fromArtifact('OwnableMock'); |
||||
|
||||
describe('Ownable', function () { |
||||
const [ owner, ...otherAccounts ] = accounts; |
||||
|
||||
beforeEach(async function () { |
||||
this.ownable = await Ownable.new({ from: owner }); |
||||
}); |
||||
|
||||
shouldBehaveLikeOwnable(owner, otherAccounts); |
||||
}); |
@ -1,68 +0,0 @@ |
||||
const { accounts, contract } = require('@openzeppelin/test-environment'); |
||||
|
||||
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); |
||||
const { ZERO_ADDRESS } = constants; |
||||
|
||||
const { expect } = require('chai'); |
||||
|
||||
const SecondaryMock = contract.fromArtifact('SecondaryMock'); |
||||
|
||||
describe('Secondary', function () { |
||||
const [ primary, newPrimary, other ] = accounts; |
||||
|
||||
beforeEach(async function () { |
||||
this.secondary = await SecondaryMock.new({ from: primary }); |
||||
}); |
||||
|
||||
it('stores the primary\'s address', async function () { |
||||
expect(await this.secondary.primary()).to.equal(primary); |
||||
}); |
||||
|
||||
describe('onlyPrimary', function () { |
||||
it('allows the primary account to call onlyPrimary functions', async function () { |
||||
await this.secondary.onlyPrimaryMock({ from: primary }); |
||||
}); |
||||
|
||||
it('reverts when anyone calls onlyPrimary functions', async function () { |
||||
await expectRevert(this.secondary.onlyPrimaryMock({ from: other }), |
||||
'Secondary: caller is not the primary account' |
||||
); |
||||
}); |
||||
}); |
||||
|
||||
describe('transferPrimary', function () { |
||||
it('makes the recipient the new primary', async function () { |
||||
const { logs } = await this.secondary.transferPrimary(newPrimary, { from: primary }); |
||||
expectEvent.inLogs(logs, 'PrimaryTransferred', { recipient: newPrimary }); |
||||
expect(await this.secondary.primary()).to.equal(newPrimary); |
||||
}); |
||||
|
||||
it('reverts when transferring to the null address', async function () { |
||||
await expectRevert(this.secondary.transferPrimary(ZERO_ADDRESS, { from: primary }), |
||||
'Secondary: new primary is the zero address' |
||||
); |
||||
}); |
||||
|
||||
it('reverts when called by anyone', async function () { |
||||
await expectRevert(this.secondary.transferPrimary(newPrimary, { from: other }), |
||||
'Secondary: caller is not the primary account' |
||||
); |
||||
}); |
||||
|
||||
context('with new primary', function () { |
||||
beforeEach(async function () { |
||||
await this.secondary.transferPrimary(newPrimary, { from: primary }); |
||||
}); |
||||
|
||||
it('allows the new primary account to call onlyPrimary functions', async function () { |
||||
await this.secondary.onlyPrimaryMock({ from: newPrimary }); |
||||
}); |
||||
|
||||
it('reverts when the old primary account calls onlyPrimary functions', async function () { |
||||
await expectRevert(this.secondary.onlyPrimaryMock({ from: primary }), |
||||
'Secondary: caller is not the primary account' |
||||
); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue