Smart Contract
Dr. Ditipriya Sinha
C.S.E Department, NIT Patna
Cryptocurrency vs conventional
currency
Conventional Cryptocurrency
currency
Secure Moderate High
Scarce Low High
Sovereign Yes No
Decentralized No yes
Smart No yes
Why we use crypto currency ?
Fast and cheap
Easy to use
Free to transfer and hold
Decentralized control
Privacy and Security
Transparency is maintained through public
ledger system.
Reduced Fraud
Some crypto currencies
Year Name Description
2009 Bitcoin First c.c. used SHA-
256 as hashing
function
April 2011 Name coin Decentralized DNS
Oct 2011 Litecoin First successful script
cryptocurrency
2012 Peercoin First use of POW and
POS functions
Early 2014 Monero Uses CryptoNote
Protocol ,
Wha is Bitcoin?
Bitcoin Value Proposition
The technology behind the Bitcoin-
The Blockchain
The Bitcoin Transaction Life Cycle –
The Sender
The Bitcoin Transaction Life Cycle –
The Network
The technology behind the Bitcoin-
The Blockchain
The Bitcoin Transaction Life Cycle –
The Receiver
Blockchain 2.0
Blockchain 2.0 and Smart Contracts
Cryptocurrency: Bitcoin
A Bitcoin transaction:
Block Header (Bitcoin)
Block Header (Bitcoin)
Transactions in a Block (Bitcoin)
Transactions in a Block (Bitcoin)
Cryptocurrency: Bitcoin
Average Bitcoin Block size Vs Time:
Smart Contracts
The term was coined by Nick Szabo a
computer scientist and cryptographer in 1996
Szabo claimed that smart contracts can be
realized with the help of a public ledger.
Block chain can be a pioneering technology to
realize smart contracts
Contracts in a Centralized Platform -
Crowdfunding
The Crowdfunding Platform
Both the product team and
the supporters need to trust
the crowd funding platform
The product team expects the
money to be get paid based
on the project progress
The supporters expect the
money to go to the project
However, the crowd funding
platform, the middleman,
takes significant charge to
manage the entire process
Crowd funding Platform using Smart
Contracts
The contract is written in a code which is available to all the
stakeholders
- The supporters and the product team –
- Do you see an application of Blockchain here
Crowd funding Platform using Smart
Contracts
If certain goals of the project are reached, then the code
automatically transfers the money from supporters to the
production team
Smart Contracts – The Advantage
Immutable: No party will be able to change the
contract once it is fixed and written to the
public ledger(the Blockchain)
Diatributed: All the steps of the contract can be
validated by every participating party –no one
claim later that the contract was not validated
Why Blockchain?
-The blocks are immutable
-The information is open –Every one can check
and validate.
Smart Contracts Platforms
Cryptocurrency: Ethereum
What is Ethereum?
◦ Ethereum is an open platform that enables developers to build and
deploy decentralized applications such as smart contracts and other
complex legal and financial applications.
◦ Ethereum is now currently the cryptocurrency with the second highest
coin market cap and is expected by some to surpass Bitcoin as both a
valued investment and as the world’s most popular cryptocurrency
Cryptocurrency: Ethereum
Why Ethereum?
◦ Uptime , Security , Almost Free , Transparency , Micro payments,
DAOs, Consensus applications, governance Identity / Reputation
Services.
There are several popular clients to work with the Ethereum blockchain.
They are:
◦ eth – a client written in C++
◦ geth – a client written in Go
◦ pyethapp – a client written in Python
◦ Parity – a client written in Rust
◦ Mantis – a client written in Scala
◦ Harmony – a client written in Java
Cryptocurrency: Ethereum
What is Geth?
◦ Geth is a go-ethereum client multipurpose command line tool that runs a
full Ethereum node implemented in Go.
◦ It offers three interfaces: the command line subcommands and options, a
Json-rpc server and an interactive console.
◦ It will connect to the existing live blockchain or create its own,
depending on provided settings.
◦ By installing and running geth, we can take part in the ethereum frontier
live network and we can participating in making the Ethereum network
better and stronger.
HOW SMART CONTRACT WORKS?
Ethereum Blockchain
Components
Front-end Back-end Contracts SW language
(HTML-CSS) (Java script) (Solidity, Serpent)
Ethereum Swarm Whisper
(Decentralized (Decentralized (Decentralized
Computation Storage) Messaging)
Ethereum Networking
Peer reputation Peer
Encrypted Handshake
preference
Encrypt transport
Node discovery P2P networking
Ethereum Protocol
EVM Blockchain Accounts
Contracts
Mining
Transactions
Decentralized Application
(Dapp)
• Dapp is an application that uses smart contracts.
• Dapp provides a user-friendly interface to smart contracts
• Dapp main components are smart contract and files for web user interface front-end/back-end
• Implement smart contract in a high-level language
Step 1
• Compile the contracts in a high-level language
Step 2
• Deploy the contracts on Ethereum Blockchain network
Step 3 using Ethereum clients
• Build web applications that interact with the smart
Step 4 contracts
Dapp Creation
Workflow
1. Send smart contract
for compilation
Compiler
Smart Contract
2. Contract binary
Front end
HTML/CSS
(index. html)
3. Deploy Contract
Front end
App.(js)
4. Contract address and
ABI sent back to Dapp Ethereum Client
Back-end
-Geth
(Javascript) -PyEthApp
5. Send transaction to Ethereum Node
Dapp contract
Cryptocurrency: Ethereum
Setting Up A Private Blockchain(Geth Client):
◦ Go the Go Ethereum (geth) site and download the binary for your
operating system.
Environment Setup:
◦ Links for Geth Installation :
Website: [Link]
GitHub: [Link]
Wiki: [Link]
Cryptocurrency: Ethereum
Geth Installation:
Cryptocurrency: Ethereum
Create Custom Ethereum Blockchain:
◦ Create a working folder/directory for this exercise. It will be used to
hold binaries and configuration files.
◦ Create the genesis block
◦ Create storage of the blockchain
◦ Deploy blockchain nodes
Genesis block
This block is the first block in the chain and a json file that stores the
configuration of the chain and the only one without a predecessor.
Create a new project directory. Within it create the [Link] file.
Genesis block
Contents of the Genesis file:
◦ chainId – This is the chain identification number that is used to
distinguish between Blockchains.
◦ homesteadBlock, eip155Block, eip158Block, byzantiumBlock –
These properties are related to chain forking and versioning.
We don’t need these for now, so let’s set them to 0.
◦ difficulty – This number decides how difficult the blocks will be
to mine. For Private networks, it’s good to set a lower number
as it lets you mine blocks quickly, which results in fast
transactions.
◦ gasLimit – This number is the total amount of gas that can be
used in each block. We don’t want our network to hit the limit,
so we have set this high.
◦ alloc – This part is used to allocate ethers to already created
accounts.
Cryptocurrency: Ethereum
Create the storage of the blockchain:
◦ Once the [Link] file is saved, you are ready to create your first
node. To create your first node, open a new terminal window and
navigate to your project folder, and type in the following command.
geth –datadir firstBC init [Link]
Cryptocurrency: Ethereum
Inside the Blockchain Folder:
◦ gethfolder: Store your database.
◦ keystore: Store your Ethereum accounts.
Cryptocurrency: Ethereum
Start the Ethereum peer node:
◦ Start the blockchain through geth console.
◦ The geth console offers a command line interface with a
javascript runtime.
geth --datadir "firstBC" --networkid 1234 --port 11111 "--
allow-insecureunlock“ --nodiscover --ipcdisable console
◦ Networkid provides privacy for your network.
◦ Other peers joining your network must use the same
networkid but port number should be different.
◦ The console is a Javascript console that lets we send
commands to Geth.
Cryptocurrency: Ethereum
Create an account:
◦ Type [Link](‘Type your password here’) to
create as many accounts as we need
◦ See the created account(s) by type command [Link].
Cryptocurrency: Ethereum
Mining:
◦ Before start mining — we have checked current blocks in
our private blockchain.
> [Link]
0
> [Link]([Link][0])
0
Cryptocurrency: Ethereum
Lets start mining :
◦ Type [Link]() to start mining .
◦ [Link] takes an optional parameter for the number of miner
threads.
◦ Once mining is started, let’s check balance ether for the number of
miner threads.
◦ >acc1 = [Link][0]
◦ >[Link](acc1)
Cryptocurrency: Ethereum
This will generate the new DAG structure used in ETHash mining, and
then start mining blocks.
Type [Link]() to stop mining.
Cryptocurrency: Ethereum
Now check again blocknumber and account balance
> [Link]
1472
> [Link]([Link][0])
7.36e+21
Cryptocurrency: Ethereum
Balance is showing in wei. We can convert it to ether:
> [Link]([Link]([Link][0]))
385
Cryptocurrency: Ethereum
Trying to list all accounts :
> [Link] []
> [Link]({from:[Link][0], to:[Link][1],
value: [Link](10, "ether")})
Now we send ether from one account to other accounts.
Cryptocurrency: Ethereum
> [Link]([Link][0])
Unlock account 0x4d7287b92bde40e93b0e069d95a2fb829bbd37ef Passphrase:
true
> [Link]({from:[Link][0], to:[Link][1], value:
[Link](10, "ether")})
Lets check balance of the receiver ->
> [Link](to:[Link][1])
0
Cryptocurrency: Ethereum
It is marked as a pending transaction :
Cryptocurrency: Ethereum
Cryptocurrency: Ethereum
Adding More Peers/Nodes in one system:
◦ Now let’s setup a second node in the blockchain network.
The process will be similar to setting up Node1.
◦ Open a new terminal window and navigate to the project
folder that contains the [Link] file.
◦ Initialize the new node with the following command:
geth --datadir “secondBC" --networkid 1234 --port 11112
"--allowinsecure-unlock" --nodiscover --ipcdisable console
◦ Three important things to note here.
You must use the same [Link] file.
You must use a different datadir folder.
Ensure you use the same network id and port should be different
Cryptocurrency: Ethereum
In the console of the second node (you can use either), run
[Link] . You should get something similar to this.
Cryptocurrency: Ethereum
Initialize the data directory on a new system with the same [Link]
file (because the default is to use the mainnet).
On the first system, look up its "enode" discovery address in the console
by inspecting this variable:
[Link]
Cryptocurrency: Ethereum
In the console, peer it with the first node by using the "enode" variable
where the host part is replaced with the IP address:
[Link]("enode://
6fc14916cefae9082d017a9266a4eed4360719838ba656f59c7bbfdeffc7d9
33bfeceeae0d7cbdc2f82da2129a6524828ac00afa6778413f0cb
2633427745d83@[Link]:30303")
Cryptocurrency: Ethereum
Cryptocurrency: Ethereum
Application Template: Many-to-one for
IoT
Account
Account Address State Variables
Creates/
Call/Transactions
Owns Account Keys
(Private &Public)
Contract Functions
Owner
Call/Transactions
Account Balance
(Ether)
Modifiers
IoT
Device
Call/Transactions
Account
Events
User
Call/Transactions Smart Contract
Account
Examples: IoT apps Energy meters,
Solar switch
User
Account
State Variables
Account Address . address public owner
.mapping (address=>unit) public usersPaid
Creates Creat .unit public numUsers
/ Owns Transaction: e
Create Contract
Contract
Account Keys Contract
Owner
(Private & Public)
Functions
.SmartSwitch()
Account Balance .payToSwitch()
(Ether) .refundUser(address recipeint, unit amount)
Refund a User .withdrawFunds()
Transaction:
.kill()
refundUser()
Withdraw funds in
Transaction:
withdrawFunds(
) Modifiers
Contract account
.onlyOwner()
Users
Events
Send Ether to .Deposit(address_from, unit_amount)
sends Transaction: smart Switch .Refund(address_to, unit_amount)
transactions contract
Accounts paytoSwitch(
)
.value
Smart Contract
Check balance and turn
Switch on/off
IoT Device
Smart switch Controller Service
Smart Cities
Smart City
It is not only Technology, it is an
ecosystem.
Economic Social
Technolog
y
Environmental Legal
Smart Cities Ecosystem
Citizens applications portal (Smart Apps Center) Digital signage
Command center
To aggregate and share urban data
Vertical applications
Security
Public Mobility Urban/ Smart retail Smart Smar
City infrastructure management safety services services home
environ services t
Lights Traffic Parking Waste ment buildi
monitori ng
ng servic
es
Application tools (authentication, payment, monetization, NFC,
IoT platform,
geolocalization, etc.)big data, open data (live objects manage/flexible data)
IT infrastructure: VPN, LAN, WAN, Flexible Computing (Cloud), Cybesecurity
Network: fiber*, 4G.5G, Wi-Fi, LoRa IoT 5G
network
City objects/sensors/actuators
How we can think Blockchain for Smart
City?
Anything that is capable of being owned or controlled to
produce value, is considered an asset can be tangible or
intangible value can be converted into cash.
A participant is a member of a business network Customer,
Supplier, Government, Regulator. Usually reside in an
organization. Have specific identities and roles.
A transaction is an asset transfer between two or
more participants
A contract is set of conditions under which transactions
occur
Blockchain Technology Stack
for Smart
City
Application Layer Application Layer
Safe City Smart Living Smart People dApp dApp
dApp dApp
Smart Environment Smart Government Smart Mobility
dApp
dApp
Smart City Enabling Platforms Platform Layer Blockchain
Internet
Smart City Infrastructure
Systems P2P network
Data Center
Passive Infrastructure Data Center- Power Computing
Active Infrastructure
Storage and Content
Infrastructure Layer Infrastructure Layer
References
• National Interagency Fire Center (NIFC) report (2019) retrieved from.” [Online]. Available:
[Link]
• Brazilian-amazon-fires-deforestation-report/ (2019) retrieved from.” [Online]. Available:
[Link]
• Forest Survey of India (2011), retrieved from.” [Online]. Available: [Link]
• Zheng, Z., Xie, S., Dai, H. N., Chen, X., Wang, H.: Blockchain challenges and opportunities: A survey. International Journal of Web
and Grid Services, Vol. 14(4), pp. 352-375 (2018).DOI: 10.1504/IJWGS.2018.095647
• Kaur, H., Sood, S.K:Fog-assisted IoT-enabled scalable network infrastructure for wildfire surveillance. Journal of Network and
Computer Applications. 144, 171–183 (2019). doi:10.1016/[Link].2019.07.005
• Sinha D, Kumari R, Tripathi S Semisupervised Classification Based Clustering Approach in WSN for Forest Fire Detection. Wireless
Personal Communications(2019). [Link]
• Kaur, H., Sood, S.K.: Adaptive Neuro Fuzzy Inference System (ANFIS) based wildfire risk assessment. Journal of Experimental and
Theoretical Artificial Intelligence. 00, 1–21 (2019).Doi:10.1080/0952813X.2019.1591523
• Mishra, A. K., Tripathy, A. K., Puthal, D., & Yang, L. T. Analytical Model for Sybil Attack Phases in Internet of Things. IEEE Internet of
Things Journal, 1–1. (2018). doi:10.1109/jiot.2018.2843769
• Rajan, A., Jithish, J. and Sankaran, S.: Sybil attack in IOT: Modelling and defenses. International Conference on Advances in
Computing, Communications and Informatics (ICACCI),pp. 2323-2327. IEEE, (2017).DOI: 10.1109/ICACCI.2017.8126193.
• Reeds J.,"A review of Sybil attack in wireless sensor networks", [Link]
• Newsome J., Shi E., Song D., Perrig A., “The Sybil Attack in Sensor Networks: Analysis &Defenses” IPSN’04, April 26-27, 2004,
Berkeley, California, USA.(2004)DOI: 10.1109/IPSN.2004.1307346
• Jan, M.A., Nanda, P., He, X. and Liu, R.P.. A Sybil attack detection scheme for a forest wildfire monitoring application. Future
Generation Computer Systems, 80, pp.613-626.(2018)DOI: 10.1016/[Link].2016.05.034
• Mahmoud, M.A.I., Ren, H.: Forest Fire Detection Using a Rule-Based Image Processing Algorithm and Temporal Variation.
Mathematical Problems in Engineering. (2018)[Link]
• Sudhakar, S., Vijayakumar, V., Kumar, C. S., Priya, V., Ravi, L., & Subramaniyaswamy, V. Unmanned Aerial Vehicle (UAV) based
Forest Fire Detection and monitoring for reducing false alarms in forest-fires. Computer Communications(2019)..
doi:10.1016/[Link].2019.10.007
• Cui, F. Deployment and integration of smart sensors with IoT devices detecting fire disasters in huge forest environment.
Computer Communications(2019).. doi:10.1016/[Link].2019.11.051
• Moinet, A., Darties, B., Baril, J. L.: Blockchain based trust & authentication for decentralized sensor networks (2017). arXiv preprint
arXiv:1706.01730 .
• Novo, O.: Scalable Access Management in IoT using Blockchain: A Performance Evaluation. IEEE Internet of Things Journal, Vol.
6(9), pp. 4694-4701. IEEE, (2019).DOI: 10.1109/JIOT.2018.287967
• Tosh, D., Shetty, S., Foytik, P., Kamhoua, C., Njilla, L.: CloudPoS: A proof-of-stake consensus design for Blockchain integrated
cloud. In: 2018 IEEE 11th International Conference on Cloud Computing (CLOUD), pp. 302-309. IEEE.(2018)DOI:
10.1109/CLOUD.2018.00045
• Chen, B., Tan, Z., Fang, W.: Blockchain-Based Implementation for Financial Product Management. In: 2018 28th International
Telecommunication Networks and Applications Conference (ITNAC), pp. 1-3. IEEE, (2018)doi:.10.1109/ATNAC.2018.8615246
• Liu, Q., Guan, Q., Yang, X., Zhu, H., Green, G., Yin, S.: Education-Industry Cooperative System Based on Blockchain. In: 2018 1st
IEEE International Conference on Hot Information-Centric Networking (HotICN), pp. 207-211. IEEE, (2018).DOI:
10.1109/HOTICN.2018.8606036
• Wang, X., Xu, X., Feagan, L., Huang, S., Jiao, L., Zhao, W.: Inter-Bank Payment System on Enterprise Blockchain Platform.
In: 2018 IEEE 11th International Conference on Cloud Computing (CLOUD), pp. 614-621. IEEE, (2018).DOI:
10.1109/CLOUD.2018.00085
• Ramezan, G., & Leung, C. (2018).: A Blockchain-Based Contractual Routing Protocol for the Internet of Things Using Smart
Contracts. Wireless Communications and Mobile Computing, (2018)[Link]
• Bahga, A., Madisetti, V. K.: Blockchain platform for industrial internet of things. Journal of Software Engineering and
Applications, Vol. 9(10), p. 533. (2016) [Link]
• Biswas, K., Muthukkumarasamy, V.: Securing smart cities using Blockchain technology. In: 2016 IEEE 18th international
conference on high performance computing and communications; IEEE 14th international conference on smart city; IEEE 2nd
international conference on data science and systems (HPCC/SmartCity/DSS,) pp. 1392-1393. IEEE, (2016)DOI:
10.1109/MNET.001.1900178..
• Tuli, S., Mahmud, R., Tuli, S., &Buyya, R. FogBus: A Blockchain-based Lightweight Framework for Edge and Fog Computing.
Journal of Systems and Software(2019). doi:10.1016/[Link].2019.04.050
• W. Yánez, R. Mahmud, R. Bahsoon, Y. Zhang and R. Buyya, "Data Allocation Mechanism for Internet of Things Systems with
Blockchain," in IEEE Internet of Things Journal(2020) doi:10.1109/JIOT.2020.2972776..
• Roy, D. G., Das, P., De, D., &Buyya, R.. QoS-aware secure transaction framework for internet of things using blockchain
mechanism. Journal of Network and Computer Applications, 144, 59-78(2019).doi:10.1016/[Link].2019.06.014
• R. Agrawal et al., "Continuous Security in IoT Using Blockchain," IEEE International Conference on Acoustics, Speech and Signal
Processing (ICASSP), Calgary, AB, 2018, pp. 6423-6427(2018)doi:10.1109/ICASSP.2018.8462513
• Bochem, A., Leiding, B. and Hogrefe, D.. Unchained Identities: Putting a Price on Sybil Nodes in Mobile Ad hoc Networks. In International
Conference on Security and Privacy in Communication Systems (pp. 358-374). Springer, Cham.(2018) doi:10.1007/978-3-030-01701-9_20
• Gervais, A., Karame, G. O., Wüst, K., Glykantzis, V., Ritzdorf, H., &Capkun, S.. On the Security and Performance of Proof of Work
Blockchains. Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security - CCS’16(2019).
doi:10.1145/2976749.2978341
• Hameed, S.. HMAC Modification Using New Random Key Generator.(2014) IJCCCE. 1414. 72-82.
• Yoshida, H., &Biryukov, A. Analysis of a SHA-256 Variant. Lecture Notes in Computer Science, 245–260(2006). doi:10.1007/11693383_17
• Clemence R, Ali D, Raja J, Salil S. K, “On the Activity Privacy of Blockchain for IoT”(2018). [Link]
• IttayEyal and EminG¨unSirer, “Majority is not Enough: Bitcoin Mining is Vulnerable”.
[Link] [Link]
• Satoshi N, “Bitcoin: A Peer-to-Peer Electronic Cash System”(2009). [Link] ?
• Mauro C , Senior Member, IEEE, E. Sandeep Kumar, Member, IEEE, ChhaganLal, Member, IEEE, and SushmitaRuj, Senior Member, IEEE, “A
Survey on Security and Privacy Issues of Bitcoin” [Link]
• Hammi, Mohamed Tahar&Hammi, Badis&Bellot, Patrick &Serhrouchni, Ahmed. . Bubbles of Trust: a decentralized Blockchain-based
authentication system for IoT. Computers & Security.(2018) 78. 10.1016/[Link].2018.06.004.
• K. Christidis and M. Devetsikiotis, "Blockchains and Smart Contracts for the Internet of Things," in IEEE Access, vol. 4, pp. 2292-2303,
(2016).doi: 10.1109/ACCESS.2016.2566339
• A. Dorri, S. S. Kanhere and R. Jurdak, "Towards an Optimized BlockChain for IoT," 2017 IEEE/ACM Second International Conference on
Internet-of-Things Design and Implementation (IoTDI), Pittsburgh, PA, 2017, pp. [Link]: 10.1145/3054977.3055003
• Alachkar, K. Blockchain-based Sybil Attack Mitigation : A Case Study of the I 2 P Network(2018)doi:10.1007/3-540-45748-8_24
• Datta S; Das A K; Kumar A; Khushboo; Sinha D, “Authentication and PrivacyPreservation in IoT based Forest Fire detection by using
Blockchain – A Review”, 4 th International Conference onInternet of Things and Connected Technologies (ICIoTCT), [Link]:
10.1007/978-3-030-39875-0_14
• Behera, T.M., Mohapatra, S.K., Samal, U.C., Khan, M.S., Daneshmand, M. and Gandomi, A.H. Residual energy-based cluster-head
selection in WSNs for IoT application. IEEE Internet of Things Journal, 6(3), pp.5132-5139 (2019).DOI: 10.1109/JIOT.2019.2897119
• Roy, D.G., De, D., Alam, M.M., Chattopadhyay, S., 2016, March. Multi-cloud scenario based QoS enhancing virtual resource brokering. In:
2016 3rd International Conference on Recent Advances in Information Technology (RAIT). IEEE, pp. 576–581.(2016)DOI:
10.1109/RAIT.2016.7507962
• O. Chipara, Z. He, G. Xing et al., “Real-time power-aware routing in sensor networks,” in Proceedings of the 14th IEEE International
Workshop on Quality of Service (IWQoS '06), pp. 83–92, New Haven, Conn, USA, June 2006Doi:I: 10.1109/IWQOS.2006.250454
Thank you
Questions?