BC 5
BC 5
EXPERIMENT-5
AIM:
To write and deploy chain code in Hyperledger Fabric.
THEORY:
Hyperledger is an open-source project created to support the development of blockchain-based
distributed ledgers. Hyperledger consists of a collaborative effort to create the needed
frameworks, standards, tools and libraries to build blockchains and related applications.
Hyperledger was created by the Linux Foundation in 2016.
Hyperledger Fabric, an open-source project from the Linux Foundation, is the modular
blockchain framework and standard for enterprise blockchain platforms. Intended as a foundation
for developing enterprise-grade applications and industry solutions, the open, modular
architecture uses plug-and play components to accommodate a wide range of use cases.
Hyperledger Composer is a set of collaboration tools for building blockchain business networks
that make it simple and fast for business owners and developers to create smart contracts and
blockchain applications to solve business problems. Built with JavaScript, leveraging modern
tools including node.js, npm, CLI and popular editors, Composer offers business centric
abstractions as well as sample apps with easy to test devops processes to create robust blockchain
solutions that drive alignment across business requirements with technical development.
A smart contract in Hyperledger Fabric is a program, called chaincode which is written in Go,
JavaScript, and other programming languages such as Java that implement a prescribed interface.
“Architecture of the Hyperledger Blockchain Fabric” is the topic for the research paper.
Hyperledger is basically a permissioned model where the nodes form a consortium and control
who participates in validation of protocol. Hyperledger is an enterprise grade, open source
distributed ledger framework and codebase. Hyperledger Fabric is an implementation of a
distributed ledger platform for running smart contracts, leveraging familiar and proven
technologies, with a modular architecture allowing pluggable implementations of various
functions. It is one of multiple projects currently in incubation under the Hyperledger Project.
Hyperledger Fabric is a very important part of the Hyperledger framework and hence its
architecture needs to be understood and what the underlying technology is used. This paper tries
to expound upon this and explains what the architecture is, tells what the Hyperledger Fabric
does and what its features are.
The Fabric blockchain platform operates on a permissioned ledger and incorporates a security
infrastructure for authentication and authorization. The validating peers use a Byzantine Fault
Tolerance (BFT) consensus protocol to execute a replicated state machine that processes deploy,
invoke, and query transactions for chaincode defined by the user. Each chaincode may define its
own persistent entries in the state, and the blockchain's hash chain is computed over executed
transactions and the resulting persistent state.
The BFT consensus protocol assumes that among the n validating peers, at most f < n/3 may
behave arbitrarily, but all others execute the chaincode correctly. To ensure that the chaincode
transactions are deterministic when executed on top of PBFT consensus, a modular solution is
used to filter out non-deterministic transactions that may diverge. This solution has been
implemented in the SIEVE protocol.
Consensus protocols for blockchain are currently being debated very actively, in research but also
by fintech startup companies. Thefabric’s design uses a modular notion of consensus, which is
aligned with the well-established concept of consensus in distributed computing. This ensures
that the blockchain-related features of the fabric can be developed independently of the specific
consensus protocol. The PBFT protocol has been implemented as the first one in the fabric
because of its prominence: it benefits from the experience of almost 20 years of systems-level
research on Byzantine consensus, is closely related to well-known protocols like viewstamped
replication and Paxos, has been analyzed in many environments, and is described in textbooks.
The topic of consensus protocols for blockchain is currently a subject of active debate in both
research and industry, with involvement from fintech startups such as tendermint.com and
kadena.io. The Fabric blockchain platform uses a modular approach to consensus, which is in line
with the established concept of consensus in distributed computing. This modular approach
allows the blockchain-related features of
Fabric to be developed independently of the specific consensus protocol. The PBFT protocol has
been implemented as the first consensus protocol in Fabric due to its prominence, extensive
research over almost 20 years on Byzantine consensus, and its relation to well-known protocols
like viewstamped replication and Paxos. PBFT has been analyzed in many environments and is
described in textbooks.
OUTPUT:
Step 1: Set up the environment
First, we need to set up the development environment for Hyperledger Fabric. We need to install
the required software, including Docker, Docker Compose, Go, and Node.js. Here's how to do it:
a. Install Docker: Download and install Docker from the official website:
https://www.docker.com/get-started
b. Install Docker Compose: Download and install Docker Compose from the official website:
https://docs.docker.com/compose/install/
c. Install Go: Download and install Go from the official website: https://golang.org/dl/
d. Install Node.js: Download and install Node.js from the official website:
https://nodejs.org/en/download/
a. Download the test-network: Download the test-network by cloning the repository from
GitHub using the following command in your terminal or command prompt:
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/test-network
b. Start the network: Start the network by running the following command:
`./network.sh up createChannel -ca`
This command will start the network, create a channel, and generate the required cryptographic
material for the network. It may take a few minutes for the network to start up.
c. Verify the network: Verify that the network is running by running the following command:
`./network.sh list`
This command will list the running containers and should show a list of containers with names
starting with "peer".
Step 3: Write the chaincode: We need to write the chaincode in a programming language
supported by Hyperledger Fabric. We can use a text editor or an Integrated Development
Environment (IDE) to write the code. Here is an example of a simple chaincode written in Go:
a. Create a new directory: Create a new directory for the chaincode using the following
command: `mkdir -p chaincode/mychaincode`
b. Create a new file: Create a new file named "mychaincode.go" in
the chaincode/mychaincode directory using the following
command:
`touch chaincode/mychaincode/mychaincode.go`
c. Write the chaincode code.go file in a text editor or IDE and write the sample chaincode
in Go language. Here's an example:
Step 4: Package and install the chaincode:Once we have written the chaincode, we need package
it into a binary format and install it on the peers. Follow these steps:
b. Install the chaincode: Install the chaincode on the peers using the
following command:
c. Approve the chaincode: Approve the chaincode definition using the following command:
`peer lifecycle chaincode approveformyorg --channelID mychannel --name mychaincode --
version 1.0 --package-id PACKAGE_ID --sequence 1`
Replace PACKAGE_ID with the package ID generated in the previous step. This command
will approve the chaincode definition for your organization.
d. Commit the chaincode: Commit the chaincode definition using the following command:
`peer lifecycle chaincode commit -o localhost:7050 --channelID mychannel –name
mychaincode --version 1.0 --sequence 1 --tls true –cafile /path/to/orderer/ca.crt --
peerAddresses localhost:7051 –tlsRootCertFiles /path/to/peer/tls/ca.crt`
CONCLUSION:
In the above experiment we understood how to write and deploy chain code in Hyperledger Fabric.