Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Solidity Programming Essentials. - Second Edition

You're reading from  Solidity Programming Essentials. - Second Edition

Product type Book
Published in Jun 2022
Publisher Packt
ISBN-13 9781803231181
Pages 412 pages
Edition 2nd Edition
Languages
Concepts
Author (1):
Ritesh Modi Ritesh Modi
Profile icon Ritesh Modi

Table of Contents (21) Chapters

Preface 1. Part 1: The Fundamentals of Solidity and Ethereum
2. Chapter 1: An Introduction to Blockchain, Ethereum, and Smart Contracts 3. Chapter 2: Installing Ethereum and Solidity 4. Chapter 3: Introducing Solidity 5. Chapter 4: Global Variables and Functions 6. Chapter 5: Expressions and Control Structures 7. Part 2: Writing Robust Smart Contracts
8. Chapter 6: Writing Smart Contracts 9. Chapter 7: Solidity Functions, Modifiers, and Fallbacks 10. Chapter 8: Exceptions, Events, and Logging 11. Chapter 9: Basics of Truffle and Unit Testing 12. Chapter 10: Debugging Contracts 13. Part 3: Advanced Smart Contracts
14. Chapter 11: Assembly Programming 15. Chapter 12: Upgradable Smart Contracts 16. Chapter 13: Writing Secure Contracts 17. Chapter 14: Writing Token Contracts 18. Chapter 15: Solidity Design Patterns 19. Assessments 20. Other Books You May Enjoy

Consensus

Consensus is one of the most important concepts in the world of blockchain. As we know, a blockchain comprises multiple nodes that are running the same software and storing the same data. There should be some agreement between the nodes to accept a given state as a known agreeable state. With thousands of nodes running together, intertwined using a Peer-to-Peer (P2P) network, they should agree on what is stored historically on the blocks and what should be included as transactions on the new block. It is quite possible that a node goes rogue and adds transactions that are not valid. The consensus mechanism or agreements among nodes would ensure that those transactions are not accepted by other ones. A rogue actor has to minimally control a majority of the nodes to make everyone agree on the dubious transactions.

There are various ways to implement and achieve consensus among nodes. Bitcoin is implemented using the Proof-of-Work (PoW) consensus algorithm. Ethereum also implements the PoW consensus algorithm as part of Ethereum 1.0. The new upcoming Ethereum 2.0 is changing its consensus algorithm to the Proof-of-Stake (PoS) consensus algorithm.

One of the main drawbacks of the PoW consensus algorithm is that is it quite costly to generate and agree on blocks due to its mining process, which will be explained later. Mining involves heavy usage of electricity and that has negative consequences toward environmental hazards and cost. Both PoW and PoS will be explained in the next subsections.

Proof of work

A miner is responsible for writing transactions to the Ethereum chain. A miner's job is very similar to that of an accountant. An accountant is responsible for writing and maintaining a ledger; similarly, a miner is solely responsible for writing a transaction to an Ethereum ledger. A miner is interested in writing transactions to a ledger because of the reward associated with it. Miners get two types of reward – a reward for writing a block to the chain and cumulative gas fees from all transactions in the block. There are generally many miners available within a blockchain network, each trying and competing to write transactions. However, only one miner can write the block to the ledger, and the rest will not be able to write the current block.

The miner responsible for writing the block is determined by way of a puzzle. The challenge is given to every miner, and they try to solve the puzzle using their computing power. The miner who solves the puzzle first writes the block containing transactions to their own ledger and sends the block and nonce value to other miners for verification. Once verified and accepted, the new block is written to all ledgers belonging to miners. In this process, the winning miner also receives Ether as a reward. Every mining node maintains its own instance of the Ethereum ledger, and the ledger is ultimately the same across all miners. It is the miner's job to ensure that their ledger is updated with the latest blocks. The following are the three important functions performed by miners or mining nodes:

  • Mine or create a new block with a transaction and write this to the Ethereum ledger.
  • Advertise and send a newly mined block to other miners.
  • Accept new blocks mined by other miners and keep their own ledger instance up to date.

Mining nodes refer to nodes that belong to miners. These nodes are part of the same network where the EVM is hosted. At some point in time, miners will create a new block, collect all transactions from the transaction pool, and add them to the newly created block. Finally, this block is added to the chain. There are additional concepts such as consensus and the solving of a target puzzle before writing the block, which will be explained in the following section.

Miners are always looking forward to mining new blocks and are also listening actively to receive new blocks from other miners. They are listening for new transactions stored in the transaction pool. Miners also broadcast the new transactions to other connected nodes after validation. A miner collects all transactions available within the transaction pool, subject to constraints such as block size and block gas limits, and creates a block with them. This activity is done by all miners.

The miner constructs a new block and adds all transactions to it. Before adding these transactions, it will check whether any of the transactions are not already written in a block that it might receive from other miners. If so, it will discard those transactions.

The miner will add its own coinbase transaction for getting rewards for mining the block.

The next task for a miner is to generate the block header and perform the following tasks:

  1. The miner takes hashes of two transactions at a time to generate a new hash till they get a single hash from all transactions. The hash is referred to as a root transaction hash or Merkle root transaction hash. This hash is added to the block header.
  2. The miner also identifies the hash of the previous block. The previous block will become a parent to the current block, and its hash will also be added to the block header.
  3. The miner calculates the state and receipts of the transaction root hashes and adds them to the block header.
  4. A nonce and timestamp are also added to the block header.
  5. A block hash consisting of both a block header and body is generated.
  6. The mining process starts where the miner keeps changing the nonce value and tries to find a hash that will satisfy as an answer to the given puzzle. Keep in mind that everything mentioned here is executed by every miner in the network.
  7. Eventually, one of the miners will be able to solve the puzzle and advertise the result to other miners in the network. The other miners will verify the answer and, if found correct, further verify every transaction, accept the block, and append it to their ledger instance.

This entire process is also known as PoW wherein a miner provides proof that they have worked on computing the final answer that is satisfactory as a solution to the puzzle. The header block and its content are shown in the following diagram:

Figure 1.7 – PoW generating a Merkle root and using transactions

Figure 1.7 – PoW generating a Merkle root and using transactions

Let's now discuss the next mechanism.

Proof of stake

Ethereum 2.0 will launch a new consensus mechanism known as PoS. It is another algorithm to reach consensus within a distributed network architecture. While PoW is computationally heavy, PoS does not perform any compute-heavy activities. Under this mechanism, interested stakeholders can stake their Ether (a minimum of 32 ETH) with the network. Once the Ethers are staked, they are locked, and these stakeholders become validators. These validators have their nodes running, and their job is to attest to new blocks for their validity. Attestation here means that the validator is vouching for the correctness of the block and its constituent transactions.

After a minimum number of validators attest, the block is finalized and becomes a permanent part of the chain. The network will at random also choose a validator to create a new block for every epoch. The validators are not competing with each other anymore as in the case of the PoW consensus mechanism. This makes PoS more environmentally friendly by being energy efficient and also anyone with 32 Ethers can potentially become a validator. Now that we understand the different consensus mechanisms, it's time to visit the different types of nodes supported by Ethereum.

You have been reading a chapter from
Solidity Programming Essentials. - Second Edition
Published in: Jun 2022 Publisher: Packt ISBN-13: 9781803231181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}