运行环境:ubuntu18.04, 默认本地已安装docker,go,
一、Fabric源码地址
下载Fabric源码,GitHub地址
下载Fabric-Sample源码,GitHub地址
下载Fabric执行程序,GitHub地址
拉取镜像
docker pull hyperledger/fabric-peer:2.3.0
docker pull hyperledger/fabric-orderer:2.3.0
docker pull hyperledger/fabric-ccenv:2.3.0
docker pull hyperledger/fabric-tools:2.3.0
docker pull hyperledger/fabric-baseos:2.3.0
docker pull hyperledger/fabric-ca:1.4.9
准备工作
解压sample文件,放在scripts文件夹下。
解压执行程序,放在sample文件夹内。
搭建步骤
- 创建fabric网络,包括两个节点,一个Order服务
./network.sh up 2.3.0
Creating network "fabric_test" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating peer0.org2.example.com ... done
Creating orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1667543b5634 hyperledger/fabric-tools:latest "/bin/bash" 1 second ago Up Less than a second cli
b6b117c81c7f hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up 1 second 0.0.0.0:7051->7051/tcp peer0.org1.example.com
703ead770e05 hyperledger/fabric-orderer:latest "orderer" 2 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp, 0.0.0.0:7053->7053/tcp orderer.example.com
718d43f5f312 hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up 1 second 7051/tcp, 0.0.0.0:9051->9051/tcp peer0.org2.example.com
- 在组织Org1和Org2之间创建通道,并将节点加入通道
./network.sh createChannel
Channel 'mychannel' joined
- 部署智能合约
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
Chaincode is packaged
- 初始化账本
export FABRIC_CFG_PATH=$PWD/../config/
# Environment variables for Org1
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
-> INFO 001 Chaincode invoke successful. result: status:200
- 数据查询
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[
{"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300},
{"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400},
{"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500},
{"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600},
{"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700},
{"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800}
]
二、Fabric浏览器搭建
BlockChain-exploer源码,GitHub地址
拉取镜像
docker pull hyperledger/explorer
docker pull hyperledger/explorer-db
解压源码
修改配置文件,docker-compose.yaml:
services:
explorer.mynetwork.com:
...
volumes:
- ./examples/net1/config.json:/opt/explorer/app/platform/fabric/config.json
- ./examples/net1/connection-profile:/opt/explorer/app/platform/fabric/connection-profile
- /root/fabric/fabric-2.3.0/scripts/fabric-samples/test-network/organizations:/tmp/crypto
- walletstore:/opt/explorer/wallet
启动容器服务
docker-compose up -d
成功后,打开网址:http://localhost:8080
默认登录账号:exploreradmin 密码:exploreradminpw