Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Unit-5

ETHEREUM AND
SMART CONTRACTS
Whatis Ethereum?
Ethereum is a Blockchainnetwork thatintroduced a built-in Turing-
complete programming language that can be used for creating various
decentralized applications(also called Dapps).The Ethereum network is
fueled by its own cryptocurrency called ‘ether’.
Moreon Ethereum
➢The Ethereum network is currently famous for allowing the implementation of smart
contracts. Smart contracts can be thought of as ‘cryptographic bank lockers’ which
contain certain values.
➢These cryptographic lockers can only be unlocked when certain conditions are met.
➢Unlike bitcoin, Ethereum is a network that can be applied to various other sectors.
➢Ethereum is often called Blockchain 2.0 since it proved the potential of blockchain
technology beyond the financial sector.
➢The consensus mechanism used in Ethereum is Proof of Stakes(PoS), which is more
energy efficient when compared to that used in the Bitcoin network, that is,
Proof of Work(PoW). PoS depends on the amount of stake a node holds.
Ethereum Architecture
1. Application layer:
It is divide into 2 application:
⮚Decentrailzed Applications(DApps)
⮚Ethereum-Derived Applications
*Decentralized Applications(DApps)*:
- Applicationsthat run on decentralized networks like Ethereum.
- Examples: DeFi platforms, gaming DApps, social networks.

*Ethereum-Derived Applications*:
- Apps built on or for the Ethereum blockchain, utilizingits
features.
- Examples: Wallets (MetaMask), token sales platforms,
decentralized exchanges (DEXs).
Both contribute to Ethereum's ecosystem by
providing decentralized services and utilities.
2.Contract Layer
contract layer is classified into 2 types:
⮚Smart Contracts
⮚Ethereum Virtual Machine(EVM)

1.Smart Contracts:
- A smart contract is a self-executing computer program that automatically
executes the terms of a contract without the involvement of third parties.
- For example, a smart contract can automatically transfer funds when
certain conditions are met, such as reaching a specific date or receiving a
certain input.
2.Contract Layer
2. Ethereum Virtual Machine (EVM):
- Ethereum Virtual Machine (EVM) is designed as the runtime environment
for smart contracts in Ethereum..
- It executes the code of smart contracts in a secure and deterministic manner.
- This ensures that all nodes on the network reach the same result when
executing the same smart contract.
- The EVM operates using a set of instructions called opcodes, which are
generated from the bytecode of smart contracts.

Together, smart contracts and the EVM form the foundation of the contract
layer in the Ethereum architecture, enabling decentralized execution of
agreements and transactions.
A vending machine is an automated machine that provides items such as snacks, beverages, and lottery tickets to consumers after cash, a
credit card, or other forms of payment are inserted into the machine or otherwise made.
Consensus Layer
It consists of two types:
⮚ Proof of Work(POW)
⮚ Proof of StakePOS)
Proof Of Work:
Maintaining the integrity and security of all transactions in the network is the
ultimate objective behind the proof-of-work consensus mechanism.
Proof-of-stake :
Proof-of-stake is a cryptocurrency consensus mechanism for processing
transactions and creating new blocks in a blockchain. A consensus mechanism is a
method for validating entries into a distributed database and keeping the database
secure. In the case of cryptocurrency, the database is called a blockchain—so the
consensus mechanism secures the blockchain.
4. Protocol Layer
Inthe protocol layer of the Ethereum architecture, several protocols facilitate communication and interaction
within the network:

1. **HTTP (Hypertext Transfer Protocol)**:


- While not exclusive to Ethereum, HTTP is commonly used to interact with Ethereum nodes via APIs.
- It allows clients to send requests to nodes to retrieve information or execute transactions.
- HTTP endpoints provide access to data such as blockchain information, account balances, and transaction
history.

2. **RPC (Remote Procedure Call)**:


- Ethereum nodes expose RPC interfaces that allow clients to interact with them programmatically.
- RPC methods enable tasks such as sending transactions, deploying contracts, and querying blockchain data.
- Developers can use RPC interfaces to build applications or interfaces that interact with the Ethereum network.
3. ETH Protocol:
- This protocol governs the core functionality of the Ethereum network, including transaction processing,
block validation, and consensus mechanisms.
- It defines the rules and procedures that nodes follow to maintain the integrity of the blockchain and execute
transactions.

4. LES (Light Ethereum Subprotocol):


- LES is a protocol designed to facilitate light client synchronization with the Ethereum network.
- It allows devices with limited resources, such as smartphones or IoT devices, to synchronize with the
Ethereum blockchain efficiently.
- LES enables lightweight clients to retrieve block headers, account information, and other relevant data from
Ethereum nodes.

5. Whisper:
- Whisper is a communication protocol that enables decentralized messaging and communication within the
Ethereum network.
- It allows users to send and receive encrypted messages without relying on centralized servers.
- Whisper messages are routed through the network in a peer-to-peer manner, providing privacy and censorship
resistance0
5. Network Layer

Ethereum is a peer-to-peer network with thousands of nodes that must be able to


communicate with one another using standardized protocols. The "networking layer" is the stack
of protocols that allow those nodes to find each other and exchange information. This includes
"gossiping" information (one-to-many communication) over the network as well as swapping
requests and responses between specific nodes (one-to-one communication). Each node must
adhere to specific networking rules to ensure they are sending and receiving the correct
information.

There are two parts to the client software (execution clients and consensus clients),
each with its own distinct networking stack. As well as communicating with other
Ethereum nodes, the execution and consensus clients have to communicate with each other.
This page gives an introductory explanation of the protocols that enable this
communication.
5. Network Layer

Execution clients gossip transactions over the execution-layer peer-to-peer network.


This requires encrypted communication between authenticated peers. When a validator is
selected to propose a block, transactions from the node's local transaction pool will be passed
to consensus clients via a local RPC connection, which will be packaged into Beacon blocks.
Consensus clients will then gossip Beacon blocks across their p2p network. This requires two
separate p2p networks: one connecting execution clients for transaction gossip and one
connecting consensus clients for block gossip.
6.Data Layer
It consistsof:
1.Block
2.Blockchain
3. Transaction pool
4.Merkle Tree
5.Asymmetric Encryprtion Event
In the Ethereum architecture, several components play vital roles in maintaining the network's
integrity and functionality:

1. **Block**:
- Blocks are containers that store transactions and other data on the Ethereum blockchain.
- Each block contains a header and a list of transactions.
- Blocks are linked together in a chain, with each block referencing the previous block through
its header's hash.
- Mining nodes compete to add new blocks to the blockchain by solving cryptographic puzzles.
6.Data Layer
2. **Blockchain**:
- The blockchain is a decentralized ledger that records all transactions and data in a
chronological order.
- It provides transparency, immutability, and security by distributing copies of the ledger across
multiple nodes in the network.
- The Ethereum blockchain is composed of a series of interconnected blocks, forming a
continuous chain of transaction history.

3. **Transaction Pool**:
- The transaction pool (or mempool) is a collection of unconfirmed transactions waiting to be
included in the next block.
- Nodes broadcast transactions to the network, where they are temporarily stored in the
transaction pool.
- Miners select transactions from the pool to include in the blocks they mine based on factors
like transaction fees and gas limits.
3.**Asymmetric Encryption**:
- Asymmetric encryption is a cryptographic technique used in Ethereum for secure communication and
transaction signing.
- Each Ethereum account has a public key (used for encryption and verification) and a private key (used for
decryption and signing).
- Transactions are signed with the sender's private key to prove ownership and ensure authenticity.
- Public keys are used to verify the signature and confirm the transaction's legitimacy.

6. **Event**:
- Events are a mechanism in Ethereum smart contracts used to notify external parties about specific
occurrences within the contract.
- They allow smart contracts to communicate with off-chain applications and users by emitting structured log
events.
- DApps and external services can listen for these events and react accordingly, enabling real-time updates and
interactions with the blockchain.

These components collectively form the backbone of the Ethereum architecture, enabling secure, transparent,
and decentralized execution of transactions and smart contracts on the blockchain.
7. Storage Layer:
In the storage layer of the Ethereum architecture, two important components are
``

Log and LevelDB:

1. **Log**:
- Logs are data structures used by Ethereum nodes to record events emitted by smart
contracts during transaction execution.
- Smart contracts can emit logs using the `LOG` opcode, specifying topics and data
to include in the log.
- Logs are stored in the transaction receipt, which is included in the block alongside
the transaction.
- They are commonly used to provide information about state changes, such as
token transfers, contract interactions, or custom events defined by the smart
contract.
7. Storage Layer:
2.**LevelDB**:
- LevelDB is a key-value storage library developed by Google and used by Ethereum
nodes to store blockchain data efficiently.
- It provides fast read and write access to data and supports atomic operations,
making it suitable for managing blockchain state and metadata.
- Ethereum nodes use LevelDB to store various types of data, including block
headers, transaction receipts, account balances, and contract code.
- LevelDB organizes data into ordered key-value pairs and utilizes memory and disk
storage to maintain a compact and efficient database.

Together, Log and LevelDB play crucial roles in storing and retrieving data
within the Ethereum blockchain, facilitating transaction processing, state
management, and event tracking.
Solidity programming :
Solidity is a brand-new programming language created by Ethereum which is the second-
largest market of cryptocurrency by capitalization, released in the year 2015 and led by
Christian Reitwiessner. Some key features of solidity are listed below:
➢Solidity is a high-level programming language designed for implementing smart contracts.
➢It is a statically typed object-oriented(contract-oriented) language.
➢Solidity is highly influenced by Python, c++, and JavaScript which run on the Ethereum
Virtual Machine(EVM).
➢Solidity supports complex user-defined programming, libraries, and inheritance.
➢Solidity is the primary language for blockchains running platforms.
➢Solidity can be used to create contracts like voting, blind auctions, crowdfunding, multi-
signature wallets, etc.
Solidity programming :
Explanation:
1. Version Pragma:

pragma solidity >=0.4.16 <0.9.0;


Pragmas are instructions to the compiler on how to treat the code. All solidity source code
should start with a “version pragma” which is a declaration of the version of the solidity
compiler this code should use. This helps the code from being incompatible with the future
versions of the compiler which may bring changes. The above-mentioned code states that it is
compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.9.0
2. The contract keyword:
contract Test
{
//Functions and Data
}
The contract keyword declares a contract under which is the code encapsulated.
3.State variables:

uint public
var1; uint
public
var2; uint
public
sum;
State variables are permanently stored in contract storage that they are written in Ethereum
Blockchain. The line uint public var1 declares a state variable called var1 of type uint
(unsigned integer of 256 bits). Think of it as adding a slot in a database. Similarly, goes with
the declaration uint public var2 and uint public sum.
4.A function declaration:

function set(uint x, uint y)


public function get() public
view returns (uint)
This is a function named set of access modifier type public which takes a variable x and variable
y of datatype uint as a parameter.
This was an example of a simple smart contract that updates the value of var1 and var2. Anyone
can call the function set and overwrite the value of var1 and var2 which is stored in the Ethereum
blockchain. This is an example of a decentralized application that is censorship-proof and
unaffected to the shutdown of any centralized server. As long as someone is running a single
node of the Ethereum blockchain, this smart contract will be accessible.
The variable sum is calculated by adding the values of the variables var1 and var2. Function get
will retrieve and print the value of the state variable sum.
sample program:
this is a basic hello world
program:
How to Execute The Code:

There are practically two ways to execute a solidity smart contract:

1. Offline Mode: Running a solidity smart contract in Offline mode requires three prerequisites and 4 major steps to be followed to get the smart
contract running:

Prerequisites:

Download and install node.js.


Install Truffle globally.
Install ganache-cli.

Objectives:
Create a truffle project and configure a development network
Create and deploy smart contracts
Interact with the smart contract from Truffle console Write
tests for testing the main features offered by Solidity.

2. Online Mode: Remix IDE is generally used to compile and run Solidity smart contracts in the Online Mode
online
mode:

You might also like