详细的环境安装可以微信扫一扫


1.先进入sample的目录启动服务

cd chaincode-docker-devmode

启动相应的docker实例中使用了已经写了相应的编排文件如下所示

.
├── chaincode
├── docker-compose-simple.yaml
├── msp
│   ├── admincerts
│   │   └── admincert.pem
│   ├── cacerts
│   │   └── cacert.pem
│   ├── keystore
│   │   └── key.pem
│   ├── signcerts
│   │   └── peer.pem
│   ├── tlscacerts
│   │   └── tlsroot.pem
│   └── tlsintermediatecerts
│       └── tlsintermediate.pem
├── myc.block
├── myc.tx
├── orderer.block
├── README.rst
└── script.sh

执行命令这个命令是docker-compose编排启动文件如果想后台运行的话可以加个-d参数

docker-compose -f docker-compose-simple.yaml up

注意如果这里出现了证书错误的情况就切换到master主分支

2.开始编译区块链

在第一步中启动程序会为我们启动一个开发环境的容器名为chinacode,我们编译直接进入该容器进行操作

docker exec -it chaincode bash

我看到默认的主目录为chaincode

root@d2629980e76b:/opt/gopath/src/chaincode#

现在编译sacc

cd sacc
go build

编译完直接运行

CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc

显示如下

/saccchaincode/sacc#CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc 
2018-05-04 08:56:18.486 UTC [shim] SetupChaincodeLogging -> INFO 001 Chaincode log level not provided; defaulting to: INFO
2018-05-04 08:56:18.486 UTC [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...

peer容器logs显示已注册channelID不存在

peer         | 2018-05-04 09:40:33.425 UTC [chaincode] beforeRegisterEvent -> DEBU 338 Received REGISTER in state created
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] registerHandler -> DEBU 339 registered handler complete for chaincode mycc:0
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] beforeRegisterEvent -> DEBU 33a Got REGISTER for chaincodeID = name:"mycc:0" , sending back REGISTERED
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] notifyDuringStartup -> DEBU 33b nothing to notify (dev mode ?)
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] notifyDuringStartup -> DEBU 33c sending READY
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] processStream -> DEBU 33d []Move state message READY
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] handleMessage -> DEBU 33e []Fabric side Handling ChaincodeMessage of type: READY in state established
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] enterReadyState -> DEBU 33f []Entered state ready
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] notify -> DEBU 340 notifier Txid:, channelID: does not exist

此时虽然程序已经注册但是未和其他的管道间关联

3.安装实例化

由于上步的执行进行了堵塞的shell,所以这步需要重新开启一个shell进行安装实例化

进入cli开发环境

docker exec -it cli bash

安装

peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["a","10"]}' -C myc

好了这步基本上a向区块链写入了10

如果上步错了想修改20执行如下

peer chaincode invoke -n mycc -c '{"Args":["set", "a", "20"]}' -C myc

想查看下使用如下命令

peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C myc

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