Blockchain and smart contracts basics in 2 minutes - part 2
Tags: bitcoin, blockchain, webdev, ethereum
Published: 2022-10-28 14:00:42

## Another part?
yes. There were some terms that I thought were very important which I missed in my previous post. If you haven't checked out my previous post, it's okay. As long as you have at least knowledge of what blockchain is, you won't feel loss. However, if it's your first time, please read the part 1 first. Let's go!

## Mainnet and TestNet

Mainnet is the blockchain network on which most “real world” applications and transactions take place. It is here that real money(in the form of crypto) is transacted.
Testnet is the blockchain that behaves like mainnet except there is no real money involved, and it is mainly used for deploying test projects and doing test transactions. You can send and receive fake money on it, but you cannot transfer it to mainnet.

## Gas
Every time a transaction is made, a node will have to solve a complex mathematical problem to approve it. The measurement of computational power required to approve the transaction is called **gas**. This depends on the complexity of the problem, demand and supply capacity of the network at the time of the transaction.

To make a transaction happen on a blockchain, the nodes or miners will charge a small fee for running the blockchain. The fees depend on the amount of gas required for the transaction. The more gas used, the more fees you’ll have to pay. On top of it, some coins(or crypto) are also “burnt”. This means during the transaction, some ether/coin/crypto is lost forever. This is done to decrease the supply of the crypto and increase its price.


## How blockchain works

A blockchain is a chain of blocks.

Each block has the following data:
- Block number 
- Nonce - the solution to the problem
- The actual data we want to deploy - usually contains a list of transactions.
- It's hash
- Previous block’s hash - the hash of the block before it
Hash

![block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/il90rz31jsq01i592tbh.png)


When the data, block number and nonce is put through a “hash algorithm”, a string of fixed length is created that helps us identify the data.

When miners try to verify this block, they will try to figure out the nonce of the block again and again until they get it right for the above hash.

#### Why blockchain is immutable

If any one block in a blockchain changes any value, its hash will also change (since the hashing algorithm will change it based on data). This breaks the chain, since all the blocks after it will have its previous hash.

![blockchainImage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aux35cqff08g24w88bw1.png)
[Image Source](https://www.google.com/url?sa=i&url=https%3A%2F%2Fmoney.com%2Fwhat-is-blockchain%2F&psig=AOvVaw2NAwPAvebmM-EMdQIUmn5I&ust=1667051542931000&source=images&cd=vfe&ved=0CA4QjhxqFwoTCJDSwoqJg_sCFQAAAAAdAAAAABAE)


Now in a distributed blockchain, each node, containing a copy of the blockchain that acts as a ledger of transactions, is connected with one another(peer-to-peer). Now, if any data is altered in any of the copy of the blockchain, that whole record will become invalid and the node will be thrown out of the network and denied mining rewards. This is why blockchains are “immutable”. This is similar to keeping, say, 30 copies of ledgers(or record of transactions) among your friends. If any one friend tries to be smart and alter any data for his greed, he will be caught as his data does not tally with others.

If you change data in one block, the entire copy of the blockchain gets invalidated. And even if you somehow are able to change the hash of each and every block in the (copy)blockchain, which is an extreme computation heavy task, that node will still be invalid since the hashes will not match with the hashes of block of its peers (nodes). 



## Keys

Keys are a string of characters that is used for verifying a transaction.
There are 2 types of keys:
Private - these are our “passwords” that should be kept secret. This is used for signing or confirming a transaction.
Public key - this key is generated from the private key itself that is used for verifying any transaction. This can be displayed in public. This acts as an "ID" that lets you connect your wallet on different website/dApps.