区块链从入门到精通

【Hyperledger第三讲】:Ubuntu16.04部署hy

2019-03-17  本文已影响0人  洞链

基础环境搭建


操作系统:阿里云Ubuntu16.04
Git、Golang、Nodejs
Docker环境支持:docker、docker-compose
Fabric组件Docker镜像
fabric-samples源码库
Nodejs: 8.9.4

#先看这个搭建Hyperledger Fabric基础环境
https://www.jianshu.com/p/7ff74cfc7125
#(看到安装docker-compose完成即可,接下来看此篇)

安装Node.js


1. 首先,使用下面的命令来安装 nvm(第一种方式,两种选其一即可)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
 nvm ls
nvm install v8.9.4
 nvm list
nvm use v8.9.4
查看 node 版本信息和切换node版本

2. 手动安装Node.js(第二种方式)

wget wget https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.gz
tar -zxvf node-v8.9.4-linux-x64.tar.gz
sudo mv node-v8.9.4-linux-x64 /usr/local/node
sudo ln -s /usr/local/node/bin/node /usr/local/bin/node
sudo ln -s /usr/local/node/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm

3. 测试是否安装成功,以及Node.js并设置镜像加速

node -v
npm -v
npm config set registry https://registry.npm.taobao.org

构建网络


mkdir -p github.com/hyperledger/fabric-samples
git clone https://github.com/hyperledger/fabric-samples.git
git tag
查看版本分支
git checkout  v1.1.0
git branch
git branch -d v1.1.0
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.1.0/hyperledger-fabric-linux-amd64-1.1.0.tar.gz
tar -zxvf hyperledger-fabric-darwin-amd64-1.1.0.tar.gz -C /home/go/src/github.com/hyperledger/fabric-samples/
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/

下载hyperledger-fabric-ca-linux-amd64-1.1.0.tar.gz安装包

hyperledger-fabric-ca-linux-amd64-1.1.0.tar.gz
wget  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.1.0/hyperledger-fabric-ca-linux-amd64-1.1.0.tar.gz
tar -zxvf hyperledger-fabric-ca-linux-amd64-1.1.0.tar.gz -C /home/go/src/github.com/hyperledger/fabric-samples/

第一种方式:(推荐)

wget https://github.com/hyperledger/fabric/blob/release-1.1/scripts/bootstrap.sh
echo "===> Downloading platform specific fabric binaries"
#curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/hyperledger-fabric-${ARCH}-${VERSION}.tar.gz | tar xz

echo "===> Downloading platform specific fabric-ca-client binary"
#curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/${ARCH}-${VERSION}/hyperledger-fabric-ca-${ARCH}-${VERSION}.tar.gz | tar xz

第二种方式:

curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.1.0

第三种方式:(需要kx上网)

curl -sSL https://goo.gl/6wtTN5 | sudo bash -s 1.1.0

cd first-network
是否已经构建,如果构建关闭重新构建
  • .env:存储一些环境变量
  • base:存储docker-compose的一些公共服务
  • byfn.sh:执行脚本
  • configtx.yaml和crypto-config.yaml:根据之前生成的2个工具,生成相应的配置文件,用来启动网络,放到当前目录的channel-artifacts和crypto-config里面
  • dockper-compose:用于启动网络
  • scripts:存放测试脚本,做的事:创建通道、加入通道,安装链码,实例化链码,链码交互

创建first-network


第一种方式:自动创建

./byfn.sh -m down
./byfn.sh -m generate
./byfn.sh -m up

第二种方式:手动创建

./byfn.sh -m down
  1. 生成创世区块
../bin/cryptogen generate --config=./crypto-config.yaml
export FABRIC_CFG_PATH=$PWD
设置变量并创建初始区块
../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
  1. 生成应用通道的配置信息


    生成应用通道的配置信息
export CHANNEL_NAME=mychannel
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channelID $CHANNEL_NAME
  1. 生成锚节点配置更新文件
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

操作网络

编辑docker-compose-cli.yaml,注释command命令

vim docker-compose-cli.yaml
注释`command`命令
运行docker-compose-cli.yaml
image.png
CHANNEL_NAME=$CHANNEL_NAME TIMEOUT=600 docker-compose -f docker-compose-cli.yaml up -d

1. 创建和加入通道

docker exec -it cli bash
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --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
image.png
docker logs orderer.example.com
peer channel join -b mychannel.block
peer channel list

2. 安装并实例化链码


 peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
 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'\'')'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
查询结果
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"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

输入exit退出docker容器并关闭网络

exit
./byfn.sh -m down

参数说明:

Node.js和fabcar交互


https://hyperledger-fabric.readthedocs.io/en/release-1.1/write_first_app.html
cd fabric-samples/fabcar

查看package.json文件

查看`package.json`文件
cat package.json

Fabcar启动


./byfn.sh -m down
docker rm -f $(docker ps -aq)
docker network prune
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
npm install
npm install 中1
npm install 中2
./startFabric.sh  node
node enrollAdmin.js
node registerUser
查询账本

node query.js
node query.js
重新运行query.js
更新账本

vim invoke.js

找到下方代码


image.png
// createCar chaincode function - requires 5 args, ex: args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'],
// changeCarOwner chaincode function - requires 2 args , ex: args: ['CAR10', 'Barry'],
// must send the proposal to endorsing peers
var request = {
  //targets: let default to the peer assigned to the client
  chaincodeId: 'fabcar',
  fcn: '',
  args: [''],
  chainId: 'mychannel',
  txId: tx_id
};

将上方代码更改为以下代码

var request = {
  //targets: let default to the peer assigned to the client
  chaincodeId: 'fabcar',
  fcn: 'createCar',
  args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
  chainId: 'mychannel',
  txId: tx_id
};
node invoke.js
vim query.js
node query.js
可以看到刚才创建的CAR10相关信息

FAQ


  1. 运行./byfn.sh -m down出现错误的解决方法:
https://segmentfault.com/a/1190000014221967
  1. 执行node registerUser.js出现错误:Failed to register: Error: fabric-ca request register failed with errors [[{"code":0,"message":"No identity type provided. Please provide identity type"}]]
    执行`node registerUser.js `出现错误
vim registerUser.js
returnfabric_ca_client.register({enrollmentID: 'user1', affiliation:'org1.department1',role: 'client'}, admin_user);
node registerUser.js替换
  1. 创建通道失败,出现以下几种问题:
  1. 设置marbles . step 4 error error: Caught exception: TypeError: Cannot read property 'getConnectivityState' of undefined出错
npm uninstall grpc
rm -rf node_modules/
npm install
  1. node registerUser.js
    image.png
image.png

参考文档


https://github.com/IBM-Blockchain/marbles/blob/master/docs/use_local_hyperledger.md
https://hyperledger-fabric.readthedocs.io/en/release-1.0/chaincode4noah.html
https://hyperledger-fabric.readthedocs.io/en/release-1.1/write_first_app.html
https://github.com/hyperledger/fabric-samples/tree/release-1.1
上一篇下一篇

猜你喜欢

热点阅读