BT - Unit 3
BT - Unit 3
OF IT/SMVEC
ETHEREUM:
Ethereum is a platform powered by blockchain technology that is best known for its native
cryptocurrency, called ether, or ETH, or simply ethereum. The distributed nature of blockchain
technology is what makes theEthereum platform secure, and that security enables ETH to accrue
value.
Ethereum, just like any other blockchain, can be visualized as a transaction-based state machine. The
idea is that a genesis state is transformed into a final state by executing transactions incrementally.
The final transformation is then accepted as the absolute undisputed version of the state. In the
following diagram, the Ethereum state transition function is shown, where a transaction execution
has resulted in a state transition.
EVM also supports exception handling in case exceptions occur, such as not having enough gas or
invalid instructions, in which case the machine would immediately halt and return the error to the
executing agent. EVM is a fully isolated and sandboxed runtime environment. EVM is a stack-
based architecture. EVM is big-endian by design and it uses 256-bit wide words. This word size
allows for Keccak 256-bit hash and elliptic curve cryptography computations.
EVM also supports exception handling in case exceptions occur, such as not having enough gas or
invalid instructions, in which case the machine would immediately halt and return the error to the
executing agent.EVM is a fully isolated and sandboxed runtime environment.
As discussed earlier, EVM is a stack-based architecture. EVM is big-endian by design and it uses
256-bit wide words. This word size allows for Keccak 256-bit hash and elliptic curve cryptography
computations.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
The following diagram shows the design of the EVM where the virtual ROM stores the program code
that is copied into main memory using CODECOPY. The main memory is then read by the EVM by
referring to the program counter and executes instructions
EVM operation
EVM optimization is an active area of research and recent research has suggested that EVM can
be optimizedand tuned to a very fine degree in order toachieve high performance. Research into the
possibility of using Web assembly (WASM) is underway already. WASM is developed by
Google, Mozilla, and Microsoft and is now being designed as an open standard by the W3C
community group. The aim of WASM is to be able to run machine code in the browser that will
result in execution at native speed. Similarly, the aim of EVM 2.0 is tobe able to run the EVM
instruction set (Opcodes) natively in CPUs, thus making it faster and efficient.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
PRE-COMPILED CONTRACTS:
There are four precompiled contracts in Ethereum. Hereis the list of these contracts and details.
The elliptic curve public key recovery function
It takes four inputs: H, which is a 32 byte hash of the message to be signed and V, R, and S,
which representthe ECDSA signature with the recovery ID and produce a64 byte public key. V,
R, and S have been discussedin detail previously in this chapter.
The identity function: The identity function is available at address 4 and is denoted by the ID. It
simply defines output as input; in other words, whatever input is given to the ID function, it will output
the same value. Gas requirement is calculated by a simple formula: 15 + 3 [Id/32] where Id is the
input data. This means that at a high level, the gas requirement is dependent on the size of the input
data albeit with some calculation performed, as shown in the preceding equation.All the previously
mentioned precompiled contracts canbecome native extensions and can be included in the EVM
opcodes in the future.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
Accounts are one of the main building blocks of the Ethereum blockchain. The state is created or
updated asaresult of the interaction between accounts. Operations performed between and on the
accounts represent state transitions. State transition is achieved using what's called the Ethereum
state transition function, which works as follows:
1. Confirm the transaction validity by checking the syntax, signature validity, and nonce.
2. Transaction fee is calculated and the sending address is resolved using the signature.
Furthermore, sender's account balance is checked and subtracted accordingly and nonce
is incremented. Anerror isreturned if the account balance is not enough.
3. Provide enough ether (gas price) to cover the cost of the transaction. This is charged per byte
incrementally according to the size of the transaction.
4. In this step, the actual transfer of value occurs. The flow is from the sender's account to
receiver's account. The account is created automatically if the destination account specified in
the transaction does not exist yet. if the destination account is a contract, then the contract
code is executed. If enough gas is available, then the contract code will be executed fully;
otherwise, it will run up to the point where it runs out of gas.
5. In cases of transaction failure due to insufficient account balance or gas, all state changes are
rolled back with the exception of fee payment, which is paid to the miners.
6. Finally, the remainder (if any) of the fee is sent back to the sender as change and fee is
paid to the miners accordingly. At this point, the function returns the resulting state.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
The first is externally owned accounts (EOAs) and the other is contract accounts. EOAs are
similar to accounts that are controlled by a private key in bitcoin. Contract accounts are the
accounts that have code associated with them along with the private key. An EOA has ether
balance, is able to send transactions, andhas no associated code, whereas a Contract Account
(CA) has ether balance, associated code, and the ability to get triggered and execute code in
response to a transaction or a message that due to the Turing- completeness property of the
Ethereum blockchain, the code within contract accounts can be of any level of complexity. The
code is executed by EVM by each mining node on the Ethereum network. In addition, contract
accounts are able to maintain their own permanent state and can call other contracts. It is
envisagedthat in the serenity release, the distinction between externally owned accounts and
contract accounts may beeliminated.
Block:
As discussed earlier, blocks are the main building blocks of a blockchain. Ethereum blocks consist
of variouscomponents, which are described as follows:
• The block header
• The transactions list
• The list of headers of ommers or uncles
The transaction list is simply a list of all transactions included in the block. In addition, the list of
headers ofUncles is also included in the block. The most important and complex part is the block
header.
BLOCK HEADER
Block headers are the most critical and detailed components of an Ethereum block. The header
containsvaluable information, which is described in detail here.
PARENT HASH
This is the Keccak 256-bit hash of the parent (previous)block's header.
OMMERS HASH
This is the Keccak 256-bit hash of the list of Ommers (Uncles) blocks included in the block.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
BENEFICIARY
Beneficiary field contains the 160-bit address of the recipient that will receive the mining reward
once the block is successfully mined.
STATE ROOT
The state root field contains the Keccak 256-bit hash ofthe root node of the state trie. It is
calculated after alltransactions have been processed and finalized.
TRANSACTIONS ROOT
The transaction root is the Keccak 256-bit hash of the root node of the transaction trie.
Transaction trie represents the list of transactions included in the block.
RECEIPTS ROOT
The receipts root is the keccak 256 bit hash of the root node of the transaction receipt trie. This
trie is composed of receipts of all transactions included in the block.
Transaction receipts are generated after each transaction is processed and contain useful post-
transaction information. More details on transaction receipts.
LOGS BLOOM
The logs bloom is a bloom filter that is composed of the logger address and log topics from the log
entry of each transaction receipt of the included transaction list in the block. Logging is explained in
detail in the next section.
DIFFICULTY
The difficulty level of the current block.
NUMBER
The total number of all previous blocks; the genesis block is block zero.
GAS LIMIT: The field contains the value that represents the limit set on the gas consumption per
block.
GAS USED: The field contains the total gas consumed by the transactions included in the block.
TIMESTAMP: Timestamp is the epoch UNIX time of the time of block initialization.
EXTRA DATA: Extra data field can be used to store arbitrary data related to the block.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
MIXHASH:
Mixhash field contains a 256-bit hash that once combined with the nonce is used to prove that
adequatecomputational effort has been spent in order to create this block.
NONCE
Nonce is a 64-bit hash (a number) that is used to prove, in combination with the mixhash field, that
adequatecomputational effort has been spent in order to create this block.
The following figure shows the detailed structure of the block and block header:
ETHER:
Ether is minted by miners as a currency reward for the computational effort they spend in order to
secure the network by verifying and with validation transactions and blocks. Ether is used within the
Ethereum blockchain to pay for the execution of contracts on the EVM. Ether is used to purchase gas
as crypto fuel, which is required in order to perform computation on the Ethereum blockchain.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
Fees are charged for each computation performed bythe EVM on the blockchain.
Gas
Gas is required to be paid for every operation performed on the ethereum blockchain. This is a
mechanism that ensures that infinite loops cannot cause the whole blockchain to stall due to the
Turing-complete nature of the EVM.A fee is paid for transactions to be included by miners for
mining. If this fee is too low, the transaction may never be picked up; the more the fee,the higher
are the chances that the transactions will bepicked upby the miners for inclusion in the block.
Conversely, if the transaction that has an appropriate feepaid is included in the block by miners but
has too many complex operations to perform, it can result in an out-of- gas exception if the gas cost
is not enough. In this case, the transaction will fail but will still be made part of the block and the
transaction originator will not get any refund.
Transaction cost can be estimated using the following formula:
Each operation costs some gas; a high level fee schedule of a few operations is shown as an example
here:
Based on the preceding fee schedule and the formuladiscussed earlier, an example calculation of
the SHA3operation can be calculated as follows:
• SHA3 costs 30 gas
• Current gas price is 25 GWei, which is 0.000000025
• EtherMultiplying both: 0.000000025 * 30 = 0.00000075
Ether In total, 0.00000075 Ether is the total gas that will becharged.
Fee schedule
Gas is charged in three scenarios as a prerequisite tothe execution of an operation:
• The computation of an operation
• For contract creation or message call
• Increase in the usage of memory
A list of instructions and various operations with the gasvalues has been provided.
MESSAGES:
• Messages, as defined in the yellow paper, are the data and value that are passed between
two accounts. A message is a data packet passed between two accounts. This data packet
contains data and value (amount of ether).
• Contracts can send messages to other contracts. Messages only exist in the execution
environment and are never stored. Messages are similar to transactions; however, the main
difference is that they are produced by the contracts, whereas transactions are produced by
entities external to the Ethereum environment.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
Messages are generated when CALLor DELEGATECALLOpcodes are executed by the contracts.
As the name suggests, this is the private network that can be created by generating a new genesis
block. Thisis usually the case in distributed ledger networks, whereas private group of entities start
their own blockchain supporting protocols:
There are various supporting protocols that are in development in order to support
the complete decentralized ecosystem. This includes whisper and Swarm protocol.
Whisper
In essence,whisper is a communication protocol that nodes use inorder to communicate with each
other.
SWARM
Swarm is being developed as a distributed file storage platform. It is a decentralized, distributed, and
peer-to-peer storage network. Files in this network are addressed by the hash of their content.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF
IT/SMVEC
PROGRAMMING LANGUAGES:
Ethereum is a decentralized blockchain platform that enables developers to build decentralized
applications (dApps) using smart contracts. Smart contracts are self-executing contracts that are
programmed to automatically execute when certain conditions are met.
The primary programming language used for creating smart contracts on the Ethereum blockchain
is Solidity. Solidity is a contract-oriented, high-level programming language that is influenced by
C++, Python, and JavaScript. It is specifically designed to write smart contracts that run on the
Ethereum Virtual Machine (EVM).
Apart from Solidity, there are several other programming languages that can be used to write smart
contracts on the Ethereum blockchain. Some of these include:
Vyper: Vyper is a relatively new programming language for smart contracts on Ethereum. It is a
contract-oriented, Python-inspired language that is designed to be more secure and easier to audit
than Solidity.
Serpent: Serpent is an older programming language that was initially used for Ethereum smart
contracts before Solidity became more popular. It is a high-level language that is similar to Python.
LLL: LLL stands for Low-Level Lisp-like Language. As the name suggests, it is a low-level
programming language that is similar to Lisp. It is primarily used for developing low-level
components of smart contracts.
Overall, Solidity remains the most popular and widely used programming language for creating
smart contracts on the Ethereum blockchain, but there are other options available depending on the
developer's preferences and requirements.
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
Ethereum client: An Ethereum client is a software program that allows you to connect to the
Ethereum network and interact with it. The most popular Ethereum clients are Geth and Parity.
Integrated Development Environment (IDE): An IDE is a software application that provides a suite
of tools for coding, testing, and debugging smart contracts. Some popular IDEs for Ethereum
development include Remix, Truffle, and Visual Studio Code with the Solidity extension.
Solidity compiler: The Solidity compiler is a program that compiles Solidity code into bytecode that
can be executed on the Ethereum Virtual Machine (EVM). The compiler is included in most IDEs,
but you can also use it as a standalone command-line tool.
Testnet: A testnet is a separate blockchain network that is used for testing smart contracts before
deploying them on the mainnet. The most popular testnet for Ethereum development is the Rinkeby
testnet.
Ethereum wallet: An Ethereum wallet is a software program that allows you to send, receive, and
store Ethereum and other cryptocurrencies. It is used to deploy and interact with smart contracts on
the Ethereum network. Some popular Ethereum wallets include MetaMask and MyEtherWallet.
Documentation: Ethereum has extensive documentation and resources available on its official
website, including the Solidity documentation, Ethereum Improvement Proposals (EIPs), and the
Ethereum Whitepaper.
Truffle: Truffle is a development framework that provides a suite of tools for building, testing, and
deploying Ethereum dApps. It includes a smart contract development environment, a testing
framework, and a deployment pipeline.
Remix: Remix is an online IDE for developing and testing smart contracts written in Solidity. It
provides a web-based interface that allows you to write, test, and debug your smart contracts directly
in your browser.
Hardhat: Hardhat is a development environment and testing framework for Ethereum dApps. It
includes a smart contract development environment, a testing framework, and a deployment pipeline.
It is designed to be flexible and customizable, allowing you to easily integrate it with other
development tools.
Embark: Embark is a framework for developing and deploying decentralized applications on the
Ethereum blockchain. It includes a smart contract development environment, a web development
framework, and a deployment pipeline.
Brownie: Brownie is a development framework for Ethereum dApps that focuses on simplicity and
ease of use. It includes a smart contract development environment, a testing framework, and a
deployment pipeline.
OpenZeppelin: OpenZeppelin is a library of reusable smart contracts and tools for building Ethereum
dApps. It includes a range of smart contract templates for common use cases, such as ERC20 and
ERC721 tokens, as well as a suite of tools for testing and deploying smart contracts.
Ganache: Ganache is a personal blockchain for Ethereum development that allows you to test and
debug your smart contracts in a local environment. It includes a range of features for testing and
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
The Compound Protocol is a suite of Ethereum smart contracts that enable software developers to
interface with cryptocurrency money markets. In order to supply or borrow assets from the protocol,
you need to write to the Ethereum blockchain.
Ethereum is a public network of decentralized nodes that process transactions and append them to an
ever-growing ledger, which is known as the blockchain. Developers can write software called smart
contracts that are hosted on the Ethereum network. Anyone with access to Ethereum can invoke write
and read operations to and from the blockchain.
In order to create a program for Ethereum and interact with Compound’s smart contracts, you need
to have an access point to the network, some knowledge of smart contract architecture, and also some
knowledge of JSON RPC.
Solidity - The most popular programming language for writing smart contracts on Ethereum.
Web3.js - A JavaScript library for web browsers and Node.js that enables developers to elegantly
read and write to the Ethereum blockchain. Smart contracts written in Solidity can be executed using
JSON RPC from Web3.js.
Infura.io - A company that provides an API for simple Ethereum network access through HTTP and
WebSockets. To access the network without a service like Infura’s API, you must host your own
Ethereum network node.
Ethereum Networks - The Ethereum Main Network (a.k.a Homestead) is considered to be the
production environment. This is where real Ether (ETH) can be exchanged, burned, and mined. Also,
BLOCKCHAIN TECHNOLOGY – U20ITT616 DEPT. OF IT/SMVEC
there are popular public test networks: Ropsten, Göerli, Kovan, and Rinkeby. All of these networks
can be accessed via Infura’s API.
Cloudflare’s Ethereum Gateway - Cloudflare, a popular DNS and general web-infrastructure
company provides a free API for accessing the Ethereum Main Net. Unlike Infura, Cloudflare’s
gateway can be used without an account or API. The network can be interfaced with at
https://cloudflare-eth.com. However, you can interface with only Homestead; not Ropsten, Göerli,
Kovan, or Rinkeby.
Truffle - A company that creates Ethereum development tools like Truffle, Ganache, and Drizzle. In
this guide, we’ll be installing Ganache CLI so we can run our Solidity and Web3.js code locally
during development.
Ganache CLI - A command line interface for running a locally hosted instance of Ethereum. We can
start up a blank Ethereum blockchain, or a fork of a public network’s blockchain on your local
machine.
Solc - The Solidity compiler. This turns our human-readable Solidity code into Ethereum bytecode,
which Ethereum network nodes understand how to execute.
MetaMask - A web browser extension that facilitates invocation of Ethereum smart contracts from a
web page. MetaMask uses Web3.js under the hood. It is a tool for end users to use ETH, Ethereum
based cryptocurrency tokens, and keep track of their Ethereum wallets when using decentralized,
Web3 applications (a.k.a. DApps).