[go: up one dir, main page]

0% found this document useful (0 votes)
29 views35 pages

Smart Contracts and DApps in Blockchain

The document discusses second-generation applications of blockchain technology, focusing on smart contracts, tokens, decentralized applications (dApps), decentralized autonomous organizations (DAOs), and Hyperledger Fabric. It outlines how smart contracts automate agreements on the blockchain, the types and standards of tokens, and the characteristics of dApps that operate without central control. Additionally, it describes the architecture of Hyperledger Fabric and its components, emphasizing the collaborative approach to blockchain development.

Uploaded by

Rakshith Ah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views35 pages

Smart Contracts and DApps in Blockchain

The document discusses second-generation applications of blockchain technology, focusing on smart contracts, tokens, decentralized applications (dApps), decentralized autonomous organizations (DAOs), and Hyperledger Fabric. It outlines how smart contracts automate agreements on the blockchain, the types and standards of tokens, and the characteristics of dApps that operate without central control. Additionally, it describes the architecture of Hyperledger Fabric and its components, emphasizing the collaborative approach to blockchain development.

Uploaded by

Rakshith Ah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit IV

Second Generation Applications of Blockchain


Technology
Smart Contracts:
• They are logic or code that operate on block chain
• They are part of the block chain in Ethereum
• They are immutable, it should be carefully coded
• Solidity is popular to code smart contract, EVM (Ethereum virtual
machine) is the computer that runs the code.
• EVM is the engine of ethereum. Gas is the fuel for the engine.
• In 1994, Nick Szabo, a legal scholar, and cryptographer realized that the
decentralized ledger could be used for smart contracts, otherwise called
as self-executing contracts, blockchain contracts, or digital contracts.
• In this format, contracts could be converted to computer code, stored
and replicated on the system and supervised by the network of
computers that run the blockchain.
• This would also result in ledger feedback such as transferring money and
receiving the product or service.
• As Vitalik Buterin, founder of ethereum, says that, in a smart contract
approach, an asset or currency is transferred into a program and the
program runs this code.
• And at some point it automatically validates a condition and it
automatically determines whether the asset should go to one person or
back to the other person, or whether it should be immediately refunded
to the person who sent it or some combination.
• Smart contracts are lines of code that are stored on a blockchain and
automatically execute when predetermined terms and conditions are met.
• The benefits of smart contracts are most apparent in business
collaborations, in which
• They are typically used to enforce some type of agreement so that all
participants can be certain of the outcome without an intermediary’s
involvement.
How Do They work:
• 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 (releasing funds to the
appropriate parties; registering a vehicle; sending notifications;
issuing a ticket) when predetermined conditions have been met and
verified.
• The blockchain is then updated when the transaction is completed.
Creating or Deploying of Smart
Contracts: (Life Cycle of Smart Contracts)
• The phases involved in smart contract development and execution:

• [Link]: Defining and Drafting the Smart Contract

• [Link]: Deploying the Smart Contract on the Blockchain

• [Link]: Triggering and Performing Contract Actions

• [Link]: Updating the State and Validating Transactions


