How are blocks added to the blockchain?

How are blocks added to the blockchain?

This page covers how Bitcoin miners "solve" a block of transactions and add it to the blockchain.

535f0119

The Block.

A "block" isn't actually a fixed group of transactions (to start with). In fact, a "block" doesn't really exist until it is added to the blockchain.

Until then, it makes more sense to think of the current "block" as a bunch of unconfirmed transactions in the bitcoin network, along with a few other bits of relevant information combined together to create what we call a "block header".

The Block Header (basic)

This is what miners are working with when they are trying to add a block to the blockchain.

Lets look at the information a miner uses in the block header…

The block header.

Here's the block header for Block 125552 (before it got added to the blockchain).

Version 1
Previous block 00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81
Merkle root 2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3
Time 2011-05-21 17:26:31
Bits 1a44b9f2

Allow me to explain what each field means…

Version
Previous block
This the solution for the previous block. We're trying to get one of these for the current block. Don't worry, this will make more sense in a moment.
Merkle root
This the hash of all the currently unconfirmed transactions. This is the reason why we're adding blocks to the blockchain, so it's a pertinent field.
Time
This the current time in seconds (unix time). It's always good to include a timestamp.
Bits
This tells us how difficult the block is going to be to solve. We want to find a block hash that is below this value to add this block to the chain.

The block hash.

We can hash all of these individual fields together to create a what's called a "block hash". This is like a unique ID for the block.

Here's what we get if we hash together all the fields in the block header above.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The Block Hash (basic)

That's all well and good (and easy to do), but we're trying to add this block to the blockchain. To do this, we need a block hash that's below a certain target value (i.e. begins with a certain number of zeros).

Getting below the target value.

To try and get a block hash below the target value, we hash all the block header fields together with another number. We call this a nonce.

Version 1
Previous block 00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81
Merkle root 2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3
Time 1305998791
Bits 440711666
Nonce 2504433986

Now the hash looks like this:

00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d

Hey presto, the block (and all its transactions) has just been added to the blockchain. Good work captain.

Finding the block hash.

I may have lied a little. One does not simply hash all of these fields together in their current form to get the final block hash (like I just showed you in the example above).

Sorry.

But don't worry, all we need to do make a few small adjustments to each field and we're good to go.

Preparing the fields for hashing.

All that really happens here is that some fields get re-ordered a bit (mostly reversed) and others get converted to another format (called Little-endian).

Here's what the new fields look like:

Version 01000000
hashPrevBlock 81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
hashMerkleRoot e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b
Time c7f5d74d
Bits f2b9441a
Nonce 42a14695

The hashPrevBlock and hashMerkleRoot fields got reversed. If look closely you can see that the strings get reversed, and then each pair of characters get switched around along the way too.

The Version, Time, Bits, and Nonce got converted to hexadecimal format. Computers find it easier to work with data in this format (the other fields were already hexadecimal).

Now we're ready for some hashing…

The final hashdown.

This is where we hash all of these fields together, and hope to get a result that is below a certain value (i.e. begins with a certain number of zeros).

But don't forget that this is Bitcoin, so it's not like we just straight-up hash those fields together. But don't worry, it's only a straightforward 6-step process to the finish line.

1. First we combine the fields it to one long string, in this order: (Version + hashPrevBlock + hashMerkleRoot + Time + Bits + Nonce)

0100000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695

2. Convert from hex to binary

~V͓DԚ u#ۋBqח+MDBF

3. Hash it

b9d751533593ac10cdfb7b8e03cad8babc67d8eaeac0a3699b82857dacac9390

4. Convert from hex to binary

QS5{غgi}

5. Hash it for the second time

1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000

6. Fix the order

00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d

Ta da!

We've found a hash value that is below the target value. This means we have solved the puzzle and the block can be added to the blockchain.


Warning: include(/home/thepoker/public_html/_footer/index.php): failed to open stream: No such file or directory in /home/thepoker/public_html/bitcoin/archive/mining/index.php on line 37

Warning: include(): Failed opening '/home/thepoker/public_html/_footer/index.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/thepoker/public_html/bitcoin/archive/mining/index.php on line 37