Unit 4 BC

You might also like

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

Tutor: Dr Prasanna B T

Associate Professor
Dept. of CS&E
Sri Jayachamarajendra College of Engineering
JSS Science and Technology University

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
BLOCKCHAIN : UNIT 4

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Knowledge:Nick Szabo, an American computer
scientist who invented a virtual currency called
"Bit Gold" in 1998, defined smart contracts as
computerized transaction protocols that execute
terms of a contract.

Smart contracts provides transactions traceable,


transparent, and irreversible.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
What is a Smart Contract?
* A smart contract is a self-executing contract with the
terms of the agreement between buyer and seller being
directly written into lines of code.
• The code and the agreements contained distributed,
decentralized blockchain network.
• The code controls(provides) the execution, and
transactions are trackable and irreversible.
What smart contract will do:
Smart contracts allows trusted transactions and
agreements to be carried out among disparate,
anonymous parties without the need for a central
authority, legal system, or external enforcement
mechanism.
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
• Smart contracts are simply programs stored on a
blockchain that run when predetermined
conditions are met.
• * They typically are used to automate(create) the
execution of an agreement so that all participants
can get immediately outcome, without any
intermediary’s involvement or time loss.(rem: no
broker)
* They can also automate a workflow, triggering the
next action when conditions are met.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
How smart contract works:
* Smart contracts work by following simple “if/when…
then…” statements that are written into code on a
blockchain.
• A network of computers executes the actions when
predetermined conditions have been met and verified.
• These actions could include releasing funds to the
appropriate parties, registering a vehicle, sending
notifications, or issuing a ticket.
• The blockchain is then updated when the transaction is
completed. That means the transaction cannot be
changed, and only parties who have been granted
permission can see the results.
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
* Within a smart contract, there can be as many
stipulations(condition required for agreement) as
needed to satisfy the participants .
* To establish the terms, participants must
determine how transactions and their data are
represented on the blockchain, agree on the
“if/when...then…” rules that govern those
transactions, explore all possible exceptions, and
define a framework for resolving
disputes(disagreement).
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Smart Contracts

• Executable code
• Turing Complete
• Function like an external account
 Hold funds
 Can interact with other accounts and smart contracts
 Contain code
• Can be called through transactions

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Code Execution
• Every node contains a etherium virtual machine

 Compiles code from high-level language(programming lang.) to


bytecode
 Executes smart contract code and broadcasts state
• Every full-node on the blockchain processes
every transaction and stores the entire state

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Benefits of smart contracts / needs of SC
Speed, efficiency and accuracy
Once a condition is met, the contract is executed
immediately. Because smart contracts are digital and
automated, there’s no paperwork to process and no time
spent on checking errors that often result from manually
filling in documents(rem: ledger).

Trust and transparency


Because there’s no third party involved, and because
encrypted records of transactions are shared across
participants, there’s no doubt whether information has
been altered for personal benefit.
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Security

Blockchain transaction records are encrypted, which


makes them very hard to hack. Moreover, because each
record is connected to the previous and subsequent
records on a distributed ledger, hackers would have to
alter the entire chain to change a single record.

Savings
Smart contracts remove the intermediaries to handle
transactions , their associated time delays and fees.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Some of the key properties of a smart contract are:

1. Autonomy
2. Decentralisation
3. Auto sufficiency(rem:auto)

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
pragma solidity ^0.4.19;

contract Escrow {

address public buyer;


address public seller;
address public arbiter;
address owner;

function Escrow(address _seller, address _arbiter) {


buyer = msg.sender;
seller = _seller;
arbiter = _arbiter;
}

modifier onlyOwner {
require(msg.sender == owner);
_;
}

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
function payoutToSeller() {
if(msg.sender == buyer || msg.sender == arbiter) {
seller.transfer(this.balance);
}
}

function refundToBuyer() {
if(msg.sender == seller || msg.sender == arbiter) {
buyer.transfer(this.balance);
}
}

function getBalance() constant returns (uint) {


return this.balance;
}
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
function setSeller(address _seller) public {
seller = _seller;
}

function setArbiter(address _arbiter) public {


arbiter = _arbiter;
}

function getSeller() public constant returns (address) {


return seller;
}

function getArbiter() public constant returns (address) {


return arbiter;
}

}
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Uses of smart contracts