• Create:
• First, developers create the smart contract by writing down the rules
and conditions of the agreement. They specify when certain actions
should happen. For example, in a rental contract, they’d define when
rent is due and what happens if it’s late.
• They write these rules in code, like a computer program, to make the
contract smart and able to work on its own.
• Freeze:
• Once the contract is written, it’s frozen or set in place by putting it
onto the blockchain model. This ensures that nobody can change the
rules later. It’s like sealing it in a tamper-proof box.
• By putting it on the blockchain, everyone involved can see it, and it’s
safe from tampering.
• Execute:
• Now that the contract is on the blockchain, it can start doing its job.
When the conditions written in the code are met, the contract
automatically carries out its actions. For example, if it’s a contract for
buying goods, when the buyer sends the payment, the contract
releases the goods.
• This happens without anyone needing to do anything manually. It’s all
automatic and happens exactly as the contract says.
• Finalize:
• As the contract does its job, it updates its status on the blockchain.
This includes keeping track of payments made or goods delivered.
• Validators on the blockchain make sure everything is correct and fair.
They solve any problems that come up and make sure everyone
involved knows what’s going on.
Tokens:
• Tokens are a key primitive used in blockchain applications. Tokens can
be used to represent anything.
• For example, they can represent a store of value, digital or real-world
asset such as property, art, goods on a supply chain and securities.
• There are no limits to what can be represented as a token. Tokens are
considered to be a key primitive of web3, in a similar vein to how the
website was a key primitive of the original web.
• Tokens can be divided into two general categories, fungible and non-
fungible.
• Fungible Tokens
• Fungible tokens are tokens that are non-unique, one token of the
same type is interchangeable with another token of the same type.
• For instance, money is a type of fungible asset. You can change one
dollar bill for another and they both have the same utility.
• Fungible token properties include a name, short name or symbol, a
total supply and decimal precision supported by it.
• Non-fungible tokens (NFTs)
• Non-fungible tokens (NFTs) are tokens where every token is unique.
• One token may be similar to another, but they have properties that
ensure no two tokens can ever be the same.
• Dollar bills have a serial number on them which can uniquely identify
them, although in terms of utility very few people care about serial
numbers on them.
Token Standards:
• A set of regulations and technical specifications known as token standards
delineate the behavior of tokens on a given blockchain.
• By ensuring that tokens conform to a particular format, these standards
facilitate their interoperability with a multitude of services and applications
within the ecosystem, including wallets and exchanges.
• The implementation of this standard is vital in order to facilitate the smooth
transfer and use of tokens across various platforms and applications.
• Ethereum, being a leader in the field of blockchain technology, has taken the
initiative to establish token standards.
• Among these, the ERC-20 standard is the most well-known; it established the
foundation for fungible tokens, which are interchangeable and identical.
• The ICO (Initial Coin Offerings) surge was significantly aided by ERC-20
tokens, which facilitate the creation of new cryptocurrencies.
• ERC-721 is an additional crucial Ethereum standard that serves as the
foundation for Non-Fungible Tokens (NFTs).
• In contrast to ERC-20 tokens, ERC-721 tokens are one-of-a-kind and non-
exchangeable, rendering them ideal for signifying possession of
distinctive digital objects such as artwork or collectibles.
• Additional Ethereum standards, such as ERC-777, ERC-1155, and ERC-
4626, have surfaced, providing enhanced functionality and adaptability.
• To facilitate the creation and administration of tokens within their
respective ecosystems, additional blockchains have developed their own
token standards.
• BEP-20 and BEP-721, which are functionally comparable to Ethereum’s
ERC-20 and ERC-721 but optimized for Binance’s architecture, are
implemented on the Binance Smart Chain.
• Additionally, Polkadot, Cosmos, and other more recent blockchains have
implemented their own distinct token standards, thereby enhancing the
crypto world’s diversity and capabilities.
• The principal drawback associated with token standards within the
cryptocurrency sector is their inability to function in tandem.
• Tokens generated in accordance with disparate standards generally
lack interoperability on account of the unique regulations and
protocols governing each blockchain.
Decentralized Applications
(Dapps):
• Decentralized applications or dApps are distributed, decentralized open-
source software applications that run on a decentralized peer-to-peer
network.
• Imagine the Twitter application that you have on your phone. You can post
anything you want on Twitter but ultimately it’s controlled by a single
company that can delete your tweets if they violate community guidelines
or some other reason.
• But if there was a Twitter-type dApp, then it would be decentralized and
not owned by any one person.
• If you posted something there, nobody would be able to delete it
including its creators.
• Multiple people can create content and consume content on these
applications that is free of any control and interference from one person.
• Below are some of the requirements of dApps:
• Open Source: dApps should be open source and its codebase should
be freely available for all. Any changes in the structure or working of
the app should only be taken with the agreement of the majority.
• Decentralized: dApps should be decentralized with all the information
and operations stored on a public and decentralized Blockchain which
would ensure security and transparency.
• Incentive: dApps should offer some sort of incentive to their users in
the form of cryptographic tokens. These are a sort of liquid assets and
they provide incentives for users to support the Blockchain dApp
ecosystem.
• Protocol: dApps should have a particular protocol to demonstrate
proof of value. This means showing the value of a particular process
in a way that can be easily verified by others.
How Do dApps Work?
• A dApp has a backend code running on a decentralized peer-to-peer network.
• It can also have a frontend code and a user interface that can be written in
any language just as it is done for normal applications.
• The front end can be hosted on any decentralized server like IPFS.
• The dApp working has the following features:
• Decentralized: A dApp operates on Ethereum which is an open public
decentralized platform.
• Deterministic: dApps perform the same function irrespective of the
environment in which they are executed.
• Turing complete: dApps can perform any action given the required resources.
• Isolated: dApps are executed in an Ethereum Virtual Machine which is a
virtual environment that ensures that even if there is a bug in the smart
contract, it won’t hamper the normal functioning of the blockchain network.
Most Common Platforms For
Creating dApps:
• There are many Blockchain platforms created by various companies.
While the most popular and commonly heard one is Bitcoin, there are
many others that are used to create dApps.
• These Blockchain platforms are further used as a base to create dApps.
• So let’s see some of these now:
• Ethereum: Ethereum is the most popular decentralized, open-source
blockchain in the world currently.
• It is used as the base for many Blockchain projects, including more than
2500 dApps.
• In fact, Ethereum is only second to Bitcoin in terms of its market value.
• Ethereum also has a native cryptocurrency known as BTH which is their
version of Bitcoin. All in all, this is an excellent option for creating a dApp
although it’s a bit expensive.
• NEO: NEO is also a decentralized, open-source blockchain that aims to
create a smart economy.
• It is also called the Chinese Ethereum and provides better options for
scalability in dApps as compared to other Blockchain platforms.
• NEO is currently less popular than Ethereum with only about 100
dApps built using this technology. It’s also quite expensive and can
even charge higher fees than Ethereum in some cases.
• TRON: TRON is a comparatively new Blockchain platform as compared
to Ethereum or NEO.
• However, it’s quite popular and may even become a competitor to
Ethereum in the future.
• TRON is particularly famous for gaming applications and gambling
sites. There are around 1500 dApps created using this platform which
makes it an excellent choice.
Popular dApps:
• Below are some of the popular dApps:
• CryptoKitties: CryptoKitties is a fun app that is used for entertainment.
• You can buy kitties on the app using cryptocurrency and then breed and
sell them at a profit.
• Apparently, cute cats are popular everywhere, even on Blockchain
because CryptoKitties was once responsible for 10% of Ethereum
transactions daily.
• OpenSea: Open Sea is a dApp that encourages interaction between
various games based on Blockchain.
• Gamers can exchange their collectibles from any cryptocurrency-based
game on OpenSea.
• Currently, this dApp supports collectibles from Ethereum only, but they
plan to expand in the future.
• WINk: WINk is the most popular dApp for gambling-based games in
the market.
• It included everything from poker to dice games to sports betting.
• WINk is based on the TRON platform and the winners in betting get
WIN tokens which can then be exchanged for BTT which is a sort of
cryptocurrency like Bitcoin.
• IPSE: IPSE or InterPlanetary Search Engine is actually a search engine
with Blockchain as its base.
• IPSE is based on the EOS blockchain and it uses the InterPlanetary File
System (IPFS) which is an improvement over HTTP on the internet.
• A big advantage of IPSE over other conventional search engines is that
it guarantees security and privacy on the internet.
• Blockchain Cuties: For anyone interested in CryptoKitties, Blockchain
Cuties is a great option.
• It allows you to focus on multiple cue animals like puppies, bear cubs,
lizards, etc. apart from kitties.
• Blockchain Cuties is a dApp that is accessible using multiple
Blockchain platforms like Ethereum, NEO, TRON, etc., unlike
CryptoKitties which favors Ethereum only.
How are dApps Constructed?
• Steps to Build a dApp:
• [Link] the Use Case:
Identify the problem your dApp aims to solve and outline its features and functionalities.
• 2. Choose a Blockchain Platform.
Select a blockchain that aligns with your dApp's requirements, considering factors like
transaction fees, scalability, and community support.
• 3. Develop Smart Contracts.
Write smart contracts using languages like Solidity (for Ethereum) or Rust (for Solana).
Ensure thorough testing and auditing to prevent vulnerabilities.
• 4. Build the Frontend
Create a user-friendly interface using web development frameworks. Integrate with the
blockchain using libraries like [Link] or [Link].
• 5. Integrate Wallets
Enable users to connect their wallets for authentication and transaction signing.
• 6. Deploy and Test
Deploy your smart contracts on a testnet to simulate real-world conditions. After thorough
Decentralized Autonomous Organization (DAO):
• A decentralized autonomous organization (DAO) is an organizational
structure with no central governing body.
• DAOs, which were popularized by blockchain enthusiasts have members
who share a common goal of acting in the community's best interests.
• They are completely transparent and make decisions using a bottom-up
management approach.
• How DAOs Work?
• Digital currencies are decentralized, so they aren't controlled by one
institution like a government or central bank.
• Rather, they are divided among different computers, networks, and
nodes.
• This inspired a group of developers, and they developed the idea for a
decentralized autonomous organization in 2016.
• DAOs rely on smart contracts to function. These scripts generally
automate the group's decisions when the required number of votes is
reached.
• If a proposal fails, the smart contract doesn't execute anything.
• Users with a larger investment in the DAO are incentivized to act in
good faith.
• For instance, imagine that a DAO member owns a majority of the
organization's voting power or a majority of the tokens.
• This user could act in bad faith; however, if the DAO is programmed to
penalize bad actors, the user will jeopardize the value of their
holdings.
Hyperledger Fabric:
• The Linux Foundation founded the Hyperledger project in 2015 to
advance cross-industry blockchain technologies.
• Rather than declaring a single blockchain standard, it encourages a
collaborative approach to developing blockchain technologies via a
community process, with intellectual property rights that encourage
open development and the adoption of key standards over time.
• Hyperledger Fabric is one of the blockchain projects within
Hyperledger.
• Like other blockchain technologies, it has a ledger, uses smart
contracts, and is a system by which participants manage their
transactions.
• The members of a Hyperledger Fabric network enroll through a
trusted Membership Service Provider (MSP).
Hyperledger Fabric Architecture & core
components:
• The key components of HF that make up its architecture are as
follows:
• Peer Nodes: Peer nodes serve as the primary execution environment
within a Fabric network.
• They maintain a copy of the distributed ledger and execute smart
contracts, known as chain codes.
• Each organization participating in the network typically has one or
more peer nodes.
• They are distributed across the network and communicate with other
peers to maintain consensus and ensure the validity of transactions.
• Types of Peer Nodes:
1. Endorsing Peers: These peer nodes receive transaction proposals
from clients, execute the chaincode, and return the results along
with an endorsement signature.
-Endorsing peers don't update the ledger but are critical for achieving
transaction endorsement.
2. Committing Peers: After receiving the endorsed transaction,
committing peers validate the endorsements and ensure the
correctness of the endorsements before committing the transaction to
the ledger.
3. Anchor Peers: These are specific peers in each organization that
maintain communication with peers in other organizations. They are
used to facilitate cross-organization communication.
• Membership Services Provider (MSP): The MSP manages the identity and
access control policies for participants in the network.
• It authenticates and authorizes network participants, ensuring secure
interactions.
• MSPs enable flexible membership models, such as Certificate Authorities (CAs)
and token-based systems.
• It is a module within HF that manages identities, certificates, and
cryptographic materials for the participants of the network.
• It is responsible for authenticating and authorizing network entities, such as
peers, orderers, clients, and administrators.
• Orderer Nodes: Orderer nodes manage the consensus mechanism in
Hyperledger Fabric.
• Orderer nodes are responsible for receiving transaction proposals from
clients, ordering them into blocks, and delivering the blocks to the endorsing
and committing peers in the network.
• They establish a consistent transaction order and ensure that all peers have a
synchronized view of the blockchain ledger.
• Types of Orderer Nodes:
• Solo Orderer: In a development or testing environment, a solo
orderer node can be used. It operates as a single, centralized entity
responsible for ordering transactions.
• Kafka-based Orderer: In a production environment, a Kafka-based
orderer service is commonly used. It employs Apache Kafka, a
distributed streaming platform, to achieve fault tolerance and
scalability.
• Raft-based Orderer: Hyperledger Fabric 2.0 introduced a new
consensus mechanism called Raft. Raft-based orderer nodes utilize
the Raft consensus algorithm to achieve fault tolerance and
consensus.
• Channels:
• Channels in Fabric allow for the segregation of network participants into
smaller, private subgroups.
• Each channel has its own ledger and chaincode, ensuring data confidentiality.
• Channels enable secure collaboration between specific parties without
revealing information to the entire network.
• Channels in Hyperledger Fabric are private subnetworks within a larger
blockchain network.
• They allow a subset of network participants to create a separate
communication and transaction layer, ensuring privacy and confidentiality for
the transactions and data shared within the channel.
• They are particularly useful in scenarios where multiple organizations need to
collaborate while maintaining data privacy.
• They are commonly used in supply chain management, financial networks,
and any other scenario that requires selective data sharing among
participants.
• Chaincode:
• Chaincode (smart contracts) is the executable code that defines the
rules and logic for the interactions and transactions within the
Hyperledger Fabric network.
• It encapsulates the business logic of an application and determines
how transactions are processed and the state of the ledger is
updated.
• It can be written in various programming languages, such as Go,
JavaScript, and Java.
• And chaincode runs in a distributed manner across multiple endorsing
peers in the network.
• Each endorsing peer executes the chaincode independently and
validates the transactions based on the defined logic.
• Lifecycle Management:
• It has a lifecycle that involves installation, approval, and activation.
• This ensures that chaincode is properly deployed, reviewed, and endorsed by the relevant
network participants before being executed.
• This crucial components of Fabric defines the rules for transaction processing, data
validation, and state updates, enabling secure and decentralized execution of business
processes.
• Ledger:
• The distributed ledger in Fabric maintains a tamper-proof record of all transactions
executed within the network. It is implemented as a combination of the world state and
the transaction log. The world state represents the latest values of assets, while the
transaction log ensures immutability and transparency.
• Types of Ledger:
• Hyperledger Fabric maintains mainly two types of ledgers that are as follows:
• ‍Transaction Log (Blockchain): This ledger contains an immutable, ordered log of all the
transactional data, organized into blocks. Each block contains a batch of transactions.‍
• World State: The world state ledger maintains the current state of the blockchain network
after applying all the transactions. It represents the most recent state of all assets and data
within the network.
Working on Hyperledger & Transaction
Processing:
• Hyperledger Fabric transaction processing involves a multi-stage
process where a client application initiates a transaction proposal,
which is then endorsed by peers (nodes) based on the endorsement
policy and smart contracts (chaincode).
• These endorsed proposals are then sent to the ordering service for
ordering and validation, ultimately resulting in a block of transactions
being added to the channel's ledger.
• Here is a detailed breakdown of the process:
• 1. Transaction Proposal:
• A client application initiates a transaction proposal, which specifies the
transaction data and the chaincode to be executed.
• This proposal is sent to peers belonging to the organizations specified
by the smart contract's endorsement policy.
• 2. Endorsement:
• Peers verify the client's identity and authority.
• They then simulate the transaction proposal and, if valid, generate a
signed endorsement response (YES/NO) based on the endorsement
policy and the transaction's outcome.
• 3. Transaction Formation:
• The application collects the endorsements and combines them with
the transaction proposal to form a fully endorsed transaction.
• 4. Ordering:
• The application forwards the endorsed transaction to the ordering
service, which is responsible for ordering the transactions into blocks.
• The chosen consensus mechanism (e.g., PBFT, Raft) ensures a
consistent order of transactions across the network.
• 5. Validation:
• The ordering service provides blocks of transactions to all peers on
the channel.
• Each peer validates the transactions within the block using the
validation policy and a concurrency control version check.
• Transactions that fail validation are marked as invalid, and only valid
transactions update the state database.
• 6. Commitment:
• Valid transactions are committed to the channel's ledger, updating the
state database with the modified key/value pairs.
• An event is emitted by each peer to notify the client application of the
transaction's outcome (valid or invalid).

You might also like