Fabric-sdk-go的测试案例分析(3)-针对test-network编写1个SDK案例

fabric的test-network

fabric源码包提供1个测试案例目录test-network,见下图。

Fabric-sdk-go的测试案例分析(3)-针对test-network编写1个SDK案例_第1张图片
在该目录中,执行./network.sh createChannel -c mychannel,将启动1个网络,创建名为mychannel的通道。

在这里插入图片描述
在这个网络中,存在peer0.org1.example.com、peer0.org2.example.com、orderer.example.com共3个节点。

Fabric-sdk-go的测试案例分析(3)-针对test-network编写1个SDK案例_第2张图片

配置/etc/hosts

配置/etc/hosts的目的,是让另1个终端SDK程序能够访问到3个节点:peer0.org1.example.com、peer0.org2.example.com、orderer.example.com。
使用docker exec -it peer0.org1.example.com /bin/ash进入其中1个节点。

在这里插入图片描述
在peer0.org1的节点中执行ping peer0.org1.example.com,获得该节点的IP地址。类似地,获得其他2个节点的IP地址。
在这里插入图片描述
另开1个终端,修改/etc/hosts文件如下。并确认,这个终端,与上述3个节点的网络是相通的。

Fabric-sdk-go的测试案例分析(3)-针对test-network编写1个SDK案例_第3张图片

Fabric-sdk-go的测试案例分析(3)-针对test-network编写1个SDK案例_第4张图片

用Go编写SDK案例,访问上面的网络

新建1个目录queryChannel,这个案例用来查询网络中已经创建的通道名称。在这个目录中,新建2个文件e2e.yaml和queryChannel.go。
在这里插入图片描述配置文件e2e.yaml内容如下。

version: 1.0.0
client:
  organization: Org1
  logging:
    level: info
  cryptoconfig:
    path: /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations
  credentialStore:
    path: "/tmp/state-store"
    cryptoStore:
      path: /tmp/msp
  tlsCerts:
    client:
      key:
        path: /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/tls/client.key
      cert:
        path: /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/tls/client.crt
        
organizations:
  Org1:
    mspid: Org1MSP
    cryptoPath:  peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp
    peers:
      - peer0.org1.example.com

peers:
  peer0.org1.example.com:
    url: peer0.org1.example.com:7051
    tlsCACerts:
      path: /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem



SDK程序queryChannel.go的内容如下。

package main

import (
	"log"
	"strings"

	"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
	"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
	"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
	"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
	"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
	"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
	"github.com/pkg/errors"
)

const (
	orgName  = "Org1"
	orgAdmin = "Admin"
)

func main() {
	configPath := "./e2e.yaml"
	configProvider := config.FromFile(configPath)
	sdk, err := fabsdk.New(configProvider)
	if err != nil {
		log.Fatalf("Failed to create new SDK: %s", err)
	}
	defer sdk.Close()
	queryChannel(sdk)
}

func queryChannel(sdk *fabsdk.FabricSDK) {
	configBackend, err := sdk.Config()
	if err != nil {
		log.Fatalf("Failed to get config backend from SDK: %s", err)
	}
	targets, err := orgTargetPeers([]string{orgName}, configBackend)
	if err != nil {
		log.Fatalf("creating peers failed: %s", err)
	}

	clientContext := sdk.Context(fabsdk.WithUser("User1"), fabsdk.WithOrg("Org1"))
	resMgmtClient, err := resmgmt.New(clientContext)
	if err != nil {
		log.Fatalf("failed to query channel management client:%s", err)
	}
	channelQueryResponse, err := resMgmtClient.QueryChannels(
		resmgmt.WithTargetEndpoints(targets[0]), resmgmt.WithRetry(retry.DefaultResMgmtOpts))
	if err != nil {
		log.Fatalf("QueryChannels return error: %s", err)
	}
	for _, channel := range channelQueryResponse.Channels {
		log.Printf("***  Channel :%s\n", channel.ChannelId)
	}
}

func orgTargetPeers(orgs []string, configBackend ...core.ConfigBackend) ([]string, error) {
	networkConfig := fab.NetworkConfig{}
	err := lookup.New(configBackend...).UnmarshalKey("organizations", &networkConfig.Organizations)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to get organizations from config ")
	}

	var peers []string
	for _, org := range orgs {
		orgConfig, ok := networkConfig.Organizations[strings.ToLower(org)]
		if !ok {
			continue
		}
		peers = append(peers, orgConfig.Peers...)
	}
	return peers, nil
}

执行go mod init .初始化该案例,会下载一些依赖包。
在这里插入图片描述
执行go run . ,运行这个SDK案例程序,在指定网络中查询并输出已经安装的通道名称。
在这里插入图片描述

你可能感兴趣的:(Fabric,区块链)