Author

Topic: What is the exactly mechanism of mining? (Read 953 times)

donator
Activity: 1218
Merit: 1079
Gerald Davis
May 06, 2014, 12:16:26 PM
#6
I don't fully understand when you say... "When a node has new information". From what part that information is coming from?

Either from itself or from its immediate peers.

If you create a new tx on your client (a bitcoin node) your node now "knows" something the rest of the network doesn't so it verifies it, store it locally (memory pool of unconfirmed txs), and relays it to its immediate peers (the other nodes you are directly connected to).  Likewise when those peers receive your new tx they "know" something the rest of the network doesn't.  So they verify it, store it, and relay it to their peers.  Those peers verify it, store it, and relay it to their peers until eventually every node on the network is aware of this new tx.

Now I used a tx as an example but the same is true of any information.   If your node is new and joins the network the fact that it exists is new information so it will relay it to its peers.   If you are a miner and solve a block the knowledge of that block is new information so you will relay it to your peers.  Those peers will verify the block, store it locally, and relay it to their peers.  The same thing applies to any information about the network, transactions, known peers, blocks, alert messages, etc.

The key thing to take away from all that is the network is at the simplest level a information relaying network.  Nodes verify information first (just because another node sends you a tx doesn't mean your node trusts it), stores it locally, and then relays it to its peers.

Quote
Another question I've about the mechanism of finding the correct hash is...
How the mining software knows that it has created the winning hash? As far I know the mining software looks for the correct hash, but when the software knows that the last hash created is the correct/incorrect one?

The mining software is told either by the node directly or by the pool server (which acts as a type of proxy).  The network has a target which is just a 256 bit number it varies inversely with difficulty (difficulty goes up, target gets smaller).  All nodes know the current difficulty because they compute it every 2016 blocks based on how long the last 2016 blocks took.  

The current target in hex is: 0x0000000000000000896C00000000000000000000000000000000000000000000
http://blockexplorer.com/q/hextarget

If the hash produced is smaller than the current target it "wins" and the block produced is valid (and relayed to the rest of the network).
If the hash produced is larger than the current target is "loses" and the block produced is invalid (and discarded).

It really is that simple.  Each hash is simply a random (very small) chance of solving a block.   You keep trying modifying the blockheader until you find a "winner".

To make it more obvious I am going to shorten the target so we just look at the first 20 hex digits: 0x000000000000000A896C

So say you hash a blockheader and the leading 20 digits are:
0xF128547630A12032896C = loser
0x30A12032F16896C28547 = loser
0x00008547630A2032896C = getting closer but still a loser
0x0000000000000032896C = oh so close but still 100% a loser
0x000000000000000003FF = winner

The last one is a winner simply because it is smaller than the target.
0x000000000000000003FF is less than
0x0000000000000000896C

or if you prefer full length
0x000000000000000003FFxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
0x0000000000000000896C00000000000000000000000000000000000000000000

Any value for hex is still less than the target much like 1xx is always less than 200.
legendary
Activity: 2590
Merit: 1022
Leading Crypto Sports Betting & Casino Platform
it's like finding the combination of a strongbox, the coins inside is the reward, and blockchain is a chain of strongbox
member
Activity: 118
Merit: 10
So, you've explained to me that the Bitcoin network is made of thousands of computer, each computer is called a node. These computers run a software that is compatible with the Bitcoin network. Ok, it would be like if someone wants to download a file from Torrent network. That person would need to run Torrent software or a software that is compatible with the Torrent network protocol.

I don't fully understand when you say... "When a node has new information". From what part that information is coming from?

It's a peer-to-peer mesh network.  Peers relay information to each other.  For example, if you create a transaction to spend your coins, you will broadcast that to your peers, and they will first validate it and then relay it to their peers, and this process repeats until all nodes in the network have your transaction in their memory pool.  Note that being in the memory pool does NOT make your transaction confirmed.  It needs to be mined to be confirmed.

A confirmation is when a miner finds a block.  A block is broadcast in the same way as transactions are.  The miner who discovered the block sends it to his/her peers, then those peers first validate it and then forward it, until everyone sees the new block.

Quote
Another question I've about the mechanism of finding the correct hash is...
How the mining software knows that it has created the winning hash? As far I know the mining software looks for the correct hash, but when the software knows that the last hash created is the correct/incorrect one?

So to mine, you first construct a candidate block for the next block in the chain.  This will consist of a block header including a reference to the previous block, a timestamp, plus a merkle root referring to the transactions you include in the block.   You can read more about the specific data structure here: https://en.bitcoin.it/wiki/Blocks

You can include whatever transactions you want in the block (eg. the unconfirmed ones taken from your memory pool) so long as they are valid.  You also include a coinbase transaction to pay yourself the 25 BTC reward.

In order to make the block valid and accepted by the rest of the network, you need to satisfy the proof-of-work criteria.  That is, the block header, when double-sha256-hashed, must result in a number below the current target.  In order to keep attempting new hashes, you have to slightly vary the block header, which is why there is a nonce field.  When the none field overflows, you can try vary the merkle root by changing the coinbase transaction.

So, to answer your question, you know you've found a valid block when you find one that hashes to a low enough number.  Everyone knows what the target is because it's derived directly from the chain you are working on.

Take a look at the latest block hashes on blockchain.info, eg: http://blockchain.info/block-index/404079/00000000000000006fae4d3d108cfdbb0d29f3e1e611061e40e267a6f7d1607e

As you can see, the block's hash is a *very* low number relative to the 256-bit hashing space (lots of leading zeros).  You can verify it for yourself by hashing the block header (80 bytes) twice and confirming that you get the same number.  The fact that the miner produced that block with such a low hash, proves that they put a lot of work into finding it.
newbie
Activity: 2
Merit: 0
Well, after reading your reply I understand a little bit better how the system works, but I've not totally understood it all so I'm reading more information.

So, you've explained to me that the Bitcoin network is made of thousands of computer, each computer is called a node. These computers run a software that is compatible with the Bitcoin network. Ok, it would be like if someone wants to download a file from Torrent network. That person would need to run Torrent software or a software that is compatible with the Torrent network protocol.

I don't fully understand when you say... "When a node has new information". From what part that information is coming from?

Another question I've about the mechanism of finding the correct hash is...
How the mining software knows that it has created the winning hash? As far I know the mining software looks for the correct hash, but when the software knows that the last hash created is the correct/incorrect one?
donator
Activity: 1218
Merit: 1079
Gerald Davis
There is no "decodifying" (is that a word).

You can't really understand blocks until you understand nodes, and transactions.

So a couple of quick concepts
1) The Bitcoin network is made up of thousands of nodes which are computers running compatible software compatible with the protocol.
2) When a node has new information (block, peers, transactions) it verifies it independently, stores it locally, and relays it to peers.
3) Nodes maintain a list of valid (because invalid tx are dropped not stored or relayed) but unconfirmed transactions.  This list is commonly called the "memory pool".


