github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/sample_clients/broadcast_config/client.go (about)

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