0

I have been trying to learn about blockhash in Solidity, I have made search about it and still I cant get it. I deployed two contracts on Polygon Mumbai by using Remix. One of them is with block number 30876918, the other one with 30876932, but their blockhash is the same (0x2f2e26204a8fa4569f3f3efe3932aaca31a6f3d61d631fe90be5cdcf459e26bd).

Do you know why?

blockhash(uint blockNumber) returns (bytes32): hash of the 
given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero

3 Answers 3

2

I suggest to redo the "experiment" on Ethereum Testnet Goerli. It sounds very strange to me and I would expect to have a different result on Goerli. Please give a response with the result if you try it on Goerli.

1

blockhash gives you the hash of the block within which the transaction has been processed.

So if you are calling blockhash in some function that is not a constructor, it will give you the current block hash.

If this is not the root of your problem, share your contract and transactions, so we can give some more insight.

0

I think I found the answer. blockhash takes block.number as parameter. And when I was calling the functions to get the blockhash on two different contracts, I was doing it one after another, without waiting. And that means both function calls were included in the same block and consequently they both had the same block.number and consequently same blockhash

My mistake was not to realize that block.number in getBlock and getBlockhash functions could be different. Thank you for your answers. And here my contracts for reference.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.7;

contract Blockhash1 {

    uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    function getBlock1() external view returns(uint) {
        return block.number;
    }

    function getBlockHash1() external view returns(bytes32) {
        return blockhash(block.number-1);
    }

    function getHashNumber1() external view returns(uint256) {
        return uint256(blockhash(block.number-1));
    }


    function getDivision1() external view returns(uint256) {
        uint256 coinFlip = uint256(blockhash(block.number-1)) / FACTOR;
        return coinFlip;
    }
}
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.7;

contract Blockhash2 {

    uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    function getBlock2() external view returns(uint) {
        return block.number;
    }

    function getBlockHash2() external view returns(bytes32) {
        return blockhash(block.number-1);
    }

    function getHashNumber2() external view returns(uint256) {
        return uint256(blockhash(block.number-1));
    }

    function getDivision2() external view returns(uint256) {
        uint256 coinFlip = uint256(blockhash(block.number-1)) / FACTOR;
        return coinFlip;
    }
}

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.