Blockchain-Fabric操作手记

进入docker对应主机

docker exec -it dockercompose_vp0_1 bash

注册用户“test_user0”/“jim”

# 由多个预设的用户,有两种注册的方式

peer network login test_user0 -p MS9qrN8hFjlE

# 或者在Rest APIs中

{
  "enrollId": "jim",
  "enrollSecret": "6avZQLwcUe9b"
}

更改Fabric开发形式为:本地模式

### 进入对应主机
docker exec -it dockercompose_vp0_1 bash

### cd /home/blockchain/fabric-images/docker-compose/base
### 备份一下peer-secure-pbft-base.yaml
cp peer-secure-pbft-base.yaml backup-pbft.yaml

开始编辑peer-secure-pbft-base.yaml,

version: '2'
services:
  peer-secure-pbft-base:
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - CORE_PEER_DISCOVERY_PERIOD=60s
      - CORE_PEER_DISCOVERY_TOUCHPERIOD=61s
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_VM_ENDPOINT=unix:///var/run/docker.sock
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
      - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
      - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
      - CORE_SECURITY_ENABLED=true
      - CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=pbft
      - CORE_PBFT_GENERAL_MODE=batch
      - CORE_PBFT_GENERAL_N=4
    command: sh -c "sleep 10; peer node start --peer-chaincodedev" # 修改这里

保存后继续:

### 删除已有的docker容器
docker rm -f $(docker ps -aq)

### 将自己的脚本复制到容器中的某个目录中
docker cp ./give-up-chat.go b489632aa29e:/opt/gopath/src/github.com/giveup

### 进入/opt/gopath/src/github.com/giveup
go build

### 设置Chaincode ID Name, 开始运行
CORE_CHAINCODE_ID_NAME=mycc2 CORE_PEER_ADDRESS=0.0.0.0:7051 ./giveup

至此,本地开发模式已经完成。之后可以再打开一个shell对已有的chaincode节点进行操作。

REST API模式

1. Registrar

地址: IP:7050/registrar, POST

payload/raw模式:

{
  "enrollId": "jim",
  "enrollSecret": "6avZQLwcUe9b"
}

2. Deploy

地址: IP:7050/chaincode, POST

payload/raw模式:

{
  "jsonrpc": "2.0",
  "method": "deploy",
  "params": {
    "type": 1,
    "chaincodeID":{
        "path":"https://github.com/ChinW/learn-chaincode/finished", # 这个可以填写github地址远程启动
        "name": "mycc2" # 在开发模式时必须指定name,可以是上文中指定的Chaincode Id
    },
    "ctorMsg": { # 参数
        "function":"init",
        "args":[]
    },
    "secureContext": "test_user0" #用户名
  },
  "id": 1
}

3. Query

地址: IP:7050/chaincode, POST

payload/raw模式:

{
  "jsonrpc": "2.0",
  "method": "query",
  "params": {
    "type": 1,
    "chaincodeID":{
        "name": "mycc2"
    },
    "ctorMsg": {
        "function":"promiseID",
        "args":[]
    },
    "secureContext": "test_user0"
  },
  "id": 1
}

4. Invoke

Invoke函数中不能有Output 地址: IP:7050/chaincode, POST

payload/raw模式:

{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
    "type": 1,
    "chaincodeID":{
        "name": "mycc2"
    },
    "ctorMsg": {
        "function":"promise",
        "args":[]
    },
    "secureContext": "test_user0"
  },
  "id": 1
}

命令形式

peer chaincode deploy -u test_user0 -p github.com/hyperledger/fabric/examples/giveup -c '{"Args": ["init","a", "100", "b", "200"]}'

set $CCID=XXXXXXXX

peer chaincode query -u test_user0 -n $CCID -c '{"Args": ["promiseID", "a"]}'

peer chaincode deploy -u test_user0 -n mycc2 –c '{"Args":["init"]}'

peer chaincode query -u test_user0 -n mycc2 –c {"Args": ["promiseID", "a"]}'

peer chaincode invoke -u test_user0 -n mycc –c {"Args": ["promiseID", "a"]}'

删除Docker容器并重启Docker容器

docker rm -f $(docker ps -aq)
## cd /root/blockchain-hackzone/scripts/
./startscripts

参考链接

https://github.com/IBM-Blockchain/learn-chaincode

https://yeasy.gitbooks.io/blockchain_guide/content/