Insurance

Due to a lack of automated administration, it can take


months for an insurance claim to be processed and paid.
This is as problematic for insurance companies and
customers, leading to admin costs, inefficiency. Smart
contracts can simplify the process by automatically
triggering a claim when certain events occur. For example,
if you are travelling and your car mets with an accident,
the smart contract would recognise this and begin the
claim . Specific details such as extent of damage, car
number, location where the incident occurred could be
recorded on the blockchain in order to determine exact
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Supply chain management
Supply chain management involves the flow of goods .
Smart contracts can record ownership rights as items move
through the supply chain, confirming who is responsible for
the product at any given time

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Protecting copyrighted content(rem: prime, Netflix)
Smart contracts can ensure that royalties go to the
intended recipients by recording ownership rights in a
decentralised blockchain system. A blockchain would keep
track of all ownership rights.
Digital Identity
Smart contracts can allow individuals to own and control
their digital identity containing data, reputation and
digital assets(rem:locker money).
Financial Data Recording
Financial organisations can utilise smart contracts for
accurate(rem: correct money), transparent financial data
Ref: https://site.ieee.org/sb-ritb/why-are-smart-contracts-so-important/
recording. Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Life Cycle Of Smart Contract Development

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Knowledge:
Imperative Steps while developing Smart Contracts
Understand the use-case of smart contract.

Create a basic architecture of smart contracts interaction or


flowchart how functions will interact with each other.

Start development using any IDE or development tools like


Truffle, remix with proper documentation of each and every
function.

Once the development is completed start testing smart


contracts on test-net or private blockchain.(this is called a
manual testing).
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
• 10 most used ethereum development tools
Understanding:
1. Truffle:Truffle is a command-line development tool that offers a complete ecosystem for
developing and testing Ethereum-based applications
2. Remix IDE:Remix IDE is an open-source, JavaScript-based debugging and compiling tool that is
primarily used for writing Solidity contracts
3. MetaMask:MetaMask is a browser-based tool designed for Ethereum. In essence, it is a wallet
that functions as a browser extension
4. Solidity:Solidity is the primary programming language used to write smart contracts on the
Ethereum framework.
5. Ganache:Ganache is an Ethereum development tool that is a part of the Truffle Suite. It is
basically a personal Blockchain framework for Ethereum development that can be used to
deploy contracts, develop DApps, and run tests
6. Mist :Mist is an end-user interface for Ethereum. It is explicitly designed for non-technical
users and is not only a user-friendly browser interface but also a wallet
7. Geth:Geth is an implementation of an Ethereum node written in the Go programming
language. It also functions as a multi-purpose command-line tool that is imbued with similar
functionalities as Mist
8. DAppBoard:DAppBoard is an analytics platform for smart contracts to keep visual transactions
of all the operatios
9. Ether.js:Ether.js is a complete library ecosystem for Ethereum
10. Infura:Infura is an Infrastructure-as-a-Service offered by Consensys. It consists of a suite of
tools to connect your decentralized apps to the Ethereum network while also ensuring the
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
maximum scalability of your DApps CE,to meet user demand
Mysuru
Knowledge:
Record all the transaction while testing on test-net, analyze
results of all transactions with actual use case

Unit testing will be the next step in smart contract


development life cycle, there are multiple frameworks for unit
and integration testing that can be use to test smart contract.
Example : Truffle framework.

Once unit testing is done using truffle framework on ganache,


smart contract author should go for 3rd party Audit of smart
contract.

Last but not the least, bug bounty programs are also very
efficient to secure smart contracts..
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Understand the Use case :
Use case of smart contracts should be clear before
development is started, the developer should gather all

the information of smart contracts like business logic, also


