fabric 双节点共用一套chaincode实现资产管理

#

借鉴官方下载的fabric-sample示例,我想实现在两个节点上同时使用一套chaincode,可以在不同节点上进行资产管理

chaincode可以自行百度,我使用的功能大致有以下几种:
if fn == “set” {
result, err = set(stub, args)
}
if fn == “get” {
// assume ‘get’ even if fn is nil
result, err = get(stub, args)
}
if fn == “invoke”{
//make payment of X units from A to B
return t.invoke(stub, args)
}
if fn == “delete” {
return t.delete(stub, args)
}
if fn == “history”{
return t.HistoryQuery(stub, args)
}
if fn == “add” {
return t.add(stub, args)
}
if fn == “deposit” {
return t.deposit(stub, args)
}
if fn == “cert” {
return t.testCertificate(stub, args)
}
if err != nil {
return shim.Error(err.Error())
}
首先修改配置文件docker-compose-simple,新增一个peer1、一个cli1以及一个chaincode1

peer1:
container_name: peer1
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer1
- CORE_PEER_ADDRESS=peer1:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1:7051
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start –peer-chaincodedev=true -o orderer:7050
ports:
- 8051:7051
- 8053:7053
depends_on:
- orderer
cli1:
container_name: cli1
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=cli1
- CORE_PEER_ADDRESS=peer1:7051
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
working_dir: /opt/gopath/src/chaincodedev
command: /bin/bash -c ‘./script1.sh’
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
- ./../chaincode:/opt/gopath/src/chaincodedev/chaincode
- ./:/opt/gopath/src/chaincodedev/
depends_on:
- orderer
- peer1
chaincode1:
container_name: chaincode1
image: hyperledger/fabric-ccenv
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=chaincode1
- CORE_PEER_ADDRESS=peer1:7051
- CORE_CHAINCODE_ID_NAME=mycc:0
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
working_dir: /opt/gopath/src/chaincode
command: /bin/bash -c ./sacc/sacc ‘sleep 6000000’
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
- ./../chaincode:/opt/gopath/src/chaincode
depends_on:
- orderer
- peer1

cli1运行一个script1.sh脚本主要用于创建通道以及把节点加入通道
peer channel create -c myc -f myc.tx -o orderer:7050
peer channel join -b myc.block
chaincode1配置中运行一个脚本/bin/bash -c ./sacc/sacc 执行我的chaincode,在对应的working_dir下
注意
在多个peer中只需要第一个脚本创建通道,其余的peer只需要加入到通道就OK了,多次创建通道会导致前面的peer在通道无效。

执行 docker-compose -f docker-compose-simple up
然后进入cli0: docker exec -it cli0 bash
安装链码:peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0
实例化链码:peer chaincode instantiate -n mycc -v 0 -c ‘{“Args”:[“a”,”100”,”b”,”200”]}’ -C myc
执行invoke:peer chaincode invoke -n mycc -c ‘{“Args”:[“set”, “c”, “300”]}’ -C myc
进入cli1: docker exec -it cli1 bash
安装链码:peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0
查询C的值:peer chaincode invoke -n mycc -c ‘{“Args”:[“get”, “c”]}’ -C myc

这就实现了在不同节点上可以对资产的管理
注意
链码需要在每个节点都安装一遍,但是实例化只需要执行一次

你可能感兴趣的:(区块链,fabric,区块链)