In this blog, we will be learning fine details of the Blockchain including Why Bitcoin, Transactions, UTXOs, Protocols, SegWit and Wallets. Tighten your seat belts and enjoy the ride!
Side context : This week 3 of Blockchain, finally we will look at Protocols, Cryptography, Tokens, Wallets and a lot more. Stay tuned!
In the last blog, we discussed the process of mining in detail, here is a small recap —
A person wants to transact / write something on the blockchain, he/she provides the**details of the transaction and some GAS fees*. The higher the GAS fees, the sooner it gets picked by a miner. Similarly all of these pending transactions are kept in* Mempool*, miner picks several transactions from the Mempool (of course, transactions that offer high GAS will be picked first). Now, miner wants to write this data to the chain, it requires to perform tasks based on the Consensus Protocol, suppose for now it is* Proof of Work.
In Proof of Work, a miner needs to solve a complex mathematical task which we discussed in the last blog, it is to**calculate a HASH which is below Target HASH*. We know Hashing Algorithm like* SHA-256*generates unique output for every unique Data, since we cannot change the content within the Data, we have Nonce. The Hash is calculated using :Block No.(cannot be changed),Data(cannot be changed),Last Block’s Hash(cannot be changed),Nonceand*Timestamp*.*
Nonce is a 32 bit number, which is approximately 4 billion unique numbers. So the miner tries every permutation of these Nonces to generate a Hash below our Target HASH ( this target is changed every 2 weeks ). Assuming our miner has insane mining power, it would take a fraction of a second to exhaust the Nonce range.**So what to do ? Should the miner wait for the timestamp to change i.e. 1 second ? No.
The miner then p**icks up another transaction replacing the least paying according to the GAS Fees, and tries this process again*. When the miner generates the hash successfully, the block is added to his personal copy of the chain. Now the new block added to the chain is* verified by other miners and once it is validated, it is successfully added to the chain*. The* miner getsrewarded the GAS Fees of all the transactions inside the block, plus some coins*/ tokens of the network.*
( Let me know if you liked this storyline recap, would include them in upcoming posts too! )
Now**, what was the need of Bitcoin ?**
Do not confuse Blockchain and Bitcoin, Blockchain is the technology where as Bitcoin is a protocol / coin built using Blockchain. A protocol is introduced to solve a problem. What problem does Bitcoin solve ?
( Read about 2008 Financial Crisis ) After several banks declaring bankruptcy the government intervened and bailed out a few banks. So a group of people thought “Why does government hold the power to print money and bail out a few selected banks ?” In 2008, Satoshi Nakamoto ( We just know a name, his face is unknown to the world ) introduced a White Paper ( Research Paper ) of Bitcoin. What is so special about Bitcoin ?
) Limited Supply. There will be a limited amount of Bitcoins in circulation which is 21 million (only 1.32 million BTC are left to be introduced).
) Decentralized, so the decision-making power is in the hands of the public.
) Monetary Policy. Coins are added to the network after a miner successfully adds a block to the network. In every 4 years, the coins introduced to the BTC network per mined block arereduced to half. So if a miner was rewarded 6.25 BTC per block mined in 2020, now he/she will get only 3.125 BTC in 2024.
) Proof of Work. Just similar to how you have to do something that requires some form of work to earn money, Proof of Work is a consensus protocol where miner needs to solve complex mathematical problems, which costs electricity and computational power. Therefore, adding value to Bitcoin.
( Learn this via animation : Tuttle Twins )
Where and how are these transactions, coins, etc stored ? Wallets**.**
A wallet only contains Private and Public keys, all your Bitcoin is on Blockchain and all your holdings are in the form of UTXOs. While creating a wallet you get a SEED Phrase which could be 12 or 24 words long, through which you can generate a Private Key of your wallet. You can access your wallet only using Private Key, and in fact if it gets compromised anyone can access your wallet. Therefore it is recommended to write down the Private key and keep it safe.
A Public Key is generated using a Private Key, but it can be shared to the public. Now using this Public Key, your bitcoin address is generated. You receive Bitcoins on your Bitcoin address but you send bitcoins through your Public Address / Key. Why separate addresses ? Bitcoin community thought, currently there exists no technology which could reverse calculate the Private key from Public key but maybe in future that could be a possible threat. So they decided to add a new layer i.e. Bitcoin address which is derived from Public Key. ( See How it is impossible to guess Private Key )
So what are transactions in Bitcoin and how is it made ?
So transactions are stored as UTXOs i.e. Unspent Transactions Output, this is list of all the transactions in coming to your wallet / Bitcoin address. Unlike a bank, a wallet does not add up your transactions. So, how do you send someone Bitcoin ?
Simple, when you want to send someone x amount of BTC, you take UTXOs that at least add up to x amount + GAS Fees. If the amount exceeds, you receive it back to your wallet and it will show up in UTXOs. When the transaction is successful, the UTXOs used are removed from your wallet.
But how is the transaction validated ?
So, when you create a wallet you get a Private Key, which is like access to your wallet. A Public key is generated from the Private key, which cannot be used to backtrack the Private key. Whenever you make a transaction / write a message, a signature is generated using your Private key and that particular Transaction / Message. A transaction in mempool does not just contain the amount, but also your Public key ( as I said, you use your Public Key to send BTC ) and the Signature. Miners have a function which intakes your Public Key, Message/Transaction and Signature and it can determine if the transaction was really made by you.
So if your Private Key is compromised, anyone can generate a transaction, then a Public Key and a Signature using it and the miner will push those changes to the BTC network, thus compromising your coins.
Since every transaction contains Data, Public Key and Signature*( takes about 65% space ), it drastically shrinks the number of transactions we could store in one single block. Therefore **Segregated Witness (SegWit)* came into the picture. As Public key and Signature were essential to verify the payments but we wanted only the transactions to be pushed on chain, we started sending the Public key and Signature separately. ( We will look into details when we learn about Hard & Soft Fork )
Can anyone keep track of my Public key and estimate the amount of Bitcoins I send or receive as they can easily generate my Bitcoin address using my Public key ? Sadly, yes, but to prevent this HD wallets were introduced.
Hierarchically Deterministic Wallets gives you one Master Private key which allows you to create multiple Private keys and thus could diverse your Bitcoin transactions through multiple Public keys and Bitcoin Addresses. Note, using Master Private key you can track all the spending of generated Private keys but not vice versa.
That's it for this week! Stay tuned, Next Week : Ethereum, Nodes, Types of Accounts, DApps and Smart Contracts.
Source : Code Eater