I need to test some contract calls against testrpc that have a time constraint (after "x" hours can see the posted value) so I would like to change the timestamp in order to test if I get the expected result.
Thanks
I would need this as well.
Currently Blockchain is using Date.getTime() : https://github.com/ethereumjs/testrpc/blob/21f2a7a813350a13f612c0bc2c9f512ca3e123fe/lib/blockchain.js#L187
Since Blockchain accept an option parameter that is passed down from the provider() maybe a time function could be accepted there and would be called instead?
Or through an rpc call (like snapshot functionality) so it become availabe to server mode ?
By the way I use this in the mean time:
function setTime(newTime){
Date.prototype.getTime = function(){return newTime;}
}
I need it for the same reason.
you can use this fork https://github.com/Georgi87/testrpc @mpolci and the custom functions:
evm_setTimestamp : Sets timestamp of current block. Takes one parameter (unix timestamp). This is useful, if your tests have time-constraints for challenge periods etc..
evm_mineBlocks : Mines a number of blocks. Takes one parameter (block count). This is useful, if your tests have block-number-constraints for challenge periods etc..
Thank you @denisgranha, I created a small snippet to make the _evm_setTimestamp_ rpc call
https://gist.github.com/mpolci/f2c4a80667b47acdfd016664a0c41999
I'm trying to do the same thing. As this is not implemented yet, how exactly do I monkey patch Date.prototype.getTime ? I'm using truffle and trying to monkey patch it from one of my test files but it seems they are using a different environment
@zweicoder see my comment above, that should do the trick
@wighawag I did put that into my .js test files but that didn't help, which was why I assumed they used a separate environment than testrpc
The problem was that I was running testrpc in a separate terminal window, so I can't just monkey patch via my test files that run on truffle's environment. I did a simple hack to get around this:
var TestRPC = require("ethereumjs-testrpc");
var server = TestRPC.server();
var port = 8545;
function setTime(newTime) {
Date.prototype.getTime = function() {
return newTime;
}
}
server.listen(port, function(err, blockchain) {
console.log('Listening on ', port);
});
var backdoor = require('http').createServer((request, response) => {
var headers = request.headers;
var method = request.method;
var url = request.url;
var body = [];
request.on('error', function(err) {
// console.error(err);
}).on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
switch (method) {
case 'POST':
var payload;
payload = JSON.parse(body);
if (payload.time) {
setTime(payload.time* 1000);
}
return;
default:
return;
}
})
})
backdoor.listen(port+1, (err)=>{
if (err) console.err(err)
})
So now I send a simple POST request to the backdoor port with a JSON containing a time attribute to change the time.
Edit: for convenience the accepted time is multiplied by 1000 as testrpc converts from millis to seconds
I've pushed a new version that allows you to set the start time the first block should receive, and the time will move forward normally from that point onward. Additionally, you can use the evm_increaseTime to jump forward. Check out the updated readme.
This functionality is available in v2.2.2. This _should_ provide all you need to write tests for code that's dependent on time. Closing this ticket. Please open a new ticket if you run into issues. Thanks!
Most helpful comment
I've pushed a new version that allows you to set the start time the first block should receive, and the time will move forward normally from that point onward. Additionally, you can use the
evm_increaseTimeto jump forward. Check out the updated readme.This functionality is available in v2.2.2. This _should_ provide all you need to write tests for code that's dependent on time. Closing this ticket. Please open a new ticket if you run into issues. Thanks!