PutState and GetState: The API in Chaincode Dealing with the State in the Ledger (Part 2)

Overview

Composite Key

APIs related to Composite Key

func (stub *ChaincodeStub) CreateCompositeKey(objectType string, attributes []string) (string, error)
func (stub *ChaincodeStub) SplitCompositeKey(compositeKey string) (string, []string, error)
func (stub *ChaincodeStub) GetStateByPartialCompositeKey(objectType string, attributes []string) (StateQueryIteratorInterface, error)
func (stub *ChaincodeStub) GetStateByPartialCompositeKeyWithPagination(objectType string, keys []string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error)

Setup

cd fabric-samples/chaincode
cp -r fabcar/go/ testcompositekey/
cd testcompositekey
mv fabcar.go testcompositekey.go

Design Principle

Modify the Chaincode

initLedger()

changeCarOwner()

createCar()

queryCarByOwner()

Demonstration

cd fabric-samples/basic-network
./teardown.sh
./start.sh
docker-compose up -d clidocker exec cli peer chaincode install -n mycc -p github.com/testcompositekey -v 0docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc github.com/testcompositekey -v 0 -c '{"Args": []}' -P "OR('Org1MSP.member')"docker exec cli peer chaincode invoke -C mychannel -n mycc -c '{"Args":["initLedger"]}'

Closing

--

--

Visit http://www.ledgertech.biz/kcarticles.html for all my works. Reach me on https://www.linkedin.com/in/ktam1/ or follow me @kctheservant in Twitter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store