I know how to use a miner, how to use a pool and stuff like that, and I have done it before (and am right now), but I've always wondered what the actual process was to calculating hashes? What is my miner doing right now?
The idea of mining is to calculate a cryptographic hash of a block (a block is basically just a list of recent transactions, a hash of the previous block (so you can't prepare fraudulent blocks in advance), and some other stuff) such that the value of the hash is lower than a predetermined target. If it is, you win: your block gets included in the block chain and you receive the block reward (currently 50 BTC) as well all the transaction fees associated with that block. If the hash doesn't come under the target, you lose, and you have to try again by changing a small piece of data in the block (called the nonce) which causes the whole hash to change in a random way. The key point about a cryptographic hash is that it's impossible to predict what effect changing the nonce (or any other part of the data) will have on the hash: the only way to get a hash that meets the target is to try every possible nonce (a typical mining system is capable of trying millions per second) until you find one that works. This is key to the security of Bitcoin: it's impossible for anyone to introduce a fraudulent block without first using a ton of computing power to find one that meets the hash target. Not only that, they'd need (on average) more computing power than the rest of the network combined, because if anyone else finds a real block first, all the effort trying to produce a fraudulent block is wasted.
I've seen this:
doHash(block_header) {
hasher = new SHA256();
hasher.update(block_header[0..63]); // update A1
hasher.update(block_header[64..127]); // update A2
hash1 = hasher.getHash();
hasher.reset();
hasher.update(hash1); // update B
hash2 = hasher.getHash();
return hash2
}
but I have pretty much no clue what it means.
This code just calls the hashing function on the blocks, there's nothing special about it.