So mining at a very high abstract level.:
1 ) Miners* are nodes and thus have a list of transactions awaiting confirmation (memory pool).
2 ) A miner will independently select a set of transactions to include in the next block.  
3 ) The miner can select as many or few transactions as it likes as long as the transactions and resulting block would be valid (i.e. a block larger than 1MB is currently invalid, a transaction which spends already spent outputs is invalid).
4 ) The miner reduces this set of transactions to a single hash using a merkle tree structure.
5 ) The miner creates a block header which consists of the merkle tree and seven other elements.
6 ) The miner hashes the block header and compares the hash to a number known as the target.  The higher the difficulty the smaller the target hash.
7 ) If the hash is larger than the target then it is invalid and the miner modifies one element of the blockheader and tries again.  It takes on average quadrillions of attempted hashes to find one smaller than the current target.
8 ) When the miner finds a hash of the blockheader which is smaller than the target, the miner has a new block.
10) This new information is shared, verified, and stored by the rest of the network.
11) All nodes update the memory pool by removing the transactions found in the new blocks.  Miners then start the process all over by selecting a new set of transactions to include in the new block.


To date this process has looped almost 300,000 times.  It will continue as long as bitcoin exists.

newbie
Activity: 2
Merit: 0
I'm a newbie and I've read some information, but I want to know from the begining to the end how a solo mining process work.
Until now I understand that mining is like decodify a packet of information. This decodification is made by trying keys (hashes) and to see which one is the winner.

My question is what original codified information have to be downloaded, from where, what it looks like... and what is the technical process to decodify that information.
Jump to: