Blockchain
Presented by:
Sathya Peri
Associate Professor
CSE Department,
Indian Institute of Technology
Hyderabad, India
1 of 39
Outline of the Talk
Introduction to Blockchain
Ethereum
Hands-on Ethereum
2 of 39
Introduction
• Blockchain
2 of 39
Introduction
Why Blockchain?
Centralized vs Distributed Platformsa
• Centralized Platforms:
◦ Information going through
a single point.
◦ Lack of transparency.
◦ Needs to pay transaction fees.
• Blockchain Technology:
◦ Decentralized Distributed System.
◦ No centralized control.
◦ Stores information on
a peer-to-peer network.
Clearing
House 
Centralized Ledger
Distributed Ledger
a
Source: https://www.rathbones.com/knowledge-and-insight/blockchain-internet-value
3 of 39
Introduction
Blockchain
• Blockchain is a decentralized, distributed database or ledger of
records.
b
https://bitcoin.org/en/
c
https://www.ethereum.org/
d
https://www.hyperledger.org/
4 of 39
Introduction
Blockchain
• Blockchain is a decentralized, distributed database or ledger of
records.
• It maintains information in the form of blocks.
b
https://bitcoin.org/en/
c
https://www.ethereum.org/
d
https://www.hyperledger.org/
4 of 39
Introduction
Blockchain
• Blockchain is a decentralized, distributed database or ledger of
records.
• It maintains information in the form of blocks.
• Each block is a collection of transactions and has a pointer to its
previous block.
b
https://bitcoin.org/en/
c
https://www.ethereum.org/
d
https://www.hyperledger.org/
4 of 39
Introduction
Blockchain
• Blockchain is a decentralized, distributed database or ledger of
records.
• It maintains information in the form of blocks.
• Each block is a collection of transactions and has a pointer to its
previous block.
• By design these blocks are immutable but publicly readable.
b
https://bitcoin.org/en/
c
https://www.ethereum.org/
d
https://www.hyperledger.org/
4 of 39
Introduction
Blockchain
• Blockchain is a decentralized, distributed database or ledger of
records.
• It maintains information in the form of blocks.
• Each block is a collection of transactions and has a pointer to its
previous block.
• By design these blocks are immutable but publicly readable.
• Example: Bitcoinb, Ethereumc, and Hyperledgerd etc.
b
https://bitcoin.org/en/
c
https://www.ethereum.org/
d
https://www.hyperledger.org/
4 of 39
Introduction
Transaction Flow in Blockchain
A	wants	to
send	money
to	B.
A
The	transaction	is
broadcasted	to	every
peer	in	the	network.
21
? ?
?
??
?
3
Miners	execute	the
transactions	and
form	a	block.	Then
block	is
broadcasted	to
every	peer	in	the
network.
4
Validator	validate
the	transactions	in
the	block.
The	block	then	can	be	added
to	the	chain,	which	provides
an	indelible	and	transparent
record	of	transactions.
5
The	money	moves
from	A	to	B.
B
6
Figure: High-level Overview of Transaction Flow in Blockchain
5 of 39
Introduction
Blockchain Applications
Figure: Blockchain applicationse
e
https://medium.com/practical-blockchain/use-cases-of-blockchain-tech-application-13a940edf6fb
6 of 39
Introduction
Blockchain Classification
Three Different types of Blockchain depending on their applications.
• Public Blockchain
• Private Blockchain
7 of 39
Introduction
Blockchain Classification
Three Different types of Blockchain depending on their applications.
• Public Blockchain
• Private Blockchain
Properties Public Private
Consensus
Participants
All nodes (permission less) Organization (permissioned node)
Access
Permission
Public Restricted
Efficiency Low High
Data
Security
Very High Data Breach Possible
Example Bitcoin, Ethereum Hyperledger Fabric
7 of 39
Ethereum
7 of 39
Ethereum
High Level Design
• Ethereum nodes form a peer-to-peer system.
8 of 39
Ethereum
High Level Design
• Ethereum nodes form a peer-to-peer system.
• Clients (external to the system) wishing to execute smart
contracts, contact a peer of the system.
8 of 39
Ethereum
High Level Design
• Ethereum nodes form a peer-to-peer system.
• Clients (external to the system) wishing to execute smart
contracts, contact a peer of the system.
Peer1
Peer2
Peer3
Peer4
Client1
Client2
Client3
T1
T2
T3
B1 B2 B3
B1 B2 B3
B1 B2 B3
B1 B2 B3
Figure: Clients send Transaction T1, T2 and T3 to Miner (Peer4)
8 of 39
Ethereum
High Level Design
Peer4 T1 T2 T3 FS
Hash of the
Previous
Block
B1 B2 B3 B4
Figure: Miner forms a block B4 and compute final state (FS)
8 of 39
Ethereum
High Level Design
Peer1
Peer2
Peer3
Peer4
B1 B2 B3
B1 B2 B3
B1 B2 B3
B1 B2 B3 B4
B4
B4B4
Figure: Miner broadcasts the block B4
8 of 39
Ethereum
High Level Design
Peer1
Peer2
Peer3
B1 B2 B3
B1 B2 B3
B1 B2 B3
B4
B4
B4
T1 T2 T3 FS
Hash of the
Previous
Block
T1 T2 T3 FS
Hash of the
Previous
Block
T1 T2 T3 FS
Hash of the
Previous
Block
Compute CS
Compute CS Compute CS
Figure: Validators (Peer 1, 2 and 3) compute current state (CS)
8 of 39
Ethereum
High Level Design
Peer1
Peer2
Peer3
B1 B2 B3
B1 B2 B3
B1 B2 B3
B4
B4
B4
T1 T2 T3 FS
Hash of the
Previous
Block
T1 T2 T3 FS
Hash of the
Previous
Block
T1 T2 T3 FS
Hash of the
Previous
Block
CS = FS
Reject Block B4
No Yes Agree on
Block B4
CS = FS
Reject Block B4
No Agree on
Block B4
Yes
CS = FS
Agree on
Block B4
No
Reject Block B4
Yes
Figure: Validators verify the FS and reach the consensus protocol (PoW)
8 of 39
Ethereum
High Level Design
Peer1
Peer2
Peer3
Peer4
B1 B2 B3
B1 B2 B3
B1 B2 B3
B1 B2 B3 B4
B4
B4
B4
Figure: Block B4 successfully added to the blockchain
8 of 39
Ethereum
Smart Contracts
• Modern Blockchain systems interpose an additional software layer
between clients and the blockchain.
9 of 39
Ethereum
Smart Contracts
• Modern Blockchain systems interpose an additional software layer
between clients and the blockchain.
• Client requests are directed to scripts, called smart contracts.
Examples: Ballot, Coin, Simple Auction etc.
9 of 39
Ethereum
Smart Contracts
• Modern Blockchain systems interpose an additional software layer
between clients and the blockchain.
• Client requests are directed to scripts, called smart contracts.
Examples: Ballot, Coin, Simple Auction etc.
• Smart contracts are similar to a legal contract in which terms are
recorded in a legal language.
9 of 39
Ethereum
Smart Contracts
• Modern Blockchain systems interpose an additional software layer
between clients and the blockchain.
• Client requests are directed to scripts, called smart contracts.
Examples: Ballot, Coin, Simple Auction etc.
• Smart contracts are similar to a legal contract in which terms are
recorded in a legal language.
• Advantages:
1. No need for a trusted third party
to validate the contract.
2. Low contracting enforcement.
3. Compliance costs.
9 of 39
Ethereum
Ballot Smart Contract
Initializes
contract	
with	a	list
of	
proposals
Create
Contract
Deploy
struct	Proposal	{
					bytes32	name;
					uint	voteCount
};
struct	Voter	{
					uint	weight;
					bool	voted;
					address	delegate;
					uint	vote;
};
Proposal[	]	public	proposals;
mapping(address=>Voter)	public	voters;
constructor	(	proposalNames	[		])	{	}
function	giveRightToVote	(	address	voter	)	{	}
function	delegate	(	address	to	)	{		}
function	vote	(uint	proposal)	{	}
function	winningProposal	(	uint	winningProposal	)	{	}
function	winnerName	(bytes32	winnerName)	{	}
State
Methods
vote	(proposal	Id)
or
delegate	(address	to)
throw
Voter.votedTrue
Increment	voteCount	
for	proposal	Id
or
add	weight	to
address	"to"
False
Chairperson Voter
Figure: Execution of Ballot Smart Contract
10 of 39
Ethereum Ballot Smart Contract
1 pragma solidity >=0.4.22 <0.7.0;
2 contract Ballot {
3 struct Voter {
4 uint weight; //weight is accumulated by delegation
5 bool voted; //if true, that person already voted
6 address delegate; //person delegated to
7 uint vote; //index of the voted proposal
8 }
9 struct Proposal {
10 bytes32 name; // short name (up to 32 bytes)
11 uint voteCount; // number of accumulated votes
12 }
13 address public chairperson; //chairperson of ballot
14 Proposal[] public proposals; //state variable
15 mapping(address => Voter) public voters; //state variable
16 //methods
17 constructor(bytes32[] memory proposalNames) public {};
18 function giveRightToVote(address voter) public {};
19 function delegate(address to) public {};
20 function vote(uint proposal) public {};
21 function winningProposal() public view returns (uint winningProposal_){};
22 function winnerName() public view returns (bytes32 winnerName_){};
23 }
11 of 39
Ethereum
Ballot Smart Contract
1 constructor(bytes32[] proposalNames) public {
2 chairperson = msg.sender;
3 voters[chairperson].weight = 1;
4 for (uint i = 0; i < proposalNames.length; i++) {
5 // Proposal({..}) creates a Proposal object
6 // proposals.push(..) appends it to the end of `proposals`.
7 proposals.push(Proposal({name:proposalNames[i],voteCount:0}));
8 }
• Constructor is called only once during contract deployment.
12 of 39
Ethereum
Ballot Smart Contract
1 constructor(bytes32[] proposalNames) public {
2 chairperson = msg.sender;
3 voters[chairperson].weight = 1;
4 for (uint i = 0; i < proposalNames.length; i++) {
5 // Proposal({..}) creates a Proposal object
6 // proposals.push(..) appends it to the end of `proposals`.
7 proposals.push(Proposal({name:proposalNames[i],voteCount:0}));
8 }
• Constructor is called only once during contract deployment.
• Chairperson deploys the contract.
12 of 39
Ethereum
Ballot Smart Contract
1 constructor(bytes32[] proposalNames) public {
2 chairperson = msg.sender;
3 voters[chairperson].weight = 1;
4 for (uint i = 0; i < proposalNames.length; i++) {
5 // Proposal({..}) creates a Proposal object
6 // proposals.push(..) appends it to the end of `proposals`.
7 proposals.push(Proposal({name:proposalNames[i],voteCount:0}));
8 }
• Constructor is called only once during contract deployment.
• Chairperson deploys the contract.
• Given ‘proposalNames’ it create proposal objects and add to ‘Proposal[]’.
12 of 39
Ethereum
Ballot Smart Contract
1 function giveRightToVote(address voter) public {
2 require( msg.sender == chairperson,
3 "Only chairperson can give right to vote.");
4 require( !voters[voter].voted, "The voter already voted.");
5 require(voters[voter].weight == 0);
6
7 voters[voter].weight = 1;
8 }
• Chairperson gives right to voter to cast the vote on the ballot contract.
13 of 39
Ethereum
Ballot Smart Contract
1 function delegate(address to) public {
2 Voter storage sender = voters[msg.sender];
3 require(!sender.voted, "You already voted.");
4 require(to != msg.sender, "Self-delegation is disallowed.");
5
6 while (voters[to].delegate != address(0)) {
7 to = voters[to].delegate;
8 require(to != msg.sender, "Found loop in delegation.");
9 }
10 sender.voted = true;
11 sender.delegate = to;
12 Voter storage delegate_ = voters[to];
13 if (delegate_.voted) {
14 proposals[delegate_.vote].voteCount += sender.weight;
15 } else {
16 delegate_.weight += sender.weight;
17 }
18 }
• If the delegate already voted, directly add to voted proposal.
• If the delegate did not voted yet, add to his/her weight to respective voter.
14 of 39
Ethereum
Ballot Smart Contract
1 function vote(uint proposal) public {
2 Voter storage sender = voters[msg.sender];
3 require(sender.weight != 0, "Has no right to vote");
4 require(!sender.voted, "Already voted.");
5
6 sender.voted = true;
7 sender.vote = proposal;
8 proposals[proposal].voteCount += sender.weight;
9 }
• Voter casts their vote (including votes delegated to you) to proposal
‘proposals[proposal]’.
15 of 39
Ethereum
Ballot Smart Contract
1 function winningProposal() public view returns (uint winningProposal_){
2 uint winningVoteCount = 0;
3 for (uint p = 0; p < proposals.length; p++) {
4 if (proposals[p].voteCount > winningVoteCount) {
5 winningVoteCount = proposals[p].voteCount;
6 winningProposal_ = p;
7 }
8 }
9 }
• Computes the winning proposal while considering all votes into account.
16 of 39
Ethereum
Ballot Smart Contract
1 function winnerName() public view returns (bytes32 winnerName_) {
2 winnerName_ = proposals[winningProposal()].name;
3 }
• Calls winningProposal() function to get the index of the winner contained in the
proposals array.
• Returns the name of the respective winner.
17 of 39
Ethereum
Solidity
• High-level Object-Oriented language for smart contracts.
18 of 39
Ethereum
Solidity
• High-level Object-Oriented language for smart contracts.
• A Contract programming language that has similarities to
Javascript and C.
18 of 39
Ethereum
Solidity
• High-level Object-Oriented language for smart contracts.
• A Contract programming language that has similarities to
Javascript and C.
• Solidity is compiled to bytecode that is executable on the
Ethereum Virtual Machine (EVM).
18 of 39
Ethereum
Solidity
• High-level Object-Oriented language for smart contracts.
• A Contract programming language that has similarities to
Javascript and C.
• Solidity is compiled to bytecode that is executable on the
Ethereum Virtual Machine (EVM).
18 of 39
Ethereum
Solidity
• High-level Object-Oriented language for smart contracts.
• A Contract programming language that has similarities to
Javascript and C.
• Solidity is compiled to bytecode that is executable on the
Ethereum Virtual Machine (EVM).
Figure: Solidity Execution
18 of 39
Hands-on Ethereum
18 of 39
Hands-on
Goal of this session
How to write the smart contracts and deploy it on Ethereum
network?
19 of 39
Hands-on
Introduction to Remix
• Remix is IDE to create, deploy, track and test the smart contracts.
20 of 39
Hands-on
Introduction to Remix
• Remix is IDE to create, deploy, track and test the smart contracts.
• Remix provides three kinds of environments.
20 of 39
Hands-on
Introduction to Remix
• Remix is IDE to create, deploy, track and test the smart contracts.
• Remix provides three kinds of environments.
1. Javascript VM: It provides five accounts each with 100 ethers to work
with smart contracts.
20 of 39
Hands-on
Introduction to Remix
• Remix is IDE to create, deploy, track and test the smart contracts.
• Remix provides three kinds of environments.
1. Javascript VM: It provides five accounts each with 100 ethers to work
with smart contracts.
2. Injected Web3: It injects network from extensions like Metamask. Any
number of accounts present in Metamask included network are
provided to work with smart contracts.
20 of 39
Hands-on
Introduction to Remix
• Remix is IDE to create, deploy, track and test the smart contracts.
• Remix provides three kinds of environments.
1. Javascript VM: It provides five accounts each with 100 ethers to work
with smart contracts.
2. Injected Web3: It injects network from extensions like Metamask. Any
number of accounts present in Metamask included network are
provided to work with smart contracts.
3. Web3 Provider: It injects network which is running in local system.
Probably it injects local networks provided by Truffle, Ganache-cli etc.,
and whatever accounts present in local network are provided to work
with smart contracts.
20 of 39
Hands-on
Introduction to Remix
Figure: Remix Ethereum Browser
21 of 39
Hands-on
Introduction to Remix
Figure: GUI – Provides to import/create files
22 of 39
Hands-on
Introduction to Remix
Figure: GUI tools to import/create files
23 of 39
Hands-on
Introduction to Remix
Figure: IDE - provides to create/edit smart contracts
24 of 39
Hands-on
Introduction to Remix
Figure: Terminal – to view the transaction details
25 of 39
Hands-on
Introduction to Remix
Figure: Compile Tab – to compile/configure the Solidity version
26 of 39
Hands-on
Introduction to Remix
Figure: Run tab – to deploy and interact with contract
27 of 39
Hands-on
Introduction to Remix
Figure: Run tab – to deploy and interact with contract
28 of 39
Hands-on
Introduction to Remix
Figure: Run tab – to deploy and interact with contract
29 of 39
Hands-on
Metamask
• MetaMask is a bridge that allows you to visit the distributed web
of tomorrow in your browser today.
30 of 39
Hands-on
Metamask
• MetaMask is a bridge that allows you to visit the distributed web
of tomorrow in your browser today.
• It allows you to run Ethereum Decentralize Application right in
your browser without running a full Ethereum node.
30 of 39
Hands-on
Metamask
• MetaMask is a bridge that allows you to visit the distributed web
of tomorrow in your browser today.
• It allows you to run Ethereum Decentralize Application right in
your browser without running a full Ethereum node.
30 of 39
Hands-on
Metamask
31 of 39
Hands-on
Metamask
32 of 39
Hands-on
Metamask
33 of 39
Hands-on
Metamask
34 of 39
Hands-on
Metamask
35 of 39
Hands-on
Metamask
36 of 39
Conclusion
• Blockchain like Ethereum is a decentralized, distributed database
or ledger of records.
37 of 39
Conclusion
• Blockchain like Ethereum is a decentralized, distributed database
or ledger of records.
• Ethereum can make wonderful Dapps using smart contract which
can solve real life issues.
37 of 39
Conclusion
• Blockchain like Ethereum is a decentralized, distributed database
or ledger of records.
• Ethereum can make wonderful Dapps using smart contract which
can solve real life issues.
• Smart contracts achieve trust between peers or organizations
without the help of third party.
37 of 39
Conclusion
• Blockchain like Ethereum is a decentralized, distributed database
or ledger of records.
• Ethereum can make wonderful Dapps using smart contract which
can solve real life issues.
• Smart contracts achieve trust between peers or organizations
without the help of third party.
• With the help of Solidity, Remix, Metamask, Truffle and other
tools, we can build good smart contracts and Dapps.
37 of 39
Conclusion
• Blockchain like Ethereum is a decentralized, distributed database
or ledger of records.
• Ethereum can make wonderful Dapps using smart contract which
can solve real life issues.
• Smart contracts achieve trust between peers or organizations
without the help of third party.
• With the help of Solidity, Remix, Metamask, Truffle and other
tools, we can build good smart contracts and Dapps.
• Finally, we can achieve beyond cryptocurrencies by using Ethereum
Blockchain.
37 of 39
Assignment
Write your first smart contract in Solidity
Develop a Money transfer contract with following functions:
• Create 5 unique accounts.
• transferMoney(): To transfer the money from an account to
another account.
• getbalance(): To view the balance of account.
38 of 39
Thanks &
Questions Please!!
39 of 39

All about blockchain

  • 1.
    Blockchain Presented by: Sathya Peri AssociateProfessor CSE Department, Indian Institute of Technology Hyderabad, India 1 of 39
  • 2.
    Outline of theTalk Introduction to Blockchain Ethereum Hands-on Ethereum 2 of 39
  • 3.
  • 4.
    Introduction Why Blockchain? Centralized vsDistributed Platformsa • Centralized Platforms: ◦ Information going through a single point. ◦ Lack of transparency. ◦ Needs to pay transaction fees. • Blockchain Technology: ◦ Decentralized Distributed System. ◦ No centralized control. ◦ Stores information on a peer-to-peer network. Clearing House  Centralized Ledger Distributed Ledger a Source: https://www.rathbones.com/knowledge-and-insight/blockchain-internet-value 3 of 39
  • 5.
    Introduction Blockchain • Blockchain isa decentralized, distributed database or ledger of records. b https://bitcoin.org/en/ c https://www.ethereum.org/ d https://www.hyperledger.org/ 4 of 39
  • 6.
    Introduction Blockchain • Blockchain isa decentralized, distributed database or ledger of records. • It maintains information in the form of blocks. b https://bitcoin.org/en/ c https://www.ethereum.org/ d https://www.hyperledger.org/ 4 of 39
  • 7.
    Introduction Blockchain • Blockchain isa decentralized, distributed database or ledger of records. • It maintains information in the form of blocks. • Each block is a collection of transactions and has a pointer to its previous block. b https://bitcoin.org/en/ c https://www.ethereum.org/ d https://www.hyperledger.org/ 4 of 39
  • 8.
    Introduction Blockchain • Blockchain isa decentralized, distributed database or ledger of records. • It maintains information in the form of blocks. • Each block is a collection of transactions and has a pointer to its previous block. • By design these blocks are immutable but publicly readable. b https://bitcoin.org/en/ c https://www.ethereum.org/ d https://www.hyperledger.org/ 4 of 39
  • 9.
    Introduction Blockchain • Blockchain isa decentralized, distributed database or ledger of records. • It maintains information in the form of blocks. • Each block is a collection of transactions and has a pointer to its previous block. • By design these blocks are immutable but publicly readable. • Example: Bitcoinb, Ethereumc, and Hyperledgerd etc. b https://bitcoin.org/en/ c https://www.ethereum.org/ d https://www.hyperledger.org/ 4 of 39
  • 10.
    Introduction Transaction Flow inBlockchain A wants to send money to B. A The transaction is broadcasted to every peer in the network. 21 ? ? ? ?? ? 3 Miners execute the transactions and form a block. Then block is broadcasted to every peer in the network. 4 Validator validate the transactions in the block. The block then can be added to the chain, which provides an indelible and transparent record of transactions. 5 The money moves from A to B. B 6 Figure: High-level Overview of Transaction Flow in Blockchain 5 of 39
  • 11.
    Introduction Blockchain Applications Figure: Blockchainapplicationse e https://medium.com/practical-blockchain/use-cases-of-blockchain-tech-application-13a940edf6fb 6 of 39
  • 12.
    Introduction Blockchain Classification Three Differenttypes of Blockchain depending on their applications. • Public Blockchain • Private Blockchain 7 of 39
  • 13.
    Introduction Blockchain Classification Three Differenttypes of Blockchain depending on their applications. • Public Blockchain • Private Blockchain Properties Public Private Consensus Participants All nodes (permission less) Organization (permissioned node) Access Permission Public Restricted Efficiency Low High Data Security Very High Data Breach Possible Example Bitcoin, Ethereum Hyperledger Fabric 7 of 39
  • 14.
  • 15.
    Ethereum High Level Design •Ethereum nodes form a peer-to-peer system. 8 of 39
  • 16.
    Ethereum High Level Design •Ethereum nodes form a peer-to-peer system. • Clients (external to the system) wishing to execute smart contracts, contact a peer of the system. 8 of 39
  • 17.
    Ethereum High Level Design •Ethereum nodes form a peer-to-peer system. • Clients (external to the system) wishing to execute smart contracts, contact a peer of the system. Peer1 Peer2 Peer3 Peer4 Client1 Client2 Client3 T1 T2 T3 B1 B2 B3 B1 B2 B3 B1 B2 B3 B1 B2 B3 Figure: Clients send Transaction T1, T2 and T3 to Miner (Peer4) 8 of 39
  • 18.
    Ethereum High Level Design Peer4T1 T2 T3 FS Hash of the Previous Block B1 B2 B3 B4 Figure: Miner forms a block B4 and compute final state (FS) 8 of 39
  • 19.
    Ethereum High Level Design Peer1 Peer2 Peer3 Peer4 B1B2 B3 B1 B2 B3 B1 B2 B3 B1 B2 B3 B4 B4 B4B4 Figure: Miner broadcasts the block B4 8 of 39
  • 20.
    Ethereum High Level Design Peer1 Peer2 Peer3 B1B2 B3 B1 B2 B3 B1 B2 B3 B4 B4 B4 T1 T2 T3 FS Hash of the Previous Block T1 T2 T3 FS Hash of the Previous Block T1 T2 T3 FS Hash of the Previous Block Compute CS Compute CS Compute CS Figure: Validators (Peer 1, 2 and 3) compute current state (CS) 8 of 39
  • 21.
    Ethereum High Level Design Peer1 Peer2 Peer3 B1B2 B3 B1 B2 B3 B1 B2 B3 B4 B4 B4 T1 T2 T3 FS Hash of the Previous Block T1 T2 T3 FS Hash of the Previous Block T1 T2 T3 FS Hash of the Previous Block CS = FS Reject Block B4 No Yes Agree on Block B4 CS = FS Reject Block B4 No Agree on Block B4 Yes CS = FS Agree on Block B4 No Reject Block B4 Yes Figure: Validators verify the FS and reach the consensus protocol (PoW) 8 of 39
  • 22.
    Ethereum High Level Design Peer1 Peer2 Peer3 Peer4 B1B2 B3 B1 B2 B3 B1 B2 B3 B1 B2 B3 B4 B4 B4 B4 Figure: Block B4 successfully added to the blockchain 8 of 39
  • 23.
    Ethereum Smart Contracts • ModernBlockchain systems interpose an additional software layer between clients and the blockchain. 9 of 39
  • 24.
    Ethereum Smart Contracts • ModernBlockchain systems interpose an additional software layer between clients and the blockchain. • Client requests are directed to scripts, called smart contracts. Examples: Ballot, Coin, Simple Auction etc. 9 of 39
  • 25.
    Ethereum Smart Contracts • ModernBlockchain systems interpose an additional software layer between clients and the blockchain. • Client requests are directed to scripts, called smart contracts. Examples: Ballot, Coin, Simple Auction etc. • Smart contracts are similar to a legal contract in which terms are recorded in a legal language. 9 of 39
  • 26.
    Ethereum Smart Contracts • ModernBlockchain systems interpose an additional software layer between clients and the blockchain. • Client requests are directed to scripts, called smart contracts. Examples: Ballot, Coin, Simple Auction etc. • Smart contracts are similar to a legal contract in which terms are recorded in a legal language. • Advantages: 1. No need for a trusted third party to validate the contract. 2. Low contracting enforcement. 3. Compliance costs. 9 of 39
  • 27.
  • 28.
    Ethereum Ballot SmartContract 1 pragma solidity >=0.4.22 <0.7.0; 2 contract Ballot { 3 struct Voter { 4 uint weight; //weight is accumulated by delegation 5 bool voted; //if true, that person already voted 6 address delegate; //person delegated to 7 uint vote; //index of the voted proposal 8 } 9 struct Proposal { 10 bytes32 name; // short name (up to 32 bytes) 11 uint voteCount; // number of accumulated votes 12 } 13 address public chairperson; //chairperson of ballot 14 Proposal[] public proposals; //state variable 15 mapping(address => Voter) public voters; //state variable 16 //methods 17 constructor(bytes32[] memory proposalNames) public {}; 18 function giveRightToVote(address voter) public {}; 19 function delegate(address to) public {}; 20 function vote(uint proposal) public {}; 21 function winningProposal() public view returns (uint winningProposal_){}; 22 function winnerName() public view returns (bytes32 winnerName_){}; 23 } 11 of 39
  • 29.
    Ethereum Ballot Smart Contract 1constructor(bytes32[] proposalNames) public { 2 chairperson = msg.sender; 3 voters[chairperson].weight = 1; 4 for (uint i = 0; i < proposalNames.length; i++) { 5 // Proposal({..}) creates a Proposal object 6 // proposals.push(..) appends it to the end of `proposals`. 7 proposals.push(Proposal({name:proposalNames[i],voteCount:0})); 8 } • Constructor is called only once during contract deployment. 12 of 39
  • 30.
    Ethereum Ballot Smart Contract 1constructor(bytes32[] proposalNames) public { 2 chairperson = msg.sender; 3 voters[chairperson].weight = 1; 4 for (uint i = 0; i < proposalNames.length; i++) { 5 // Proposal({..}) creates a Proposal object 6 // proposals.push(..) appends it to the end of `proposals`. 7 proposals.push(Proposal({name:proposalNames[i],voteCount:0})); 8 } • Constructor is called only once during contract deployment. • Chairperson deploys the contract. 12 of 39
  • 31.
    Ethereum Ballot Smart Contract 1constructor(bytes32[] proposalNames) public { 2 chairperson = msg.sender; 3 voters[chairperson].weight = 1; 4 for (uint i = 0; i < proposalNames.length; i++) { 5 // Proposal({..}) creates a Proposal object 6 // proposals.push(..) appends it to the end of `proposals`. 7 proposals.push(Proposal({name:proposalNames[i],voteCount:0})); 8 } • Constructor is called only once during contract deployment. • Chairperson deploys the contract. • Given ‘proposalNames’ it create proposal objects and add to ‘Proposal[]’. 12 of 39
  • 32.
    Ethereum Ballot Smart Contract 1function giveRightToVote(address voter) public { 2 require( msg.sender == chairperson, 3 "Only chairperson can give right to vote."); 4 require( !voters[voter].voted, "The voter already voted."); 5 require(voters[voter].weight == 0); 6 7 voters[voter].weight = 1; 8 } • Chairperson gives right to voter to cast the vote on the ballot contract. 13 of 39
  • 33.
    Ethereum Ballot Smart Contract 1function delegate(address to) public { 2 Voter storage sender = voters[msg.sender]; 3 require(!sender.voted, "You already voted."); 4 require(to != msg.sender, "Self-delegation is disallowed."); 5 6 while (voters[to].delegate != address(0)) { 7 to = voters[to].delegate; 8 require(to != msg.sender, "Found loop in delegation."); 9 } 10 sender.voted = true; 11 sender.delegate = to; 12 Voter storage delegate_ = voters[to]; 13 if (delegate_.voted) { 14 proposals[delegate_.vote].voteCount += sender.weight; 15 } else { 16 delegate_.weight += sender.weight; 17 } 18 } • If the delegate already voted, directly add to voted proposal. • If the delegate did not voted yet, add to his/her weight to respective voter. 14 of 39
  • 34.
    Ethereum Ballot Smart Contract 1function vote(uint proposal) public { 2 Voter storage sender = voters[msg.sender]; 3 require(sender.weight != 0, "Has no right to vote"); 4 require(!sender.voted, "Already voted."); 5 6 sender.voted = true; 7 sender.vote = proposal; 8 proposals[proposal].voteCount += sender.weight; 9 } • Voter casts their vote (including votes delegated to you) to proposal ‘proposals[proposal]’. 15 of 39
  • 35.
    Ethereum Ballot Smart Contract 1function winningProposal() public view returns (uint winningProposal_){ 2 uint winningVoteCount = 0; 3 for (uint p = 0; p < proposals.length; p++) { 4 if (proposals[p].voteCount > winningVoteCount) { 5 winningVoteCount = proposals[p].voteCount; 6 winningProposal_ = p; 7 } 8 } 9 } • Computes the winning proposal while considering all votes into account. 16 of 39
  • 36.
    Ethereum Ballot Smart Contract 1function winnerName() public view returns (bytes32 winnerName_) { 2 winnerName_ = proposals[winningProposal()].name; 3 } • Calls winningProposal() function to get the index of the winner contained in the proposals array. • Returns the name of the respective winner. 17 of 39
  • 37.
    Ethereum Solidity • High-level Object-Orientedlanguage for smart contracts. 18 of 39
  • 38.
    Ethereum Solidity • High-level Object-Orientedlanguage for smart contracts. • A Contract programming language that has similarities to Javascript and C. 18 of 39
  • 39.
    Ethereum Solidity • High-level Object-Orientedlanguage for smart contracts. • A Contract programming language that has similarities to Javascript and C. • Solidity is compiled to bytecode that is executable on the Ethereum Virtual Machine (EVM). 18 of 39
  • 40.
    Ethereum Solidity • High-level Object-Orientedlanguage for smart contracts. • A Contract programming language that has similarities to Javascript and C. • Solidity is compiled to bytecode that is executable on the Ethereum Virtual Machine (EVM). 18 of 39
  • 41.
    Ethereum Solidity • High-level Object-Orientedlanguage for smart contracts. • A Contract programming language that has similarities to Javascript and C. • Solidity is compiled to bytecode that is executable on the Ethereum Virtual Machine (EVM). Figure: Solidity Execution 18 of 39
  • 42.
  • 43.
    Hands-on Goal of thissession How to write the smart contracts and deploy it on Ethereum network? 19 of 39
  • 44.
    Hands-on Introduction to Remix •Remix is IDE to create, deploy, track and test the smart contracts. 20 of 39
  • 45.
    Hands-on Introduction to Remix •Remix is IDE to create, deploy, track and test the smart contracts. • Remix provides three kinds of environments. 20 of 39
  • 46.
    Hands-on Introduction to Remix •Remix is IDE to create, deploy, track and test the smart contracts. • Remix provides three kinds of environments. 1. Javascript VM: It provides five accounts each with 100 ethers to work with smart contracts. 20 of 39
  • 47.
    Hands-on Introduction to Remix •Remix is IDE to create, deploy, track and test the smart contracts. • Remix provides three kinds of environments. 1. Javascript VM: It provides five accounts each with 100 ethers to work with smart contracts. 2. Injected Web3: It injects network from extensions like Metamask. Any number of accounts present in Metamask included network are provided to work with smart contracts. 20 of 39
  • 48.
    Hands-on Introduction to Remix •Remix is IDE to create, deploy, track and test the smart contracts. • Remix provides three kinds of environments. 1. Javascript VM: It provides five accounts each with 100 ethers to work with smart contracts. 2. Injected Web3: It injects network from extensions like Metamask. Any number of accounts present in Metamask included network are provided to work with smart contracts. 3. Web3 Provider: It injects network which is running in local system. Probably it injects local networks provided by Truffle, Ganache-cli etc., and whatever accounts present in local network are provided to work with smart contracts. 20 of 39
  • 49.
    Hands-on Introduction to Remix Figure:Remix Ethereum Browser 21 of 39
  • 50.
    Hands-on Introduction to Remix Figure:GUI – Provides to import/create files 22 of 39
  • 51.
    Hands-on Introduction to Remix Figure:GUI tools to import/create files 23 of 39
  • 52.
    Hands-on Introduction to Remix Figure:IDE - provides to create/edit smart contracts 24 of 39
  • 53.
    Hands-on Introduction to Remix Figure:Terminal – to view the transaction details 25 of 39
  • 54.
    Hands-on Introduction to Remix Figure:Compile Tab – to compile/configure the Solidity version 26 of 39
  • 55.
    Hands-on Introduction to Remix Figure:Run tab – to deploy and interact with contract 27 of 39
  • 56.
    Hands-on Introduction to Remix Figure:Run tab – to deploy and interact with contract 28 of 39
  • 57.
    Hands-on Introduction to Remix Figure:Run tab – to deploy and interact with contract 29 of 39
  • 58.
    Hands-on Metamask • MetaMask isa bridge that allows you to visit the distributed web of tomorrow in your browser today. 30 of 39
  • 59.
    Hands-on Metamask • MetaMask isa bridge that allows you to visit the distributed web of tomorrow in your browser today. • It allows you to run Ethereum Decentralize Application right in your browser without running a full Ethereum node. 30 of 39
  • 60.
    Hands-on Metamask • MetaMask isa bridge that allows you to visit the distributed web of tomorrow in your browser today. • It allows you to run Ethereum Decentralize Application right in your browser without running a full Ethereum node. 30 of 39
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
    Conclusion • Blockchain likeEthereum is a decentralized, distributed database or ledger of records. 37 of 39
  • 68.
    Conclusion • Blockchain likeEthereum is a decentralized, distributed database or ledger of records. • Ethereum can make wonderful Dapps using smart contract which can solve real life issues. 37 of 39
  • 69.
    Conclusion • Blockchain likeEthereum is a decentralized, distributed database or ledger of records. • Ethereum can make wonderful Dapps using smart contract which can solve real life issues. • Smart contracts achieve trust between peers or organizations without the help of third party. 37 of 39
  • 70.
    Conclusion • Blockchain likeEthereum is a decentralized, distributed database or ledger of records. • Ethereum can make wonderful Dapps using smart contract which can solve real life issues. • Smart contracts achieve trust between peers or organizations without the help of third party. • With the help of Solidity, Remix, Metamask, Truffle and other tools, we can build good smart contracts and Dapps. 37 of 39
  • 71.
    Conclusion • Blockchain likeEthereum is a decentralized, distributed database or ledger of records. • Ethereum can make wonderful Dapps using smart contract which can solve real life issues. • Smart contracts achieve trust between peers or organizations without the help of third party. • With the help of Solidity, Remix, Metamask, Truffle and other tools, we can build good smart contracts and Dapps. • Finally, we can achieve beyond cryptocurrencies by using Ethereum Blockchain. 37 of 39
  • 72.
    Assignment Write your firstsmart contract in Solidity Develop a Money transfer contract with following functions: • Create 5 unique accounts. • transferMoney(): To transfer the money from an account to another account. • getbalance(): To view the balance of account. 38 of 39
  • 73.