0% found this document useful (0 votes)
22 views7 pages

Hyperledger Fabric Chaincode Guide

This guide outlines the steps for deploying and testing a simple key-value chaincode in Hyperledger Fabric version 2.5 and above. It includes prerequisites, network setup, chaincode preparation, installation on organizations, approval of chaincode definitions, committing chaincode, invoking, and querying chaincode. The document provides detailed commands for each step to ensure a successful deployment.

Uploaded by

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

Hyperledger Fabric Chaincode Guide

This guide outlines the steps for deploying and testing a simple key-value chaincode in Hyperledger Fabric version 2.5 and above. It includes prerequisites, network setup, chaincode preparation, installation on organizations, approval of chaincode definitions, committing chaincode, invoking, and querying chaincode. The document provides detailed commands for each step to ensure a successful deployment.

Uploaded by

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

Hyperledger Fabric Chaincode

Deployment (Latest Version)


This guide provides a fully working setup for deploying and testing a simple key-value
chaincode in Hyperledger Fabric (v2.5+).

Step 1: Prerequisites
 Docker and Docker Compose installed.
 Fabric samples downloaded from [Link]
 Go (1.18+) installed for chaincode development.

Step 2: Cleanup
cd fabric-samples/test-network
./[Link] down
docker system prune -af --volumes

Step 3: Start Network


./[Link] up createChannel -ca

Step 4: Prepare Chaincode


mkdir -p ../chaincode/freshkv/go
cd ../chaincode/freshkv/go
go mod init freshkv
go get [Link]/hyperledger/fabric-contract-api-go/contractapi
Create [Link] with Put/Get methods
Run:
go mod tidy

Step 5: Prepare Chaincode


cd ~/fabric-samples/test-network
./[Link] down
./[Link] up createChannel -ca
Step 6: Package Chaincode
cd ~/fabric-samples/test-network

set peer path

export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/

peer lifecycle chaincode package [Link] \


--path ../chaincode/freshkv/go \
--lang golang --label freshkv_1

Step 7: Install Chaincode on Org1 and Org2


 Set Org1 environment and run install

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_ADDRESS=localhost:7051
export CORE_PEER_TLS_ENABLED=true
export
CORE_PEER_MSPCONFIGPATH=$PWD/organizations/peerOrganizations/[Link]
.com/users/Admin@[Link]/msp
export
CORE_PEER_TLS_ROOTCERT_FILE=$PWD/organizations/peerOrganizations/[Link]
[Link]/peers/[Link]/tls/[Link]

peer lifecycle chaincode install [Link]

 Set Org2 environment and run install

export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_ADDRESS=localhost:9051
export
CORE_PEER_MSPCONFIGPATH=$PWD/organizations/peerOrganizations/[Link]
.com/users/Admin@[Link]/msp
export
CORE_PEER_TLS_ROOTCERT_FILE=$PWD/organizations/peerOrganizations/[Link]
[Link]/peers/[Link]/tls/[Link]
peer lifecycle chaincode install [Link]
Step 8: Query Installed Chaincode

peer lifecycle chaincode queryinstalled

Step 9: Approve Chaincode Definition

Approve for Org 1:

export CORE_PEER_LOCALMSPID="Org1MSP"

export CORE_PEER_ADDRESS=localhost:7051

export CORE_PEER_TLS_ENABLED=true

export
CORE_PEER_MSPCONFIGPATH=$PWD/organizations/peerOrganizations/[Link]
m/users/Admin@[Link]/msp

export
CORE_PEER_TLS_ROOTCERT_FILE=$PWD/organizations/peerOrganizations/[Link].
com/peers/[Link]/tls/[Link]

peer lifecycle chaincode approveformyorg \

--channelID mychannel \

--name freshkv \

--version 1.0 \

--package-id freshkv_1:<your_package_id> \

--sequence 1 \

--signature-policy "OR('[Link]','[Link]')" \

--tls \

--orderer localhost:7050 \

--ordererTLSHostnameOverride [Link] \
--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]"

Now Org 2 , approval

export CORE_PEER_LOCALMSPID="Org2MSP"

export CORE_PEER_TLS_ENABLED=true

export CORE_PEER_ADDRESS=localhost:9051

