Explaining blockchain basics
Tags: blockchain, bitcoin
Published: 2018-08-12 01:03:26

---
title: Explaining blockchain basics
published: true
description: 
tags: #blockchain, #bitcoin
---

Following my studies of blockchain, I've focused to have a deeper understanding of what is a blockchain and how it works. Of course a lot of you may have already know what is it, but I'll share with you what I've learned so far.

#What is it in simple words ?
Blockchain is a virtual ledger, where each record of this is a block. That records are  distributed in a network for everybody, where them can read and write, but not change the information of it. To understand this better, let's get to know what is a block and what is inside of it, and then how a block connects with other block. For this explanation, I'm going to explain blockchain based in the bitcoin implementation.

#Anatomy of a block
Each block contains a header and a body.

![Block](https://thepracticaldev.s3.amazonaws.com/i/ps5ay7rlfco877p7eua7.png)

These are the data that each block have. Besides that, every block has a max size in bytes that is defined by the developer. When this max size is reached, this block is added to the blockchain and a new one starts to be created. I'll explain each of those items below.

####Block Number
Every block in a blockchain have a number that represents your position in the chain. The 1st block added to the chain has a special name, they are called **genesis block**.

####PrevBlockHash
Every block have in the header a reference of the previous block hash number. This is really important, because this is how the blockchain keeps consistent and control the immutability.
The genesis block, doesn't have nothing that refers a previous block, since it is the first block.

####MerkleRoot
The merkleRoot is the hashing result of all transactions inside the block. If anything is changed inside the block, this hash also changes and invalidate the changed block and the next ones also. 
Now you can wonder: why this field is called MerkleRoot instead of blockHash ? This is because Bitcoin uses the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) to generate the hash from block's data.

####Nonce
This is a 32-bit number that is related with the mining process of a block. This number is used only once to find a hash that matches the difficulty of the blockchain. I'm going to write about mining later and hopefully this will be more clear for you.

####CreationTimestamp
This is the timestamp of the block creation.

####Data
Here is where all the transactions data remains.

##Hashing
I've talked a lot about hashing, right? What kind of hashing algorithm is used here? 
That's really depends of each implementation of the blockchain, but for the cases that I talked here this should be a One-Way hashing algorithm. Bitcoin uses the SHA-256 (Secure Hash Algorithm) with 256-bit length. If you want to read more about: [SHA-2](https://en.wikipedia.org/wiki/SHA-2)

Now that we know what is inside a block and what type of hashing is used, let's show how is it inside a blockchain.

#Blockchain
Here is where we will have our blocks linked together by the prevBlockHash as you can see below.

![Blockchain](https://thepracticaldev.s3.amazonaws.com/i/a521yws9wrjbjz9tn482.png)

The merkleRoot value is generated from the Data information, and if anything changes in there, the merkleRoot will change too and the rest of the blocks that was linking the prevBlockHash after the changed block won't be valid anymore. That's why the blockchain is immutable.

I think this is a good start and if you want to know more, there is a really good website that you can use to play around and understand better what I said here: [Anders Block](https://anders.com/blockchain/block.html) and [Anders Blockchain](https://anders.com/blockchain/blockchain.html). Also you can read the [whitepaper](https://bitcoin.org/bitcoin.pdf) from the bitcoin's anonymous creator, Satoshi Nakamoto.

Hope that this post helps you to understand more about blockchain and in my next post I'll explain about the blockchain network.