Inside my contract, I want to verify a given block hash is valid or not.

block_hash_validation(string blockhash) returns (bool){
   return true; || return false;

Q1: If yes, from block hash value could I also learn its block number?

Q2: Or should I provide block number as well block_hash_validation(uint blockNumber, string blockhash) to see that does the block number points to the correct block hash?

block_hash_validation(uint blockNumber, string blockhash) returns (bool){
   if(block.blockhash(blockNumber) == blockhash)
       return true;
       return false;

Thank you for your valuable time and help.


Q1: I don't see an obvious way to do that without getting into a gas-expensive loopy process that won't scale. A web3 client would be better suited to searching for it in my opinion.

Q2: I would incline to this approach if it's necessary.

It looks like it should be bytes32 instead of string. According to this: http://solidity.readthedocs.io/en/develop/units-and-global-variables.html it will return bytes32, so probablly better to compare to same type.

Notice the warning about the limited scope: hash of the given block - only works for 256 most recent blocks excluding current.

It looks like you can reduce the if/else to a one-liner, so this looks right to me:

function block_hash_validation(uint blockNumber, bytes32 blockHash) 
    constant returns(bool isValid) 
    return (block.blockhash(blockNumber) == blockHash);

No time to test. No warranty :-)

Hope it helps.

  • 2
    It is possible to prove that a hash existed more than 256 blocks ago, if you provide the block headers form that block on, but it starts getting pretty expensive. – Tjaden Hess Feb 3 '17 at 5:10
  • Maybe, could we implement our own custom contract for storing hashes of past blocks? which may has to trigger in every block (using ethereum alarm clock) to push the newly generated hash. – alper Feb 3 '17 at 7:28
  • 1
    Sounds mighty expensive to me. I'd be looking for a different way to solve the purpose of it. Hard to say what to suggest. I'm curious about the use-case. – Rob Hitchens Feb 3 '17 at 8:14
  • Instead of defining blockHash as bytes32, I guess we need to define it as string, since byte format is stored as string for example: "0x... b = web3.eth.getBlock(0).hash; block_hash_validation(b, 0)" this works when blockHash is defined as string @RobHitchens – alper Feb 3 '17 at 21:35
  • 1
    There's a type conversion in the javascript API ... toHex() (also, fromAscii seems to work). You can convert "myStringVal" toHex. Then you'll have a bytes32 representation of a string to work with. My thinking is still that the blockHash is returns a bytes32, so that's what you would want to compare. Just my impression from reading the docs. – Rob Hitchens Feb 3 '17 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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