fabric-sdk-go v1.4安裝下載

1.啓動fabric網絡

實現我們從github 上拉取 fabric

git clone "https://github.com/hyperledger/fabric.git"

 

# 進入對應的文件
cd /opt/gopath/src/hyperledger/fabric
#下載必備的文件
cd scripts/
#這一步會下載官方的例子以及所需要的Docker鏡像,並下載相應的二進制文件以及fabric-sample
#下載是比較慢的,如果出現錯誤或者長時間沒有速度只需要重新運行就可以了,我是在阿里雲上面進行下載,所以沒有翻牆,如果你要是在本地一定要翻牆
sudo ./bootstrap.sh

當我們完成一系列操作以後我們需要將下載的configtxgen,configtxlator,cryptogen添加進環境變量中因爲我們在下一步時候需要他們。我們生成的configtxgen,configtxlator,cryptogen 二進制文件在 fabric目錄的.build/bin 目錄下,我的地址是

/opt/gopath/src/github.com/hyperledger/fabric/.build/bin 

將對應的二進制文件添加進環境變量(我一般都是講這些二進制文件,copy 到/usr/local/bin 目錄下)

vim ~/.profile

#文件中最後添加以下內容

export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/./build/bin

#更新一下

source ~/.profile

 

上面我們就完成了全部的準備工作,我們可以開始啓動我們的fabric 網絡了。

#進入first-network文件夾,fabric-samples 文件是我們在完成./bootstrap.sh 命令後出現的

cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/

#執行命令

 ./byfn.sh up  //時間會比較長我們需要耐心等待

 

當出現
===================== Query successful on peer1.org2 on channel 'mychannel' =====================

========= All GOOD, BYFN execution completed ===========

_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/

證明我們啓動完成。

./byfn.sh down 關閉fabric 網絡。

 

2.部署fabric-sdk-go

fabric-sdk-go 本身的項目還是很複雜的,尤其代碼寫的是相當難以閱讀,再加上fabric項目的配置項本身就很複雜,所以使得fabric-sdk-go 這個項目還是比較難以掌握的。在這裏我先記錄下fabric-sdk-go 最簡單的啓動配置。至於更加複雜的部分以後我再補上。

首先我們先從github 拉去最新的fabric-sdk-go 代碼。我當前版本是fabirc-sdk-go 1.4 版本。

git clone https://github.com/hyperledger/fabric-sdk-go.git

 

因爲在新版的fabric-sdk-go 中我們使用的是golang 1.13,我們知道golang 在1.13 版本中已經支持了mod。

#進入fabric-sdk-go 地址

cd /opt/gopath/src/github.com/hyperledger/fabric-sdk-go

# 執行下載fabric-sdk-go 依賴第三方庫,科學上網(當時我就是無法科學上網導致浪費了很多時間)

go mod download

# 建立vendor 並copy相應的依賴。

go mod vendor

 

此時fabric-sdk-go 項目相應的依賴庫我們此時已經下載完畢。下一步我們需要將我們在搭建fabric生成的密鑰文件拷貝到我們的fabric-sdk-go 項目中。

# fabric 網絡中的密鑰文件地址

/opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/crypto-config

# 在 fabric-sdk-go 需要存放的地址

/opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/v1

# 我們copy fabric 下的crypto-config 目錄,到fabric-sdk-go 的v1 目錄下

cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/

cp -r crypto-config /opt/gopath/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/fabric/v1

 

 

當我們完成密鑰文件的替換後我們需要檢查下,fabric 網絡的端口是否是通的。當然如果我們的fabric與fabric-sdk-go是在同一臺服務器上沒有問題,如果不是在同一臺我們可以使用telnet 127.0.0.1 7050 來檢查我們的端口是否是開通的。 

下面我先將我寫的測試代碼貼出來給大家看一下。至於爲什麼我需要一個新的測試代碼而不用fabric-sdk-go 的測試代碼,因爲我只是想簡單測試一下我的fabric-sdk-go 是否可以與我的fabric 網絡的節點鏈接。fabric-sdk-go 的測試代碼,是將整個的週期都測試一遍。還是很複雜的,這樣如果出錯我們也不是很好排查。所以我們仿照 的e2e 測試例子自己實現了一個簡單的測試代碼。我們也可以將測試代碼放在end_2_end_test.go 文件中。

