github.com/kaituanwang/hyperledger@v2.0.1+incompatible/orderer/sample_clients/broadcast_config/client.go (about)

     1  // Copyright IBM Corp. All Rights Reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  
    12  	cb "github.com/hyperledger/fabric-protos-go/common"
    13  	ab "github.com/hyperledger/fabric-protos-go/orderer"
    14  	"github.com/hyperledger/fabric/bccsp/factory"
    15  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    16  	"github.com/hyperledger/fabric/orderer/common/localconfig"
    17  	"google.golang.org/grpc"
    18  )
    19  
    20  type broadcastClient struct {
    21  	ab.AtomicBroadcast_BroadcastClient
    22  }
    23  
    24  func (bc *broadcastClient) broadcast(env *cb.Envelope) error {
    25  	var err error
    26  	var resp *ab.BroadcastResponse
    27  
    28  	err = bc.Send(env)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	resp, err = bc.Recv()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	fmt.Println("Status:", resp)
    39  	return nil
    40  }
    41  
    42  // cmdImpl holds the command and its arguments.
    43  type cmdImpl struct {
    44  	name string
    45  	args argsImpl
    46  }
    47  
    48  // argsImpl holds all the possible arguments for all possible commands.
    49  type argsImpl struct {
    50  	consensusType  string
    51  	creationPolicy string
    52  	chainID        string
    53  }
    54  
    55  var conf *localconfig.TopLevel
    56  
    57  func init() {
    58  	var err error
    59  	conf, err = localconfig.Load()
    60  	if err != nil {
    61  		fmt.Println("failed to load config:", err)
    62  		os.Exit(1)
    63  	}
    64  
    65  	// Load local MSP
    66  	err = mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID)
    67  	if err != nil {
    68  		panic(fmt.Errorf("failed to initialize local MSP: %s", err))
    69  	}
    70  }
    71  
    72  func main() {
    73  	cmd := new(cmdImpl)
    74  	var srv string
    75  
    76  	flag.StringVar(&srv, "server", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort), "The RPC server to connect to.")
    77  	flag.StringVar(&cmd.name, "cmd", "newChain", "The action that this client is requesting via the config transaction.")
    78  	flag.StringVar(&cmd.args.consensusType, "consensusType", "solo", "In case of a newChain command, the type of consensus the ordering service is running on.")
    79  	flag.StringVar(&cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", "In case of a newChain command, the chain creation policy this request should be validated against.")
    80  	flag.StringVar(&cmd.args.chainID, "chainID", "mychannel", "In case of a newChain command, the chain ID to create.")
    81  	flag.Parse()
    82  
    83  	signer, err := mspmgmt.GetLocalMSP(factory.GetDefault()).GetDefaultSigningIdentity()
    84  	if err != nil {
    85  		fmt.Println("Failed to load local signing identity:", err)
    86  		os.Exit(0)
    87  	}
    88  
    89  	conn, err := grpc.Dial(srv, grpc.WithInsecure())
    90  	defer func() {
    91  		_ = conn.Close()
    92  	}()
    93  	if err != nil {
    94  		fmt.Println("Error connecting:", err)
    95  		return
    96  	}
    97  
    98  	client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
    99  	if err != nil {
   100  		fmt.Println("Error connecting:", err)
   101  		return
   102  	}
   103  
   104  	bc := &broadcastClient{client}
   105  
   106  	switch cmd.name {
   107  	case "newChain":
   108  		env := newChainRequest(
   109  			cmd.args.consensusType,
   110  			cmd.args.creationPolicy,
   111  			cmd.args.chainID,
   112  			signer,
   113  		)
   114  		fmt.Println("Requesting the creation of chain", cmd.args.chainID)
   115  		fmt.Println(bc.broadcast(env))
   116  	default:
   117  		panic("Invalid command given")
   118  	}
   119  }