Week 4
Week 4
Smart Contracts
Week – 4
Pritom Rajkhowa
Consensus Model
Consen Model
sus
Consen
sus
Consen
sus
Valid
Work
51% Computing
More work you put work in, More chances power of network
to win reword.
You need billions or trillions of dollars
Miner 1
Finish Line
Miner 2
Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Continually races,
Back to Back
Miner 3 Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Continually races,
Back to Back
Miner 3 Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Miner 3
Proof of work
Miner 1
Finish Line
Miner 2
Miner 3
Proof of work
Miner 1
Miner 2
Finish Line
Miner 2
Miner 3
Miner 3 Miner 4
Validator 2
Finish Line
Validator 3
Validator 4
Proof of stake
Validator 1
Validator 2
Finish Line
Validator 3
Proof of stake
Validator 1
Validator 2
Finish Line
Validator 3
Validator 4
Proof of stake
Validator 1
Validator 2
Finish Line
Validator 3
Validator 4
Proof of stake
Finish Line
Proof of stake
Finish Line
Proof of stake
Finish Line
Proof of stake
Finish Line
Proof of stake
Finish Line
Proof of stake
Finish Line
Proof of stake
Miner 1
Miner 1
Miner 2
Finish Line
Finish Line
Miner 2
Miner 3
Miner 3 Miner 4
Proof of stake
Proof of work
How do they pick?
Validator
How do they pick?
Validator
Finish Line
How long is the Race?
Finish Line
Proof of Work
Finish Line
Proof of Stake
How long is the Race?
Free coin
Stake
Fact Check
Fake Transaction
Validator
will lose part Report it
of its stake
Proof of Stake
Transition
Network Grows
Network Grows
51% Attack
If it is too late
How It Works: Validators are chosen to create new blocks based on the number of
coins they hold and are willing to “stake” as collateral.
Pros:
Energy-efficient compared to PoW.
Reduces the risk of centralization.
Cons:
Wealth concentration can lead to centralization.
"Nothing at stake" problem, where validators can vote on multiple chains.
Delegated Proof of Stake (DPoS)
How It Works: Coin holders vote for a small number of delegates who validate
transactions and create blocks on their behalf.
Pros:
• Fast transaction times due to fewer validators.
• Democratic voting process.
Cons:
• Risk of centralization as a few delegates hold significant power.
• Voter apathy can lead to unrepresentative governance.
Proof of Authority (PoA)
How It Works: A limited number of trusted nodes are authorized to create new
blocks and validate transactions, based on their identity and reputation.
Pros:
• High throughput and low latency.
• Simple and efficient for private networks.
Cons:
• Centralization risk as it relies on a few trusted authorities.
• Less secure as it can be vulnerable to collusion.
Practical Byzantine Fault
Tolerance (PBFT)
How It Works: Nodes communicate with each other to reach a consensus, even
when some nodes may fail or act maliciously. A transaction is accepted once a
supermajority (typically 2/3) agrees.
Pros:
• High fault tolerance and security against malicious nodes.
• Fast transaction finality.
Cons:
• Scalability issues with a large number of nodes.
• Communication overhead can slow down the process.
Proof of Space and Time (PoST)
How It Works: Participants prove that they have allocated a certain amount of disk
space and time to secure the network. The more space they allocate, the higher the
chances of being selected to create a block.
Pros:
• More energy-efficient than PoW.
• Utilizes underused disk space.
Cons:
• Requires significant storage resources.
• Potential for centralization around those with more storage capacity.
Proof of Burn (PoB)
How It Works: Participants wait for a randomly chosen time period before they can
create a new block. The winner is the one whose wait time expires first.
Pros:
• Energy-efficient and simple.
• Reduces the risk of centralization.
Cons:
• Requires a trusted execution environment to ensure randomness.
• Vulnerable to attacks if the environment is compromised.
Hybrid Proof of Work/Proof of
Stake (PoW/PoS)
How It Works: Combines both PoW and PoS mechanisms, allowing miners to
contribute computational power while validators stake their coins for added
security.
Pros:
• Balances security and efficiency.
• Reduces energy consumption compared to pure PoW.
Cons:
• Complexity in implementation.
• Can inherit weaknesses from both mechanisms.
Federated Byzantine Agreement
(FBA)
How It Works: A group of trusted nodes (validators) reach consensus without
requiring all nodes to communicate with each other. Each node can choose which
validators to trust.
Pros:
• Fast consensus with low latency.
• Flexible and scalable.
Cons:
• Trust relies on the chosen validators, risking centralization.
• Less secure than some fully decentralized mechanisms.
Proof of Responsibility (PoR)
How It Works: Validators are chosen based on their reputation and performance in
maintaining the network. They must hold a certain amount of the network's
tokens.
Pros:
• Encourages responsible behavior among validators.
• Reduces the risk of malicious actions.
Cons:
• Requires a robust reputation system.
• Potential for collusion among validators.
Proof of Stake Velocity (PoSV)
How It Works: This mechanism rewards users not only for staking their coins but
also for the speed at which they are transacted, combining elements of PoS and
transaction activity.
Pros:
• Encourages active participation in the network.
• Balances rewards between holding and usage.
Cons:
• Complexity in calculation can lead to confusion.
• Risk of gaming the system through rapid transactions.
How Blockchain works
User Requested
Transaction is Transaction will be validated
User Once the transaction is verified by Transaction Is
Broadcasted to by any node in this Network
Requests A multiple Nodes in the Blockchain completed
Blockchain through some algorithms and
transaction peer-to-peer network then new block gets added to
smart contract chain and replicated across all the
network
nodes
Ethereum Blockchain framework
Ethereum is a decentralized platform that runs smart
contracts: applications that run exactly as programmed
without any possibility of downtime, censorship, fraud or
third-party interference.
Smart Contract
Agreements are written as Smart Contracts.
Financial derivatives
Insurance premiums
Breach contracts
Property law
Credit enforcement
Financial services
Legal processes
Crowdfunding agreements
And more
Analyze Blockchain data with
SQL and Dune Analytics
What is Dune Analytics?
Simply put, Dune makes it simple for crypto users to turn blockchain
data into insights, charts, and analytics.
https://benny.hashnode.dev/analyze-blockchain-and-defi-data-in-minutes-with-sql-and-dune-analytics-a-quickstart-guide
How does Dune Analytics work?
Objective:
66
STRUCTURED vs. OO PROGRAMMING
STRUCTURED PROGRAMMING:
MAIN PROGRAM GLOBAL DATA
FUNCTION 4 FUNCTION 5
67
Structured Programming
• Using function
• Function & program is divided into modules
• Every module has its own data and function which
can be called by other modules.
68
OBJECT ORIENTED PROGRAMMING
Object 2
Object 1
Data Data
Function Function
Object 3
Data
Function
69
OBJECT ORIENTED PROGRAMMING
70
Basic terminology
object
- usually a person, place or thing (a noun)
method
- an action performed by an object (a verb)
attribute
- description of objects in a class
class
- a category of similar objects (such as automobiles)
- does not hold any values of the object’s attributes
71
Example for attributes and methods
Attributes: Methods:
• manufacturer’s name • Define data items
• model name (specify manufacturer’s
• year made name, model, year,
• color etc.)
• number of doors • Change a data item
(color, engine, etc.)
• size of engine
• Display data items
• etc.
• Calculate cost
• etc.
72
Why OOP?
73
Design Principles of OOP
Encapsulation
Abstraction
Polymorphism
Inheritance
74
Encapsulation
Analogy:
ATM machine can only update accounts of one person
or object only.
75
Abstraction
• Focus only on the important facts about the
problem at hand
• to design, produce, and describe so that it can be
easily used without knowing the details of how it
works.
Analogy:
• When you drive a car, you don’t have to know
how the gasoline and air are mixed and ignited.
• Instead you only have to know how to use the
controls.
• Draw map
76
Polymorphism
Analogy:
In English, bank can mean side of a river or a place to
put money
move -
77
Function Overloading
78
Inheritance
Superclass
Vehicle
Subclasses
80
Object-Oriented Programming Languages
· Pure OO Languages
Smalltalk, Eiffel, Actor, Java
· Hybrid OO Languages
C++, Objective-C, Object-Pascal
81
Enum
Struct types are used to represent a record. Suppose you want to keep track of yourbooks in a library.
You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
Defining a Struct
To define a Struct, you must use the struct keyword. The struct keyword
defines a new data type, with more than one member. The format of
the struct statement is as follows −
Structure Example
Data Locations - Storage, Memory
and Calldata
Modifiers are code that can be run before and / or after a function call.
Modifiers can be used to:
• Restrict access
• Validate inputs
• Guard against reentrancy hack
Events
Events allow logging to the Ethereum blockchain. Some use cases for
events are:
• Listening for events and updating user interface
• A cheap form of storage
Constructor
Functions and state variables have to declare whether they are accessible by other contracts.
Functions can be declared as
• public - any contract and account can call
• private - only inside the contract that defines the function
• internal- only inside contract that inherits an internal function
• external - only other contracts and accounts can call
State variables can be declared as public, private, or internal but not external.
Function Visibility Specifier in Solidity
Function in Solidity is a set of code that performs a specific task. The function
provides reusability of code in smart contracts. In Solidity, functions are defined
using the function keyword. The syntax of the function definition in Solidity is:
Function visibility in Solidity defines the visibility of the function to other functions
within the contract, or in other contracts. Function visibility helps to identify where in
the contract or in other contracts, functions can be used. It is specified by the developer
of the smart contract. The default variable visibility in Solidity is internal. The default
function visibility in Solidity is public.
How do Function Visibility Modifiers and Inheritance Work
Together?
Calling Parent Contracts
Functions and addresses declared payable can receive ether into the
contract.
Sending Ether (transfer, send, call)
When a function is called, the first 4 bytes of calldata specifies which function to call.
This 4 bytes is called a function selector.
Take for example, this code below. It uses call to execute transfer on a contract at the
address addr.