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.
This commit is contained in:
@ -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 () {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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,
|
||||
};
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user