Basics of blockchain
Notes on basic of blockchain
A chain of blocks that contain information. It’s a distributed ledger (book or collection of accounts in which transactions are recorded) open to anyone
Once data is recorder, it’s very hard to change it.
Each block contains it’s data, a hash (unique fingerprint) and the has of the previous block.
The data depends on the type of blockchain. For example, in bitcoin the data contains information about the sender, receiver and the amount.
If some content of the block changes, the hash also changes. So hashes are good to indentify changes and if some block changes, it’s no longer the same.
The previous hash in each block is what forms the blockchain and makes it so secure. The first block ever is called genesis block because it has no previous hash.
Imagine we have three blocks, each pointing to the previous one. If you temper (chnage) the second block, it’s hash changes and it will affect all the following blocks because the next block no longer has a valid reference to the previous block.
This alone is not enough to mitigate hacking, because computers nowadays are very fast and can calculate new hashes for all the following blocks after the changed one.
So there is another concept called proof-of-work, to mitigate these vulnerabilities. It basically slows down the creation of new blocks.
In bitcoin, it takes 10 minutes to calculate the proof-of-work and add a new block to the chain. So if you temper with a block, it will take a veeery long time to calculate new hashes for the following blocks.
Another security concept blockchains have is their distributed system. Instead of a centralized entity, blockchain is a p2p (peer-to-peer) system, and every participant has a full copy of the blockchain.
When someone creates a new block, this new block is sended to everyone in the chain. It is verified by each blockchain, and if everything is okay, it’s get added to each chain. Therefore, all the nodes in the network creates a consensus. Tempered blocks will be rejected by other nodes, so they won’t create consensus.
To successfully temper a block, you have to change it, change every hash of following blocks redoing the proof-of-work and have control of more than 50% of the chain. Very hard!
Blockchain is always evolving and one nice use is smart contracts. Smart contracts are simply programs stored in the blockchain, and can be used to automatically change coins based on certain conditions.
Smart Contracts
Smart contracts are just like contracts in the real world, but they are like a computer program stored inside a blockchain.
An example would a smart contract that is like a crowdfunding. People will send money to the smart contract and if the funding is reached, the smart contract will automatically send the money to the receivers. Otherwise, it will return the money to the people.
But because smart contracts are inside the blockchain, it inherits immutability and distributed characteristics.
Once a smart contract is created, it cannot be changed. So nobody can temper the contract code.
The distributed characteristics make hard the contract to force an action because other people in the network will spot this attempt and refuse the invalid action.
There are several blockchains that acccept smart contracts and the most famous is Ethereum. In Ethereum you can program smart contracts in the Solidity programming language.
Proof-of-stake vs Proof-of-work
Proof-of-work is used in blockchain for achieving consensus and it makes every node of the network solve a cryptographic puzzle. This puzzle is solved by miners, and the first one to find the solution gets the a reward. This led many people and companies around the world to build miner farmers to do this. This is not sustainable, because it consumes a lot of energy.
Also, this encourages people to join forces in miner pools, when they combine computational power to solve the puzzle and distribute the reward between them. This makes bitcoin more centralized, which is not good.
A new consensus algorithm called proof-of-stake chooses randomly one node of the network to validate the next block. Proof-of-stake doesn’t have miners, it has validators, who mint or forge new blocks.
Validators are not chosen completely random. They have to insert a certain amount of coins into the network as stake, just like a security deposit. The more money you deposit, more chances you have to be chosen to validate a block.
After the validator checks if everything is ookay with the block, it gets added to the blockchain. As a reward, the node receives a fee associated with the transation, which are inside the block.
Validators will lose a part of the stake if they approve fraudulent transactions.
As long as the stake is higher than what the validator gets from the transaction fees, we can trust them. Because if they don’t do their job correctly, they will lose more money than will gain.
If a node stops being a validator, the stake and the transaction fees will be released to him after some time (time to discover if some of your blocks were fraudulent).
So Proof-of-Stake don’t let everyone mine new blocks, so it consumes less energy. It’s also more descentralized because it don’t reward mining pools and computational power, so everyone has a chance to validate.
But is has downsides too. If someone (or a group) buys a majority of stake in the network, it could approve fraudulent transactions. This is called the 51% attack. But it can be highy impracticle because it would be A LOT of money in stakes.
Another potential problem is the selection of the next validator. If the stake amount counts too much on the choice, it will favor rich people, who will be chosen and be even more richer with the rewards.