Consensus
Every node in the Ethereum network holds a copy of the blockchain. We need to make sure that nodes cannot tamper with the blockchain, and we also need a mechanism to check whether a block is valid or not. And also, if we encounter two different valid blockchains, we need to have a way to find out which one to choose.
Ethereum uses the proof-of-work consensus protocol to keep the blockchain tamper-proof. A proof-of-work system involves solving a complex puzzle to create a new block. Solving the puzzle should require a significant amount of computational power thereby making it difficult to create blocks. The process of creating blocks in the proof-of-work system is called mining. Miners are the nodes in the network that mine blocks. All the DApps that use proof-of-work do not implement exactly the same set of algorithms. They may differ in terms of what the puzzle miners need to solve, how difficult the puzzle is, how much time it takes to solve it, and so on. We will learn about proof-of-work with respect to Ethereum.
Anyone can become a miner in the network. Every miner solves the puzzle individually; the first miner to solve the puzzle is the winner and is rewarded with five ether and transaction fees of all the transactions in that block. If you have a more powerful processor than any other node in the network, that doesn't mean that you will always succeed because the parameters for the puzzle are not exactly same for all the miners. But instead, if you have a more powerful processor than any other node in the network, it gives you a higher chance at succeeding. Proof-of-work behaves like a lottery system, and processing power can be thought as the number of lottery tickets a person has. Networks security is not measured by total number of miners; instead, it's measured by the total processing power of the network.
There is no limit to the number of blocks the blockchain can have, and there is no limit to the total ether that can be produced. Once a miner successfully mines a block, it broadcasts the block to all other nodes in the network. A block has a header and a set of transactions. Every block holds hash of the previous block, thereby creating a connected chain.
Let's see what the puzzle the miners need to solve is and how it's solved at a high level. To mine a block, first of all, a miner collects the new un-mined transactions broadcasted to it, and then it filters out the not-valid transactions. A transaction to be valid must be properly signed using the private key, the account must have enough balance to make the transaction, and so on. Now the miner creates a block, which has a header and content. Content is the list of transactions that the block contains. The header contains things such as the hash of the previous block, block number, nonce, target, timestamp, difficulty, address of the miner, and so on. The timestamp represents the time at the block's inception. Then nonce is a meaningless value, which is adjusted in order to find the solution to the puzzle. The puzzle is basically to find such nonce values with which when the block is hashed, the hash is less than or equal to the target. Ethereum uses ethash hashing algorithm. The only way to find the nonce is to enumerate all possibilities. The target is a 256-bit number, which is calculated based on various factors. The difficulty value in the header is a different representation of the target to make it easier to deal with. The lower the target, the more time it takes to find the nonce, and the higher the target, the less time it takes to find the nonce. Here is the formula to calculate the difficulty of the puzzle:
current_block_difficulty = previous_block_difficulty + previous_block_difficulty // 2048 * max(1 - (current_block_timestamp - previous_blocktimestamp) // 10, -99) + int(2 ** ((current_block_number // 100000) - 2))
Now any node in the network can check whether the blockchain they have is valid or not by first checking whether the transactions in the blockchain are valid, the timestamp validation, then whether the target and nonce of all the blocks are valid, a miner has assigned a valid reward itself, and so on.
Now, for example, if a node in the network alters some transactions in a block, then the node needs to calculate the nonce of all the succeeding blocks. By the time it re-finds the nonce of the succeeding blocks, the network would have mined many more blocks and therefore reject this blockchain as its combined difficulty would be lower.