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

Overview

APIs used in Chaincode to update / obtain state info from world state database

Setup

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

Fabcar Chaincode Review

PutState

func (stub *ChaincodeStub) PutState(key string, value []byte) error
Directly observe the 10 car records after initLedger().
CAR0 record.

GetState

func (stub *ChaincodeStub) GetState(key string) ([]byte, error)

GetStateByRange

func (stub *ChaincodeStub) GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

GetStateByRangeWithPagination

func (stub *ChaincodeStub) GetStateByRangeWithPagination(startKey, endKey string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error)
cd fabric-samples/chaincode
cp -r fabcar/go/ testrangepage/
cd testrangepage
mv fabcar.go testrangepage.go
Line 16–18 added for the new function.
cd fabric-samples/basic-network
./teardown.sh
./start.sh
docker-compose up -d cli
docker exec cli peer chaincode install -n mycc -p github.com/testrangepage -v 0
docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc github.com/testrangepage -v 0 -c '{"Args": []}' -P "OR('Org1MSP.member')"docker exec cli peer chaincode invoke -C mychannel -n mycc -c '{"Args":["initLedger"]}'

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