I am wondering - is it possible to schedule or somehow trigger function calls to an Ethereum smart contract based on the current block height or timestamp? This would involve some pre-payment of gas for execution understandably, but it might be beneficial to have something like:

  • If current block height is 1'000'000, send 1000 ether to the block miner
  • If it's 2016-01-01 00:00:00, release funds from escrow

Of course those functions could be triggered by transactions at an appropriate time, but some sort of "execution scheduling" might be a useful automation of the process.

  • why couldn't this be done? this should be super simple to program... – user26705 May 30 '15 at 2:39
up vote 3 down vote accepted

It is not, sadly. There was a proposal for an ALARM opcode, but it was marked WONTFIX -

https://github.com/ethereum/go-ethereum/issues/117

I can see why - timers require that you keep your contract topped up with Ether, and making them too easy to create could litter the blockchain with them and impose an overhead on miners.

You either have to have an external caller for your function (as in chriseth's solution), or use a PHP-style "poor man's cron" function that you call from every function in the contract to check and see which periodic tasks should fire -

https://stackoverflow.com/questions/1857741/run-php-script-every-5-10-minutes-without-cron-perl-etc

This kind of solution can also be seen in LambdaMOO code, where events are triggered periodically only when someone speaks, but that's a bit more esoteric. ;-)

It is not in "the" ethereum spec, so no. But it is a feature that has been discussed for "ethereum 2.0" or similar. It is possible that there may even be a hard fork that will enable it in the then-existing network, but there are none currently planned.

This feature can be easily realised outside the Ethereum virtual machine (and thus keeping the VM simple): Create a (reusable) contract where contracts can register to be called at a certain time (and provide a reward as compensation for the gas costs). A script can poll this contract from outside and call it at the appropriate times. The contract could be similar to the following.

contract CallbackRegistry
{
  struct Entry { uint time; address addr; bytes data; uint reward; }
  mapping(bytes32 => Entry) entries;
  function register(uint _time, address _addr, bytes _data) external returns (bytes32 _hash)
  {
    _hash = sha3(_time, _addr, _data);
    entries[_hash].time = _time;
    entries[_hash].addr = _addr;
    entries[_hash].data = _data;
    entries[_hash].reward = msg.value;
  }
  function invoke(bytes32 _hash) external
  {
    if (entries[_hash].time > 0 && now >= entries[_hash].time)
    {
      msg.sender.send(entries[_hash].reward);
      entries[_hash].addr.call(entries[_hash].data);
      delete entries[_hash];
    }
  }     
}

You can do this using http://www.ethereum-alarm-clock.com/

It's a contract on the network that facilitates call scheduling by providing a financial incentive for independent parties to execute them for you. Disclaimer that I'm the author of the service.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.