Fabric 2.0 除了原有的 docker builder
之外新增了 external builder
,一个 external builder
的实现提供 detect
、build
、release
、run
可执行命令。
detect
:接受两个参数source dir
、metadata dir
,用于探测客户端提交的chaincode
是否可用此builder
处理build
:接受两个参数source dir
、metadata dir
、build dir
,从源码构建出目标文件并放入build dir
release
:可选命令,接受两个参数build dir
、release dir
run
:接受两个参数build dir
、launch dir
,运行chaincode
,launch dir
目录下有chaincode.json
包含运行需要的参数。
External Builder Build 过程
实践
这里只实践 external builder
build 部分
Fabric v2.0.1
go version go1.13.7 darwin/amd64
1. 准备工作
可使用 Fabric 2.0 debug 环境准备 脚本快速搭建,手动步骤如下:
- 创建临时目录
export TMP=/tmp/fabric
mkdir -p $TMP Builder
准备,复制 externalbuilders 到$TMP
下Chaincode
准备,复制 chaincode 到 $TMP下- 配置文件准备,复制 sampleconfig 下的文件到
$TMP
下,不是sampleconfig
本身是其下内容 peer
可执行程序,编译或下载peer
到$TMP/bin
下- 修改
peer
配置(即core.yaml
)peer: # Path on the file system where peer will store data (eg ledger). This # location must be access control protected to prevent unintended # modification that might corrupt the peer operations. fileSystemPath: production/peer vm: # Endpoint of the vm management system. For docker can be one of the following in general # unix:///var/run/docker.sock # http://localhost:2375 # https://localhost:2376 endpoint: chaincode: # List of directories to treat as external builders and launchers for # chaincode. The external builder detection processing will iterate over the # builders in the order specified below. externalBuilders: - path: /tmp/fabric/externalbuilders/golang name: external-golang environmentWhitelist: - GOPROXY - GOCACHE - GOPATH
- 设置配置文件路径
export FABRIC_CFG_PATH=$TMP
- 完整目录结构如下
. ├── bin │ └── peer ├── configtx.yaml ├── core.yaml ├── externalbuilders │ ├── binary │ │ └── bin │ │ ├── build │ │ ├── detect │ │ ├── release │ │ └── run │ └── golang │ └── bin │ ├── build │ ├── detect │ ├── release │ └── run ├── module │ ├── go.mod │ ├── go.sum │ └── main.go ├── msp │ ├── admincerts │ │ └── admincert.pem │ ├── cacerts │ │ └── cacert.pem │ ├── config.yaml │ ├── keystore │ │ └── key.pem │ ├── signcerts │ │ └── peer.pem │ ├── tlscacerts │ │ └── tlsroot.pem │ └── tlsintermediatecerts │ └── tlsintermediate.pem └── orderer.yaml
2. 打包 Chaincode
参数选项说明:
-p
:链码路径
-l
:链码实现语言
--label
:链码标签元信息,值 golang-external
请参考 detect 脚本
args[0]
:./chaincode.tgz,打包输出文件
bin/peer lifecycle chaincode package --label golang-external -p ./module/ -l golang ./chaincode.tgz
3. 启动 peer
测试过程中未能正确找到 GOCACHE
,原因暂不明。 启动 peer
时手动设置一个。
GOCACHE=$(go env GOCACHE) bin/peer node start
2020-03-12 14:31:25.024 CST [nodeCmd] serve -> INFO 001 Starting peer:
Version: 2.0.1
Commit SHA: 1cfa5da98
Go version: go1.13.7
OS/Arch: darwin/amd64
Chaincode:
Base Docker Namespace: hyperledger
Base Docker Label: org.hyperledger.fabric
Docker Namespace: hyperledger
2020-03-12 14:31:25.024 CST [peer] getLocalAddress -> INFO 002 Auto-detected peer address: 192.168.43.62:7051
2020-03-12 14:31:25.024 CST [peer] getLocalAddress -> INFO 003 Host is 0.0.0.0 , falling back to auto-detected address: 192.168.43.62:7051
2020-03-12 14:31:25.091 CST [gossip.service] New -> INFO 004 Initialize gossip with endpoint 192.168.43.62:7051
2020-03-12 14:31:25.092 CST [gossip.gossip] New -> INFO 005 Creating gossip service with self membership of Endpoint: , InternalEndpoint: 192.168.43.62:7051, PKI-ID: 0d46737a45894d123895671221dbaddf8480fb0364f404be3aed491df442945f, Metadata:
2020-03-12 14:31:25.092 CST [gossip.gossip] New -> WARN 006 External endpoint is empty, peer will not be accessible outside of its organization
2020-03-12 14:31:25.092 CST [gossip.gossip] start -> INFO 007 Gossip instance 192.168.43.62:7051 started
2020-03-12 14:31:25.094 CST [ledgermgmt] NewLedgerMgr -> INFO 008 Initializing LedgerMgr
2020-03-12 14:31:25.240 CST [leveldbhelper] openDBAndCheckFormat -> INFO 009 DB is empty Setting db format as 2.0
2020-03-12 14:31:25.252 CST [fsblkstorage] NewProvider -> INFO 00a Creating new file ledger directory at /Users/dian/tmp/fabric/production/peer/ledgersData/chains/chains
2020-03-12 14:31:25.341 CST [leveldbhelper] openDBAndCheckFormat -> INFO 00b DB is empty Setting db format as 2.0
2020-03-12 14:31:25.504 CST [leveldbhelper] openDBAndCheckFormat -> INFO 00c DB is empty Setting db format as 2.0
2020-03-12 14:31:25.516 CST [ledgermgmt] NewLedgerMgr -> INFO 00d Initialized LedgerMgr
2020-03-12 14:31:25.518 CST [lifecycle] InitializeLocalChaincodes -> INFO 00e Initialized lifecycle cache with 0 already installed chaincodes
2020-03-12 14:31:25.519 CST [nodeCmd] computeChaincodeEndpoint -> INFO 00f Entering computeChaincodeEndpoint with peerHostname: 192.168.43.62
2020-03-12 14:31:25.519 CST [nodeCmd] computeChaincodeEndpoint -> INFO 010 Exit with ccEndpoint: 192.168.43.62:7052
2020-03-12 14:31:25.519 CST [nodeCmd] createChaincodeServer -> WARN 011 peer.chaincodeListenAddress is not set, using 192.168.43.62:7052
2020-03-12 14:31:25.523 CST [sccapi] DeploySysCC -> INFO 012 deploying system chaincode 'lscc'
2020-03-12 14:31:25.523 CST [sccapi] DeploySysCC -> INFO 013 deploying system chaincode 'cscc'
2020-03-12 14:31:25.523 CST [sccapi] DeploySysCC -> INFO 014 deploying system chaincode 'qscc'
2020-03-12 14:31:25.524 CST [sccapi] DeploySysCC -> INFO 015 deploying system chaincode '_lifecycle'
2020-03-12 14:31:25.524 CST [nodeCmd] serve -> INFO 016 Deployed system chaincodes
2020-03-12 14:31:25.524 CST [discovery] NewService -> INFO 017 Created with config TLS: false, authCacheMaxSize: 1000, authCachePurgeRatio: 0.750000
2020-03-12 14:31:25.524 CST [nodeCmd] registerDiscoveryService -> INFO 018 Discovery service activated
2020-03-12 14:31:25.524 CST [nodeCmd] serve -> INFO 019 Starting peer with ID=[jdoe], network ID=[dev], address=[192.168.43.62:7051]
2020-03-12 14:31:25.524 CST [nodeCmd] serve -> INFO 01a Started peer with ID=[jdoe], network ID=[dev], address=[192.168.43.62:7051]
2020-03-12 14:31:25.524 CST [kvledger] LoadPreResetHeight -> INFO 01b Loading prereset height from path [/Users/dian/tmp/fabric/production/peer/ledgersData/chains]
2020-03-12 14:31:25.524 CST [fsblkstorage] preResetHtFiles -> INFO 01c No active channels passed
4. Install Chaincode
开启一个新终端,设置 FABRIC_CFG_PATH
为前面创建的临时目录。
--peerAddresses
为 peer
RPC
接口地址
./chaincode.tgz
是第2步打包的 chaincode
export FABRIC_CFG_PATH=/tmp/fabric
bin/peer lifecycle chaincode install --peerAddresses 127.0.0.1:7051 ./chaincode.tgz
2020-03-12 14:42:21.533 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nPgolang-external:14893d10c6c01ce892588e83ecc0fb911a0de131d51a4d23547056775840f495\022\017golang-external" >
2020-03-12 14:42:21.533 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: golang-external:14893d10c6c01ce892588e83ecc0fb911a0de131d51a4d23547056775840f495
peer
服务端输出如下:
2020-03-12 14:42:20.380 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 01d { command=detect
2020-03-12 14:42:20.380 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 01e "path": "module", command=detect
2020-03-12 14:42:20.380 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 01f "type": "golang", command=detect
2020-03-12 14:42:20.380 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 020 "label": "golang-external" command=detect
2020-03-12 14:42:20.380 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 021 } command=detect
2020-03-12 14:42:20.402 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 022 { command=build
2020-03-12 14:42:20.402 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 023 "path": "module", command=build
2020-03-12 14:42:20.402 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 024 "type": "golang", command=build
2020-03-12 14:42:20.402 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 025 "label": "golang-external" command=build
2020-03-12 14:42:20.402 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 026 } command=build
2020-03-12 14:42:20.572 CST [chaincode.externalbuilder.external-golang] waitForExit -> INFO 027 module command=build
2020-03-12 14:42:21.532 CST [lifecycle] InstallChaincode -> INFO 028 Successfully installed chaincode with package ID 'golang-external:14893d10c6c01ce892588e83ecc0fb911a0de131d51a4d23547056775840f495'
2020-03-12 14:42:21.532 CST [endorser] callChaincode -> INFO 029 finished chaincode: _lifecycle duration: 1167ms channel= txID=40ba5ea9
2020-03-12 14:42:21.532 CST [comm.grpc.server] 1 -> INFO 02a unary call completed grpc.service=protos.Endorser grpc.method=ProcessProposal grpc.peer_address=127.0.0.1:52249 grpc.code=OK grpc.call_duration=1.167976475s
构建完成 externalbuilder/builds
目录内容如下:
production/peer/externalbuilder/builds/
└── golang-external-14893d10c6c01ce892588e83ecc0fb911a0de131d51a4d23547056775840f495
├── bld
│ └── chaincode
├── build-info.json
└── release