3

"block.timestamp" and "now" are known. I would like to know how to get a timestamp of a specified block.number?

Like: uint blockDoneTime = (block.number - 1000).now; // Or something like this...? :)

2
  • 1
    Welcome to the Ethereum Stack Exchange! I've attempted an answer but this question might be an XY problem.
    – eth
    Aug 27, 2017 at 10:05
  • 1
    There's the EIP github.com/ethereum/EIPs/pull/210 which proposes accessing the blockchain data through a contract.
    – Ismael
    Aug 27, 2017 at 23:28

3 Answers 3

2

As user eth correctly indicated, you cannot get the timestamp of arbitrary blocks directly.

One work around is to you pull in data via external service providers and reading them via oracles. You could, e.g. use the etherscan API and then use a simple oraclize.it query to trustlessly load the data into your contract.

A lot more cumbersome, costly and involved than what you had in mind but IMO the best you can do.

Theoretically you could feed in a lot of full blocks to the smart contract, validate the required parts of the chain to make sure that the data is not fake. Both the required amounts of data and the computational complexity would probably make this pretty much impossible in practise.

3

Solidity is one of the higher level languages for the EVM. The EVM and the blockchain are different, and the EVM has limited access to the blockchain: What OPCODES are available for the Ethereum EVM?

If your contract wants to know information about 1000 blocks ago, you need to store that data in your contract (maybe as part of initializing the contract). Alternatively, if your contract was invoked 1000 blocks ago, that's when it should have stored the timestamp.

0

There is a way where you can get reliably the timestamp of an old block without oracles and without investing much gas in the transaction. Following an optimistic approach: Trust in anyone providing the timestamp and provide a mechanism to the smartcontract to validate the correct timestamp if needed.

Explanation:

Let's suppose your smartcontract needs the timestamp of the block n-20 (Tn20) for operating the function FA(VariableA) correctly. Then, you can "just" leave the function trigger (the caller, Alice) to provide the timestamp as part of the function, as a function variable FA(VariableA,Tn20) and "trust her".

Then you give a time, let´s say 10 blocks, to anyone to question if that timestamp was correct. If the timestamp was false, someone (a challenger, Bob) can claim that the timestamp was false and provide the real one. Next, the caller can rectify and recognize the error, but if she doesn't, then the challenger can feed the smartcontract with the the full information of block n-20 and start a judgment. The smart contract judges the block by calculating the blockchash and providing a reward to the challenger if his proposed time was the correct one, including also a punishment for Alice.

The idea is coming from here, section 5.4

This is the same mechanism used in the Optimistic rollups to reduce the "gas" of the smart contracts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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