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

   日期:2020-09-21     浏览:172    评论:0    
核心提示:fabric的test-networkfabric源码包提供1个测试案例目录test-network,见下图。在该目录中,执行./network.sh createChannel -c mychannel,将启动1个网络,创建名为mychannel的通道。在这个网络中,存在peer0.org1.example.com、peer0.org2.example.com、orderer.example.com共3个节点。配置/etc/hosts配置/etc/hosts的目的,是让另1个终端SDK程序

fabric的test-network

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


在该目录中,执行./network.sh createChannel -c mychannel,将启动1个网络,创建名为mychannel的通道。


在这个网络中,存在peer0.org1.example.com、peer0.org2.example.com、orderer.example.com共3个节点。

配置/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个节点的网络是相通的。

用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/User1@org1.example.com/tls/client.key
      cert:
        path: /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/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案例程序,在指定网络中查询并输出已经安装的通道名称。

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服