Fabric编译安装,go和docker环境,fabric源码,fabric网络配置,peer和orderer配置,链码配置。
更多区块链技术与应用分类:
一.Fabric编译安装
wget及git环境
yum install wget
yum install git
git --version
git config --global user.name "centos7_test2"
git config --global user.email [email protected]
git config --list
配置ssh
ssh-keygen -t rsa -C "[email protected]"
cat ~/.ssh/id_rsa.pub
复制到github上
go环境
wget -c https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
(下载包含bin的tar包)
sudo tar -C /usr/local/ -zxvf go1.9.2.linux-amd64.tar.gz
sudo vim /etc/profile
底下加:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=/project/goworkspace
source /etc/profile
安装docker
sudo yum -y install epel-release
sudo yum install docker python-pip
sudo pip install --upgrade pip
sudo pip install docker-compose
(使用 curl -fsSL https://get.docker.com/ | sh 安装最新版docker免去后面编译error)
sudo service docker start
检查是否打开http通道
sudo curl -XPOST --unix-socket /var/run/docker.sock -d '{"Image":"nginx"}' -H 'Content-Type: application/json' http://localhost/containers/create
下载fabric源码
sudo mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
sudo git clone https://github.com/hyperledger/fabric.git
sudo git checkout -b v1.1.0
安装编译相关依赖
sudo go get github.com/golang/protobuf/protoc-gen-go
sudo mkdir -p $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
sudo cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
yum install -y gcc libtool libltdl-dev libtool-ltdl-devel openssl
编译fabric
cd $GOPATH/src/github.com/hyperledger/fabric
sudo make release (go版本必须1.10以上,使用1.10.3)
sudo make docker (保证网络可以翻墙)
注意点
1.若上一次chaintool 下载不完全出错,先把.build/bin/chaintool 文件删除,再输命令
2.若提示.build/docker/gotools/bin/protoc-gen-go no such file ,则:
sudo cp protoc-gen-go /project/goworkspace/src/github.com/hyperledger/fabric/.build/docker/gotools/bin/
3.出现curl: (7) Failed to connect to 127.0.0.1 port 8118: Connection refused:
(shadowsocks+privoxy)换个xshell窗口却没问题
生成可执行文件
生成可执行二进制文件路径:
/project/goworkspace/src/github.com/hyperledger/fabric/release/linux-amd64/bin
将生成的二进制文件放入系统目录
sudo cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* /usr/local/bin
进入该目录执行
sudo chmod -R 775 configtxgen
sudo chmod -R 775 configtxlator
sudo chmod -R 775 cryptogen
sudo chmod -R 775 peer
sudo chmod -R 775 orderer
测试:(出现正常版本号)
peer version
xx version
...
注意点
peer 运行出现错误:
目前无法解决
弃用文件夹中执行文件:
/project/goworkspace/src/github.com/hyperledger/fabric/release/linux-amd64/bin
换用官方可执行二进制包到/usr/local/bin
二. fabric运行配置
docker镜像文件获取方式(但不用)
(1)使用源码(前面)
make docker
(2)从Docker仓库下载
开始运行fabric网络
sudo mkdir -p /opt/hyperledger/fabricconfig
cryptogen showtemplate
vim /opt/hyperledger/fabricconfig/crypto-config.yaml
OrdererOrgs:
- Name: Orderer
Domain: qklszzn.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.qklszzn.com
Template:
Count: 2
Users:
Count: 3
- Name: Org2
Domain: org2.qklszzn.com
Template:
Count: 2
Users:
Count: 2
执行:
cryptogen generate --config=crypto-config.yaml --output ./crypto-config
提取域名:
tree -L 5
vim /etc/hosts
192.168.127.41 orderer.qklszzn.com
192.168.127.41 peer0.org1.qklszzn.com
192.168.127.41 peer1.org1.qklszzn.com
192.168.127.41 peer0.org2.qklszzn.com
192.168.127.41 peer1.org2.qklszzn.com
192.168.127.41 peer3.org1.qklszzn.com
cp -r /project/goworkspace/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml /opt/hyperledger/order/
(层级之间严格对齐,不可有制表符)
Profiles:
TestTwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TestTwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/ordererOrganizations/qklszzn.com/msp
- &Org1
# DefaultOrg defines the organization which is used in the sampleconfig
# of the fabric.git development environment
Name: Org1MSP
# ID to load the MSP definition as
ID: Org1MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/msp
AnchorPeers:
# AnchorPeers defines the location of peers which can be used
# for cross org gossip communication. Note, this value is only
# encoded in the genesis block in the Application section context
- Host: peer0.org1.qklszzn.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org2.qklszzn.com/msp
AnchorPeers:
- Host: peer0.org2.qklszzn.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.qklszzn.com:7050
# Batch Timeout: The amount of time to wait before creating a batch
BatchTimeout: 2s
# Batch Size: Controls the number of messages batched into a block
BatchSize:
# Max Message Count: The maximum number of messages to permit in a batch
MaxMessageCount: 10
# Absolute Max Bytes: The absolute maximum number of bytes allowed for
# the serialized messages in a batch.
AbsoluteMaxBytes: 98 MB
# Preferred Max Bytes: The preferred maximum number of bytes allowed for
# the serialized messages in a batch. A message larger than the preferred
# max bytes will result in a batch larger than preferred max bytes.
PreferredMaxBytes: 512 KB
Kafka:
# Brokers: A list of Kafka brokers to which the orderer connects
# NOTE: Use IP:port notation
Brokers:
- 127.0.0.1:9092
# Organizations is the list of orgs which are defined as participants on
# the orderer side of the network
Organizations:
Application: &ApplicationDefaults
# Organizations is the list of orgs which are defined as participants on
# the application side of the network
Organizations:
cd /opt/hyperledger/order
生成创世块文件(orderer.genesis.block)
export FABRIC_CFG_PATH=$PWD
configtxgen -profile TestTwoOrgsOrdererGenesis -outputBlock ./orderer.genesis.block
账本初始块的生成(roberttestchannel.tx,该文件用来生成channel)
configtxgen -profile TestTwoOrgsChannel -outputCreateChannelTx ./roberttestchannel.tx -channelID roberttestchannel
生成锚点文件
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID roberttestchannel -asOrg Org1MSP
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID roberttestchannel -asOrg Org2MSP
Orderer节点启动
复制源码中启动文件到orderer文件夹
cp /project/goworkspace/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml /opt/hyperledger/order/
并修改
General:
LedgerType: file
ListenPort: 7050
Enabled: false
RootCAs:
ClientAuthRequired: false
ClientRootCAs:
Keepalive:
ServerMinInterval: 60s
ServerInterval: 7200s
ServerTimeout: 20s
LogLevel: debug
GenesisMethod: file
GenesisProfile: TestOrgsOrdererGenesis
GenesisFile: /opt/hyperledger/order/orderer.genesis.block
LocalMSPID: OrdererMSP
Profile:
Enabled: false
Address: 0.0.0.0:6060
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
Authentication:
TimeWindow: 15m
FileLedger:
Location: /var/hyperledger/production/orderer
Prefix: hyperledger-fabric-ordererledger
RAMLedger:
HistorySize: 1000
Kafka:
Retry:
ShortInterval: 5s
ShortTotal: 10m
LongInterval: 5m
LongTotal: 12h
NetworkTimeouts:
DialTimeout: 10s
ReadTimeout: 10s
WriteTimeout: 10s
Metadata:
RetryBackoff: 250ms
RetryMax: 3
Producer:
RetryBackoff: 100ms
RetryMax: 3
Consumer:
RetryBackoff: 2s
Verbose: false
TLS:
Enabled: false
PrivateKey:
Certificate:
LocalMSPID: OrdererMSP
Profile:
Enabled: false
Address: 0.0.0.0:6060
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
Authentication:
TimeWindow: 15m
FileLedger:
Location: /opt/hyperledger/order/production/orderer
Prefix: hyperledger-fabric-ordererledger
RootCAs:
Version:
Debug:
BroadcastTraceDir:
DeliverTraceDir:
启动orderer
cd /opt/hyperledger/order
orderer start
三. Peer节点启动
配置文件
mkdir -p /opt/hyperledger/peer
cp /project/goworkspace/src/github.com/hyperledger/fabric/sampleconfig/core.yaml /opt/hyperledger/peer/
修改为(见附件):
export set FABRIC_CFG_PATH=/opt/hyperledger/peer/
peer node start >> log_peer.log 2>&1 &
创建通道
cd ../order/
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/users/[email protected]/msp
(若退出:则 lsof -i:7051杀掉进场重进,如下才是正确的)
peer channel create -t 50 -o orderer.qklszzn.com:7050 -c roberttestchannel -f /opt/hyperledger/order/roberttestchannel.tx
(一定要在cd ../order/,生成.block文件)
加入通道
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/users/[email protected]/msp
peer channel join -b /opt/hyperledger/order/roberttestchannel.block
(注意roberttestchannel.block生成路径)
更新锚节点
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.qklszzn.com:7051
export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/users/[email protected]/msp
peer channel update -o orderer.qklszzn.com:7050 -c roberttestchannel -f /opt/hyperledger/order/Org1MSPanchors.tx
部署chaincode
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.qklszzn.com:7051
export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/users/[email protected]/msp
(此部若报错,则将/opt/hyperledger/peer/core.yaml中的mode值改为net,再重启orderer和peer)
peer chaincode install -n r_test_cc6 -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
实例化chaincode代码
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_ADDRESS=peer0.org1.qklszzn.com:7051
export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/users/[email protected]/msp
peer chaincode instantiate -o orderer.qklszzn.com:7050 -C roberttestchannel -n r_test_cc6 -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
peer chaincode invoke -o orderer.qklszzn.com:7050 -C roberttestchannel -n r_test_cc6 -c '{"Args":["invoke","a","b","1"]}'