your image

Concept of bitcoin, how it works and the different components | big data consulting services | Clairvoyant Blog

Sumit V
blog.clairvoyantsoft.
Related Topic
:- Adaptability

Bitcoin - Under the Hood

 

Sumit V

Follow

Jun 5, 2018 · 11 min read

 

 

 

 

 

Source: dailyforexport.com

I started my journey to study blockchain a couple of weeks ago, but I found most of the blogs just give a brief idea about what is blockchain or bitcoin. For a technical/Developer person who wants to understand the details about blockchain, have to scan lots of blogs and collect information in bits and pieces. Here I have tried to collate all required understanding about Bitcoin and Blockchain in one blog. Hope I have saved your days of research. Throughout the blog, I had provided reference links from where I collated specific information. Feel free to visit them for more information. So let's start.

Blockchain is the backbone of bitcoin. Bitcoin works on blockchain technology. This was introduced by an unknown person called Satoshi Nakamoto in January 2009. The first block was mined by himself on January 2009 and that block is called as Genesis block (the very first block in blockchain). The first reward which was drawn after mining was 50 BTC’s. Current rewards that minors get is 12.5 BTC + transaction fees. This reward decreases to half every 4 years and it is assumed that the last bitcoin which is going to be mined will be around the year 2041. After that only transaction fees will be awarded to minors who confirms the block.

Blockchain is a platform which maintains the distributed ledger. In a blockchain network, there are multiple nodes(either miners or non-miners), which form a blockchain network. These nodes maintain their own ledger. Due to which this becomes a public ledger and can be accessed by any node who is a part of the blockchain network. Blockchain is a collection of blocks which are linked together which forms a chain of blocks. A block can contain n number of transactions and these transactions are included in a block and are published in a blockchain network, if this block is verified and accepted by other nodes then this new block becomes a part of the blockchain. Lets deep dive into this concept.

 

How bitcoin works from sending money to forming a new block. Source: wiki.p2pfoundation.com

What do you mean by distributed ledger? Ledger is nothing but maintaining a list of transactions. In our centralized system, let's take an example of Banks, where banks maintain a ledger of each account holder who possesses an account with a bank. It maintains all debit and credit history for an account. Now, these details are accessible to only bank and account holder and the transactions are maintained only by the bank and no one else. This becomes a centralized system.

Here in case of decentralized systems, there is no central authority like a bank who controls all inflow and outflow of transactions. Here all nodes who are a part of a blockchain network have the authority to approve if a transaction is correct or not. If the majority of nodes flags green for the transaction then blockchain accepts that transaction.

The main advantage of this system is almost impossible to hack and make any fraud transaction as if any node tries to make any fake transaction, then there are other nodes who are sitting to validate the transaction if it is false this will never make a part of the blockchain. Once the transaction is part of a blockchain, then only the transaction is considered as committed and this cannot be revert back in any case.

Node is able to verify the validity of any transaction because each node maintains the history of all transactions happened till now.

Suppose person Alice sends a 50BTC to person Bob. This is a transaction. Where the balance in Alice’s account goes down by 50BTC and Bob’s account goes up by 50BTC. These transactions can either be carried out on one of the node machines or on a node which hosts wallets for customers. Transactions can only be carried out with the help of an address. The address is nothing but a cryptographic key. This is explained in a later topic.

A transaction consists of four main sections:

  • Transaction ID: hashed unique id
  • Metadata: Details about the transaction
  • Input: Explained below.
  • Output: Address to which bitcoin needs to send.

For instance consider Alice creates a new wallet and instantly receives 0.2 BTC, 0.01 BTC, and 3 BTCs to address. When we see collectively the wallet amount it will be shown as 3.21 BTC. But inside wallet it is actually 0.2, 0.01 and 3 BTC’s. These 3 different amounts don’t mix up but remain as is. These are called as Unspent Transaction output (UTXO). These transactions are totally locked and cannot be modified. These are marked as output when the next transaction is carried out.

Now suppose Alice wants to return back 0.15 BTC to Bob, now in this case bitcoin script analyze all sets available and picked one. So in this case, the script pics up 0.2 BTC transaction as an input to a new transaction. Here bitcoin network does not take only 0.15 BTC and keep 0.05 BTC as a balance. But instead, it takes 0.2BTC as an input. And here it will create transactions.

  1. 0.15 BTC to B’s address
  2. 0.05 BTC to A’s address as change.

