123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- const { constants, expectEvent, expectRevert } = 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 [ voter ] = 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 is proposer, 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.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',
- 'GovernorTimelock',
- ]);
- 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('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();
- });
- });
- });
|