From 6fd0010325740926487788bb5b711f28b242decf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 29 Nov 2018 18:23:20 -0300 Subject: [PATCH] Moved advanceBlock to time. (#1523) * Added advanceBlock to time, moved tests around. * Removed the standalone advanceBlock. * Removed the 'id' field * Fixed linter error. * Removed the 'latest' test, since it only works if time hasn't been fast-forwarded. * Removed .only directive. --- test/crowdsale/FinalizableCrowdsale.test.js | 3 +- .../IncreasingPriceCrowdsale.test.js | 3 +- test/crowdsale/PostDeliveryCrowdsale.test.js | 3 +- test/crowdsale/RefundableCrowdsale.test.js | 3 +- test/crowdsale/TimedCrowdsale.test.js | 3 +- test/examples/SampleCrowdsale.test.js | 3 +- test/helpers/advanceToBlock.js | 15 ---- test/helpers/test/advanceToBlock.test.js | 19 ----- test/helpers/test/time.test.js | 71 +++++++++++-------- test/helpers/time.js | 38 +++++----- 10 files changed, 63 insertions(+), 98 deletions(-) delete mode 100644 test/helpers/advanceToBlock.js delete mode 100644 test/helpers/test/advanceToBlock.test.js diff --git a/test/crowdsale/FinalizableCrowdsale.test.js b/test/crowdsale/FinalizableCrowdsale.test.js index 93d406432..48b71d51d 100644 --- a/test/crowdsale/FinalizableCrowdsale.test.js +++ b/test/crowdsale/FinalizableCrowdsale.test.js @@ -1,5 +1,4 @@ const expectEvent = require('../helpers/expectEvent'); -const { advanceBlock } = require('../helpers/advanceToBlock'); const time = require('../helpers/time'); const shouldFail = require('../helpers/shouldFail'); @@ -17,7 +16,7 @@ contract('FinalizableCrowdsale', function ([_, wallet, anyone]) { before(async function () { // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache - await advanceBlock(); + await time.advanceBlock(); }); beforeEach(async function () { diff --git a/test/crowdsale/IncreasingPriceCrowdsale.test.js b/test/crowdsale/IncreasingPriceCrowdsale.test.js index 41906d224..34b476186 100644 --- a/test/crowdsale/IncreasingPriceCrowdsale.test.js +++ b/test/crowdsale/IncreasingPriceCrowdsale.test.js @@ -1,5 +1,4 @@ const { ether } = require('../helpers/ether'); -const { advanceBlock } = require('../helpers/advanceToBlock'); const time = require('../helpers/time'); const shouldFail = require('../helpers/shouldFail'); @@ -27,7 +26,7 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) const rateAtTime450000 = new BigNumber(6439); beforeEach(async function () { - await advanceBlock(); + await time.advanceBlock(); this.startTime = (await time.latest()) + time.duration.weeks(1); this.closingTime = this.startTime + time.duration.weeks(1); this.afterClosingTime = this.closingTime + time.duration.seconds(1); diff --git a/test/crowdsale/PostDeliveryCrowdsale.test.js b/test/crowdsale/PostDeliveryCrowdsale.test.js index 723cb1fca..58d8c1123 100644 --- a/test/crowdsale/PostDeliveryCrowdsale.test.js +++ b/test/crowdsale/PostDeliveryCrowdsale.test.js @@ -1,4 +1,3 @@ -const { advanceBlock } = require('../helpers/advanceToBlock'); const time = require('../helpers/time'); const shouldFail = require('../helpers/shouldFail'); const { ether } = require('../helpers/ether'); @@ -18,7 +17,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) { before(async function () { // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache - await advanceBlock(); + await time.advanceBlock(); }); beforeEach(async function () { diff --git a/test/crowdsale/RefundableCrowdsale.test.js b/test/crowdsale/RefundableCrowdsale.test.js index 8472b87f2..ce16f66f5 100644 --- a/test/crowdsale/RefundableCrowdsale.test.js +++ b/test/crowdsale/RefundableCrowdsale.test.js @@ -1,5 +1,4 @@ const { ether } = require('../helpers/ether'); -const { advanceBlock } = require('../helpers/advanceToBlock'); const { balanceDifference } = require('../helpers/balanceDifference'); const shouldFail = require('../helpers/shouldFail'); const time = require('../helpers/time'); @@ -22,7 +21,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon before(async function () { // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache - await advanceBlock(); + await time.advanceBlock(); }); beforeEach(async function () { diff --git a/test/crowdsale/TimedCrowdsale.test.js b/test/crowdsale/TimedCrowdsale.test.js index 5b0b457b7..4689ad820 100644 --- a/test/crowdsale/TimedCrowdsale.test.js +++ b/test/crowdsale/TimedCrowdsale.test.js @@ -1,5 +1,4 @@ const { ether } = require('../helpers/ether'); -const { advanceBlock } = require('../helpers/advanceToBlock'); const shouldFail = require('../helpers/shouldFail'); const time = require('../helpers/time'); @@ -19,7 +18,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) { before(async function () { // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache - await advanceBlock(); + await time.advanceBlock(); }); beforeEach(async function () { diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index da0bf1f7c..51df3c40e 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -1,5 +1,4 @@ const { ether } = require('../helpers/ether'); -const { advanceBlock } = require('../helpers/advanceToBlock'); const shouldFail = require('../helpers/shouldFail'); const time = require('../helpers/time'); const { balanceDifference } = require('../helpers/balanceDifference'); @@ -20,7 +19,7 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) { before(async function () { // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache - await advanceBlock(); + await time.advanceBlock(); }); beforeEach(async function () { diff --git a/test/helpers/advanceToBlock.js b/test/helpers/advanceToBlock.js deleted file mode 100644 index 225502022..000000000 --- a/test/helpers/advanceToBlock.js +++ /dev/null @@ -1,15 +0,0 @@ -function advanceBlock () { - return new Promise((resolve, reject) => { - web3.currentProvider.sendAsync({ - jsonrpc: '2.0', - method: 'evm_mine', - id: Date.now(), - }, (err, res) => { - return err ? reject(err) : resolve(res); - }); - }); -} - -module.exports = { - advanceBlock, -}; diff --git a/test/helpers/test/advanceToBlock.test.js b/test/helpers/test/advanceToBlock.test.js deleted file mode 100644 index cd8f1c352..000000000 --- a/test/helpers/test/advanceToBlock.test.js +++ /dev/null @@ -1,19 +0,0 @@ -const advanceToBlock = require('../advanceToBlock'); - -const BigNumber = web3.BigNumber; -require('chai') - .use(require('chai-bignumber')(BigNumber)) - .should(); - -describe('advanceToBlock', function () { - beforeEach(function () { - this.startingBlock = web3.eth.blockNumber; - }); - - describe('advanceBlock', function () { - it('increases the block number by one', async function () { - await advanceToBlock.advanceBlock(); - web3.eth.blockNumber.should.be.bignumber.equal(this.startingBlock + 1); - }); - }); -}); diff --git a/test/helpers/test/time.test.js b/test/helpers/test/time.test.js index 22199906c..556ae92f8 100644 --- a/test/helpers/test/time.test.js +++ b/test/helpers/test/time.test.js @@ -1,6 +1,5 @@ const time = require('../time'); const shouldFail = require('../shouldFail'); -const { advanceBlock } = require('../advanceToBlock'); const BigNumber = web3.BigNumber; require('chai') @@ -10,36 +9,6 @@ require('chai') describe('time', function () { const TOLERANCE_SECONDS = 1; - beforeEach(async function () { - await advanceBlock(); - this.start = await time.latest(); - }); - - describe('increase', function () { - it('increases time by a duration', async function () { - await time.increase(time.duration.hours(1)); - - const end = this.start + time.duration.hours(1); - (await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS); - }); - - it('throws with negative durations', async function () { - await shouldFail(time.increase(-1)); - }); - }); - - describe('increaseTo', function () { - it('increases time to a time in the future', async function () { - const end = this.start + time.duration.hours(1); - await time.increaseTo(end); - (await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS); - }); - - it('throws with a time in the past', async function () { - await shouldFail(time.increaseTo(this.start - 30)); - }); - }); - describe('duration', function () { it('converts seconds to seconds', function () { time.duration.seconds(1).should.equal(1); @@ -65,4 +34,44 @@ describe('time', function () { time.duration.years(1).should.equal(60 * 60 * 24 * 365); }); }); + + describe('advanceBlock', function () { + it('increases the block number by one', async function () { + const startingBlock = web3.eth.blockNumber; + await time.advanceBlock(); + web3.eth.blockNumber.should.be.bignumber.equal(startingBlock + 1); + }); + }); + + context('with starting time', function () { + beforeEach(async function () { + await time.advanceBlock(); + this.start = await time.latest(); + }); + + describe('increase', function () { + it('increases time by a duration', async function () { + await time.increase(time.duration.hours(1)); + + const end = this.start + time.duration.hours(1); + (await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS); + }); + + it('throws with negative durations', async function () { + await shouldFail(time.increase(-1)); + }); + }); + + describe('increaseTo', function () { + it('increases time to a time in the future', async function () { + const end = this.start + time.duration.hours(1); + await time.increaseTo(end); + (await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS); + }); + + it('throws with a time in the past', async function () { + await shouldFail(time.increaseTo(this.start - 30)); + }); + }); + }); }); diff --git a/test/helpers/time.js b/test/helpers/time.js index 26109db30..727250831 100644 --- a/test/helpers/time.js +++ b/test/helpers/time.js @@ -1,4 +1,12 @@ const { ethGetBlock } = require('./web3'); +const { promisify } = require('util'); + +function advanceBlock () { + return promisify(web3.currentProvider.sendAsync)({ + jsonrpc: '2.0', + method: 'evm_mine', + }); +} // Returns the time of the last mined block in seconds async function latest () { @@ -7,29 +15,16 @@ async function latest () { } // Increases ganache time by the passed duration in seconds -function increase (duration) { - const id = Date.now(); +async function increase (duration) { + if (duration < 0) throw Error(`Cannot increase time by a negative amount (${duration})`); - return new Promise((resolve, reject) => { - if (duration < 0) throw Error(`Cannot increase time by a negative amount (${duration})`); - - web3.currentProvider.sendAsync({ - jsonrpc: '2.0', - method: 'evm_increaseTime', - params: [duration], - id: id, - }, err1 => { - if (err1) return reject(err1); - - web3.currentProvider.sendAsync({ - jsonrpc: '2.0', - method: 'evm_mine', - id: id + 1, - }, (err2, res) => { - return err2 ? reject(err2) : resolve(res); - }); - }); + await promisify(web3.currentProvider.sendAsync)({ + jsonrpc: '2.0', + method: 'evm_increaseTime', + params: [duration], }); + + await advanceBlock(); } /** @@ -57,6 +52,7 @@ const duration = { }; module.exports = { + advanceBlock, latest, increase, increaseTo,