All instructions below are composed by referring to

I just tried to simplify them.

Fork this repo and then go to your GitHub account and clone the forked repo


  • Exec all commands in the root folder (in the outermost folder of the project or cloned repo).
  • Do you want to use your chaincode?
    • Yes: Replace “rajan-31” in “cmd/main.go” with your GitHub username
    • No: Go ahead!

Main Instructions

  1. Install binaries for orderer, peer, and configtxgen (there are many possible ways to achieve this. So, I didn’t mention any instructions)

  2. Only if you want to use your chaincode

    • Put chaincode in “my_chaincode” folder (put “main” function in cmd/main.go and other logic in a go file, in my case “fabcar.go”)
  3. Install go packages

    go mod init
    go mod tidy
  4. Set environment variables (if anywhere in the process like no config file found, then check if this env variable is set with echo $FABRIC_CFG_PATH)

    export FABRIC_CFG_PATH=$(pwd)/sampleconfig
  5. Default location Fabric use to store blocks and other data (in 2nd command, use your username)

    sudo mkdir /var/hyperledger
    sudo chown rajan /var/hyperledger
  6. Generate the genesis block for the ordering service

    configtxgen -profile SampleDevModeSolo -channelID syschannel -outputBlock genesisblock -configPath $FABRIC_CFG_PATH -outputBlock "$(pwd)/sampleconfig/genesisblock"
  7. Start orderer

    export FABRIC_CFG_PATH=$(pwd)/sampleconfig
  8. Start the peer in DevMode (in a new terminal)

    export FABRIC_CFG_PATH=$(pwd)/sampleconfig
    FABRIC_LOGGING_SPEC=chaincode=debug CORE_PEER_CHAINCODELISTENADDRESS= peer node start --peer-chaincodedev=true
  9. Create a channel and join (in a new terminal)

    export FABRIC_CFG_PATH=$(pwd)/sampleconfig
    configtxgen -channelID ch1 -outputCreateChannelTx ch1.tx -profile SampleSingleMSPChannel -configPath $FABRIC_CFG_PATH
    peer channel create -o -c ch1 -f ch1.tx
    peer channel join -b ch1.block
  10. Build the chaincode

    # it will build the chaincode and put it in build/bin
    go build -o ./build/bin/smartContract ./my_chaincode/cmd
  11. Start the chaincode in the root folder

    CORE_CHAINCODE_LOGLEVEL=debug CORE_PEER_TLS_ENABLED=false CORE_CHAINCODE_ID_NAME=mycc:1.0 ./build/bin/smartContract -peer.address
  12. Approve and commit the chaincode definition

    peer lifecycle chaincode approveformyorg  -o --channelID ch1 --name mycc --version 1.0 --sequence 1 --init-required --signature-policy "OR ('SampleOrg.member')" --package-id mycc:1.0
    peer lifecycle chaincode checkcommitreadiness -o --channelID ch1 --name mycc --version 1.0 --sequence 1 --init-required --signature-policy "OR ('SampleOrg.member')"
    peer lifecycle chaincode commit -o --channelID ch1 --name mycc --version 1.0 --sequence 1 --init-required --signature-policy "OR ('SampleOrg.member')" --peerAddresses
  13. Query or Invoke chaincode

    peer chaincode invoke -o -C ch1 -n mycc -c '{"Function":"InitLedger","Args":[]}' --isInit
    peer chaincode invoke -o -C ch1 -n mycc -c '{"Function":"QueryAllCars","Args":[]}'
    peer chaincode invoke -o -C ch1 -n mycc -c '{"Function":"QueryCar","Args":["CAR0"]}'
    peer chaincode invoke -o -C ch1 -n mycc -c '{"Function":"CreateCar","Args":["CAR10","Mercedes-Benz", "EQS", "Black", "Rajan"]}'

**Note: See “after-tutorial” branch to compare generated files, such as genesisblock, ch1.block, ch1.tx, go.mod, go.sum, smartContract

After following the above steps, to update your chaincode, you need to do is:

  1. Make any changes you want to chaincode (in my case to fabcar.go)

  2. stop chaincode with “ctrl+c” (chaincode process that you started in step 11)

  3. Repeat step 10 (to rebuild updated chaincode)

  4. start chaincode again (follow step 11)

  5. test chaincode (step 13)

*When you are done, stop orderer, peer, and chaincode.

When you will again start development, you need to do is

  1. Start orderer (step 7)

  2. Start peer in devmode (step 8)

  3. Start Chaincode (step 11)


View Github