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:
- Package -- Bundle chaincode source code into a deployable package (
.tar.gz). - Install -- Install the chaincode package on each peer that needs to execute it.
- Approve -- Each organization approves the chaincode definition for their org.
- 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
| Method | Endpoint | Description |
|---|---|---|
POST | /api/v1/sc/fabric/peer/{peerId}/chaincode/install | Install chaincode on a peer |
POST | /api/v1/sc/fabric/peer/{peerId}/chaincode/approve | Approve chaincode definition |
POST | /api/v1/sc/fabric/peer/{peerId}/chaincode/commit | Commit chaincode definition |
POST | /api/v1/sc/fabric/deploy | Deploy chaincode (automated full lifecycle) |
GET | /api/v1/sc/fabric/chaincodes | List all chaincodes |
POST | /api/v1/sc/fabric/chaincodes/{chaincodeId}/invoke | Invoke chaincode |
POST | /api/v1/sc/fabric/chaincodes/{chaincodeId}/query | Query 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:
- Packages the chaincode from the specified path.
- Installs it on all peers in the network.
- Approves the chaincode definition for each organization.
- 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:
- Install the new chaincode package on all peers.
- Approve with
chaincodeSequence: 2. - Commit with
chaincodeSequence: 2.
Or use the automated deploy endpoint with the updated sequence number.
Troubleshooting
| Issue | Possible Cause | Solution |
|---|---|---|
| Install fails | Peer not running or unreachable | Verify peer status in the nodes list |
| Approve fails | Chaincode not installed on the peer | Install chaincode before approving |
| Commit fails | Not enough organizations have approved | Ensure all required orgs have approved |
| Invoke returns error | Chaincode not committed on channel | Verify chaincode is committed and peers have joined the channel |
| Query returns empty | Data not yet written | Invoke a write transaction first |