Setting up your development environment for Hyperledger Fabric | Skcript

(S/SVR) #1

The Directory Structure

Having a clean directory structure enables Fabric programmers to instinctively know where each file should sit. Here is our approach to organising the Fabric dev environment.

Where should the Chaincode go?

A lot of developers, mount the chaincode directly from the development folder - that’s a terrible idea.

What you should do is develop chaincode in your GOPATH under the vendor-name You can also write a script which will copy your chaincode under GOPATH and run a go build command to spit out the go-errors before running it in your Fabric tools. This will reduce your development to production time.

Here’s a simple script to copy your chaincode,

#!/bin/bash echo "Copying your chaincode to the place where it belongs." rm -rf $GOPATH/src/ & mkdir -p $GOPATH/src/ cp -Rf chaincode/\* $GOPATH/src/

If you want to build the code, just add this,

#!/bin/bash echo "Stepping into the chaincode's directory" cd $GOPATH/src/ echo "Building your chaincode. Tada." go build .

This will build once when you finish your chaincode development. It will deal with all the ‘unused-variables’, ‘scope-problems’ and any other Go warnings.

Starting Your Network

Let’s assume that your Fabric network is up and running. Ok? What if you didn’t have a network running? Yeah. I can understand.

I am keeping this article as simple as possible. So, follow these steps as of now. The deep philosophies behind every single step will be explained later.

git clone

If you don’t have Fabric installed in your environment,

cd fabric-samples/scripts ./

Then proceed with this,

cd fabric-samples/basic-network vi docker-compose.yml

Under CLI services, see the volumes mounted. You can see that the chaincode is mounted with a relative folder. Let’s change it,

cli: container_name: cli image: hyperledger/fabric-tools tty: true environment: - GOPATH=/opt/gopath - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=cli - - CORE_PEER_LOCALMSPID=Org1MSP - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/ - CORE_CHAINCODE_KEEPALIVE=10 working_dir: /opt/gopath/src/ command: /bin/bash volumes: - /var/run/:/host/var/run/ - $GOPATH/src/ - ./crypto-config:/opt/gopath/src/ networks: - basic

If you see at Line 18, it has been changed with respect to GOPATH. 😄

Now let’s start our docker-compose i.e, our network with our chaincode mounted to it.

Let’s do it in our style. Create a bash file named,

#!/bin/bash # cd basic-network ./

Now the fabric-network is up and running. You can check it by typing docker ps in console.

Installing the Chaincode

Now our time to launch our armageddon. So we have our network running. Lets install the chaincode using the bash script we wrote,

#!/bin/bash # LANGUAGE=golang CC_VERSION=1.0 docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode install -n acmebc -v $CC_VERSION -p "$CC_SRC_PATH" -l "$LANGUAGE" docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode instantiate -o -C mychannel -n acmebc -l "$LANGUAGE" -v $CC_VERSION -c '{"Args":\\\[""\\\]}' -P "OR ('Org1MSP.member')"

That’s all! You might be wondering, why do we need to write a bash script for every small process? Answer to that question: You can write a single bash master script to control everything with logical-conditions.

Now that we have our chaincode installed. You can try the query/invoke commands and check how it’s working. Or you can ping the application that you integrated with the network.

Updating the Chaincode

This is where developers waste time. Updating the code, building it and deploying it again by updating the chaincode-version. Doing it in our style, let’s write a script.

#!/bin/bash LANGUAGE=golang CC_VERSION=1.0 TEMP_CC_VERSION+=$(date +".%I.%M.%S") # Appending with temporary timestamp. # Replace the chaincode with our new updated code. rm -rf $GOPATH/src/ & mkdir -p $GOPATH/src/ cp -Rf chaincode/\\\* $GOPATH/src/ # Building the go chaincode cd $GOPATH/src/ go build . # Run the update action docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode install -n acmebc -v $TEMP_CC_VERSION -p "$CC_SRC_PATH" -l "$LANGUAGE" docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode upgrade -o -C mychannel -n acmebc -l "$LANGUAGE" -v $TEMP_CC_VERSION -c '{"Args":\\\["init"\\\]}' -P "OR ('Org1MSP.member')"

That’s it. Updated, yo! Run your queries and see the magic.

Click here to learn more about Hyperleger and Blockchain straight from our developers.

This is a companion discussion topic for the original entry at