DEV Community

gmfcastro
gmfcastro

Posted on

Explaining blockchain basics

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

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 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

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

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 and Anders Blockchain. Also you can read the whitepaper 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.

Top comments (6)

Collapse
 
bieelsoares profile image
Gabriel Soares

Great post, Guilherme. Thanks for sharing. I have a few questions to you related to that.

  1. You mentioned "MerkleRoot" and why is not called "hash". Do you think that using "hash" is wrong and can be considered an anti-pattern?

  2. You added the "creationTimestamp" as a required field to the block header (actually you didn't directly, but it seems to be in your description). This field seems to be more an optional data field, instead of a requirement to the block header. What your thoughts about this?

Thanks again, man! :)

Collapse
 
gmfcastro profile image
gmfcastro • Edited

Hey Gabriel ! Good questions, I think i can answer them:

  1. I don't think this is considered a anti-pattern at all, since I am based in the Bitcoin in this post the naming was a definition that Satoshi made, probably to let clear that the value is being generated with Merkle tree. With that said this is basically a decision that is made by the developer so if you plan to develop a fork from Bitcoin (github.com/bitcoin/bitcoin) to create your own coin you can go a ahead and change it. :)

  2. Yes Gabriel like the first answer the block is planned by the developer and they can decide if its important or not to have this field. Personally i think it is a important field to have more transparency of information in the blocks explorer around there. Checkout blockexplorer.com/ and try to find some blocks there and play around it :) (Btw those are real blocks !)

Collapse
 
gbscode profile image
Babafemi Sorinolu

Nice post. I am working on my project, Electricity monitoring using blockchain technology and I will like to seek your help regarding some issues. Kindly get back to me.

Collapse
 
skpaul82 profile image
Sanjoy K. Paul

Good post for starting blockchain. Thanks for sharing.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.