Skip to content

Chaincode server demo cheatsheet

James Taylor edited this page Apr 2, 2020 · 5 revisions

These commands should show the fabcar chaincode working with the small network from fabric-dev-networks...

Build docker images

Download the Hyperledger Fabric docker images

curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s -- 2.0.0-beta 1.4.4 0.4.18 -s -b

Build the fabric-builders builder peer image

docker build -t hyperledgendary/fabric-builder-peer .

Build the fabric-dev-networks custom tools image

docker build -t hyperledgendary/fabric-tools .

Build the fabcar external chaincode image

docker build -t hyperledger/fabcar-sample .

Create Fabric network

Generate the small network config files

docker run --rm -v small_config:/etc/hyperledger/fabric -w /etc/hyperledger/fabric --entrypoint=/bin/bash hyperledgendary/fabric-tools -c "setnet.sh"

Replace core.yaml file with one from fabric-builders

docker run --rm -v ${PWD}:/local:ro -v small_config:/etc/hyperledger/fabric -w /etc/hyperledger/fabric --entrypoint=/bin/bash hyperledgendary/fabric-tools -c "cp /local/core.yaml ."

Update the small network docker-compose.yaml file to use hyperledgendary/fabric-builder-peer images

sed -i.bak 's|hyperledger/fabric-peer|hyperledgendary/fabric-builder-peer|g' docker-compose.yaml

Bring up the docker compose network

docker-compose -f docker-compose.yaml up -d orderer.example.com peer0.humboldt.example.com couchdb cli

Create the Fabric network

docker exec humboldt.cli mknet.sh

Package and install FabCar chaincode

Create a connection.json file with details of how Fabric will connect to the external service chaincode

{
  "address": "fabcar.example.com:9999",
  "dial_timeout": "10s",
  "tls_required": false
}

Package the connection.json file using the pkgcc.sh script

pkgcc.sh -l fabcar -t external connection.json

Copy the chaincode package to a docker volume for installing

docker run --rm -v ${PWD}:/local:ro -v small_cli:/var/hyperledgendary/fdn -w /var/hyperledgendary/fdn --entrypoint=/bin/bash hyperledgendary/fabric-tools -c "cp /local/fabcar.tgz ."

Install the new chaincode package

docker exec humboldt.cli peer lifecycle chaincode install /var/hyperledgendary/fdn/fabcar.tgz

Run FabCar chaincode

Create a chaincode.env file, making sure the CHAINCODE_ID matches the chaincode code package identifier from the install command

CHAINCODE_SERVER_ADDRESS=fabcar.example.com:9999
CHAINCODE_ID=fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b

Run the chaincode

docker run -it --rm -v ${PWD}:/local:ro --name fabcar.example.com --hostname fabcar.example.com --env-file chaincode.env --network=small_fabricdev hyperledger/fabcar-sample

Approve and commit the FabCar chaincode

Approve the chaincode, making sure the CHAINCODE_ID matches the chaincode code package identifier from the install command

docker exec humboldt.cli peer lifecycle chaincode approveformyorg -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID smallchannel --name fabcar --version 1 --sequence 1 --waitForEvent --package-id fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b

Commit the chaincode

docker exec humboldt.cli peer lifecycle chaincode commit -o orderer.example.com:7050 --tls true --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID smallchannel --name fabcar --version 1 --sequence 1

Run some transactions!

Initialise some cars

docker exec humboldt.cli peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C smallchannel -n fabcar -c '{"function":"initLedger","Args":[]}'

Query all cars

docker exec humboldt.cli peer chaincode query -C smallchannel -n fabcar -c '{"Args":["queryAllCars"]}'

Create a car

docker exec humboldt.cli peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C smallchannel -n fabcar -c '{"function":"createCar","Args":["CAR99","Nissan","Leaf","Green","Amy"]}'

Query a car

docker exec humboldt.cli peer chaincode query -C smallchannel -n fabcar -c '{"function":"queryCar","Args":["CAR99"]}'

Good luck!

Clean up

Stop the FabCar chaincode, then teardown the Fabric network

docker-compose -f docker-compose.yaml down --volumes --remove-orphans
Clone this wiki locally