export
CORE_PEER_MSPCONFIGPATH=$PWD/organizations/peerOrganizations/[Link]
m/users/Admin@[Link]/msp

export
CORE_PEER_TLS_ROOTCERT_FILE=$PWD/organizations/peerOrganizations/[Link].
com/peers/[Link]/tls/[Link]

peer lifecycle chaincode approveformyorg \

--channelID mychannel \

--name freshkv \

--version 1.0 \

--package-id freshkv_1:<your_package_id> \

--sequence 1 \

--signature-policy "OR('[Link]','[Link]')" \

--tls \

--orderer localhost:7050 \

--ordererTLSHostnameOverride [Link] \

--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]"
Step 10: Commit Chaincode

peer lifecycle chaincode commit \

--channelID mychannel \

--name freshkv \

--version 1.0 \

--sequence 1 \

--signature-policy "OR('[Link]','[Link]')" \

--tls \

--orderer localhost:7050 \

--ordererTLSHostnameOverride [Link] \

--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]" \

--peerAddresses localhost:7051 \

--tlsRootCertFiles
"$PWD/organizations/peerOrganizations/[Link]/peers/[Link]
m/tls/[Link]" \

--peerAddresses localhost:9051 \

--tlsRootCertFiles
"$PWD/organizations/peerOrganizations/[Link]/peers/[Link]
m/tls/[Link]"

Step 12: Invoke Chaincode

Set terminal to Org 1:


export CORE_PEER_LOCALMSPID="Org2MSP"

export CORE_PEER_TLS_ENABLED=true

export CORE_PEER_ADDRESS=localhost:9051
export
CORE_PEER_MSPCONFIGPATH=$PWD/organizations/peerOrganizations/[Link]
m/users/Admin@[Link]/msp

export
CORE_PEER_TLS_ROOTCERT_FILE=$PWD/organizations/peerOrganizations/[Link].
com/peers/[Link]/tls/[Link]

Invoke the chaincode


peer chaincode invoke -o localhost:7050 \

--ordererTLSHostnameOverride [Link] \

--tls \

--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]" \

--peerAddresses localhost:7051 \

--tlsRootCertFiles
"$PWD/organizations/peerOrganizations/[Link]/peers/[Link]
m/tls/[Link]" \

-C mychannel -n freshkv --waitForEvent \

-c '{"function":"InitLedger","Args":[]}'

Step 13: Query Chaincode

peer chaincode query -C mychannel -n freshkv -c '{"function":"GetAllKeys","Args":[]}'

peer chaincode invoke -o localhost:7050 \


--ordererTLSHostnameOverride [Link] \
--tls \
--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]" \
--peerAddresses localhost:7051 \
--tlsRootCertFiles
"$PWD/organizations/peerOrganizations/[Link]/peers/[Link]
m/tls/[Link]" \
-C mychannel -n freshkv --waitForEvent \
-c '{"function":"Put","Args":["user1","Alice"]}'

peer chaincode query -C mychannel -n freshkv -c '{"function":"Get","Args":["user1"]}'

peer chaincode query -C mychannel -n freshkv -c '{"function":"GetHistoryForKey","Args":


["user1"]}'

peer chaincode invoke -o localhost:7050 \


--ordererTLSHostnameOverride [Link] \
--tls \
--cafile
"$PWD/organizations/ordererOrganizations/[Link]/orderers/[Link]
/msp/tlscacerts/[Link]" \
--peerAddresses localhost:7051 \
--tlsRootCertFiles
"$PWD/organizations/peerOrganizations/[Link]/peers/[Link]
m/tls/[Link]" \
-C mychannel -n freshkv --waitForEvent \
-c '{"function":"Put","Args":["user1","Bob"]}'

peer chaincode query -C mychannel -n freshkv -c '{"function":"GetHistoryForKey","Args":


["user1"]}'

Common questions

Powered by AI

The 'peer lifecycle chaincode approveformyorg' command in the chaincode deployment process allows an organization to approve a predefined chaincode definition on its peer. This command is executed for each participating organization to signify their agreement to the chaincode's specifications before it can be committed to the ledger. It considers several parameters like channel ID, chaincode name, version, package ID, sequence number, and a signature policy, which collectively enforce organizational governance by preventing any single entity from proceeding without the consensus of all stakeholders involved .