all the 3rd party libraries that developer will use while
developing a smart contract.

Architecture design of the smart contract


A basic architecture contains the business logic of a smart
contract. Architecture design in the initial phase helps
developers to follow the exact path during the
development phase.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Development phase
Start development using any IDE or development tools like
Truffle, remix with proper documentation of each and every
function.

Available IDE: Remix Ethereum

Manual Testing
In this phase smart contract should be tested on test-net
(Rinkeby/Ropsten), all the transaction and state changes
should be recorded to verify that the smart contract’s
behavior is the same as that intend to be.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Unit testing
Unit testing can be done using the truffle framework,
developer should write test cases for all the functions of the
smart contract, test cases should reflect correct the
business logic of the smart contract.

3rd party security audit


All the Smart contracts ready for production should be
audited before deploying on the main net because even
though the business logic of smart contracts is tested on the
test-net several times, a smart contract cannot be declared as
secured or bug-free contract, a smart contract may contain
some logical errors that can be identified by the audit
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Bug Bounty
Bug bounty programs are very useful in identifying bugs in
a smart contract, as your smart contract will come under
the eye of multiple experienced auditors or developers to
find the loopholes in the smart contract, even after two
successful 3rd party audits.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Code Coverage
code coverage is a special tool that evaluates how
efficient your test cases, Solidity-coverage is used to
know how much your test cases are penetrating your
smart contract functions. by this, you will be able to know
that your unit test cases have touched all of your
functions and business logic is validated in unit testing

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Up-gradable Smart contracts are also considered as a part
of a life cycle of smart contract, you can check out about
up-gradable smart contracts in the below link.

Ref: https://medium.com/quillhash/life-cycle-of-smart-contract-development-8929fa073b7f

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Some security and visualizing tools :
Visualization:
Solidity Visual Developer ,Surya , Solgraph, Evm-Labs,
Ethereum-graph-debuger

Static and Dynamic Analysis:


Slither, Mythril, Oyente, Securify, Smartcheck

Test Coverage:
Solidity-coverage
Linters and Formatters:
Linters improve the code quality.
Solcheck, Solhint, Solium, Prettier Solidity Plugin
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
B. Smart Contract Programming

• Solidity (javascript based), most popular


 Not yet as functional as other, more mature, programming
languages
• Serpent (python based)
• LLL (lisp based)

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
B. Smart Contract Programming
Solidity
Solidity is a language similar to JavaScript which allows you to develop
contracts and compile to EVM bytecode. It is currently the flagship language
of Ethereum and the most popular.
• Solidity Documentation - Solidity is the flagship Ethereum high level
language that is used to write contracts.
• Solidity online realtime compiler

Serpent
Serpent is a language similar to Python which can be used to develop
contracts and compile to EVM bytecode. It is intended to be maximally clean
and simple, combining many of the efficiency benefits of a low-level language
with ease-of-use in programming style, and at the same time adding special
domain-specific features for contract programming. Serpent is compiled
using LLL.
• Serpent EVM compiler
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
B. Smart Contract Programming

Atom Ethereum interface - Plugin for the Atom editor that features
syntax highlighting, compilation and a runtime environment (requires
backend node).
Atom Solidity Linter - Plugin for the Atom editor that provides Solidity
linting.

Vim Solidity - Plugin for the Vim editor providing syntax highlighting.
Vim Syntastic - Plugin for the Vim editor providing compile checking.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
B. Smart Contract Programming: Solidity

