Solidity Hash – How to Get the Hash of Head Block in Private Chain?

hashprivate-blockchain

I'm trying to get the hash value of the head block in an Ethereum private chain. I use the following method in my smart contract:

   blockNumber = block.number;
   blockHashNow = block.blockhash(blockNumber);

BUT, the value of

  blockHashNow

is always this:

  0x0000000000000000000000000000000000000000000000000000000000000000

Also, in the private chain, it seems the number of blocks keeps increasing but I'm not sending any transaction to it.

Question 1: How to get the hash value head/current block in a private chain?

Question 2: Why the number of blocks keeps increasing in a private chain even though no transaction is being sent to the chain?


Edit: I found the answer to the 1st question here:

Inside a smart contract, is it possible to get latest mined block's number and its hash?

In short, I should've done:

  blockHashNow = block.blockhash(blockNumber-1); // -1 has been added now

Best Answer

Question 2: Why the number of blocks keeps increasing in a private chain even though no transaction is being sent to the chain?

If you carefully look at how a blockchain works, this is something important to assure the integrity of the blockchin data. Normally a blockchain protocol works in a way to keep the block rate constant, in order to make it difficult to create a another chain which is longer. In ethereum a block is generated nearly every 14 seconds, and the difficulty is what keeps the average blockrate at that rate. You may refer this answer.

So whether there are transactions or not, miner s keep mining blocks and that's how the protocol is defined for a public network and since you are using the same protocol in your private chain (default geth client), In your private chain also its the same. The code @StevenJaxon had linked can be used to alter your private chain but then it would not be simulating the public chain anyhow for testing purposes that won't matter and it will save your computing power as well.

Question 1: How to get the hash value head/current block in a private chain?

This pretty straight forward if you refer the web3 API reference here and here,

web3.eth.blockNumber // return current block number
web3.eth.getBlock(blockHashOrBlockNumber [, returnTransactionObjects] [, callback]) // returns a block given the hash or number

//combine both
currentBlock = web3.eth.blockNumber;
web3.eth.getBlock(currentBlock, function(block){
    //do whatever with the block details
});

If what you want to do this inside a smart contract, you only be able to get the previous block number, and you can use the following code, refer this question.

contract Test {
    uint public blockNumber;
    bytes32 public blockHashNow;
    bytes32 public blockHashPrevious;

    function setValues() {
        blockNumber = block.number;
        blockHashNow = block.blockhash(blockNumber);
        blockHashPrevious = block.blockhash(blockNumber - 1);
    }    
}
Related Topic