This subproject contains Go chaincode for a Supply Chain Management use case, deployed on a Hyperledger Fabric private blockchain network.
- Chaincode:
smartcontract.go - Objective:
- Create, update, and transfer ownership of “products”
- Demonstrate ledger read/write, including
InitLedger,CreateProduct,UpdateProduct,TransferOwnership, etc.
- Language: Go (version >= 1.20 recommended)
- Tools:
- Hyperledger Fabric
- Docker & Docker Compose
- Fabric samples (
fabric-samples/test-network) for local test environment
chaincode/smartcontract.go:- Defines a
Productstruct and aSupplyChainContractstruct that implements Fabric’scontractapi.Contract. - Key methods:
InitLedger(initial data),CreateProduct,UpdateProduct,TransferOwnership,QueryProduct, etc.
- Defines a
Dockerfile(optional): Provides a custom build environment for your chaincode if needed.
- Docker & Docker Compose installed.
- Go installed (e.g.,
go version→ 1.20+). - Fabric Samples:
git clone https://github.com/hyperledger/fabric-samples.git - Fabric Binaries:
or newer scripts from the Fabric docs.
cd fabric-samples curl -sSL https://bit.ly/2ysbOFE | bash -s
- Start the Test Network
This brings up 2 orgs (Org1, Org2) and an orderer node, joined on mychannel.
cd fabric-samples/test-network ./network.sh down ./network.sh up ./network.sh createChannel - Package & Install Chaincode
- From the
test-networkdirectory, define environment variables (for Org1 or Org2).cd fabric-samples/test-network export PATH=${PWD}/../bin:$PATH export FABRIC_CFG_PATH=${PWD}/../config/ # Package peer lifecycle chaincode package supplyChain.tar.gz \ --path <path-to-chaincode> \ --lang golang \ --label supplyChain # Install on Org1 # Set Org1 env variables... peer lifecycle chaincode install supplyChain.tar.gz # Install on Org2 # Set Org2 env variables... peer lifecycle chaincode install supplyChain.tar.gz
- From the
- Approve and Commit
- Query installed chaincode, note the
Package ID, and runapproveformyorg. - Commit the chaincode to
mychannelwithpeer lifecycle chaincode commit .....
- Query installed chaincode, note the
- Invoke Chaincode
- InitLedger:
peer chaincode invoke \ -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls \ --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \ -C mychannel -n supplyChain \ --peerAddresses localhost:7051 \ --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \ --peerAddresses localhost:9051 \ --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \ -c '{"function":"InitLedger","Args":[]}' - Query products:
peer chaincode query -C mychannel -n supplyChain -c '{"Args":["GetAllProducts"]}'
- InitLedger:
- Functions to test:
InitLedger()CreateProduct(id, name, owner, description, category)UpdateProduct(id, newStatus, newOwner, newDescription, newCategory)TransferOwnership(id, newOwner)QueryProduct(id)
- Validation:
- Check if the ledger has the correct product states after each operation.
- Dataset, test cases, etc. provided by Dr. Swathi Punathumkandi from Arizona State University.
This project is released under the MIT License. That means you’re free to use, modify, and distribute the code, but you do so at your own risk.
Author: Varshith Dupati
GitHub: @dvarshith
Email: dvarshith942@gmail.com
Issues: Please open an issue on this repo if you have questions or find bugs.