Hyperledger Fabric 2.0 external builder

Fabric 2.0 除了原有的 docker builder 之外新增了 external builder ,一個 external builder 的實現提供 detectbuildreleaserun 可執行命令。

  • detect :接受兩個參數source dirmetadata dir,用於探測客戶端提交的 chaincode 是否可用此 builder 處理
  • build :接受兩個參數source dirmetadata dirbuild dir,從源碼構建出目標文件並放入build dir
  • release :可選命令,接受兩個參數 build dirrelease dir
  • run :接受兩個參數build dirlaunch dir,運行 chaincodelaunch dir 目錄下有 chaincode.json 包含運行需要的參數。

External Builder Build 過程


ContainerRouterDetectorBuilderCmdBuildCachedBuild(Instance, error)(Instance, error)opt[ Instance not nil ]detectDetectrunCommandStartWaitexit codeerrordetect resultbuilderopt[ detect ok ]loop[ iterator builders ]nilBuildrunCommandStartWaitexit codeerrorerrorReleaserunCommandStartWaitexit codeerrorerror(Instance, error)opt[ builder not nil ](nil, error)ContainerRouterDetectorBuilderCmd

實踐


這裏只實踐 external builder build 部分

Fabric v2.0.1
go version go1.13.7 darwin/amd64

1. 準備工作


可使用 Fabric 2.0 debug 環境準備 腳本快速搭建,手動步驟如下:

  1. 創建臨時目錄
    export TMP=/tmp/fabric
    mkdir -p $TMP
  2. Builder準備,複製 externalbuilders$TMP
  3. Chaincode 準備,複製 chaincode 到 $TMP下
  4. 配置文件準備,複製 sampleconfig 下的文件到 $TMP 下,不是sampleconfig本身是其下內容
  5. peer 可執行程序,編譯或下載 peer$TMP/bin
  6. 修改 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
    
  7. 設置配置文件路徑
    export FABRIC_CFG_PATH=$TMP
  8. 完整目錄結構如下
    .
    ├── 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 爲前面創建的臨時目錄。

--peerAddressespeer 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章