As following answer for How can I find out what the highest block is? guided: we can get the latest mined block's number and its hash inside geth
as follows.
> eth.syncing.currentBlock
569367
> eth.getBlock(eth.syncing.currentBlock).hash
"0xac51e27c3fe38af1a0aeb2867a741ba6aa16780e435a586d934ccea3a71874b1"
[Q] Inside a smart contract, is it possible to get the latest mined block's number and its hash? or should I keep an array inside my contract and when new block is mined, externally (my_contract.addNewHash(<some_hash>)
) should I put its hash into my array for my contract to read from?
I have tried following keywords from Solidity's website: https://github.com/iurimatias/embark-framework/wiki/Solidity on Solidity Browser and Populus, for example: block.number, block.blockhash
, but I do face with following error:
Untitled:6:17: Error: Expected identifier, got 'Semicolon'
block.number;
Thank you for your valuable time and help.
Best Answer
Transactions are always executed in the block being mined. So, at any time,
block.number
will return the block height in which the transaction is being mined.However, the hash of the block is not known until it is mined, and the output of transactions is part of the hash calculation, so the transaction has no access to the hash of the block being mined.
If this is any consolation, you can get the hash of the latest mined block, i.e. the one right before this block being mined with
block.blockhash(block.number - 1)
.Try this:
Make a transaction to
setValues()
then you will see that:blockNumber
is set at that number it was mined inblockHashNow
is set at0x
blockHashPrevious
is set at the previous block hash indeed