Now once this transaction gets confirmed by all network the amount will be credited into there respective accounts. I.e. Bob will get 0.15BTC and Alice will receive change as 0.05BTC.

 

Source:ccn.com

Merkle tree is a binary tree. The miner will choose which all transaction he wanted to put it in a block from the unconfirmed transaction pool. Once chosen, the Merkle root is calculated based on the number of transaction. Suppose we have 8 transaction that needs to be a part of a block. Each transaction will get hashed twice. And the set of 2 hashed transaction will be hashed together, which will give us 4 hashes, again set of 2 hash will give us 2 hash and at the end 1 hash. The single hash which we get is nothing but a Merkle root which is given as input to a block.

 

Source: hackernoon.com

Miner, will include this Merkle root in the newly unconfirmed block and broadcast it to network.

This Merkle root does not verify transactions, instead, it verifies a set of transaction or integrity of the transaction. Root is a set of hashes of there leaf nodes and intern their leaves and so on, and ultimately the transactions. This means that any single change in any of the transaction changes, the whole tree changes, even if the order of transaction changes the Merkle root will also be change. This gives the miner confidence that checking any transaction gives the root which matches the blocks Merkle root.

Ref: https://bitcoin.stackexchange.com/questions/48928/why-does-each-block-store-a-merkle-root?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Each bitcoin wallet account consists of public and private keys and both these keys are part of the ECDS Algorithm. A wallet can contain multiple private and public keys. (Ref: http://en.wikipedia.org/wiki/Elliptic_Curve_DSA).

Private key 256-bit number or 64 character and is a random number and needs to be kept very secure because this acts as a ticket to spend the bitcoins. Without the private key, no transactions would be carried out. Private key starts with 5. The private key is used to create signatures when a transaction is performed. And this signature can only be verified with the help of its public key.

Public key is derived from the private key. The public key is an address to where we can send bitcoins. In order to confirm if the transaction is valid, miners look for a digital signature(transaction signed using private key) and it is verified using the shared public key. If they are valid then the transaction is considered as a valid transaction.

Ref: https://bitcoin.stackexchange.com/questions/4675/what-is-a-private-key-and-a-public-key

When a transaction is generated, a digital signature is generated using the private key. This key along with the public key is used to verify if the transaction is valid or not. During verification of the transaction by nodes, nodes detect that using the public key the signature is valid and all input transactions are accessible using the keys and hence marked as a valid transaction.

Block consist of required details to be a part of the blockchain.

This contains

  • Magic Number: Is always 0xD9B4BEF9, as this represents the type of data structure or file type.
  • Block size: Size of the total block
  • Block header: Hash address of the current block
  • Version — Block version number
  • Previous block hash — 256 bit hash of prev block
  • Merkle root hash — 256 bit has of Merkle root. (hash of all transaction)
  • Time — Current timestamp in seconds
  • Bits (target) — Target is a 256-bit number. (i.e.0x1bc330*256**(0x18–3))
  • Nounce — 32-bit number starts from 0
  • Transaction Counter: number of transactions
  • Transactions: Transaction list.

Target is calculated based on difficulty.

 

 

Difficulty is adjusted every 2016 blocks, approximately 2 weeks. It is calculated based on below formulae

Expected time taken to resolve 2016 blocks / actual time taken to resolve 2016 blocks.

If miners were able to solve 2016 blocks with an average time of 9 mins, we would get value like

20160/18144 = 1.1111

If the resultant is greater than 1 then need to increase the difficulty, else decrease the difficulty.

So the new difficulty = difficulty*1.1111

And this new difficulty is now set for another 2016 blocks.

Ref: http://learnmeabitcoin.com/guide/difficulty#finding-the-target-using-the-difficulty

