github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/sample_clients/broadcast_config/client.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 24 "google.golang.org/grpc" 25 26 genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig" 27 "github.com/hyperledger/fabric/msp" 28 mspmgmt "github.com/hyperledger/fabric/msp/mgmt" 29 "github.com/hyperledger/fabric/orderer/localconfig" 30 cb "github.com/hyperledger/fabric/protos/common" 31 ab "github.com/hyperledger/fabric/protos/orderer" 32 ) 33 34 var conf *config.TopLevel 35 var genConf *genesisconfig.Profile 36 var signer msp.SigningIdentity 37 38 type broadcastClient struct { 39 ab.AtomicBroadcast_BroadcastClient 40 } 41 42 func (bc *broadcastClient) broadcast(env *cb.Envelope) error { 43 var err error 44 var resp *ab.BroadcastResponse 45 46 err = bc.Send(env) 47 if err != nil { 48 return err 49 } 50 51 resp, err = bc.Recv() 52 if err != nil { 53 return err 54 } 55 56 fmt.Println("Status:", resp) 57 return nil 58 } 59 60 // cmdImpl holds the command and its arguments. 61 type cmdImpl struct { 62 name string 63 args argsImpl 64 } 65 66 // argsImpl holds all the possible arguments for all possible commands. 67 type argsImpl struct { 68 consensusType string 69 creationPolicy string 70 chainID string 71 } 72 73 func init() { 74 conf = config.Load() 75 genConf = genesisconfig.Load(genesisconfig.SampleInsecureProfile) 76 77 // Load local MSP 78 err := mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID) 79 if err != nil { 80 panic(fmt.Errorf("Failed to initialize local MSP: %s", err)) 81 } 82 83 msp := mspmgmt.GetLocalMSP() 84 signer, err = msp.GetDefaultSigningIdentity() 85 if err != nil { 86 panic(fmt.Errorf("Failed to initialize get default signer: %s", err)) 87 } 88 } 89 90 func main() { 91 cmd := new(cmdImpl) 92 var srv string 93 94 flag.StringVar(&srv, "server", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort), "The RPC server to connect to.") 95 flag.StringVar(&cmd.name, "cmd", "newChain", "The action that this client is requesting via the config transaction.") 96 flag.StringVar(&cmd.args.consensusType, "consensusType", genConf.Orderer.OrdererType, "In case of a newChain command, the type of consensus the ordering service is running on.") 97 flag.StringVar(&cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", "In case of a newChain command, the chain creation policy this request should be validated against.") 98 flag.StringVar(&cmd.args.chainID, "chainID", "NewChannelId", "In case of a newChain command, the chain ID to create.") 99 flag.Parse() 100 101 conn, err := grpc.Dial(srv, grpc.WithInsecure()) 102 defer func() { 103 _ = conn.Close() 104 }() 105 if err != nil { 106 fmt.Println("Error connecting:", err) 107 return 108 } 109 110 client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO()) 111 if err != nil { 112 fmt.Println("Error connecting:", err) 113 return 114 } 115 116 bc := &broadcastClient{client} 117 118 switch cmd.name { 119 case "newChain": 120 env := newChainRequest(cmd.args.consensusType, cmd.args.creationPolicy, cmd.args.chainID) 121 fmt.Println("Requesting the creation of chain", cmd.args.chainID) 122 fmt.Println(bc.broadcast(env)) 123 default: 124 panic("Invalid command given") 125 } 126 }