Skip to main content

How to Deploy a Chaincode

This video provides a step-by-step walkthrough of the process for deploying chaincode (smart contracts) on a Hyperledger Fabric network using the platform's management interface. You'll learn how to package, install, approve, and commit chaincode, as well as how to verify a successful deployment.

Overview​

Hyperledger Fabric uses a multi-step chaincode lifecycle that ensures all network participants agree on the chaincode before it becomes active on a channel. The lifecycle consists of:

  1. Package -- Bundle chaincode source code into a deployable package (.tar.gz).
  2. Install -- Install the chaincode package on each peer that needs to execute it.
  3. Approve -- Each organization approves the chaincode definition for their org.
  4. Commit -- Once enough organizations have approved (satisfying the lifecycle endorsement policy), the chaincode definition is committed to the channel and becomes available for invocations.

ChainLaunch provides both a manual step-by-step API for each phase and an automated deploy endpoint that handles the entire lifecycle in one call.

API Endpoints​

MethodEndpointDescription
POST/api/v1/sc/fabric/peer/{peerId}/chaincode/installInstall chaincode on a peer
POST/api/v1/sc/fabric/peer/{peerId}/chaincode/approveApprove chaincode definition
POST/api/v1/sc/fabric/peer/{peerId}/chaincode/commitCommit chaincode definition
POST/api/v1/sc/fabric/deployDeploy chaincode (automated full lifecycle)
GET/api/v1/sc/fabric/chaincodesList all chaincodes
POST/api/v1/sc/fabric/chaincodes/{chaincodeId}/invokeInvoke chaincode
POST/api/v1/sc/fabric/chaincodes/{chaincodeId}/queryQuery chaincode

Automated Deployment​

The easiest way to deploy chaincode is the automated deploy endpoint, which handles install, approve, and commit in a single request.

curl -X POST http://localhost:8100/api/v1/sc/fabric/deploy \
-H "Content-Type: application/json" \
-d '{
"networkId": 1,
"channelName": "mychannel",
"chaincodeName": "mycc",
"chaincodeVersion": "1.0",
"chaincodeSequence": 1,
"chaincodePath": "/path/to/chaincode",
"chaincodeLanguage": "golang",
"endorsementPolicy": "",
"initRequired": false
}'

This endpoint performs the following steps automatically:

  1. Packages the chaincode from the specified path.
  2. Installs it on all peers in the network.
  3. Approves the chaincode definition for each organization.
  4. Commits the chaincode definition to the channel.

Step-by-Step Deployment​

If you need more control over the deployment process, you can execute each step individually.

Step 1: Install Chaincode on a Peer​

curl -X POST http://localhost:8100/api/v1/sc/fabric/peer/{peerId}/chaincode/install \
-H "Content-Type: application/json" \
-d '{
"chaincodePath": "/path/to/chaincode",
"chaincodeLanguage": "golang",
"chaincodeName": "mycc",
"chaincodeVersion": "1.0"
}'

Repeat this for each peer that needs to run the chaincode.

Step 2: Approve Chaincode Definition​

Each organization must approve the chaincode definition before it can be committed.

curl -X POST http://localhost:8100/api/v1/sc/fabric/peer/{peerId}/chaincode/approve \
-H "Content-Type: application/json" \
-d '{
"channelName": "mychannel",
"chaincodeName": "mycc",
"chaincodeVersion": "1.0",
"chaincodeSequence": 1,
"packageId": "mycc_1.0:abc123...",
"endorsementPolicy": "",
"initRequired": false
}'

Step 3: Commit Chaincode Definition​

Once enough organizations have approved, commit the chaincode definition to the channel.

curl -X POST http://localhost:8100/api/v1/sc/fabric/peer/{peerId}/chaincode/commit \
-H "Content-Type: application/json" \
-d '{
"channelName": "mychannel",
"chaincodeName": "mycc",
"chaincodeVersion": "1.0",
"chaincodeSequence": 1,
"endorsementPolicy": "",
"initRequired": false
}'

Listing Chaincodes​

Retrieve all deployed chaincodes:

curl http://localhost:8100/api/v1/sc/fabric/chaincodes

Invoking Chaincode​

Submit a transaction that writes to the ledger:

curl -X POST http://localhost:8100/api/v1/sc/fabric/chaincodes/{chaincodeId}/invoke \
-H "Content-Type: application/json" \
-d '{
"channelName": "mychannel",
"function": "CreateAsset",
"args": ["asset1", "blue", "10", "Alice", "1000"]
}'

Querying Chaincode​

Read data from the ledger without submitting a transaction:

curl -X POST http://localhost:8100/api/v1/sc/fabric/chaincodes/{chaincodeId}/query \
-H "Content-Type: application/json" \
-d '{
"channelName": "mychannel",
"function": "ReadAsset",
"args": ["asset1"]
}'

Upgrading Chaincode​

To upgrade an existing chaincode, repeat the lifecycle with an incremented chaincodeSequence value and (optionally) a new chaincodeVersion. For example, to upgrade from sequence 1 to 2:

  1. Install the new chaincode package on all peers.
  2. Approve with chaincodeSequence: 2.
  3. Commit with chaincodeSequence: 2.

Or use the automated deploy endpoint with the updated sequence number.

Troubleshooting​

IssuePossible CauseSolution
Install failsPeer not running or unreachableVerify peer status in the nodes list
Approve failsChaincode not installed on the peerInstall chaincode before approving
Commit failsNot enough organizations have approvedEnsure all required orgs have approved
Invoke returns errorChaincode not committed on channelVerify chaincode is committed and peers have joined the channel
Query returns emptyData not yet writtenInvoke a write transaction first