//initialize peer and start up. If security==enabled, login as vp func initPeer(chainID string) (net.Listener, error) { //start clean finitPeer(nil) 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 { return nil, fmt.Errorf("Failed to generate credentials %v", err) } opts = []grpc.ServerOption{grpc.Creds(creds)} } grpcServer := grpc.NewServer(opts...) viper.Set("peer.fileSystemPath", filepath.Join(os.TempDir(), "hyperledger", "production")) peerAddress, err := peer.GetLocalAddress() if err != nil { return nil, fmt.Errorf("Error obtaining peer address: %s", err) } lis, err := net.Listen("tcp", peerAddress) if err != nil { return nil, fmt.Errorf("Error starting peer listener %s", err) } //initialize ledger peer.MockInitialize() getPeerEndpoint := func() (*pb.PeerEndpoint, error) { return &pb.PeerEndpoint{ID: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil } // Install security object for peer if viper.GetBool("security.enabled") { //TODO: integrate new crypto / idp securityLevel := viper.GetInt("security.level") hashAlgorithm := viper.GetString("security.hashAlgorithm") primitives.SetSecurityLevel(hashAlgorithm, securityLevel) } else { // the primitives need to be instantiated no matter what. Otherwise // the escc code won't have a hash algorithm available to hash the proposal primitives.SetSecurityLevel("SHA2", 256) } ccStartupTimeout := time.Duration(30000) * time.Millisecond pb.RegisterChaincodeSupportServer(grpcServer, chaincode.NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout)) chaincode.RegisterSysCCs() if err = peer.MockCreateChain(chainID); err != nil { closeListenerAndSleep(lis) return nil, err } chaincode.DeploySysCCs(chainID) go grpcServer.Serve(lis) return lis, nil }
//NOTE - when we implment JOIN we will no longer pass the chainID as param //The chaincode support will come up without registering system chaincodes //which will be registered only during join phase. func registerChaincodeSupport(grpcServer *grpc.Server) { //get user mode userRunsCC := false if viper.GetString("chaincode.mode") == chaincode.DevModeUserRunsChaincode { userRunsCC = true } //get chaincode startup timeout tOut, err := strconv.Atoi(viper.GetString("chaincode.startuptimeout")) if err != nil { //what went wrong ? fmt.Printf("could not retrive timeout var...setting to 5secs\n") tOut = 5000 } ccStartupTimeout := time.Duration(tOut) * time.Millisecond ccSrv := chaincode.NewChaincodeSupport(peer.GetPeerEndpoint, userRunsCC, ccStartupTimeout) //Now that chaincode is initialized, register all system chaincodes. chaincode.RegisterSysCCs() pb.RegisterChaincodeSupportServer(grpcServer, ccSrv) }