github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/orderer/main.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 "fmt" 21 "io/ioutil" 22 "log" 23 "net" 24 "net/http" 25 _ "net/http/pprof" 26 "os" 27 28 genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig" 29 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 30 "github.com/hyperledger/fabric/common/flogging" 31 "github.com/hyperledger/fabric/core/comm" 32 "github.com/hyperledger/fabric/orderer/common/bootstrap/file" 33 "github.com/hyperledger/fabric/orderer/kafka" 34 "github.com/hyperledger/fabric/orderer/localconfig" 35 "github.com/hyperledger/fabric/orderer/multichain" 36 "github.com/hyperledger/fabric/orderer/sbft" 37 "github.com/hyperledger/fabric/orderer/solo" 38 cb "github.com/hyperledger/fabric/protos/common" 39 ab "github.com/hyperledger/fabric/protos/orderer" 40 "github.com/hyperledger/fabric/protos/utils" 41 42 "github.com/Shopify/sarama" 43 "github.com/hyperledger/fabric/common/localmsp" 44 mspmgmt "github.com/hyperledger/fabric/msp/mgmt" 45 logging "github.com/op/go-logging" 46 ) 47 48 var logger = logging.MustGetLogger("orderer/main") 49 50 func main() { 51 conf := config.Load() 52 53 // Set the logging level 54 flogging.InitFromSpec(conf.General.LogLevel) 55 if conf.Kafka.Verbose { 56 sarama.Logger = log.New(os.Stdout, "[sarama] ", log.Lshortfile) 57 } 58 59 // Start the profiling service if enabled. 60 // The ListenAndServe() call does not return unless an error occurs. 61 if conf.General.Profile.Enabled { 62 go func() { 63 logger.Info("Starting Go pprof profiling service on:", conf.General.Profile.Address) 64 logger.Panic("Go pprof service failed:", http.ListenAndServe(conf.General.Profile.Address, nil)) 65 }() 66 } 67 68 lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort)) 69 if err != nil { 70 logger.Error("Failed to listen:", err) 71 return 72 } 73 74 // secure server config 75 secureConfig := comm.SecureServerConfig{ 76 UseTLS: conf.General.TLS.Enabled, 77 RequireClientCert: conf.General.TLS.ClientAuthEnabled, 78 } 79 // check to see if TLS is enabled 80 if secureConfig.UseTLS { 81 logger.Info("Starting orderer with TLS enabled") 82 // load crypto material from files 83 serverCertificate, err := ioutil.ReadFile(conf.General.TLS.Certificate) 84 if err != nil { 85 logger.Fatalf("Failed to load ServerCertificate file '%s' (%s)", 86 conf.General.TLS.Certificate, err) 87 } 88 serverKey, err := ioutil.ReadFile(conf.General.TLS.PrivateKey) 89 if err != nil { 90 logger.Fatalf("Failed to load PrivateKey file '%s' (%s)", 91 conf.General.TLS.PrivateKey, err) 92 } 93 var serverRootCAs, clientRootCAs [][]byte 94 for _, serverRoot := range conf.General.TLS.RootCAs { 95 root, err := ioutil.ReadFile(serverRoot) 96 if err != nil { 97 logger.Fatalf("Failed to load ServerRootCAs file '%s' (%s)", 98 err, serverRoot) 99 } 100 serverRootCAs = append(serverRootCAs, root) 101 } 102 if secureConfig.RequireClientCert { 103 for _, clientRoot := range conf.General.TLS.ClientRootCAs { 104 root, err := ioutil.ReadFile(clientRoot) 105 if err != nil { 106 logger.Fatalf("Failed to load ClientRootCAs file '%s' (%s)", 107 err, clientRoot) 108 } 109 clientRootCAs = append(clientRootCAs, root) 110 } 111 } 112 secureConfig.ServerKey = serverKey 113 secureConfig.ServerCertificate = serverCertificate 114 secureConfig.ServerRootCAs = serverRootCAs 115 secureConfig.ClientRootCAs = clientRootCAs 116 } 117 118 // Create GRPC server - return if an error occurs 119 grpcServer, err := comm.NewGRPCServerFromListener(lis, secureConfig) 120 if err != nil { 121 logger.Error("Failed to return new GRPC server:", err) 122 return 123 } 124 125 // Load local MSP 126 err = mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID) 127 if err != nil { // Handle errors reading the config file 128 logger.Panic("Failed to initialize local MSP:", err) 129 } 130 131 lf, _ := createLedgerFactory(conf) 132 133 // Are we bootstrapping? 134 if len(lf.ChainIDs()) == 0 { 135 var genesisBlock *cb.Block 136 137 // Select the bootstrapping mechanism 138 switch conf.General.GenesisMethod { 139 case "provisional": 140 genesisBlock = provisional.New(genesisconfig.Load(conf.General.GenesisProfile)).GenesisBlock() 141 case "file": 142 genesisBlock = file.New(conf.General.GenesisFile).GenesisBlock() 143 default: 144 logger.Panic("Unknown genesis method:", conf.General.GenesisMethod) 145 } 146 147 chainID, err := utils.GetChainIDFromBlock(genesisBlock) 148 if err != nil { 149 logger.Error("Failed to parse chain ID from genesis block:", err) 150 return 151 } 152 gl, err := lf.GetOrCreate(chainID) 153 if err != nil { 154 logger.Error("Failed to create the system chain:", err) 155 return 156 } 157 158 err = gl.Append(genesisBlock) 159 if err != nil { 160 logger.Error("Could not write genesis block to ledger:", err) 161 return 162 } 163 } else { 164 logger.Info("Not bootstrapping because of existing chains") 165 } 166 167 consenters := make(map[string]multichain.Consenter) 168 consenters["solo"] = solo.New() 169 consenters["kafka"] = kafka.New(conf.Kafka.Version, conf.Kafka.Retry, conf.Kafka.TLS) 170 consenters["sbft"] = sbft.New(makeSbftConsensusConfig(conf), makeSbftStackConfig(conf)) 171 172 signer := localmsp.NewSigner() 173 174 manager := multichain.NewManagerImpl(lf, consenters, signer) 175 176 server := NewServer( 177 manager, 178 signer, 179 ) 180 181 ab.RegisterAtomicBroadcastServer(grpcServer.Server(), server) 182 logger.Info("Beginning to serve requests") 183 grpcServer.Start() 184 }