//

Setting up HyperLedger Fabric Development and Testing Environment on CentOS

Although HyperLedger Fabric provides a ready-to-use Vagrant Box environment, to deepen the understanding of it, I decided to build a development and testing environment from scratch.

I use VMWare to install CentOS. If using Virtual Box, the operation is similar.


Install CentOS

Download CentOS 7 and install it. I use the x86-64 Minimal version.

Install Docker

Preparation before installation

Check if the kernel version supports it (Docker requires 3.10+). CentOS 7 supports it completely.

# uname -a
3.10.0-327.el7.x86_64

Install Docker using yum

1. Update yum
sudo yum update
2. Add yum Repo
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
3. Install Docker
sudo yum install docker-engine
4. Enable Docker to start as a Service
sudo systemctl enable docker.service
5. Start Docker Service
sudo systemctl start docker
6. Install docker-compose

docker-compose is a docker cluster management tool, which can define and start multiple docker containers with one click.

curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Note: The docker-compose.yaml file on HyperLedger github requires the latest docker-compose version to parse.

Build Fabric Development Environment

Download related components

1. docker-compose.yml

You can use the example on HyperLedger github

curl -o docker-compose.yml https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/docker-compose.yml
2. Download Docker image

membersrvc and peer can use standard hyperledger fabric components, so pull directly

docker pull hyperledger/fabric-membersrvc:latest
docker pull hyperledger/fabric-peer:latest

Build client image

1. Download Fabric example Dockerfile
curl -o Dockerfile https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/Dockerfile
2. Build image
docker build -t hyperledger/fabric-starter-kit:latest .
3. Check if images are correct
docker ps -a

Run Fabric Development Environment

1. Start Cluster
docker-compose -f docker-compose.yml up -d
2. Enter Docker

Deploy as development mode, enter peer directly

docker exec -it peer bash
3. Logic

The official Image opens permissions by default, you need to login first, use the built-in user to login.

  • CLI
peer network login jim

Password

6avZQLwcUe9b
  • REST API

You can also use REST API:

POST http://127.0.0.1:7050/registrar
{
    "enrollId": "jim",
    "enrollSecret": "6avZQLwcUe9b"
}

Note: To use REST, you need to add port mapping in the peer of docker-compose.yml file:

ports:
   - "0.0.0.0:7050:7050"
4. Deploy Chaincode
  • CLI
CORE_PEER_ADDRESS=127.0.0.1:7051
peer chaincode deploy -n hyperledger-demo-1 -c '{"Function": "init", "Args": ["a", "1000", "b", "2000"]}' -u jim

Note: Since peer is set to development mode when starting, chaincode runs on starter, not built specifically for chaincode and executed in a separate docker vm. Note: Successful deployment only means that the submitted instruction has been received, not that the instruction execution is complete. Note: Because it runs in development mode (dev), chaincode is already registered when docker starts, so -p path cannot be carried in manual command line or REST parameters, otherwise deployment error:

sending init failed(handler not found for chaincode

If the command is not specified in docker-compose.yml, you can enter starter and register with the following command:

CORE_CHAINCODE_ID_NAME=hyperledger-demo-1 CORE_PEER_ADDRESS=0.0.0.0:7051 ./chaincode_example02
  • REST API
POST http://127.0.0.1:7050/chaincode
{
    "jsonrpc": "2.0",
    "method": "deploy",
    "params": {
        "type": 1,
        "chaincodeID": {
            "name": "hyperledger-demo-1"
        },
        "ctorMsg": {
            "function": "init",
            "args": ["a", "1000", "b", "2000"]
        },
        "secureContext": "jim"
    },
    "id": 1
}
5. Query
  • CLI
peer chaincode query -u jim -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -n hyperledger-demo-1 -c '{"Function": "query", "Args": ["a"]}'
  • REST API
POST http://127.0.0.1:7050/chaincode
{
    "jsonrpc": "2.0",
    "method": "query",
    "params": {
        "type": 1,
        "chaincodeID": {
            "name": "hyperledger-demo-1"
        },
        "ctorMsg": {
            "function": "query",
            "args": ["a"]
        },
        "secureContext": "jim"
    },
    "id": 5
}
6. Transfer
  • CLI
peer chaincode invoke -n hyperledger-demo-1 -c '{"Function": "query", "Args": ["a", "b", "10"]}'
  • REST API
POST http://127.0.0.1:7050/chaincode
{
    "jsonrpc": "2.0",
    "method": "invoke",
    "params": {
        "type": 1,
        "chaincodeID": {
            "name": "hyperledger-demo-1"
        },
        "ctorMsg": {
            "function": "invoke",
            "args": ["a", "b", "100"]
        },
        "secureContext": "jim"
    },
     "id": 3
}
7. Get Block Information
  • REST API
GET http://127.0.0.1:7050/chain/blocks/4

By now, the development environment on CentOS is set up. If you use Mac OSX, there is no need to trouble, use Docker for Mac, you can complete the above operations directly on Mac.

Finally, wish everything goes well.