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

3 Answers 3

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?
    – Aydin
    Oct 18, 2017 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, 2017 at 17:25
  • Are you sure my friend? :)
    – Aydin
    Oct 18, 2017 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, 2017 at 17:31
  • refer this question as well Oct 18, 2017 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)?
    – Aydin
    Oct 18, 2017 at 17:11
0

You cannot fetch the block hash for the current block (block.number). This is by definition, as in a smart contract function, you are etching transactions for the current block.number. The block hash is a hash that takes all transactions in a block (including the function being called in the contract), and hashes them together to make a unique hash for the specific block.

This is why from within a smart contract function, you can only access the previous block's hash.

FWIW, most agents will support buffering of 256 (?) blocks of history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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