...by Daniel Szego
quote
"On a long enough timeline we will all become Satoshi Nakamoto.."
Daniel Szego

Wednesday, April 4, 2018

Solidity Tips and Tricks - triggering timed event


In solidity or in Ethereum, you can't trigger an event internally from the environment. What you can do is to have an external trusted source that triggers for you something with the help of an explicit transaction. This raises however the problem of centralization and trust, although there is the possibility to structure decentralized oracle network for similar problems, they still remain pretty much centralized. 

Sometimes however the requirement is more simple, one should not really trigger actively an event explicitly, it has only to be made sure that a given functionality works differently before and after a given point of time. In such cases the block number or the block timestamp can be used. The following situation shows an example pattern for that:

contract TestDebugMode 
{
 uint public eventBlockNumber = 1111111111; 
 uint public eventTimestamp = 1111111111;

  function timeBasedLogic()  
    {
     if (eventBlockNumber > block.number) {
           // do logic  
     }
     if (eventTimestamp > block.timestamp) {     
          // do logic 
     }    
 }
}


It is usually proposed to use the block number because in certain situations block timestamp can be gamed by the miners.