fabric test-network
The fabric source package provides a test case directory test-network, as shown in the figure below.
In this directory, execute ./network.sh createChannel -c mychannel to start a network and create a channel named mychannel.
In this network, there are three nodes: peer0.org1.example.com, peer0.org2.example.com, and orderer.example.com.
Configure /etc/hosts
The purpose of configuring /etc/hosts is to allow another terminal SDK program to access 3 nodes: peer0.org1.example.com, peer0.org2.example.com, orderer.example.com.
Use docker exec -it peer0.org1.example.com /bin/ash to enter one of the nodes.
Execute ping peer0.org1.example.com on the node of peer0.org1 to obtain the IP address of the node. Similarly, obtain the IP addresses of the other 2 nodes.
Open another terminal and modify the /etc/hosts file as follows. And confirm that this terminal is connected to the network of the above three nodes.
Use Go to write SDK cases and visit the above network
Create a new directory queryChannel, this case is used to query the name of the channel that has been created in the network. In this directory, create two new files e2e.yaml and queryChannel.go.
The content of the configuration file e2e.yaml is as follows.
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
The content of the SDK program queryChannel.go is as follows.
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
}
Execute go mod init. Initialize this case and download some dependency packages.
Execute go run., Run this SDK example program, query and output the installed channel name in the specified network.