前言
不知不觉,研究生三年的时光即将告一段落,毕业在即。回想起当初学习的场景,那时候笔记本上记满了密密麻麻的笔记,如今想要扔掉却又舍不得。因此,我决定将当初刚入门区块链时的学习笔记分享出来。由于是跨考的,所以当时的我对区块链甚至是计算机都一窍不通,直接开始了学习之旅。因此,笔记中可能存在一些错误,仅供参考,切勿直接照搬。希望读者能够在阅读的过程中保持思考,同时,大体逻辑是可靠的。如果发现了任何错误,请务必告知我,我将及时进行修改。
备用命令
1、docker命令
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
systemctl daemon-reload
sudo service docker restart
/home/lhc/go-work/src/github.com/hyperledger/fabric/examples/chaincode/go/example02
/home/lhc/go-work/src/github.com/hyperledger/fabric/singlepeer/chaincode/go/example02
2、挂载
vmware-hgfsclient
vmhgfs-fuse .host:/ /mnt/hgfs/
cd /mnt/hgfs/myshare
sudo vmhgfs-fuse .host://mydata /mnt/hgfs -o nonempty -o allow_other
单机单节点
1、复制文件
cp -rf /mnt/hgfs/myshare/singlepeer /home/lhc/go-work/src/github.com/hyperledger/fabric
cd /home/lhc/go-work/src/github.com/hyperledger/fabric/singlepeer
2、创建singlepeer目录
cd $GOPATH/src/github.com/hyperledger/fabric
mkdir singlepeer
cd singlepeer
3、目录
/home/lhc/go-work/src/github.com/hyperledger/fabric/singlepeer
4、生成公私钥和证书
./bin/cryptogen generate --config=crypto-config.yaml
5、生成创世区块
mkdir channel-artifacts
./bin/configtxgen -profile OneOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
6、生成通道配置区块
./bin/configtxgen -profile OneOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
7、启动 orderer 和peer
docker-compose -f docker-compose-cli.yaml up -d
8、启动cli 容器
docker exec -it cli bash
9、创建通道
peer channel create -o orderer.example.com:7050 -c mychannel -f channel-artifacts/mychannel.tx
10、peer加入通道
peer channel join -b mychannel.block
11、安装与运行只能合约
1、安装智能合约
peer chaincode install -n mycc -p github.com/hyperledger/fabric/singlepeer/chaincode/go/example02/cmd -v 1.4
2、实例化智能合约
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["init","A","100","B","200"]}' -P "AND ('Org1MSP.peer')"
Error: could not assemble transaction, err proposal response was not successful, error code 500, msg transaction returned with failure: Incorrect arguments. Expecting a key and a value
版本错了
1.0改为1.4
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","A"]}'
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["A","100"]}' -P "OR ('Org1MSP.peer')"
ERROR: for orderer.example.com Cannot create container for service orderer.example.com: Conflict. The container name "/orderer.example.com" is already in use by container "e90bc8816ae2db60240e880eb02ae58ed92a3d24efa1bb096d7a3988dd416cfd". You have to remove (or rename) that container to be able to reuse that name.
重启docker和网络
3、在peer 节点查询a 显示100
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","A"]}'
4、在peer节点上a向b转账10
peer chaincode invoke -C mychannel -n mycc -c '{"Args":["invoke","A","B","10"]}'
5、在peer 节点查询B 显示210
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","B"]}'