123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
- const { expect } = require('chai');
- const Enums = require('../../helpers/enums');
- const {
- runGovernorWorkflow,
- } = require('../GovernorWorkflow.behavior');
- const {
- shouldSupportInterfaces,
- } = require('../../utils/introspection/SupportsInterface.behavior');
- const Token = artifacts.require('ERC20VotesMock');
- const Timelock = artifacts.require('TimelockController');
- const Governor = artifacts.require('GovernorTimelockControlMock');
- const CallReceiver = artifacts.require('CallReceiverMock');
- contract('GovernorTimelockControl', function (accounts) {
- const [ admin, voter, other ] = accounts;
- const name = 'OZ-Governor';
- // const version = '1';
- const tokenName = 'MockToken';
- const tokenSymbol = 'MTKN';
- const tokenSupply = web3.utils.toWei('100');
- beforeEach(async function () {
- const [ deployer ] = await web3.eth.getAccounts();
- this.token = await Token.new(tokenName, tokenSymbol);
- this.timelock = await Timelock.new(3600, [], []);
- this.mock = await Governor.new(name, this.token.address, 4, 16, this.timelock.address, 0);
- this.receiver = await CallReceiver.new();
- // normal setup: governor and admin are proposers, everyone is executor, timelock is its own admin
- await this.timelock.grantRole(await this.timelock.PROPOSER_ROLE(), this.mock.address);
- await this.timelock.grantRole(await this.timelock.PROPOSER_ROLE(), admin);
- await this.timelock.grantRole(await this.timelock.EXECUTOR_ROLE(), constants.ZERO_ADDRESS);
- await this.timelock.revokeRole(await this.timelock.TIMELOCK_ADMIN_ROLE(), deployer);
- await this.token.mint(voter, tokenSupply);
- await this.token.delegate(voter, { from: voter });
- });
- shouldSupportInterfaces([
- 'ERC165',
- 'Governor',
- 'GovernorWithParams',
- 'GovernorTimelock',
- ]);
- it('doesn\'t accept ether transfers', async function () {
- await expectRevert.unspecified(web3.eth.sendTransaction({ from: voter, to: this.mock.address, value: 1 }));
- });
- it('post deployment check', async function () {
- expect(await this.mock.name()).to.be.equal(name);
- expect(await this.mock.token()).to.be.equal(this.token.address);
- expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
- expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
- expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
- expect(await this.mock.timelock()).to.be.equal(this.timelock.address);
- });
- describe('nominal', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- },
- };
- });
- afterEach(async function () {
- const timelockid = await this.timelock.hashOperationBatch(
- ...this.settings.proposal.slice(0, 3),
- '0x0',
- this.descriptionHash,
- );
- expectEvent(
- this.receipts.propose,
- 'ProposalCreated',
- { proposalId: this.id },
- );
- expectEvent(
- this.receipts.queue,
- 'ProposalQueued',
- { proposalId: this.id },
- );
- await expectEvent.inTransaction(
- this.receipts.queue.transactionHash,
- this.timelock,
- 'CallScheduled',
- { id: timelockid },
- );
- expectEvent(
- this.receipts.execute,
- 'ProposalExecuted',
- { proposalId: this.id },
- );
- await expectEvent.inTransaction(
- this.receipts.execute.transactionHash,
- this.timelock,
- 'CallExecuted',
- { id: timelockid },
- );
- await expectEvent.inTransaction(
- this.receipts.execute.transactionHash,
- this.receiver,
- 'MockFunctionCalled',
- );
- });
- runGovernorWorkflow();
- });
- describe('executed by other proposer', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- execute: { enable: false },
- },
- };
- });
- afterEach(async function () {
- await this.timelock.executeBatch(
- ...this.settings.proposal.slice(0, 3),
- '0x0',
- this.descriptionHash,
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
- await expectRevert(
- this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'Governor: proposal not successful',
- );
- });
- runGovernorWorkflow();
- });
- describe('not queued', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { enable: false },
- execute: { error: 'TimelockController: operation is not ready' },
- },
- };
- });
- afterEach(async function () {
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
- });
- runGovernorWorkflow();
- });
- describe('to early', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- execute: { error: 'TimelockController: operation is not ready' },
- },
- };
- });
- afterEach(async function () {
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
- });
- runGovernorWorkflow();
- });
- describe('re-queue / re-execute', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- },
- };
- });
- afterEach(async function () {
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Executed);
- await expectRevert(
- this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'Governor: proposal not successful',
- );
- await expectRevert(
- this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'Governor: proposal not successful',
- );
- });
- runGovernorWorkflow();
- });
- describe('cancel before queue prevents scheduling', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { enable: false },
- execute: { enable: false },
- },
- };
- });
- afterEach(async function () {
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Succeeded);
- expectEvent(
- await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'ProposalCanceled',
- { proposalId: this.id },
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
- await expectRevert(
- this.mock.queue(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'Governor: proposal not successful',
- );
- });
- runGovernorWorkflow();
- });
- describe('cancel after queue prevents execution', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- execute: { enable: false },
- },
- };
- });
- afterEach(async function () {
- const timelockid = await this.timelock.hashOperationBatch(
- ...this.settings.proposal.slice(0, 3),
- '0x0',
- this.descriptionHash,
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
- const receipt = await this.mock.cancel(...this.settings.proposal.slice(0, -1), this.descriptionHash);
- expectEvent(
- receipt,
- 'ProposalCanceled',
- { proposalId: this.id },
- );
- await expectEvent.inTransaction(
- receipt.receipt.transactionHash,
- this.timelock,
- 'Cancelled',
- { id: timelockid },
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
- await expectRevert(
- this.mock.execute(...this.settings.proposal.slice(0, -1), this.descriptionHash),
- 'Governor: proposal not successful',
- );
- });
- runGovernorWorkflow();
- });
- describe('relay', function () {
- beforeEach(async function () {
- await this.token.mint(this.mock.address, 1);
- this.call = [
- this.token.address,
- 0,
- this.token.contract.methods.transfer(other, 1).encodeABI(),
- ];
- });
- it('protected', async function () {
- await expectRevert(
- this.mock.relay(...this.call),
- 'Governor: onlyGovernance',
- );
- });
- it('protected against other proposers', async function () {
- await this.timelock.schedule(
- this.mock.address,
- web3.utils.toWei('0'),
- this.mock.contract.methods.relay(...this.call).encodeABI(),
- constants.ZERO_BYTES32,
- constants.ZERO_BYTES32,
- 3600,
- { from: admin },
- );
- await time.increase(3600);
- await expectRevert(
- this.timelock.execute(
- this.mock.address,
- web3.utils.toWei('0'),
- this.mock.contract.methods.relay(...this.call).encodeABI(),
- constants.ZERO_BYTES32,
- constants.ZERO_BYTES32,
- { from: admin },
- ),
- 'TimelockController: underlying transaction reverted',
- );
- });
- describe('using workflow', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [
- this.mock.address,
- ],
- [
- web3.utils.toWei('0'),
- ],
- [
- this.mock.contract.methods.relay(...this.call).encodeABI(),
- ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 7 * 86400 },
- },
- };
- expect(await this.token.balanceOf(this.mock.address), 1);
- expect(await this.token.balanceOf(other), 0);
- });
- afterEach(async function () {
- expect(await this.token.balanceOf(this.mock.address), 0);
- expect(await this.token.balanceOf(other), 1);
- });
- runGovernorWorkflow();
- });
- });
- describe('cancel on timelock is forwarded in state', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.receiver.address ],
- [ web3.utils.toWei('0') ],
- [ this.receiver.contract.methods.mockFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- execute: { enable: false },
- },
- };
- });
- afterEach(async function () {
- const timelockid = await this.timelock.hashOperationBatch(
- ...this.settings.proposal.slice(0, 3),
- '0x0',
- this.descriptionHash,
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Queued);
- const receipt = await this.timelock.cancel(timelockid, { from: admin });
- expectEvent(
- receipt,
- 'Cancelled',
- { id: timelockid },
- );
- expect(await this.mock.state(this.id)).to.be.bignumber.equal(Enums.ProposalState.Canceled);
- });
- runGovernorWorkflow();
- });
- describe('updateTimelock', function () {
- beforeEach(async function () {
- this.newTimelock = await Timelock.new(3600, [], []);
- });
- it('protected', async function () {
- await expectRevert(
- this.mock.updateTimelock(this.newTimelock.address),
- 'Governor: onlyGovernance',
- );
- });
- describe('using workflow', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.mock.address ],
- [ web3.utils.toWei('0') ],
- [ this.mock.contract.methods.updateTimelock(this.newTimelock.address).encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- },
- };
- });
- afterEach(async function () {
- expectEvent(
- this.receipts.propose,
- 'ProposalCreated',
- { proposalId: this.id },
- );
- expectEvent(
- this.receipts.execute,
- 'ProposalExecuted',
- { proposalId: this.id },
- );
- expectEvent(
- this.receipts.execute,
- 'TimelockChange',
- { oldTimelock: this.timelock.address, newTimelock: this.newTimelock.address },
- );
- expect(await this.mock.timelock()).to.be.bignumber.equal(this.newTimelock.address);
- });
- runGovernorWorkflow();
- });
- });
- describe('clear queue of pending governor calls', function () {
- beforeEach(async function () {
- this.settings = {
- proposal: [
- [ this.mock.address ],
- [ web3.utils.toWei('0') ],
- [ this.mock.contract.methods.nonGovernanceFunction().encodeABI() ],
- '<proposal description>',
- ],
- voters: [
- { voter: voter, support: Enums.VoteType.For },
- ],
- steps: {
- queue: { delay: 3600 },
- },
- };
- });
- afterEach(async function () {
- expectEvent(
- this.receipts.execute,
- 'ProposalExecuted',
- { proposalId: this.id },
- );
- });
- runGovernorWorkflow();
- });
- });
|