func serve(args []string) error { peerEndpoint, err := peer.GetPeerEndpoint() if err != nil { logger.Error(fmt.Sprintf("Failed to get Peer Endpoint: %s", err)) return err } listenAddr := viper.GetString("peer.listenaddress") if "" == listenAddr { logger.Debug("Listen address not specified, using peer endpoint address") listenAddr = peerEndpoint.Address } lis, err := net.Listen("tcp", listenAddr) if err != nil { grpclog.Fatalf("Failed to listen: %v", err) } ehubLis, ehubGrpcServer, err := createEventHubServer() if err != nil { grpclog.Fatalf("Failed to create ehub server: %v", err) } if chaincodeDevMode { logger.Info("Running in chaincode development mode") logger.Info("Set consensus to NOOPS and user starts chaincode") logger.Info("Disable loading validity system chaincode") viper.Set("peer.validator.enabled", "true") viper.Set("peer.validator.consensus", "noops") viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode) // Disable validity system chaincode in dev mode. Also if security is enabled, // in obcca.yaml, manually set pki.validity-period.update to false to prevent // obcca from calling validity system chaincode -- though no harm otherwise viper.Set("ledger.blockchain.deploy-system-chaincode", "false") viper.Set("validator.validity-period.verification", "false") } logger.Info("Security enabled status: %t", viper.GetBool("security.enabled")) logger.Info("Privacy enabled status: %t", viper.GetBool("security.privacy")) var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) if err != nil { grpclog.Fatalf("Failed to generate credentials %v", err) } opts = []grpc.ServerOption{grpc.Creds(creds)} } grpcServer := grpc.NewServer(opts...) var peerServer *peer.PeerImpl if viper.GetBool("peer.validator.enabled") { logger.Debug("Running as validating peer - installing consensus %s", viper.GetString("peer.validator.consensus")) peerServer, _ = peer.NewPeerWithHandler(helper.NewConsensusHandler) } else { logger.Debug("Running as non-validating peer") peerServer, _ = peer.NewPeerWithHandler(peer.NewPeerHandler) } // Register the Peer server //pb.RegisterPeerServer(grpcServer, openchain.NewPeer()) pb.RegisterPeerServer(grpcServer, peerServer) // Register the Admin server pb.RegisterAdminServer(grpcServer, openchain.NewAdminServer()) // Register ChaincodeSupport server... // TODO : not the "DefaultChain" ... we have to revisit when we do multichain // The ChaincodeSupport needs security helper to encrypt/decrypt state when // privacy is enabled var secHelper crypto.Peer if viper.GetBool("security.privacy") { secHelper = peerServer.GetSecHelper() } else { secHelper = nil } registerChaincodeSupport(chaincode.DefaultChain, grpcServer, secHelper) // Register Devops server serverDevops := openchain.NewDevopsServer(peerServer) pb.RegisterDevopsServer(grpcServer, serverDevops) // Register the ServerOpenchain server serverOpenchain, err := openchain.NewOpenchainServer() if err != nil { logger.Error(fmt.Sprintf("Error creating OpenchainServer: %s", err)) return err } pb.RegisterOpenchainServer(grpcServer, serverOpenchain) // Create and register the REST service if configured if viper.GetBool("rest.enabled") { go rest.StartOpenchainRESTServer(serverOpenchain, serverDevops) } rootNode, err := openchain.GetRootNode() if err != nil { grpclog.Fatalf("Failed to get peer.discovery.rootnode valey: %s", err) } logger.Info("Starting peer with id=%s, network id=%s, address=%s, discovery.rootnode=%s, validator=%v", peerEndpoint.ID, viper.GetString("peer.networkId"), peerEndpoint.Address, rootNode, viper.GetBool("peer.validator.enabled")) // Start the grpc server. Done in a goroutine so we can deploy the // genesis block if needed. serve := make(chan bool) go func() { if grpcErr := grpcServer.Serve(lis); grpcErr != nil { logger.Error(fmt.Sprintf("grpc server exited with error: %s", grpcErr)) } else { logger.Info("grpc server exited") } serve <- true }() // Deploy the genesis block if needed. if viper.GetBool("peer.validator.enabled") { makeGenesisError := genesis.MakeGenesis() if makeGenesisError != nil { return makeGenesisError } } //start the event hub server if ehubGrpcServer != nil && ehubLis != nil { go ehubGrpcServer.Serve(ehubLis) } // Block until grpc server exits <-serve return nil }
func startOpenchain(t *testing.T) error { peerEndpoint, err := peer.GetPeerEndpoint() if err != nil { return fmt.Errorf("Failed to get Peer Endpoint: %s", err) } listenAddr := viper.GetString("peer.listenaddress") if "" == listenAddr { t.Log("Listen address not specified, using peer endpoint address") listenAddr = peerEndpoint.Address } lis, err := net.Listen("tcp", listenAddr) if err != nil { grpclog.Fatalf("failed to listen: %v", err) } t.Logf("Security enabled status: %t", viper.GetBool("security.enabled")) var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) if err != nil { grpclog.Fatalf("Failed to generate credentials %v", err) } opts = []grpc.ServerOption{grpc.Creds(creds)} } grpcServer := grpc.NewServer(opts...) // Register the Peer server var peerServer *peer.PeerImpl if viper.GetBool("peer.validator.enabled") { t.Logf("Running as validating peer - installing consensus %s", viper.GetString("peer.validator.consensus")) peerServer, _ = peer.NewPeerWithHandler(helper.NewConsensusHandler) } else { t.Log("Running as non-validating peer") peerServer, _ = peer.NewPeerWithHandler(peer.NewPeerHandler) } pb.RegisterPeerServer(grpcServer, peerServer) // Register the Admin server pb.RegisterAdminServer(grpcServer, openchain.NewAdminServer()) // Register ChaincodeSupport server... // TODO : not the "DefaultChain" ... we have to revisit when we do multichain var secHelper crypto.Peer if viper.GetBool("security.privacy") { secHelper = peerServer.GetSecHelper() } else { secHelper = nil } registerChaincodeSupport(chaincode.DefaultChain, grpcServer, secHelper) // Register Devops server serverDevops := openchain.NewDevopsServer(peerServer) pb.RegisterDevopsServer(grpcServer, serverDevops) // Register the ServerOpenchain server serverOpenchain, err := openchain.NewOpenchainServer() if err != nil { return fmt.Errorf("Error creating OpenchainServer: %s", err) } pb.RegisterOpenchainServer(grpcServer, serverOpenchain) // Create and register the REST service go rest.StartOpenchainRESTServer(serverOpenchain, serverDevops) rootNode, err := openchain.GetRootNode() if err != nil { grpclog.Fatalf("Failed to get peer.discovery.rootnode valey: %s", err) } t.Logf("Starting peer with id=%s, network id=%s, address=%s, discovery.rootnode=%s, validator=%v", peerEndpoint.ID, viper.GetString("peer.networkId"), peerEndpoint.Address, rootNode, viper.GetBool("peer.validator.enabled")) // Start the grpc server. Done in a goroutine so we can deploy the // genesis block if needed. serve := make(chan bool) go func() { grpcServer.Serve(lis) serve <- true }() // Deploy the geneis block if needed. if viper.GetBool("peer.validator.enabled") { makeGeneisError := genesis.MakeGenesis() if makeGeneisError != nil { return makeGeneisError } } // Block until grpc server exits <-serve return nil }