3

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
1

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);
    }    
}
8
  • thanks for the answer! then what exactly these arbitrary blocks contain?
    – Ay.
    Oct 18 '17 at 17:13
  • They will contain nothing as transaction data, but they are like normal anyother block referenced to previous one in the chain :) Oct 18 '17 at 17:25
  • Are you sure my friend? :)
    – Ay.
    Oct 18 '17 at 17:26
  • compare this block and this block previous one doesn't have any transactions but it's structure looks the same Oct 18 '17 at 17:31
  • refer this question as well Oct 18 '17 at 17:41
3

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

You can use the web3.eth.getBlock function and then get the hash from that:


web3.eth.getBlock(blockNumber, function(block) {
console.log(block.hash);
})

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

You can use this code to get your miner(s) to only create a block when there is a transaction to mine.

1
  • thanks for the answer. what about retrieving the same value in a smart contract (not just in the console)?
    – Ay.
    Oct 18 '17 at 17:11

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.