The essential prerequisites for deploying chaincode in Hyperledger Fabric include having Docker and Docker Compose installed, downloading Fabric samples from the Hyperledger GitHub repository, and installing Go version 1.18 or newer for chaincode development. These components are critical because Docker facilitates containerized environment setup, ensuring consistent deployment across different platforms. Docker Compose is used for setting up and managing the network services. The Fabric samples provide template scripts for network setup, and Go is the programming language used for writing chaincode, making these prerequisites foundational for a successful deployment .

Chaincode invoke operations differ from chaincode query operations in their impact on the network state. Invoke operations modify the state by executing transactions that result in changes, such as updating or adding new key-value pairs, and these require consensus and endorsement from the network. In contrast, query operations are read-only, and they do not affect the state, as they simply retrieve existing data from the ledger. This distinction is crucial for maintaining the integrity and consistency of the blockchain, with invoke operations altering its state and query operations providing insights without modifications .

The signature policy in the chaincode approval and commit process is significant as it determines the necessary endorsements required from participating organizations before a transaction can be validated and committed to the ledger. It ensures that only transactions meeting the specified policy, such as requiring signatures from multiple organizations, are accepted, thereby enhancing transaction integrity and protecting against unauthorized changes. This policy is central to achieving consensus and ensuring that all changes to the ledger are agreed upon by the necessary parties, maintaining a secure and trustworthy environment in Hyperledger Fabric .

It is necessary to have chaincode installed and approved by multiple organizations before committing it to a channel to ensure consensus and mutual trust among all participants. This implies a decentralized trust model in Hyperledger Fabric, where no single entity can control the network or make unilateral decisions. All organizations must approve the chaincode with a proper version and sequence, adhering to a mutual endorsement policy, which reflects the collaborative governance and enhances security and trust within the blockchain network .

The lifecycle of chaincode deployment in Hyperledger Fabric ensures security and coordination by implementing a multi-step process that requires consensus and cooperation among different organizations. The process includes packaging the chaincode, installing it on peers from all participating organizations, approving the chaincode definition for each organization, and finally, committing it to the channel. This ensures that no single entity can unilaterally deploy or modify chaincode, as each organization must approve the chaincode with a proper signature policy. This coordinated approach prevents unauthorized or malicious changes, thereby enhancing security .

The chaincode query functionality plays a critical role in managing the state of the Hyperledger Fabric network by enabling users to retrieve data from the ledger. It allows for operations like fetching current values, historical values, or key metadata without altering the ledger state. This functionality is integral for transparency, auditing, and making informed decisions based on the immutable records stored within the blockchain, supporting the network's operations and ensuring all participant transactions are accurately reflected .

Setting environmental variables is crucial when preparing to install chaincode on peer nodes, as it configures the environment with the necessary context for the command execution. Variables such as CORE_PEER_LOCALMSPID, CORE_PEER_ADDRESS, CORE_PEER_TLS_ENABLED, CORE_PEER_MSPCONFIGPATH, and CORE_PEER_TLS_ROOTCERT_FILE define the peer identity, address, security settings, and location of certificates required for secured communication. This configuration ensures that commands are executed within the correct organizational context, leading to accurate endorsement and installation of chaincode across the network .

The chaincode commit step finalizes the deployment process by disseminating the chaincode to all participating peers on the channel, officially making it operational for transactions. This requires all involved organizations to have previously approved and agreed upon the chaincode specifications, sequence, and version. The cross-organization implications include mandatory collaboration and trust, as the commit can only occur once the chaincode is agreed upon by all parties, ensuring that no single organization can unilaterally alter the ledger without consensus, thus maintaining the decentralized nature of the network .

Packaging the chaincode involves steps such as setting the peer path environmental variables, using the command 'peer lifecycle chaincode package' to bundle the chaincode into a .tar.gz file, specifying the path, language, and label for the chaincode. These steps facilitate deployment by creating a standardized package that can be easily installed on peers across different organizations, maintaining uniformity and ensuring the chaincode is ready for installation and further lifecycle processes .

You might also like