Book Image

Solidity Programming Essentials

Book Image

Solidity Programming Essentials

Overview of this book

Solidity is a contract-oriented language whose syntax is highly influenced by JavaScript, and is designed to compile code for the Ethereum Virtual Machine. Solidity Programming Essentials will be your guide to understanding Solidity programming to build smart contracts for Ethereum and blockchain from ground-up. We begin with a brief run-through of blockchain, Ethereum, and their most important concepts or components. You will learn how to install all the necessary tools to write, test, and debug Solidity contracts on Ethereum. Then, you will explore the layout of a Solidity source file and work with the different data types. The next set of recipes will help you work with operators, control structures, and data structures while building your smart contracts. We take you through function calls, return types, function modifers, and recipes in object-oriented programming with Solidity. Learn all you can on event logging and exception handling, as well as testing and debugging smart contracts. By the end of this book, you will be able to write, deploy, and test smart contracts in Ethereum. This book will bring forth the essence of writing contracts using Solidity and also help you develop Solidity skills in no time.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Ethereum nodes


Nodes represent the computers that are connected using a peer-to-peer protocol to form an Ethereum network.

There are the following two types of nodes in Ethereum:

  • EVM 
  • Mining nodes

It is to be noted that this distinction is made to clarify concepts of Ethereum. In most scenarios, there is no dedicated EVM. Instead, all nodes act as miners as well as EVM nodes.

EVM

Think of EVM as the execution runtime for an Ethereum network. EVMs are primarily responsible for providing a runtime that can execute code written in smart contracts. It can access accounts, both contract and externally owned, and its own storage data. It does not have access to the overall ledger but does have limited information about the current transaction.

EVMs are the execution components in Ethereum. The purpose of an EVM is to execute the code in a smart contract line by line. However, when a transaction is submitted, the transaction is not executed immediately. Instead it is pooled in a transaction pool. These transactions are not yet written to the Ethereum ledger.

Ethereum mining nodes

A miner is responsible for writing transactions to the Ethereum chain. A miner's job is very similar to that of an accountant. As an accountant is responsible for writing and maintaining the 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 compute power. The miner who solves the puzzle first writes the block containing transactions to his 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 5 Ether as 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. Following are the three important functions performed by miners or mining nodes:

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

Mining nodes refer to the nodes that belong to miners. These nodes are part of the same network where the EVM is hosted. At some point in time, the 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 solving of target puzzle before writing the block that will be explained in the following section.

How does mining work?

The process of mining explained here is applicable to every miner on the network and every miner keeps executing the tasks mentioned here regularly.

Miners are always looking forward to mining new blocks, and are also listening actively to receive new blocks from other miners. They are also listening for new transactions to store in the transaction pool. Miners also spread the incoming transactions to other connected nodes after validation. As mentioned before, at some point, the miner collects all transactions from the transaction pool. 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 if 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 their 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 he gets 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 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 transaction root hashes and adds them to the block header.
  4. A nonce and timestamp is also added to the block header.
  5. A block hash consisting of both 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. It is to be kept 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 same to other miners in the network. The other miners will verify the answer and, if found correct, will further verify every transaction, accept the block, and append the same to their ledger instance.

This entire process is also known as Proof of Work (PoW) wherein a miner provides proof that it is has worked on computing the final answer that could satisfy as solution to the puzzle. There are other algorithms such as Proof of Stake (PoS) and Proof of Authority (PoA), but they are not used or discussed in this book.

The header block and its content is shown in the following diagram: