目录
官方basic-network开发者模式
打开三个terminals,都进入chaincode-docker-devmode
中
Terminal 1 - Start the network
docker-compose -f docker-compose-simple.yaml up
以上使用
SingleSampleMSPSolo
排序节点配置文件启动网络,并以dev mode
启动peer节点。 它还启动了两个额外的容器——一个用于链码环境和一个用于与链码交互的 CLI。 创建和加入通道的命令在 CLI 容器中使用,因此我们可以立即跳转到链码调用。
Terminal 2 - Build & start the chaincode
执行命令
docker exec -it chaincode bash
输出为:
root@d2629980e76b:/opt/gopath/src/chaincode#
编译链码
cd chaincode_example02/go
go build -o chaincode_example02
运行链码
CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./chaincode_example02
The chaincode is started with peer and chaincode logs indicating successful registration with the peer.
Note that at this stage the chaincode is not associated with any channel. This is done in subsequent steps using theinstantiate
command.
Terminal 3 - Use the chaincode
即使处于
--peer-chaincodedev
模式,仍必须安装链码,以便生命周期系统链码可以正常通过检查。
使用CLI执行下列操作
docker exec -it cli bash
peer chaincode install -p chaincodedev/chaincode/chaincode_example02/go -n mycc -v 0
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a","100","b","200"]}' -C myc
invoke to move 10
from a
to b
.
peer chaincode invoke -n mycc -c '{"Args":["invoke","a","b","10"]}' -C myc
query a
. 返回值应为 90
.
peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C myc
Testing new chaincode
默认情况下,我们只挂载
chaincode_example02
。 但是,您可以通过将不同的链码添加到chaincode
子目录并重新启动网络来轻松测试不同的链码。 此时,它们将可以在您的chaincode
容器中访问。
链码开发的一般过程
1 本地撰写代码
2 推送到远程
3 在相应的节点上安装链码/智能合约
# (peer0.org1)
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
# (peer1.org1)
export CORE_PEER_ADDRESS=peer1.org1.example.com:7051
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
# (peer0.org2)
export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
# (peer1.org2)
export CORE_PEER_ADDRESS=peer1.org2.example.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
4 实例化链码
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'OR ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
5 查询智能合约/链码
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
6 调用链码
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
开发者模式
1 关闭之前的网络
./byfn.sh down
2 修改docker-compose-cli.yaml
在basic-network的docker-compose.yaml里边有示例代码
# command: peer node start --peer-chaincodedev=true
每一个节点都需要配置,所以直接在base/peer-base.yaml中修改,将command: peer node start
注释,增加command: peer node start --peer-chaincodedev=true
。注意对齐。
3 增加peer节点的通信端口
7051
和7053
端口用于TLS安全通信,增加7052
端口用于非加密通信。
4 以开发者模式运行网络
启动网络
export IMAGE_TAG=latest
export COMPOSE_PROJECT_NAME=devnet
docker-compose -f docker-compose-cli.yaml up -d
创建和加入通道并更新锚节点
略
安装链码
略
退出cli,在本地进行以下操作:
进入node.js链码工程文件夹(chaincode/mycc)
CORE_CHAINCODE_ID_NAME="mycc:v1.0" npm start -- --peer.address grpc://172.17.0.1:7052
标签:CORE,系列,chaincode,开发,peer,链码,com,example
From: https://www.cnblogs.com/gangao/p/16648067.html