Hyper Ledger Fabric
Hyper Ledger Fabric
Dr. S. Joshi
⚫Hyperledger Fabric :
⚫Hyperledger project is a new platform specially suited for
enterprise application.
⚫It is one of the fastest growing open source project under
Linux foundation.
Dr. S. Joshi
⚫Hyperledger Fabric :
It involves 220 leading enterprise companies.The logos of all various
companies that are participate. Its includes large technological firms,
large financial companies, banks, healthcare companies and industries
specific companies who have joined these consortium to help promote
its mission in building enterprise grade blockchain platform.
⚫ Hyperledger Fabric :
Hyperledger fabrics production grade was released in July 2017.
It has large number of organization contribution to it.
IBM is one of the largest contributor to it both in terms of code as
well as IP. There are many other projects that are also the part of this
Hyperledger consortium.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ One of them is Hyperledger composer.This is gives you the set of
higher level abstraction for business users to come and develop
application on top of blockchain.
⚫ So instead of being having to work with a details of blockchain
platform, work at higher level of abstraction where you can talk
about business concepts directly and encode that to run on
blockchain platform.
⚫ It has many interesting features that you can model your business
network.
⚫ You can talk about in terms of asset participants and transaction
rather than having to talk about low level variables &functions &
so on. And it is all exposed via APIsand can automatically generate
some of theseAPIs as well.
Dr. S. Joshi
⚫Hyperledger Fabric :
⚫It also gives you the ability to integrate with the existing
systems of records.
⚫So blockchain in many of this enterprise application be just one
part of large application.
⚫So it might involve other database where you are storing data,
bring that on blockchain.
⚫Whatever is going to record on blockchain might want to
extract that out, your own analytics and visualization.
⚫So some of these capabilities are also pert of composer which is
fully open source, open governed.
⚫To see Hyperledger fabrics in deep, understand first some
of the actors in blockchain. Dr. S. Joshi
⚫Hyperledger Fabric :
⚫ Some of the key concepts such as how the transaction is get
committed by all the peers in the blockchain network in a
consistent manner and everyone to be committing same
transaction in same order.
⚫ Hyperledger FabricArchitecture :
⚫ The key components of the Hyperledger FabricArchitecture
Dr. S. Joshi
⚫Hyperledger FabricArchitecture :
⚫Hyperledger FabricArchitecture :
⚫So from notion of access control, certain users are allowed to
perform certain transactions and if you do not have right access
then your transaction get rejected.
⚫So your certificate is having all that information about what
privilege you can have and what attributes you have and so on
and use that to transact on the blockchain.
⚫This certificate is comes from traditional certificate party.
⚫Fabric implements a certificate authority i.e. Fabric CAand
this certificate authority is optional.
⚫ It issues the certificate with public key belongs to it having
Dr. S. Joshi
⚫Hyperledger Fabric :
⚫Consensus is achieved using following transaction flow:
⚫1) Client application submit the transaction to few peers and
this few peers execute the transaction and agree that the
output is same across all of them.
⚫So find the output of transaction and will add their sign to it.
⚫So that is the part of endorsement So Client Application has
to collect endorsement from multiple peers in the network to
Dr. S. Joshi
⚫Hyperledger Fabric :
Consensus is achieved using following transaction flow:
say that this transaction is the valid transaction and all outputs
are the same.
⚫Once you collect the sufficient signature then you can submit
the transactions for ordering. Sonow multiple applications i.e.
multiple users might be submitting these transactions to
ordering and now the ordering service will be ensure that all
of them are fully ordered and totally ordered across all the
nodes.
⚫So that is the part of ordering service. So you have to order
the transactions and once you determine the order of all
transactions then do resolution of validation.
Dr. S. Joshi
⚫Hyperledger Fabric :
Consensus is achieved using following transaction flow:
⚫ Example: For instance according to double spending problem
should not perform two transactions simultaneously which
modify the same state.
⚫ That is the validation. Suppose for any state I have state variable
Dr. S. Joshi
⚫Hyperledger Fabric :
⚫Consensus is achieved using following transaction flow:
Step 2) Execute ProposedTransaction:
Dr. S. Joshi
⚫Hyperledger Fabric :
⚫Consensus is achieved using following transaction flow:
Step 3) Proposal Response:The endorsers to communicate back the
fact that this is the output along with sign so the ClientApplication
or SDK is now going to receive this read write set and all of this is
happening in parallel i.e. synchronous.
Example: For instance E0 and E1 respond 1st whereas E2 takes little
bit more time to come back with the response. So all of these are
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
Step 3) Proposal Response: happening synchronously so the information
that how many endorsements who signed this endorsement whether these
were valid endorsement will be checked later in the consensus.
Sothis is the 3rd step where the client receives the response from all the
endorsers that has communicated.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
Step 4) Ordering : Soonce the client has received some sufficient
endorsements that satisfies the endorsement policy with sign on responses,
it is going to submit this transaction to ordering service. Now such
submission to ordering service can be happening from multiple user,
multiple ClientApplication simultaneously across the network. Now
anyone node will not know who all submitted transactions, who all
submitted other transaction at the same time, it is the ordering service that
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
Step 4) Ordering : will determine that so ordering service might be
getting many such transaction across the network. Asshown all
transactions are submitting to ordering service and it determines how to
order these transactions and it will make sure everyone sees the same
order across the network.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 5) Ordering : Now once the ordering service determines an order
it is going to deliver that ordered set of transactions which is called as
block to all the peers in the network.
⚫ So ordering service forms the block with ordered set of transactions and
communicates that with all the peers and all of you can include this block
in your blockchain. Peers are architected in a way that it has hierarchy.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 5) Ordering : One Peer in the organization takes the
responsibility for communicating with other peers. Ordering service is
pluggable component in Hyperledger fabric.The different ordering
algorithms available are
⚫ A) SOLO order: Single node development-There is a dictator which is
just a single node that is going to dictate what the order should be and
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 5) Ordering : A) SOLO order: the single node development-the
single node implements FIFO first in first out order.
⚫ B) Kafka order: very popular event management service by apache. It
is open source and having crash fault tolerance consensus.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 6)ValidateTransaction : Now all of these peers have received a block
of transactions. Not all of these transactions may be valid. Reasons are
⚫ 1)Transactions has not got sufficient endorsement.
⚫ 2)Two transactions tried to update same data. So this sort of verifications
is being performed by all the peers in the network both the endorsing
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 6)ValidateTransaction : and committing peers will do the validation.
Now only the valid transaction will be updated in the world state.
All the invalid transaction will haveno effect on state maintained bythe
blockchain.
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 7) NotifyTransaction : In the final step now all the peers going to
commit the set of valid transaction, commit the block add the block into
blockchain and each of them will emit events. For each transaction in the
block whether it is valid or invalid, for both of them will get specific
transaction level events and it is also possible for ClientApplication to
embed events in the smart contract logic. So in this smart contract logic
Dr. S. Joshi
⚫ Hyperledger Fabric :
⚫ Consensus is achieved using following transaction flow:
⚫ Step 7) Notify Transaction : emit an event sayingthis variable was
modified so the business information can be embedded in an event are
called as chain code events and those are also part of the block and emit
along with the block events and transaction events. So this forms the
notification part of the whole transaction flow on peer to peer basis.
Dr. S. Joshi
⚫Hyperledger Fabric Architecture:
Dr. S. Joshi
⚫Hyperledger Fabric Architecture:
⚫ The key benefits of that transaction flow are
Dr. S. Joshi
⚫ Hyperledger Fabric:
Channels:
channel
Dr. S. Joshi
⚫ Hyperledger Fabric : Channels:
⚫ So a copy of the chain code is going to be running on each peer that
needs to run that chain code.
⚫ But a chain code is installed for a specific channel or each channel.
Each peer is going to be running a copy of chain code that it needs
to run.
⚫ Example: here there are two channels red and blue. E0 is the part
of both of these channels. E1 is the part of red channel. So this
chain code is running on E0 and E1. For E0 it needs the chain code
on both these channels whereas E1 needs this chain code only
on red channel because it does not know the existence of blue
⚫ Hyperledger Fabric : Channels:
⚫ channel. So chain code has to be installed on each channel
specifically and the state maintained is the part of ledger and it
separate for each of the channel.
⚫ so the chain code state in one channel is different from chain code
channel
Dr. S. Joshi
⚫ Hyperledger Fabric : Channels:
⚫ And this also alerts for concurrent execution for performance and
scalability. So say 20 nodes in a network, 5 of them can form a
private channels among themselves, 10 can form another private
channels among themselves and these two channels can execute
completely parallel with each other and that can help to improve
scalability of the entire network itself.
⚫ So they can all executes transaction themselves without
interfering with each other. So that is the notion of channels.
channel
Dr. S. Joshi
⚫ Hyperledger Fabric : Channels:
⚫ Example: Here is a network with just a single channel. Client
application is going to submit transaction to the network.
E0,E1,E2 and E3 are the endorsing peers in this network, there is
exactly one channel in this system and all the peers connects to
that channel i.e. blue channel.
⚫ This channel is going to maintain sequence of blocks or sequence
of transactions. A and B are smart contract. So these are chain
codes that are deployed. So these chain codes are all installed on
⚫ Hyperledger Fabric : Channels:
⚫ Example: every node in the network and it are instantiated on that
channel so when transaction is submitted by certificate authority
on network i.e. invoking a particular function in smart contract A
then all the 4 peers E0,E1,E2 and E3 they are all going to be
executing same function across all the nodes and go through
ordering and the consensus and finally validation.
Dr. S. Joshi
⚫ Hyperledger Fabric : Channels:
⚫ Example: so this is a single channel network where all the peers
are part of single channel. There could be multi channel network.
⚫ Now there could be 2 different application.
⚫ One application is transacting on red channel.
⚫ Another application is transacting on blue channel and these
application may not even know the existence of other channel.
Dr. S. Joshi
⚫Hyperledger Fabric : Channels:
⚫ In this example E0 and E3 are only part of red channel and they
are only aware of smart contracts or chain codes that are deployed
on that channel.
⚫ E1 and E2 are only part of blue channel and not known the
existence of red channel.
⚫ So this completely disjoined channels in this network and they
actually using same ordering service.
Dr. S. Joshi
⚫Hyperledger Fabric : Channels:
⚫ It is possible that even ordering service is separate across the
channels. So the transactions on one channel are only ordered for
that channel, the transactions for other channels are kept separate.
⚫ Endorsement policy also may be same or different on two
Dr. S. Joshi
⚫ Hyperledger Fabric :
Membership Service Provider (MSP):
⚫ Identities will be use to communicate with each other.
⚫ Administrators managing this network themselves.
Dr. S. Joshi
⚫ Hyperledger Fabric : Membership Service Provider (MSP):
⚫ Users are authenticating themselves by signing the transaction and
this sign is verified by the peer. So peer is going to validate this
signature provided by the user.
⚫ Signing and insurance are possible because of security framework
of MSP.
Dr. S. Joshi
⚫ Hyperledger Fabric : Membership Service Provider (MSP):
⚫ Supports different crypto standard with pluggable interface:
Different application might need different security properties that
might be provided by different certificate authorities or different
crypto standards.
⚫ It includes TLS i.e. Transport Layer Security is used for
encrypting all Communication across the network
Dr. S. Joshi
⚫ Hyperledger Fabric : Membership Service Provider (MSP):
⚫ i.e. communication among peers themselves, peer to order
communication, user to peer communication, peers and chain codes,
all of these are secured throughTLS.
Dr. S. Joshi
⚫ Hyperledger Fabric : Identities :
⚫ User Identities : Every user in network is going to be issued identity,
an enrollment certificate. Enrollment certificate has 2 parts:
1)Keystore: Private key to digitally sign transaction by user while
submitting onto network.
2) Signcert : which is public x.509 certificate in fabric implementation
⚫ Hyperledger Fabric : Identities :
⚫ User Identities : 2) Signcert : which includes this public key to
validate whether it was indeed this user who signed that transaction
and includes some attributes that certificate authority might provide
to the user.
⚫ Optionally may also includes TLS credentials: peer needs TLS
credentials for communicating with other peers and orders in the
⚫ Hyperledger Fabric : Identities :
⚫ User Identities : network.All of this can be backed by Hardware
Security Module i.e. HSM where this keys are separately stored.
⚫ HSM is used to sign transactions without anyone else or any
component in the system getting access to private keys.
⚫ Hyperledger Fabric : Identities :
⚫ Admin Identities : The admin identities is very similar in
concept to user identities.
Dr. S. Joshi
⚫ Hyperledger Fabric : Identities :
⚫ Peer and ordered Identities : For instance an organization who is
running the peer can designate one of its users as an administrator.
⚫ The specific function that administrator can do for instance joining
the peer to particular channel can only be performed by that
administrator user. If the regular user who is not an administrator
⚫ Hyperledger Fabric : Identities :
⚫ Peer and ordered Identities : tries to perform that transaction , it will
denied bythe network. It also stores the public certificate of certificate
authority that it will recognize. So it need the public certificate of that
certificate authority to able to validate users who are connecting to this
peer. That is the part of cacerts.
⚫ Hyperledger Fabric : Identities :
⚫ Peer and ordered Identities :There are also certification revocation lists :
⚫ This is important over a period of time to manage which are legitimate
users which are not. So hold your certificate for certain length of time &
revoke that certificate and have a new certificate for security purpose.
⚫ Peers and orders receive channel MSP information to know which
channels they are connected to.
Dr. S. Joshi
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ Endorsement Policy :
⚫ Asubset of the peers executing chain codes on each channel and that
is governed by endorsement policy.
⚫ So the endorsement policy states which peers have to execute or
endorse acertain transaction before it will be deemed asvalid to be
added to the blockchain. So each chain codes defines an endorsement
policy and this can be different for each channel that chain code
belongs to.
Dr. S. Joshi
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ Endorsement Policy :
⚫ System chain codes are the chain codes that are implemented internal to
fabric itself that performs certain important functions.
⚫ This system chain codes also provides the notion of pluggability.
⚫ ESCC: It is running within every endorsing peers.When a client submit
the transaction to the endorsing peer. It is invoking a particular function
that is in chain code and it first comes to ESCC.
⚫So the ESCC endorsement system chain codes is the one that will invoke
the chain codes will execute the transaction.
⚫ It will compute the read write set of transactions.
⚫ So what is the state information that was read in the chain code in that
Dr. S. Joshi
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ Endorsement Policy :
⚫ Function and what is the state information that was written.All of that is
captured by the ESCC and it will then sign the proposal response.
⚫ Example : It will say when I run this transaction, that is the read set, that
was the write set and here is my signature saying that I was the one who
run this and I endorse this. Sothis is done byevery endorsing peer. And
this system chain codes does that signing part of it.
⚫ Now client collects all these signatures from all the endorsements. It will
then submit that transactions to the order.
⚫ The order will include it in the block, deliver it to the peer.And now all
committing peers will do certain functions so that where the validation
Dr. S. Joshi
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ VSCC: System chain codes comes. In which it is going to validate these
endorsements. So what dose the validation System chain codes do?
⚫ It will look up the policy for this particular chain code. So this policy
might say organization 1, 2 and 3 in the network have to sign this
transaction.
⚫ So it will then check whether these 3 signature are available. How the
system works for some of this policies is the part of it.
⚫ The validation System chain codes does the job of going through the
endorsements that are available in the transaction, that has been
submitted with the transaction and seeing if that set of endorsements
satisfying the policy that was specified for the chain codes in that channel.
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ VSCC: So that is the validation System chain codes.
⚫ Now it is alwayspossible that the endorsement you want to use, some
other logic the endorsements or for the validation you want to use, some
other logic for the validation or even you might want to implement that
is different language other than Golang.
⚫ So all these possibilities exists.And this whole module is pluggable.
⚫ Youcan bring your own implementation for these things based on your
application needs.
⚫But for most
application we
deemed that this
exiting
implementation of
endorsement system chain codes and validation System chain codes will be
sufficient for most application that is there in enterprise to give a
comparison with bitcoin. Dr. S. Joshi
⚫ Hyperledger Fabric : Endorsement Policy :
⚫ VSCC:The validation that System chain codes does in some sense is the
validation whether the validation is duplicate or not.That is UTXO
model for bitcoin.
⚫ In Hyperledger fabric it is state based model. So the validation is based
on the state information. That we are sorting as a part of the ledger.
⚫ One part of the validation is the validation of endorsement policy. So
this is different from what bitcoin has and this is very specific to
Hyperledger fabric. It is one of the differentiation of Hyperledger fabric.
Dr. S. Joshi
Hyperledger Fabric : Access Control :
Model files describe the assets, participants and transaction in a
business network.
Access control list defines rules for sharing and privacy.
This rules automatically enforced by Hyperledger fabric composer run
time.
How to define access control?
It is possible to restrict who accesses what assets and what rights do
they have on that asset?
Dr. S. Joshi
Hyperledger Fabric : Access Control :
Whether they are able to create that asset, Whether they are able to
modify that asset, call a particular function on that asset, Whether they
are able to read it, all of these can be defined by access control rules.
It can be very simple, starting from everybody can read everything,
Who are the participants – participants that follows it.
What is the operation – it is everybody can read. So that is read
operation.
What is the resources – all are resources.
Dr. S. Joshi
Hyperledger Fabric : Access Control :
It is simple dot star and action is to allow. You can say deny
i.e. You can say deny this particular user access to this recourse.
It may have more complex access rules as well.
It may say only the owner of a particular asset can do anything with it.
It can have read write modify everything. So owner has full access.
The access here is to allow for every operation. Read write everything.
Who is the participants – the participants that follows it.
What is the resource – it is one particular asset.
Dr. S. Joshi
Hyperledger Fabric : Access Control :
And specify condition there. Let say owner is participant. So whoever is
invoking it is actually the owner. So if the condition is satisfied, they will
be allowed to perform that transaction according to access control
rules.
It may also have credentials. Based on the user secret that you are using
you can ensure that only certain users are authorized to perform certain
transaction.
Dr. S. Joshi
Writing smart contract using Ethereum :
Code as Law means the code that execute & fulfills some terms of a
contract and that code can not alter or influenced by any outside
3rd party or system.
With Ethereum, blockchain can actually deploy code onto blockchain
and execute it.
Smart contract is a program run on the blockchain.
Blockchain is decentralized distributed computing system where state
changes are allowed only after achieving consensus of a majority of
participating nodes. It is immutable (unchangeable) so to change a
smart contract to edit the code, would actually have to make changes
across the entire blockchain.
It provides high level of security as have to take little bit care in
developing smart contracts as once deployed to the blockchain can
not be changed.
Smart contract can be written in a number of different languages Rust,
Go, JavaScript, Vyper, Yul, etc
Dr. S. Joshi
Writing smart contract using Ethereum :
We are focusing on Solidity as most popular language. It has
designed specifically for use in Smart contract by some of the
code contributors of Ethereum.
Smart contract IDEs and Tools:
Remix online editor really useful for sort of doing rapid
prototype development, testing out ideas, deploying and
testing code quickly.
Write simple contract with input, output and going to create
and return a greeting as hello with your name.
Solidity is strongly typed language so always declare the type
of variable Such as string and declare axis modifier public –
means this is accessible to anybody and everyone.
Define greeting prefix so that get a name with greeting prefix
Example: Hello Dr. Shilpa
Ctrl save to save this pgm Dr. S. Joshi
Writing smart contract using Ethereum :
SPDX-License-Identifier: MIT
Declare what kind of license are present in solidity with
SPDX-License-Identifier:
MIT is just open source license with no rustication anybody
can use, copy and replicate this code
Data collection may be storage or memory for constructor.
In solidity all your variables with exception of instance
variable can be stored in different ways. of any of the 3
types- storage
Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
When paper money came into being, people started carrying
money in what can be called wallets.
With the advent of digital and cryptocurrencies, wallets no
longer need to be physical. You can carry your information on
your cards, on your phones and watches, and your
cryptocurrency wallets live in the cloud.
One example of this, is the Ethereum wallet
“applications that let you interact with your Ethereum
account”.
It lets you check your balance and send transactions.
Smart contracts are programs which govern the behavior of
accounts within the Ethereum state.
Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
1) 1st line indicates the Software Package Data Exchange
(SPDX) license the code can be used under. In my case, it is
the MIT license which is free software license originating at
the Massachusetts Institute of Technology (MIT). MIT is one
of the many free licenses available in the SPDX license list.
// SPDX-License-Identifier: MIT
2) next line is to tell compiler the version of Solidity to use for
compiling the code. This is important because each version of
Solidity might deprecate some functionality and may
introduce new build-in functions. Using the right version of
Solidity allows the user to use these functionalities in their
code.
pragma solidity ^0.8.10;
Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
3) contract EtherWallet {
The first line of real Solidity code declares contract and gives it
name. This name is used as an identifier when compiling and
deploying code. A Solidity contract can be considered like a service.
The public components of the contract can be called from outside
the contract and may result in some value being returned to caller.
4) address payable public owner;
the type “address” as on the Ethereum blockchain, every account
and smart contract has an address. The address is needed to send
and receiver “Ether” from another account.
This address identifies the contract on the blockchain. It is like a
bank account for a user. Just like a person needs bank account
information of another person to send money to their account
(unless you use Interact e-transfer), an address is needed to send
Ether to a contract. Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
4) address payable public owner;
An address is assigned to a contract when the contract is deployed on
the blockchain.
“payable” keyword makes it possible for the address to receive Ether.
If this keyword is not used, the contract won’t be able to receive Ether
and anyone trying to send Ether to the contract will receive an
Exception.
Next on the line is the keyword “public”. This keyword makes a variable
or a function accessible from outside the contract, in other words public
The last word on this line, “owner”, is the name of the variable.
Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
5) constructor() { owner = payable(msg.sender); }
The constructor is the function that gets called when an instance
of the contract is created. It generally includes statements that
initialize variables or sets them up with valid values.
In Solidity, there are certain globally available special variables
that can be accessed to extract useful information. One such
variable is “msg”. The “msg” variable has a property called
“sender”. This property can be accessed to derive the address
that called this contract.
When a contract is deployed, the sender property is the address
of the deployer. Hence setting the address in the msg.sender,
stores the address of the deployer in the owner variable. This a
smart way of storing the address of the deployer (or the owner)
of the contract so it can later be used to compare with addresses
calling this contract. Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
6) receive() external payable {}
In order for a contract to receive Ether, the contract must
have one of two type of functions:
1. A receive() external payable function; or
2. A fallback function
The receive() external payable function is called when a
contract is sent Ether, but no call data. In the case of this
wallet, it will be called each time the contract is sent Ether.
The Ether will get added to the address where this contract
is deployed. An “external” function is a part of the
contract interface. An external function cannot be called
internally from within the contract.
Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
7) function withdraw(uint _amount) external {
require(msg.sender == owner, “only the owner can call this
method”);
payable(msg.sender).transfer(_amount); }
This “external” function can be called from outside this contract
to withdraw “Ether” and send it from this wallet to the recipient.
The first line of this function checks to make sure the sender of
this message is the owner of the contract. Obviously, it would not
be appropriate to allow just anybody to withdraw from a wallet.
Only the owner of the wallet should be able to withdraw. The
second line is what
makes the transfer.
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
8) function getBalance() external view returns (uint) {
return address(this).balance; }
there is a function in the contract to check the balance. This function
can be called from outside the contract only due to the “external”
keyword.
It returns the
current balance
of the contract.
Technically, it
returns the
balance of the
address at which
the contract is
deployed, but conceptually, it’s the balance of the contract. Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
VM is virtual memory blockchain rather than real one.
While executing the owner account have slightly less than 100 ether
ie balance goes down as anything you do on Ethereum blockchain
costs gas which means there is cost to execute any type of code on
blockchain.
As this is just test environment so it cost very small fraction of ether
to deploy our contract.
Now to send some ether 1st check with getbalance ie should be 0.
Receive function allows contract to receive ether. Receive is invoked
when ether is sent in to contract and there is no calldata.
To send some ether select ether form wif button and put 25 in value
now choose another account as we said that anyone can actually
deposit funds into wallet then from low level interaction calldata hit
transact and check balance as 2500000000 now, group of o’s in 25
because default is wei which is sub denomination of ether so withdraw
is also be in wei Dr. S. Joshi
Writing smart contract using Ethereum :
How to Build a Simple Ethereum Wallet:
So use Ethereum unit converter. So new account is with 74.9999
Now just to test that only owner should be able to withdraw
from contract so put 1500000 in withdraw and click withdraw so
getting error msg of only owner can call this method
So go back to 1st account try to withdraw greater amount than
exists in contract say 35000000 click on withdraw again error
msg to say called function should be payable if value send should
be less than current balance so contract take care of transaction
if we requesting more money than contract currently holds so
change it to 150000 and click withdraw so in owner account now
99+15=114 ether in balance.
So we started with 25, withdraw 15 so balance is 25-15=10 in
owner button just make sure that address matches with current
address. Dr. S. Joshi
⚫ Overview of Ripple :
⚫ Blockchain networks or distributed ledger network that are all
payment network.
⚫ First is seller network which is global network.
⚫ It is actually decentralised hybrid blockchain platform with open
membership.
⚫ It means anyone can join this network, run a seller node and
participate in this network, execute transactions and store the state
of payments or accounts in this network.
⚫ Another interesting project is ripple protocol and the associated
network, actually ripple was the first one that came out in terms
of being creating such a payment network after bitcoin.
⚫ So it is a protocol that instead of looking end users, they actually
look at banks, they target banks to help banks to do clearing and
settlement of payments and it is done in decentralised manner.
Dr. S. Joshi
⚫ Overview of Ripple :
⚫ They have their own consensus algorithm. It allows
exchanges and remittance to happen.
⚫ They have similar transaction clearance.
⚫ They have about 5 seconds to to run their transaction
but important thing is thing there is no mining in this
case unlike bitcoin.
⚫ Bitcoin has proof of work and it involves mining but here
they are only looking to create that network among the set of
permission entities or banks.
⚫ They have a custom protocol but that protocol
unfortunately no one has validated it for correctness and
fault tolerance.
Dr. S. Joshi
⚫ Overview of Ripple :
⚫ So it is still being debated whether it is a good protocol to
use.
⚫ They have again similar to seller, they have gateway nodes
to convert currencies from flat currencies like US dollar or
EURO to ripple.
⚫ And for ripple it is the XRP that is the cryptocurrency that
use and there are market makers that help convert one
currency to another.
Ripple has a centralised governance model so ripple controls
who joins the network, how is this created and they also hold
large fraction of the cryptocurrencies still.
Dr. S. Joshi
⚫ Overview of Ripple :
Dr. S. Joshi
⚫ Overview of Corda :
⚫What is Corda?
⚫Very much similar to blockchain, Corda is a distributed
ledger platform designed to record, manage and
automate contract based legal agreements between two
or more parties.
⚫Well known for its application in business deals.
⚫Targeted towards the world's largest financial
institutions, but potential for application exceeds beyond
that.
⚫It offers a unique approach to privacy, security, and takes
care of the scalability issues that Dapps face.
Dr. S. Joshi
⚫ Overview of Corda : What is Corda?
⚫Corda is an open-source enterprise-based blockchain designed
to offer interoperability.
⚫It is a platform that is intended to record, manage, and
synchronize agreements and transfer anything valuable.
⚫It allows enterprises to communicate and transact directly
while maintaining transparency and without worrying about
privacy and to integrate Blockchain across their operations
immediately and efficiently.
⚫Moreover, Corda offers timestamping services to order
transactions temporally and ignore disputes.
⚫It has a smart contract logic, which specifies constraints that
ensure state transitions are valid as outlined in contract code.
Dr. S. Joshi
⚫ Overview of Corda :
⚫How Corda works?
⚫As in Blockchain which embodies the Distributed
Ledger Technology (DLT), the data are stored in all the
nodes in the blockchain making it highly secure.
⚫This means that an individual or any entity outside the
block can never change the data for their own agenda.
⚫This makes it highly secure but at the same time raises
the issue of the number of files and the space it takes up.
Dr. S. Joshi
⚫ Overview of Corda :
⚫Smart contracts play a vital role in blockchain, i.e. the
contract would be transparent and accessible by everyone in
the block.
⚫But the main difference with Corda, is that the smart
contract can be permission based.
⚫Only the two or more parties involved in the transaction
would have access to the smart contract.
⚫Also with the consensus of the parties involved, a
regulatory or a legal body can be given access and brought
into the network as an observer to verify the contract.
Dr. S. Joshi
⚫ Overview of Corda : Highlights
⚫Smart contracts in Corda are executed taking into account
the following features :
⚫ Regulating and streamlining the workflow between
decentralized firms
⚫ Recording and managing the evolution of financial
observers
Dr. S. Joshi
⚫ Overview of Corda : Highlights
⚫Smart contracts in Corda are executed taking into account
the following features:
⚫ Consensus algorithms and levels are between two firms
involved
Dr. S. Joshi
⚫ Overview of Corda :
⚫Why choose Corda?
⚫Due to following features:
⚫ Data privacy and security
⚫ Native tokenization and interoperability
⚫Settlement finality
⚫Sustainability
⚫Multi-tenancy
⚫Regulatory complaint
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
From an application developer’s perspective, a smart
contract, together with the ledger, form the heart of a
Hyperledger Fabric blockchain system.
Whereas a ledger holds facts about the current and historical
state of a set of business objects, a smart contract defines
the executable logic that generates new facts that are added
to the ledger.
A chain code is typically used by administrators to group
related smart contracts for deployment but can also be used
for low level system programming of Fabric.
Before businesses can transact with each other, they must
define a common set of contracts covering common terms,
data, rules, concept definitions, and processes.
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
Taken together, these contracts lay out the business
model that govern all of the interactions between transacting
parties.
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
Terminology: Hyperledger Fabric users often use the
terms smart contract and chain code interchangeably. In
general, a smart contract defines the transaction logic that
controls the lifecycle of a business object contained in the
world state.
It is then packaged into a chain code which is then deployed
to a blockchain network. Think of smart contracts as
governing transactions, whereas chain code governs how
smart contracts are packaged for deployment.
A smart contract is defined within a chain code. Multiple
smart contracts can be defined within the same chain code.
When a chain code is deployed, all smart contracts within it
are made available to applications.
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
In the diagram, we can see a vehicle chain code that contains three
smart contracts: cars, boats and trucks. We can also see an insurance
chain code that contains four smart contracts: policy, liability,
syndication and securitization. In both cases these contracts cover key
aspects of the business process relating to vehicles and insurance. In this
topic, we will use the car contract as an example. We can see that a
smart contract is a domain specific program which relates to specific
business processes, whereas a chain code is a technical container of a
group of related smart contracts.
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
Ledger : At the simplest level, a blockchain immutably records
transactions which update states in a ledger. A smart contract
programmatically accesses two distinct pieces of the ledger –
a blockchain, which immutably records the history of all transactions,
and a world state that holds a cache of the current value of these
states, as it’s the current value of an object that is usually required.
Smart contracts primarily put, get and delete states in the world state,
and can also query the immutable blockchain record of transactions.
A get typically represents a query to retrieve information about the
current state of a business object.
A put typically creates a new business object or modifies an existing
one in the ledger world state.
A delete typically represents the removal of a business object from the
current state of the ledger, but not its history.
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
Development : Smart contracts are the focus of application
development, and as we’ve seen, one or more smart contracts can be
defined within a single chain code. Deploying a chain code to a network
makes all its smart contracts available to the organizations in that
network. It means that only administrators need to worry about chain
code; everyone else can think in terms of smart contracts.
At the heart of a smart contract is a set of transaction definitions. For
example, look at assetTransfer.js here, where you can see a smart
contract transaction that creates a new asset:
Dr. S. Joshi
Writing smart contract using Hyperledger Fabric :
Running a Fabric Application : We will look into introduction to
how Fabric applications interact with deployed blockchain
networks. Use sample programs built using the Fabric
Gateway client API to invoke
a smart contract,
which queries &
updates the ledger
with the smart
contract API –
described in detail
in Deploying a
smart contract to
a channel.
Dr. S. Joshi
Two Principle parts:
Dr. S. Joshi
Deploy the smart contract: A smart contract can describe an almost
infinite array of business use cases relating to immutability of data in
multi-organizational decision making.
Dr. S. Joshi
Endorsement
Every smart contract has an endorsement policy associated with it.
This endorsement policy identifies which organizations must
approve transactions generated by the smart contract before
those transactions can be identified as valid.
An example endorsement policy might define that three of
the four organizations participating in a blockchain network must
sign a transaction before it is considered valid. All transactions,
whether valid or invalid are added to a distributed ledger, but
only valid transactions update the world state.
If an endorsement policy specifies that more than one organization
must sign a transaction, then the smart contract must be executed
by a sufficient set of organizations in order for a valid transaction
to be generated. In the example above, a smart contract
transaction to transfer a car would need to be executed and
signed by both ORG1 and ORG2 for it to be valid.
Dr. S. Joshi
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
Fabric Demo on IBM Blockchain Cloud:
References:
NPTEL Course on Blockchain