github.com/defanghe/fabric@v2.1.1+incompatible/internal/peer/channel/update.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package channel 8 9 import ( 10 "errors" 11 "fmt" 12 "io/ioutil" 13 14 "github.com/hyperledger/fabric/internal/peer/common" 15 "github.com/hyperledger/fabric/protoutil" 16 "github.com/spf13/cobra" 17 ) 18 19 func updateCmd(cf *ChannelCmdFactory) *cobra.Command { 20 updateCmd := &cobra.Command{ 21 Use: "update", 22 Short: "Send a configtx update.", 23 Long: "Signs and sends the supplied configtx update file to the channel. Requires '-f', '-o', '-c'.", 24 RunE: func(cmd *cobra.Command, args []string) error { 25 return update(cmd, args, cf) 26 }, 27 } 28 flagList := []string{ 29 "channelID", 30 "file", 31 } 32 attachFlags(updateCmd, flagList) 33 34 return updateCmd 35 } 36 37 func update(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error { 38 //the global chainID filled by the "-c" command 39 if channelID == common.UndefinedParamValue { 40 return errors.New("Must supply channel ID") 41 } 42 43 if channelTxFile == "" { 44 return InvalidCreateTx("No configtx file name supplied") 45 } 46 // Parsing of the command line is done so silence cmd usage 47 cmd.SilenceUsage = true 48 49 var err error 50 if cf == nil { 51 cf, err = InitCmdFactory(EndorserNotRequired, PeerDeliverNotRequired, OrdererNotRequired) 52 if err != nil { 53 return err 54 } 55 } 56 57 fileData, err := ioutil.ReadFile(channelTxFile) 58 if err != nil { 59 return ConfigTxFileNotFound(err.Error()) 60 } 61 62 ctxEnv, err := protoutil.UnmarshalEnvelope(fileData) 63 if err != nil { 64 return err 65 } 66 67 sCtxEnv, err := sanityCheckAndSignConfigTx(ctxEnv, cf.Signer) 68 if err != nil { 69 return err 70 } 71 72 var broadcastClient common.BroadcastClient 73 broadcastClient, err = cf.BroadcastFactory() 74 if err != nil { 75 return fmt.Errorf("Error getting broadcast client: %s", err) 76 } 77 78 defer broadcastClient.Close() 79 err = broadcastClient.Send(sCtxEnv) 80 if err != nil { 81 return err 82 } 83 84 logger.Info("Successfully submitted channel update") 85 return nil 86 }