README ¶
Blockchain
My personal notes, example codes, best practices and sample projects.
Author
Aditya Hajare (Linkedin).
Current Status
WIP (Work In Progress)!
License
Open-sourced software licensed under the MIT license.
Important Notes
- Blockchain Basics
- Why Blockchain As Opposed To An Ordinary Database
- Key Concepts
+ Blockchain + Distributed Ledger + Transactions + Chaincode + Channel Events + Chaincode Lifecycle Endorsement Policy Rule + Peer Launch Modes + Client Side API + Arguments Sent To Chaincode + Transaction Flow + Chaincode Interface + The Fabric Model: What makes fabric ideal as an enterprise blockchain solution? - Assets - Chaincode/Smart Contracts - Ledger - Privacy - Security And Membership Services - Consensus + Identities + Policies + Peer + Ledger + Orderer + Channels
- Blockchain Technology Benefits
- Smart Contracts
- Development Environment Setup
- CouchDB
- Private Data Collections
- Range Queries
- Rich Queries
- Asset History Logs
- Programmatic Access Control
- Fabric Node SDK
- Common Errors
Blockchain Basics
- It is a decentralized system. Decentralized means the network is powered by its users (Peers) without having any third party, central authority or middleman controlling it.
- Every Peer has a record of the complete history of all transactions as well as the balance of every account.
- This bookkeeping is not controlled by one party or a central authority (E.g. Central Bank).
- Its all Public, and available in one digital ledger which is fully distributed across the network. i.e. everybody sees what everybody is doing.
- The Blockchain acts as a public ledger.
- In blockchain all the transactions are logged including:
- Time
- Date
- Participants
- Amount of every single transaction
- Each node in the network owns the full copy of the blockchain.
- The nodes automatically and continuously agree about the current state of the ledger and every transaction in it.
- If anyone attempts to currupt a transaction, the nodes will not arrive at a consensus and hence will refuse to incorporate the transaction in the blockchain.
- So every transaction is public and thousands of nodes unanimously agreed that a transaction has occurred on date X at time Y.
- Everyone has access to shared single public source of truth.
- Blockchain in first instance is all about optimizing Shared B2B Processes.
Why Blockchain As Opposed To An Ordinary Database
- In a nutshell, a Database solves a Data Problem whereas a Blockchain solves a Digital Asset Problem.
- Database Solves a Data Problem:
- Mature technology exists.
- Centralized or Distributed.
- Fit for purpose.
- One party governs the data.
- Blockchain solves a Digital Asset Problem:
- Trade, trust and ownership.
- Digital Assets.
- Transactionality.
- Multiple parties govern the data.
- Remove intermediaries.
- Solve double spend problem.
- Need for time and trust in a network.
- Gives birth to new business models.
Key Concepts
- Blockchain: It is a shared, replicated transaction system (Distributed Ledger) which is updated with the help of Smart Contracts and kept consistently synchronized through a process called Consensus. It is a append-only transaction system.
- Distributed Ledger: It is a database that is concensually shared and synchronized across multiple sites, institutions or geaographies and it is accessible by multiple people.
- Smart Contracts: It is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of codes. The code and the agreement contained therein exists across a distributed, decentralized blockchain network.
- Consensus: The majority of opinion, agreement amoung a group of people.
- Private Blockchain vs. Public Blockchain: In public blockchain, anyone can send a transaction, while in private blockchain, only participants who are approved can send transactions.
- Permissioned Blockchain vs. Permissionless Blockchain: Permissionless blockchain allow people to act anonymously (you do not know their identity), while in permissioned blockchain the identities of participants are known.
- Examples of Public and Permissionless Blockchain: Bitcoin, Etherium.
- Examples of Private and Permissioned Blockchain: Hyperledger Fabric, JP Morgan.
- Cryptocurrencies (Bitcoin, Etherium etc.): Cryptocurrency is not same as a blockchain. Cryptocurrency use blockchain to store transactions. Most of the cryptocurrencies use their own type of blockchain with some aspects that make them unique and best for their own usecase.
- Distributed Ledger:
Distributed Ledger
platforms are used for tracking theState
of anAsset
.- An
Asset
is a digital representation of any real world thing. - So anything in a real world, whether it's a tangible or intangible, that may be digitally represented, can be managed on a
Distributed Ledger
. - Tangible assets examples: Cars, Houses, etc.
- Intangible assets examples: Stocks, Bonds, Certificates and any other kind of financial instruments.
- Transactions:
- The state of an
Asset
on theDistributed Ledger
is managed by the way ofTransactions
. In other words,Transactions
manages theStates
of anAsset
. Transaction
represents the invocation of business logic that changes or manages theState
of theAssets
on theDistributed Ledger
platform.Chaincode
encapsulates the business logic.- All
Transactions
are recorded in theLedger
. - The recorded
Transactions
are immutable. i.e. they cannot be updated or deleted.
- The state of an
- Chaincode:
Chaincode
implements the business logic and exposes theState
management features by way of one or more functions.- The functions exposed by
Chaincode
are executed from theApplications
by way ofTransactions
. - Not all of the
Transactions
lead to the creation of entry inLedger
. SomeTransactions
are performed to readState
of anAsset
in theLedger
. - Fabric Chaincode can be developed in
Golang
,NodeJS
orJava
. - Any real world asset that can be degitized can be represented as a
Model
in theChaincode
. - Each of the chaincode in the network is identified by a name which is unique across
Channel
. Instantiation Policy
determines whichOrg
canEndorse
theTransaction
for creation of theChaincode
.- Peers receive the chaincode package in the instantiate transaction and hence the installation of chaincode on peer is automatic: FALSE.
Shim API
is used for coding theChaincode
.- Not all
Orgs
need to installChaincode
. Following entities has theChaincode
:Orgs
participating in thetransactions
.Orgs
that will query theledgers
.Chaincode Endorsers
- Important points about
Chaincode
:Chaincode
may implement multipleSmart Contracts
.Chaincode
is packaged in standardTar
file format.Installation
generates thePackage-ID
.- Each
Org
approves the specificPackage
for theirOrg
. Package-ID
may be different acrossOrgs
.- Whenever a
Chaincode
is approved by anOrg
, theApproval
is added as aTransaction
into theLedger
of thatOrg
.
- By default
State Data
is setup inLevelDB
but we can configurePeer
to storeState Data
inCouchDB
instead ofLevelDB
. CouchDB
is aNoSQL
database which can be setup as aState Data
store forPeer
node.CouchDB
allows to execute rich queries againstState Data
whereasLevelDB
doesn't allow it.- Each
Chaincode
owns theState Data
it manages. - Direct access to
State Data
from oneChaincode
to another is not allowed. However,Chaincode
canInvoke
orQuery
otherChaincode
to access it'sState Data
. - Invoke
Chaincode
from anotherChaincode
:- Since the
Chaincode
is invoked locally, both theChaincodes
(caller and the calledChaincode
) must be on the samePeer
. Transaction
executes in the sameTxn
Context
.State
changes on both the caller and the calledChaincode
take effect only if they share a commonChannel
.- No
State
change takes place on the calledChaincode
if the caller and a calledChaincodes
are on differentChannels
. Transaction
message is passed from the callerChaincode
to the calledChaincode
.- On common
Channel
:State
changes take effect for both caller and a calledChaincode
. - On different
Channel
:State
changes take effect only for the callerChaincode
.
- Since the
- Channel Events:
- Events are emitted by the
Peers
. - Applications may subscribe to the events.
Subscription
is onChannel
level. i.e.Subscription
requires the specification of theChannel
.- Subscribers can also specify the filter criteria on which the events are evaluated on the
Peer
.
- There are 3 types of Events:
Block Event
: Emitted by thePeer
when a newBlock
is added by thePeer
to theLedger
.Transaction Event
: Emitted by thePeer
when a specifiedTransaction
is received in aBlock
.Chaincode Event
: Emitted by theChaincode
.Chaincode Events
are generated in response to theTransactions
against theChaincode
.
Chaincode Events
are not received by the subscriber when theChaincode
is executed by theEndorsers
. The events are received by the subscriber when thePeer
adds theTransaction
to theLedger
.- The subscriber can provide the name of the
Event
in which they are interested. - The
Transaction Event
andChaincode Events
are in theBlock
payload.
- Events are emitted by the
- Chaincode Lifecycle Endorsement Policy Rule:
- Network members sets up the
Lifecycle Endorsement Policy
. - It decides:
- How many approvals are needed for a commit to be successful.
- It may have a rule that says some specific
Organization
mustEndorse
theTransaction
for it can be committed successfully.
- This
Policy
is embedded in theChannel
Genesis
. Rules
can be updated withChannel
update transactions.Lifecycle Endorsement Policy
can be specified asImplicitMeta Policy
or aSignature Policy
.- Default
Policy
is theRule = "MEJORITY Endorsement"
. It means thatMore than HALF the members of the network MUST approve the Chaincode definition for it to be committed successfully
. - The
Policy
ruleRule = "ANY Endorsement"
requiresOnly one approval for committing the Chaincode
. - With the help of
Signature Policy
, we can create complex/flexible expressions.
- Network members sets up the
- Peer Launch Modes:
Net
mode:Chaincode
instance launched byPeer
. This is the Default mode.- Used in live network.
- Chaincode Logs are written to the container's file system.
Dev
mode:Chaincode
instance launched by Developer.- Development time only.
- Chaincode Logs are written to the console.
- No need to install/upgrade for changes.
- To launch
Peer
inDev
mode:
# 1. Launch peer in dev mode peer node start --peer-chaincodedev # 2. Install chaincode to Peer # 3. Run chaincode on Terminal/Shell # 4. Instantiate the chaincode to Peer
- NOTE: Chaincode instance is not launched in the Docker container.
- Helper scripts:
##### BOTH COMMANDS USE ENV VARS! ##### # Builds the Golang chaincode /network/bin/cc-build.sh # go build $CC_PATH # Runs the Golang chaincode /network/bin/cc-run.sh # go run $GOPATH/src/$CC_PATH/*.go
- Example commands:
cd /network/bin/ # Initialize dev environment in Dev mode. # -d: Dev mode ./dev-init.sh -d # Set env vars for "acme" Org source ./set-env.sh acme # Check/verify env vars for Org ./set-chain-env.sh # Package and install chaincode on Peer # -p: Package chaincode ./chain.sh install -p # In new terminal, ssh into vagrant # Set Org context in new terminal source set-env.sh acme # Start chaincode in Terminal mode cc-run.sh # In other terminal, we can run following commands chain.sh instantiate # To instantiate the chaincode chain.sh invoke # To invoke chaincode chain.sh query # To query chaincode
- Client Side API:
Chaincode
gets deployed on thePeer
.Applications
use theFabric Client SDK
for interacting with theChaincode
.- There are 2 APIs that are used by the
Applications
.- Invoke API: Used for executing the business logic in the
Chaincode
by way ofTransactions
. - Query API: Used for reading the
State
of theAssets
fromDistributed Ledger
platform.
- Invoke API: Used for executing the business logic in the
- Both
Invoke API
andQuery API
executes the functions exposed by theChaincode
.
- Arguments Sent To Chaincode:
- The
Client Side API
executes the functions by passing data in JSON format to theChaincode
. - The JSON object has a key called
Args
, which is set to an array of string types. The first element in theArgs
array is thefunction name
which will be executed by theChaincode
in response of this API invocation on the client side. Rest of the arguments in theArgs
array are the parameters passed to the function called byChaincode
.
{ "Args": ["FunctionName", "Param1", "Param2", "Param..n"] }
- The
- Transaction Flow:
- When client executes an
Invoke
API, theTransaction Proposal
is created and it is sent to theEndorsing Peers
. If everything is good with the proposed transaction, theEndorsing Peers
Sign
theTransaction Proposal
and send it back to the client. The client then sends theSigned Transaction Proposal
to theOrderer Service
for including the transaction in theBlock
.Orderer Service
at some point creates theBlock
and sends theBlock
to thePeers
in theNetwork
. Not all of thePeers
haveChaincode
installed on them. ThosePeers
will still receive theBlock
sent byOrderer Service
. - When client executes a
Query
API, any of thePeer
with aChaincode
installed on it, will execute theChaincode Function
and return response to the client. In this invocation, the client does not go through theOrderer Service
.
- When client executes an
- Chaincode Interface:
- All Golang
Chaincode
must implement following 3 functions:Init()
: Chaincode initialization logic. Called withinvoke --is-init
flag.Invoke()
: Contains business logic. Executed onQuery
andInvoke [Without init flag]
.main()
: Registers chaincode with the fabric runtime.
- All Golang
- The Fabric Model: What makes fabric ideal as an enterprise blockchain solution?
- Assets:
- Asset definitions enable the exchange of almost anything with monetory value over the network. For e.g. Whole foods, antique cars, currency features, bonds, stocks, digital goods etc.
- Asset within the network are represented as a collection of key-value pairs with state changes that records the transaction on the ledger or distributed ledger.
- Assets can be represented in
Binary
andJSON
format. - There are 2 types of Assets viz.
Tangible Assets
andIntangible Assets
- Tangible Assets: Tangible assets are typically physical assets or properties owned by a company. For e.g. Computer equipment. Tangible assets are the main type of assets that companies use to produce their product and service.
- Intangible Assets: Intangible Assets don't exists physically, yet they have a monetory value since they represent potential revenue. For e.g. stock, bond, copyright of a song. The record company that owns the copyright would get paid a royalty each time the song is played.
- Chaincode/Smart Contracts:
- Chaincode contains the smart contracts.
- Many times chaincodes and smart contracts are used interchangeably because in most cases they mean exactly the same.
- Chaincode defines the asset and also enforces rules for interacting with the asset or any other information that is stored on the distributed ledger.
- Chaincode functions execute against the ledger's current state database and are initiated through transaction proposals.
- Chaincode execution results in a set of key-value pairs which are also called a
Right Set
. Right Set
can be submitted to the network and thereby append to the ledger.- Chaincode execution is partitioned from transaction ordering, limiting the required level of trust and verification across node types, and optimizing network scalability and performance.
- Chaincode or Smart Contracts define all the business logic.
- Chaincode/Smart Contracts are stored on
Peer
nodes.
- Ledger:
- Ledger contains all of the state mutations or transactions. These state changes are produced by the invocation of chaincode.
- The immutable, shared ledger encodes the entire transaction history for each channel, and includes SQL-like query capability for efficient auditing and dispute resolution.
- Ledgers store all of the data.
- Ledgers are stored on
Peer
nodes.
- Privacy:
- Channels and Private Data Collections enable private and confidential multi-lateral transactions that are usually required by competing businesses and regulated industries that exchange assets on a common network.
- Security And Membership Services:
- In Fabric Network, all participants have known identities.
- Public key infrastructure is used to generate cryptographic cenrtificates. These certificates can be tied to an organization, a network component, a user or a client application. These certificates can be used to manage data access control.
- Role based governing with the help of certificates is the thing which made Fabric
Permissioned
. - Permissioned membership provides a trusted blockchain network, where participants know that all transactions can be detected and traced by authorized regulators and auditors.
- Consensus:
- At a very high level, we can say Consensus Model has something to do with multiple participants agreeing on something.
- A unique approch to Consensus enables the flexibility and scalability needed for the enterprise.
- Assets:
- Identities:
- Every actor in a network has a digital identity. It is represented by
X-509
certificate. - Identities determine resources and access to information for actors.
- This determination is done based on attribute called
Principals
in certificate. - The identity with attribute are called
Principals
. We can think of aPrincipal
as some sort of a userid. - Identities are created by a trusted
Certificate Authority
. In fabric, we can use aCertificate Authority
. - The process of handing out certificates is called
PKI
(Public Key Infrastructure).PKI
provides a secure way of communication. - Just having certificate is not enough for any actor. We also need the network to acknowledge the certificate. For e.g. we need organization to say - "Yes this certificate belongs to my organization". The
Identity
must be registered in the organization'sMSP
(Membership Service Provider). MSP
(Membership Service Provider) turns verifiable identities into members of the blockchain network.PKI
(Public Key Infrastructure) is a collection of internet technologies that provides secure communication in the network.PKI
(Public Key Infrastructure):- Digital Certificates
- Public and Private Keys
- Certificate Authorities
- Certificate Revocation Lists
MSP
- For a member to have an access to the network, we need 4 things:- Have an identity issued by a CA that is trusted by the network.
- Become member of an organization that is recognized and approved by the network members.
- Add the
MSP
to either a consortium on the network or a channel. - Ensure the
MSP
is included in the policy definitions on the network.
- Every actor in a network has a digital identity. It is represented by
- Policies:
- A policy is a set of rules that can define how a decision is made.
- Policies describe a who and a what.
- In Hyperledger,
Policies
are used for infrastructure management. - Uses of
Policies
in Hyperledger network:- Adding/Removing members from channel.
- Change the structure of blocks.
- Specify count of organizations for endorsement of transactions
- How do we write
Policy
in Fabric:- Signature Policies:
- Turns verifiable identities into members of a blockchain network.
<OR | AND | NOutOf>
- ImplicitMeta Policies:
- Only used for channel configuration.
<ANY | ALL | MAJORITY>
- Signature Policies:
- Peer:
Chaincodes/Smart Contracts
andLedgers
are stored onPeer
nodes that are owned byOrganizations
inside the network.Peer
nodes can host multiple instances ofChaincodes
andLedgers
.- End-users communicate with the network by using applications that connect to the
Peer
nodes of theirOrganization
. Peers
useChannels
to interact with other network components.- All
Peers
belong toOrganizations
. Peers
have anIdentity
assigned to them via a digital certificate (x.509
).- Single Peer itself cannot update information stored in ledger on it. Updating requires a consent of other Peers in network. The update transaction is done in 3 steps:
- Step #1:
Proposal
.- Independently executed by
Peers
and returnsEndorse Proposal
responses.
- Independently executed by
- Step #2:
Ordering
andPackaging
transactions into blocks.Orderer
receivesEndorsed
transactions and then creates the blocks.
- Step #3:
Validation
andCommit
of the transaction.- When
Peer
receives a new block from theOrderer
, thePeer
processes the block resulting in a new block being added to theLedger
.
- When
- Step #1:
- Ledger:
- On the ledger, we are recording facts about current state of the object.
- Change history in ledger is immutable.
- In Fabric, ledger consists of 2 parts:
World State
:- It is a database that holds the current value of the object.
- It can change frequently.
Blockchain
:- It records all the transactions for the object that together results in a
Current World State
. - Transactions are collected inside blocks that are appended to the blockchain.
- Blockchain data structure is very different from
World State
. It's immutable. - Blockchain does not use a database.
- Blockchain is implemented as a file. The reason for this is because there are just few operations done on a Blockchain.
- Primary operations of a Blockchain is to append data to it. And a file is perfect for that.
- First block is called the
Genesis Block
. It does not contain transaction data. It contains configuration of a initial state of a network channel. - Blocks are connected together with the
Header
of a block. - Each block in blockchain has following structure:
- Block Header: Contains following parts:
- Block Number: Integer starting at
0
(Genesis Block
), and increased by 1 for each new block. - Current Block Hash: Hash of all the transactions contained in the current block.
- Previous Block Header Hash: Hash from the previous
Block Header
.
- Block Number: Integer starting at
- Block Data: Contains list of transactions arranged in order.
- Block Meta-Data: Contains certificate and the signature of the block creato which is used to verify the block.
- Block Header: Contains following parts:
- It records all the transactions for the object that together results in a
- Orderer:
- Orders transactions into blocks.
- Maintains list of Organizations that can create channels. This list of Organizations is called the
Consortium
. Also, this list is stored in aSystem Channel
(Channel for the Orderers). - Enforce access control for channels (Application Channels). This way they can restric user from Reading and Writing on a Channel.
- Manage structure of the blocks.
- We can tweak the structure of the blocks by setting the
BatchSize
andBatchTimeout
parameters.- BatchSize: Maximum transaction count in one block.
- BatchTimeout: Maximum time for a new block creation. This time is measured from the first transaction received in this new block.
- Ordering service implementations:
- Kafka (Deprecated since Fabric v2)
- Solo (Deprecated since Fabric v2)
- Raft (Recommended): It is a
Crash Fault Tolerant (CFT)
ordering service. It implementsLeader-Follower
model.
- It is better to have multiple Orderer nodes that are owned by different Organizations. This way we make sure that even the ownership is decentralized.
Raft
is a protocol for implementing distributedConsensus
.- In
Raft
there are 2timeout
settings which control the elections:- Heartbeat Timeout
- Election Timeout
- Raft Election Process: It's the amount of time a
Follower
waits until becoming aCandidate
. AfterElection Timeout
, theFollower
becomes aCandidate
and starts a new election term, votes for itself and sends outRequest Vote
messages to other nodes. If the receiving node hasn't voted yet in this term then it votes for theCandidate
and the node resets it'sElection Timeout
. Once theCandidate
has a majority of votes, it becomes aLeader
. TheLeader
begins sending outAppend Entries
messages to it'sFollowers
. These messages are sent in intervals specified by theheartbeat timeout
. Followers then respond to eachAppend Entries
message. This election term will continue until a follower stops receiving heartbeats and becomes a candidate. If 2 nodes become candidate at the same time then a split vote can occur. Once we have a leader elected, we need to replicate all changes to our system to all nodes. This is done by using the sameAppend Entries
message that was used forheartbeats
. Raft can even stay consistent in the face of network partitions.
- Channels:
Channels
are created by creating the firstTransaction
and submitting theTransaction
to theOrdering Service
. ThisChannel
creationTransaction
specifies the initial configuration of theChannel
and it is used by theOrdering Service
to write theChannels Genesis Block
.- We can use
Configtxgen
tool to create the firstTransaction
which will end up creatingChannel
and writingGenesis Block
on thatChannel
. - The
Configtxgen
tool works by reading thenetwork/configtx/configtx.yaml
file which holds all of the configurations for theChannel
. This file usesChannel Profiles
. Configtxgen
:- Reads from the
network/configtx/configtx.yaml
file. - Can create a
Configuration Transaction
for theApplication Channel
. - Can create a
Genesis Block
for theSystem Channel
.
- Reads from the
Blockchain Technology Benefits
- Security:
- Since Blockchain is a distributed concensus based architecture, it's eliminates single point of failure and reduces the need for data intermediaries such as transfer agents or messaging system operators.
- It helps prevent frauds and malicious third parties from doing bad things.
- Its not fullproof and we do hear about hacks in the box or certainly in cryptocurrency or cryptocurrency exchanges but its very very difficult to hack or manipulate.
- Transparency:
- It provides transparency and in place multialise standards, protocols and shared processes.
- Trust:
- Its transparent and the immutable ledger makes it easy for different parties in a business network to collaborate, manage data and reach agreements.
- Programmability:
- Its programmable, so its able to execute things like smart contracts and help be more tamper proof and have deterministic software that automates the business logic.
- We can code to address many different things from governance to compliance, regulatory compliance, data privacy, identity, looking at things like "know your customer" types things or anti money laundring attributes.
- It manages that stakeholder participation, like for things like proxy voting.
- Privacy:
- It provides privacy.
- High-Performance:
- It can be a private network of hybrid networks and they are engineered to sustain hundreds/millions of transactions per second and also handle periodic surges in network activity.
- Scalability:
- Its highly scalable.
- Authenticity and Scarcity:
- Digitization really ensures data integrity and enables acid profits. It's really a full transaction history in that single shared source of truth.
- Streamlined Processes:
- Since it can automate almost everything, it can enable some more real time settlements, auditing, reporting and reduces processing times and the potential for error and the delays due to number of steps and intermediaries required to achieve the same level of confidence.
- Economic Benefits:
- Reduced infrastructure, operational and transaction cost.
- Market Reactivity:
- It can be very reactive to market.
Smart Contracts
- Contracts:
- A contract is formed when an offer by one party is accepted by the other party.
- Consideration is the price paid for the promise of the other party. The price may not necessarily involve money.
- For e.g. If you walk my dog, i will feed your cat.
- For e.g. If you walk my dog, i will pay you 15 Rs.
- Smart Contracts:
Contract terms are agreed to ----> Smart Contract placed on the Blockchain ----> Triggering event causes contract to be automatically executed
- Contract terms are agreed to: Hard coded and cannot be changed without both parties being aware.
- Smart Contract placed on the Blockchain: Public viewed and verified.
- Triggering event causes contract to be automatically executed: If/then statement coding.
Development Environment Setup
- Install
Oracle Virtual Box
https://www.virtualbox.org/wiki/Downloads. - Install
Vagrant
https://www.vagrantup.com/downloads.
# Initialize vagrant and create Vagrantfile
vagrant init
# +-+-+-+-+-+-+-+-+ Configure Vagrantfile +-+-+-+-+-+-+-+-+
# Checkout boxes at: https://vagrantcloud.com/search
config.vm.box = "generic/ubuntu2010"
# Setup network
config.vm.network "private_network", ip "192.168.33.10"
# Mount folder
config.vm.synced_folder "./mount", "/home/vagrant/mount"
# Configure memory
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "10000" #10 GB
end
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Bootup our virtual linux box
vagrant up
# SSH into our running virtual linux box
vagrant ssh
# +-+-+-+-+-+-+- Install HLF Pre-Requisites +-+-+-+-+-+-+-+
# https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html
# install git
sudo apt install git -y
# install curl
sudo apt install curl -y
# install docker
sudo apt install build-essential -y
sudo apt install apt-transport-https ca-certificates gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose -y
sudo usermod -aG docker $USER
newgrp docker
# install go
curl -o "go.tar.gz" https://storage.googleapis.com/golang/go1.17.1.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf "go.tar.gz"
export PATH=$PATH:/usr/local/go/bin
source ~/.bashrc
# install node
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs -y
# install ohmyzsh
# NOTE: Default password for root account on ubuntu: vagrant
sudo apt install zsh -y
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
- Install Samples, Binaries and Docker Images:
# SSH into vagrant
vagrant ssh
# Go to /mount directory
cd mount
# Install samples, binaries and docker images
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.4 1.5.2
- Install
SSH
plugin for VSCode. - While vagrant machine is up and running, execute following to get location of
IdentityFile
:
vagrant ssh-config
- Add new SSH host into VSCode SSH plugin:
# Host can be found in Vagrantfile. Look for following line:
# config.vm.network "private_network", ip: "192.168.33.10"
ssh vagrant@192.168.33.10 -i "C:/Aditya/Projects/HLF/.vagrant/machines/default/virtualbox/private_key"
- Add
\bin
to path:
# In vagrant ssh, execute:
export PATH=/home/vagrant/mount/fabric-samples/bin:$PATH
CouchDB
- We can use CouchDB as out
State Database
. Ledger
contains aBlockchain
and aState Database
. Blockchain is implemented as a file and a State Database is implemented as a database.- By default, we use a
Go Level Database
and this database is implemented in thePeer
node. LevelDB
storesChain Code
data askey-value
pairs. It supports queries based onkey
,key range
andcomposite key
.- As an alternative
LevelDB
, we can useCouchDB
as aState Database
of theLedger
. - With CouchDB:
- We can store data as
JSON
objects. - We can write more complicated queries for retrieving specific data.
- We can use
Indexes
for more efficient querying at larger data sets.
- We can store data as
- We need to decide which database we will be using as a State Database before setting up the network. Otherwise, we have to bring down the network, enable CouchDB and bring the network up again.
- Each
Peer
has its own instance of theCouchDB
. - No data replication at the
CouchDB
level. - Remote access is disabled to
CouchDB
. CouchDB
stores dates in format:YYYY-MM-DDThh:mm:ss.s
.
Private Data Collections
- There are 3 levels of data privacy:
- Channels
- Private Data Collections
- Encryption
Private Data Collections
can provide privacy for subsets ofOrganizations
within aChannel
.- Private data collection consists of:
- The private data itself
- Hash of the private data
- The Private data cannot be shared with the
Ordering
service. - The Private data is stored on a separate database. Nothing is stored in the
State Database
. - Peers that don't have access to the Private Data Collections, don't have any data on their Peer node.
Gossip Protocol
is used for communication betweenPeer
nodes. This is a reason why we need to connect at least onePeer
node of theOrganization
to theChannel
as anAnchor Peer
. Because of this,Peers
know of each other's existence. Thats how we implement Peer to Peer communication usingGossip Protocol
.- What can we do with
PDC (Private Data Collection)
:- Use a corresponding public key for tracking public state.
- Chaincode access control.
- Sharing private data out of band.
- Sharing private data with other collections.
- Transferring private data to other collections.
- Using private data for transaction approval.
- Keeping transactors private.
Members
on theChannel
can restrict visibility of data.Ledger
is COMMON so allTransactions
are still visible to all.Transaction
inLedger
hasHASH
of data that is stored inPrivate Data Collections
.Private Data Collection
are configured using JSON at the time ofChaincode
Instantiate
orUpgrade
.Peers
managesPDC
data in a separate set of datastores.PDC
are isolatednamespaces
withinChaincode
.PDC Key-Values
are accessible withinChaincode
usingChaincode Stub API
.Policies
control read access to thePDC
.
Range Queries
- Range Queries require
Start Key
and theEnd Key
. Start Key
is included in the result set.End Key
is exluced in the result set.- If
Start Key
andEnd Key
are not specified i.e. Empty strings are provided, then result set will have all the Keys from state data set. - Keys are indexed in
Lexical Order
. - Maximum results returned in result set can be restricted by
Peer Config
. - Commonly used functions from Chaincode Stub API for executing range queries are
GetStateByRange()
andGetPrivateDataByRange()
. Composite Key
:- A key formed by combining 2 or more attributes of the record.
- Uniqueness is guarenteen only if all parts of the key are used.
- None of the
Key
attribute values inComposite Key
can contain anull
character. i.e.\0x00
- When the
PutState
is executed, the index is created in thestate database
. GetState()
andGetStateByRange()
functions do not supportPartial Composite Keys
.
Rich Queries
- Requires data to be modeled as
JSON
. Peers
need to useCouchDB
.Indexes
need to be created for performance.- Restrictions on CouchDB JSON Documents:
- Key cannot begin with underscore
_
. - Fields beginning with underscore
_
are used internally. ~version
is a reserved field.
- Key cannot begin with underscore
- Rich queries for
CouchDB
are created usingMango Queries
language. Mango
query language:- Declarative
JSON
query language. - Inspired by
MongoDB
query language. - Adopted by
Cloudant
andCouchDB
.
- Declarative
- Rich queries are not executed at the time of
Validation
. This may lead to inconsistent state of chaincode. - Do not use
Rich Queries
inUpdate Transactions (Invoke)
unless we can guarenteeNo Phantom Reads
.
Asset History Logs
- Fabric manages
Assets
in theChaincode
. Invoke Transaction
changes theState
on perKey
basis.- We can use
Queries
to get theCurrent State
of theAsset
. Queries
don't provide history of theTransactions
on specificAsset
. i.e.Queries
cannot get thePast State
. for e.g. List all previous owners of the vehicle with idVin#1000
.- To get the history of
Transactions
onAsset
, we useHistory Log
andHistory API
. Asset History Log
are managed on perPeer
basis.Peers
on which theChaincode
needs access to theHistory Log
must be configured toCreate
andUpdate
theseHistory Logs
.Peer
once enabled for managing theHistory Logs
, will create the log on perChaincode
or perAsset
basis.Asset History
onPeer
is managed in a separate datastore.- Regardless of what database is used as
State Database
, theAsset History
is always managed inGoLevelDB
database. History Logs
are setup atPeer
level. They are not replicated by way ofGossip
.Peer
is responsible for managing theAsset Logs
on it's own.History API
is used from within theChaincode
to access history.- To enable history database, look for following setting in
core.yaml
:
history:
enableHistoryDatabase: true
- Just like
Rich Query API
, theHistory API
is also not re-executed inValidation
phase of theTransaction
. This may lead to inconsistentState
ofChaincode
. - Do not use
History API
inUpdate Transactions (Invoke)
unless we can guarenteeNo Phantom Reads
.
Programmatic Access Control
- Fabric uses
PKI (Public Key Infrastructure)
forIdentity Management
. - All users are issued a
X509
certificate (Enrollment
certificate). For e.g. Admin and other users in multiple roles. - All nodes in the network are also issued a
X509
certificate. For e.g.Orderer
,Peers
,Client
etc. Authorization
decision is made by the nodes based on the user'sRole
and thePolicies
that have been set up for the network. For e.g. Only the identities assigned theRole
ofAdmin
can install and instantiate the chaincode.- At the network level, the
Access
andAuthorization
control is achieved by way of configuration. i.e. we have to declare thePolicies
that will drive theAccess
andAuthorization
at node level. - For building
Access Control
inChaincode
, check theClient Identity Chaincode Library
: https://github.com/hyperledger/fabric-chaincode-go/tree/main/pkg/cid - Custom
Attributes
are added to theX509
certificates by theRegistrar
. Client Identity Chaincode Library
provides access to theAttributes
orIdentity
set inX509
certificates.Cryptogen
tool does not support addition ofAttributes
in the certificates.- For additional
Attributes
support inX509
certificates, theIdentities
need to be setup with afabric-ca
.
Fabric Node SDK
- Module:
fabric-network
:Gateway Class
: Connection point for accessing the network.Network Class
: Represents set ofPeers
belonging to a network or theApplication Channel
.Contract Class
andTransaction Class
: Exposes APIs (Invoke
andQuery
) for Chaincode interactions.Transaction Class
exposes additional APIs for providing finer control over Chaincode interactions.- Example flow: Pattern: Invoking and Querying Chaincode:
- Application creates instance of
Gateway Class
usingnew
operator. - Application then initializes the
Gateway Instance
withWallet
and theConnection Profile
. Wallet
holds the credentials information for the user.Connection Profile
is provided in the form ofYAML
orJSON
file.- After initialization, the Application creates an instance of
Network Class
by invoking a function onGateway Class
. - Then, Application creates the instance of
Contract Class
by invoking a function on theNetwork Class
instance. - Functions exposed by the
Contract Class
are then used for executing theInvoke
andQuery
functions on theChaincode
.
- Application creates instance of
fabric-network
module exposes classes for managingWallets
.
Wallet
:- A user may participate in multiple networks with different
Roles
. - In other words, a user may have multiple
Identity Profiles
to interact with different networks. Wallet
is construct that is used for managing theseIdentity Profiles
.Wallet
contains one or more userIdentity Context
and each of theseIdentity Context
orIdentity Profiles
haveCertificates
,Private Key
and aPublic Key
.Identities
inWallet
are referred to by aLabel
which is a free format string and it is unique for each of theIdentities
managed in theWallet
.Wallet
interface:InMemoryWallet
: Manages identities in memory.FileSystemWallet
: Manages identities on user's filesystem.CouchDBWallet
: Manages identities in a CouchDB Server.
- A user may participate in multiple networks with different
- Module:
Client Class
API:- Provides low level functions (via
Channel Class
) for interacting with thePeer
and theOrderer
. - Acts as a factory for
Peers
,Orderer
,Channel
andChaincode
classes. - Provides functions for managing the
client instance
configuration. - Manage
Channel Update Transactions
. - Instance is
Stateful
. i.e. We cannot re-use same instance against multipleChannel
. Channel
related updates require multipleAdmin
signatures.Channel Config
updateTx
submitted toOrderer
.- Creates instances of
Peer Class
that are used forPeer
queries. - Provides functions for managing
Channel Configuration
.
- Provides low level functions (via
- Module:
Channel Class
API:- Provides API for carrying out
Channel Aware Queries
andChannel Aware Tasks
. - For e.g.
- Create new
Channel
- Update existing
Channel
- Access to
Ledger (Blk, Txn)
- Joining
Peer
to theChannel
- Instantiation, Invoke and Querying of the
Chaincode
- Getting finer control over flow of the
Transaction
- Create new
- Channel class instance is initialized with
Peers
orOrderer
onChannel
. - Exposes functions for accessing the
Channel Configuration
.
- Provides API for carrying out
Common Errors
- Error:
zsh: ./networkdown.sh: bad interpreter: /bin/bash^M: no such file or directory
- Solution:
sed -i -e 's/\r$//' networkdown.sh
- Error:
Docker not installed
# OR
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
- Solution:
# ssh into vagrant box
vagrant ssh
# snap install docker
sudo snap install docker
# To fix permission issue
# Change docker.sock file to be owned by "vagrant" user
sudo chown vagrant /var/run/docker.sock
- Minifabric: How to install, approve, commit and initialize chaincode in a single command?
./minifab install,approve,commit,initialize -n simple -v 2.0 -p '"init","Aditya","35","Nishigandha","30"'
- Minifabric: How to update endorsement policy?
./minifab anchorupdate
./minifab discover # This will create a folder "discover" at "/vars/discover"
./minifab channelquery # This will create a channel config file at "/vars/channel1_config.json". Make the changes and save this file.
# Do the channel update to apply new changes
./minifab channelsign,channelupdate
Directories ¶
Path | Synopsis |
---|---|
09-Dev-Env
|
|
* | |
* | |
* | |
gocc/src/token/v6
This chaincode calls the token/v5 This is to JUST demonstrate the invoke mechanism This cc will act as a proxy
|
This chaincode calls the token/v5 This is to JUST demonstrate the invoke mechanism This cc will act as a proxy |
* | |
Click to show internal directories.
Click to hide internal directories.