contract Example {

uint value;

function setValue(uint pValue) {


value = pValue;
}

function getValue() returns (uint) {


return value;
}

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
B. Smart Contract Programming: Solidity

var logIncrement =
OtherExample.LogIncrement({sender: userAddress,
uint value});

logIncrement.watch(function(err, result) {
// do something with result
})

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow
Testing Cycle

Interact &
Create Fund Develop Compile Sign &
Account Account Deploy Test

● Onboard Additional Users


● Create New Accounts
● Develop New Applications

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: Create Account

Interact &
Create Fund Develop Compile Sign &
Account Account Deploy Test

• Programmatically: Go, Python, C++, JavaScript, Haskell


• Tools
 MyEtherWallet.com
 MetaMask
 TestRPC
 Many other websites

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: Fund Account

Interact &
Create Fund Develop Compile Sign &
Account Account Deploy Test

• From friends
• Faucet
• Exchanges (for public blockchain)

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: Develop

Interact &
Create Fund Develop Compile Sign &
Account Account Deploy Test

• Ethereum Application Components:


 Base application: can be developed in any language
 Smart contract: developed in Solidity or one of the other
contract compatible languages
 Connector library: facilitates communication between base
application and smart contracts (Metamask)

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: Sign and Deploy

Interact &
Create Fund Develop Compile Sign &
Account Account Deploy Test

Sign Deploy Live Smart


Transaction Signed tx
Contract

Bytecodes Connector*

*Library that facilitates communication and connection with Blockchain;


Connects your code to a running node.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: TestRPC

Interact
Create Fund Develop Compile Sign &
Account Account Deploy &
Test

TestRPC/TestChain
• Local development or Test Blockchain
• https://github.com/ethereumjs/testrpc

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
C. Development Workflow: TestRPC
• EthereumJS TestRPC: https://github.com/ethereumjs/testrpc is
suited for development and testing
• It's a complete blockchain-in-memory that runs only on your
development machine
• It processes transactions instantly instead of waiting for the
default block time – so you can test that your code works quickly
– and it tells you immediately when your smart contracts run into
errors
• It also makes a great client for automated testing
• Truffle knows how to use its special features to speed up test
runtime by almost 90%.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
What Is a Hard Fork?
A hard fork (or hardfork), as it relates to blockchain technology, is a
radical change to a network's protocol that makes previously invalid
blocks and transactions valid, or vice-versa. A hard fork requires all
nodes or users to upgrade to the latest version of the protocol software .

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
A hard fork refers to a radical change to the protocol of a
blockchain network that effectively results in two
branches, one that follows the previous protocol and one
that follows the new version.

In a hard fork, holders of tokens in the original blockchain


will be granted tokens in the new fork as well, but miners
must choose which blockchain to continue verifying.
A hard fork can occur in any blockchain, and not only
Bitcoin (where hard forks have created Bitcoin Cash and
Bitcoin SV, among several others, for example).
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Understanding a Hard Fork
A hard fork is when nodes of the newest version of a
blockchain no longer accept the older version(s) of the
blockchain; which creates a permanent divergence from the
previous version of the blockchain.
Adding a new rule to the code essentially creates a fork in the
blockchain: one path follows the new, upgraded blockchain,
and the other path continues along the old path. Generally,
after a short time, those on the old chain will realize that their
version of the blockchain is outdated or irrelevant and quickly
upgrade to the latest version.

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Reasons For a Hard Fork?
There are a number of reasons why developers may
implement a hard fork, such as correcting important
security risks found in older versions of the software, to
add new functionality, or to reverse transactions—such as
when the Ethereum blockchain created a hard fork to
reverse the hack on the
Decentralized Autonomous Organization (DAO).

Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ


CE, Mysuru
Hard Forks vs. Soft Forks

Hard forks and soft forks are essentially the same in the sense that
when a cryptocurrency platform's existing code is changed, an old
version remains on the network while the new version is created.
With a soft fork, only one blockchain will remain valid as users adopt
the update. Whereas with a hard fork, both the old and new
blockchains exist side by side, which means that the software must be
updated to work by the new rules. Both forks create a split, but a hard
fork creates two blockchains and a soft fork is meant to result in one.
Considering the differences in security between hard and soft forks,
almost all users and developers call for a hard fork, even when a soft
fork seems like it could do the job. Overhauling the blocks in a
blockchain requires a tremendous amount of computing power, but
the privacy gained from a hard fork makes more sense than using a
soft fork.
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru
Dr Prasanna B T, Assoc Prof, Dept of CSE, SJ
CE, Mysuru

You might also like