Created
April 28, 2017 06:12
-
-
Save danielyaa5/d138148de74271ec471ec706f0413520 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testRpc.increaseTime = function(secondsToJump, cb) { | |
function send(method, params, callback) { | |
if (typeof params == "function") { | |
callback = params; | |
params = []; | |
} | |
provider.sendAsync({ | |
jsonrpc: "2.0", | |
method: method, | |
params: params || [], | |
id: new Date().getTime() | |
}, callback); | |
} | |
var timestampBeforeJump; | |
web3.eth.getBlock('latest', function(err, block){ | |
if(err) return cb(err) | |
timestampBeforeJump = block.timestamp | |
send("evm_increaseTime", [secondsToJump], function(err, result) { | |
if (err) return cb(err); | |
// Mine a block so new time is recorded. | |
send("evm_mine", function(err, result) { | |
if (err) return cb(err); | |
web3.eth.getBlock('latest', function(err, block){ | |
if(err) return cb(err) | |
console.log('block', block.timestamp) | |
var secondsJumped = block.timestamp - timestampBeforeJump; | |
// Somehow it jumps an extra 18 seconds, ish, when run inside the whole | |
// test suite. It might have something to do with when the before block | |
// runs and when the test runs. Likely the last block didn't occur for | |
// awhile. | |
console.log({secondsToJump, secondsJumped}); | |
if(secondsJumped < secondsToJump) cb(Error('Testrpc time jump failed')); | |
cb() | |
}) | |
}) | |
}) | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment