Professional Documents
Culture Documents
BCT - Unit5
BCT - Unit5
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:
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
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
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
``
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:
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:
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:
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: