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);
}
}