When any transaction is generated, each and every transaction goes into the unconfirmed transaction pool. This pool consists of a huge list of unconfirmed transaction which is waiting to find a place in a block. Miner either selects all transaction or selects some transaction of his or her choice, where he can be more profitable as some transaction comes with a good amount of transaction fees which when on mined by a miner will be given to miner for the work as a reward. So miner has the freedom to choose any transaction from the unconfirmed transaction pool. Before adding miner checks if the transaction is eligible to be executed according to the blockchain. This is done by checking if the sender actually has sufficient balance by tracing past transactions.

Once the list of transactions, which will be a part of the blockchain, is selected, then miner starts creating a block. It creates a Merkle root and the root hash is fed to block, takes the previous block hash and appends it in previous block parameter in block. Once all other parameters are set, miner is now ready to mine the block. Note that every miner creates its own block and could have the different or same set of transactions in the block.

What exactly mean by mining a block?

Miner tries to solve a very complicated mathematical problem to satisfy a condition. If this condition is true then a new block is mined and it is broadcasted to a network. This solution is called a Proof of Work(PoW). Other nodes look at the solution validates if the solution is correct and if 51% of nodes accept the solution, the block becomes a permeant block in the blockchain.

What is Proof of Work(PoW)?

Every block who will be a part of the blockchain has a mathematical problem to solve. Unless this problem is not solved by the miner, this block will not be a part of the chain. This problem is nothing but a hash algorithm which should be less than or equal to the target. This is not that simple as you are thinking. This hash target comes with a certain amount of zeros appended at the start.

Now miner will create a hash out of all Merkel tree, previous hash, version, etc… This doesn’t end here. These inputs will generate a hash value. Now you would say that we have the same input and it will produce the same output, so how we are going to compare the target value? Here is the catch. Along with the set of input, we have to add another input called Nounce. Nounce is a number which is incremented by 1. Initially, it is set to 0. Now in hash input, if we have a small change will impact the whole has generated.

Miner uses this nounce each time to generate the hash. Hash generated with the input parameters and nounce=1 if it is not matched with the target, nounce will be incremented by one, and this process goes on until a hash is found which is less than or equal to the target. Based on the difficulty, miner will approximately take 10mins to get the required solution. The difficulty either increases or decreases on every 2016 block and it depends on the time taken by these blocks to solve the problem. Explained above.

Once the problem is solved the new hash generated with nounce is appended to a new block and it is broadcasted to the network.

Ref: https://medium.com/coinmonks/how-a-miner-adds-transactions-to-the-blockchain-in-seven-steps-856053271476

When block is formed, other nodes are immediately notified that the new block with a solution is available. This spreads quickly within a second throughout the network. Now all these nodes start validating the block. Solving the problem is much harder than verifying them. It hardly takes some time to verify if the block is valid or not.

Once other nodes receive the block, a parallel process is initiated in their own environment to check the validity of the block. Every node either a non-miner or a miner node check for the block validity. Firstly the integrity of transactions are checked, i.e. the Merkle root has is checked, it checks the transaction and tries to form a Merkle root along with this the available balance is also checked. If any node tries to change the transaction at this point, a complete different Merkle root will be formed and result in a completely different block hash. This hash is different than the has which other nodes received. Once the integrity of the transaction is verified, then the solution i.e. PoW is verified where we have nounce value along with the previous has and Merkle hash and the corresponding hash is less than or equal to the target hash. If all these conditions are satisfied then only the block marked as valid by a specific node.

These steps are carried out by each and every node in the network. Now once 51% of nodes accept if the block is valid, then only the block becomes the part of the blockchain and in return, miner will receive rewards of newly formed bitcoins + the transaction fees. These bitcoins are mined, i.e. these are new bitcoins generated by the network, which goes into miner’s account. For this reason, in a block there is always a coinbase transaction exists at the top which has a to address where the reward goes.

This is all about Blockchain and working on Bitcoin in the blockchain framework. I had invested almost a week in understanding the concept and working of bitcoin and searched forums, blogs, posts and find information in bits and pieces. Here I had tried to combine them into one single article which is quite exhausting to read, but trust me, you’ll gain a considerable amount of understanding about the bitcoin with this post.

Do let me know about any queries or concerns, and feel free to correct me anywhere in this article.

You can read my other blogs

Hyperledger Fabric — Components and Architecture

Hyperledger Fabric — Transaction Flow

Thanks,

Sumit V

Comments