|
| 1 | +const { expect } = require('chai') |
| 2 | +const { |
| 3 | + expectEvent, |
| 4 | + expectRevert, |
| 5 | + time, |
| 6 | +} = require('@openzeppelin/test-helpers') |
| 7 | +const BN = web3.utils.BN |
| 8 | + |
| 9 | +// helpers |
| 10 | +const deployment = require('./lib/deployment') |
| 11 | +const { defaults } = require('./lib/testHelpers') |
| 12 | + |
| 13 | +contract('EpochManager', ([me, other, governor]) => { |
| 14 | + beforeEach(async function() { |
| 15 | + // Deploy epoch manager contract |
| 16 | + this.epochManager = await deployment.deployEpochManagerContract(governor, { |
| 17 | + from: me, |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + describe('state variables functions', () => { |
| 22 | + it('should set `governor`', async function() { |
| 23 | + // Set right in the constructor |
| 24 | + expect(await this.epochManager.governor()).to.equal(governor) |
| 25 | + |
| 26 | + // Can set if allowed |
| 27 | + await this.epochManager.transferGovernance(other, { from: governor }) |
| 28 | + expect(await this.epochManager.governor()).to.equal(other) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should set `epochLength', async function() { |
| 32 | + // Set right in the constructor |
| 33 | + expect(await this.epochManager.epochLength()).to.be.bignumber.equal( |
| 34 | + defaults.epochs.lengthInBlocks, |
| 35 | + ) |
| 36 | + |
| 37 | + // Update and check new value |
| 38 | + const newEpochLength = new BN(4) |
| 39 | + const currentEpoch = await this.epochManager.currentEpoch() |
| 40 | + const { logs } = await this.epochManager.setEpochLength(newEpochLength, { |
| 41 | + from: governor, |
| 42 | + }) |
| 43 | + expect(await this.epochManager.epochLength()).to.be.bignumber.equal( |
| 44 | + newEpochLength, |
| 45 | + ) |
| 46 | + |
| 47 | + // Event emitted |
| 48 | + expectEvent.inLogs(logs, 'EpochLengthUpdate', { |
| 49 | + epoch: currentEpoch, |
| 50 | + epochLength: newEpochLength, |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('reject set `epochLength` if zero', async function() { |
| 55 | + // Update and check new value |
| 56 | + const newEpochLength = new BN(0) |
| 57 | + await expectRevert( |
| 58 | + this.epochManager.setEpochLength(newEpochLength, { from: governor }), |
| 59 | + 'Epoch length cannot be 0', |
| 60 | + ) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('epoch lifecycle', function() { |
| 65 | + // Use epochs every three blocks |
| 66 | + // Blocks -> (1,2,3)(4,5,6)(7,8,9) |
| 67 | + // Epochs -> 1 2 3 |
| 68 | + beforeEach(async function() { |
| 69 | + this.epochLength = new BN(3) |
| 70 | + await this.epochManager.setEpochLength(this.epochLength, { |
| 71 | + from: governor, |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('calculations', () => { |
| 76 | + it('should return correct block number', async function() { |
| 77 | + const currentBlock = await time.latestBlock() |
| 78 | + expect(await this.epochManager.blockNum()).to.be.bignumber.equal( |
| 79 | + currentBlock, |
| 80 | + ) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should return same starting block if we stay on the same epoch', async function() { |
| 84 | + const currentEpochBlockBefore = await this.epochManager.currentEpochBlock() |
| 85 | + |
| 86 | + // Advance blocks to stay on the same epoch |
| 87 | + await time.advanceBlock() |
| 88 | + |
| 89 | + const currentEpochBlockAfter = await this.epochManager.currentEpochBlock() |
| 90 | + expect(currentEpochBlockAfter).to.be.bignumber.equal( |
| 91 | + currentEpochBlockBefore, |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should return next starting block if we move to the next epoch', async function() { |
| 96 | + const currentEpochBlockBefore = await this.epochManager.currentEpochBlock() |
| 97 | + |
| 98 | + // Advance blocks to move to the next epoch |
| 99 | + await time.advanceBlockTo(currentEpochBlockBefore.add(this.epochLength)) |
| 100 | + |
| 101 | + const currentEpochBlockAfter = await this.epochManager.currentEpochBlock() |
| 102 | + expect(currentEpochBlockAfter).to.be.bignumber.not.equal( |
| 103 | + currentEpochBlockBefore, |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should return next epoch if advance > epochLength', async function() { |
| 108 | + const nextEpoch = (await this.epochManager.currentEpoch()).add( |
| 109 | + new BN(1), |
| 110 | + ) |
| 111 | + |
| 112 | + // Advance blocks and move to the next epoch |
| 113 | + const currentEpochBlock = await this.epochManager.currentEpochBlock() |
| 114 | + await time.advanceBlockTo(currentEpochBlock.add(this.epochLength)) |
| 115 | + |
| 116 | + const currentEpochAfter = await this.epochManager.currentEpoch() |
| 117 | + expect(currentEpochAfter).to.be.bignumber.equal(nextEpoch) |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + describe('progression', () => { |
| 122 | + beforeEach(async function() { |
| 123 | + const currentEpochBlock = await this.epochManager.currentEpochBlock() |
| 124 | + await time.advanceBlockTo(currentEpochBlock.add(this.epochLength)) |
| 125 | + }) |
| 126 | + |
| 127 | + context('epoch not run', function() { |
| 128 | + it('should return that current epoch is not run', async function() { |
| 129 | + expect(await this.epochManager.isCurrentEpochRun(), false) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should run new epoch', async function() { |
| 133 | + // Run epoch |
| 134 | + const currentEpoch = await this.epochManager.currentEpoch() |
| 135 | + const { logs } = await this.epochManager.runEpoch({ from: me }) |
| 136 | + const currentBlock = await time.latestBlock() |
| 137 | + |
| 138 | + // State |
| 139 | + const lastRunEpoch = await this.epochManager.lastRunEpoch() |
| 140 | + expect(lastRunEpoch).to.be.bignumber.equal(currentEpoch) |
| 141 | + |
| 142 | + // Event emitted |
| 143 | + expectEvent.inLogs(logs, 'NewEpoch', { |
| 144 | + epoch: currentEpoch, |
| 145 | + blockNumber: currentBlock, |
| 146 | + caller: me, |
| 147 | + }) |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + context('epoch run', function() { |
| 152 | + beforeEach(async function() { |
| 153 | + await this.epochManager.runEpoch() |
| 154 | + }) |
| 155 | + |
| 156 | + it('should return current epoch is already run', async function() { |
| 157 | + expect(await this.epochManager.isCurrentEpochRun(), true) |
| 158 | + }) |
| 159 | + |
| 160 | + it('reject run new epoch', async function() { |
| 161 | + await expectRevert( |
| 162 | + this.epochManager.runEpoch(), |
| 163 | + 'Current epoch already run', |
| 164 | + ) |
| 165 | + }) |
| 166 | + }) |
| 167 | + }) |
| 168 | + }) |
| 169 | +}) |
0 commit comments