/*

 	進行簡單的chaincode invoke,query sample

 

*/

func TestCcQuery(t *testing.T) {
result := fabric_sdk.CcQuery()
fmt.Println(string(result))
}

並同樣在fabric-sdk-go/test/integration/e2e 目錄下添加一個新的文件ccprovider.go,爲了不和e2e的代碼有衝突。

package e2e

 

import (
"fmt"
"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/client/channel"
"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/common/errors/retry"
"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/common/providers/core"
"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/core/config"
"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/fabsdk"
)

var (
channelID = "mychannel"
orgName = "org1"
ccID = "mycc"
defaultQueryArgs = [][]byte{[]byte("b")}
)
func getConfig()core.ConfigProvider{
configPath := "../fixtures/config/config_e2e.yaml"
return config.FromFile(configPath)
}
func CcQuery()[]byte{
configOpt := getConfig()
sdk, err := fabsdk.New(configOpt)
if err != nil {
fmt.Printf("Failed to create new SDK: %s \n", err)
}
defer sdk.Close()
//prepare channel client context using client context
clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser("Admin"), fabsdk.WithOrg(orgName))
// Channel client is used to query and execute transactions (Org1 is default org)
client, err := channel.New(clientChannelContext)
if err != nil {
fmt.Printf("Failed to create new channel client: %s \n", err)
}
return queryCC(client,[]string{"grpcs://××.××.××.××:7051"}...) //必須要加我們targetEndpoints 信息,我這裏添加的是pee0.org1 的ip 地址
}
func queryCC( client *channel.Client, targetEndpoints ...string) []byte {
response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: "query", Args: defaultQueryArgs},
channel.WithRetry(retry.DefaultChannelOpts),
channel.WithTargetEndpoints(targetEndpoints...),
)
if err != nil {
fmt.Printf("Failed to query funds: %s \n", err)
}
return response.Payload
}

當我們閱讀代碼的時候我們可以看到在getConfig()方法中我們引用了../fixtures/config/config_e2e.yaml 的配置文件。下面我們學習如何修改config_e2e.yaml配置文件。在這裏我們只需要修改相應的密鑰路徑配置文件,除了下面我摘出來的部分需要特別注意,其他的部分我們正常填寫就ok了。

  tlsCerts:

    # [Optional]. Use system certificate pool when connecting to peers, orderers (for negotiating TLS) Default: false

    systemCertPool: true

 

# [Optional]. Client key and cert for TLS handshake with peers and orderers
client:
key:
path: /opt/gopath/src/github.com/fabric-sdk-sample/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/users/[email protected]/tls/client.key
cert:
path: /opt/gopath/src/github.com/fabric-sdk-sample/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/users/[email protected]/tls/client.crt

#
# [Optional]. But most apps would have this section so that channel objects can be constructed
# based on the content below. If an app is creating channels, then it likely will not need this
# section.
#

在代碼部分我們看到需要我們手動填寫targetEndporting,否則將會報錯誤如下:

[fabsdk/client] 2020/02/10 06:06:35 UTC - pgresolver.(*randomLBP).Choose -> WARN No available peer groups

Failed to query funds: Client Status Code: (6) NO_PEERS_FOUND. Description: targets were not provided 

 

當我們完成配置以後我可以進行測試,測試結果如下

GOROOT=/usr/local/go #gosetup

GOPATH=/opt/gopath #gosetup

/usr/local/go/bin/go test -c -o /tmp/___TestCcQuery_in_github_com_fabric_sdk_sample_fabric_sdk_test_linux github.com/fabric-sdk-sample/fabric-sdk-test #gosetup

/usr/local/go/bin/go tool test2json -t /tmp/___TestCcQuery_in_github_com_fabric_sdk_sample_fabric_sdk_test_linux -test.v -test.run ^TestCcQuery$ #gosetup

=== RUN   TestCcQuery

210

--- PASS: TestCcQuery (0.69s)

PASS

 

Process finished with exit code 0

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