func createChaincodeSpec(ccType string, path string, args [][]byte) *pb.ChaincodeSpec { // make chaincode spec for chaincode to be deployed ccSpec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[ccType]), ChaincodeID: &pb.ChaincodeID{Path: path}, CtorMsg: &pb.ChaincodeInput{Args: args}} return ccSpec }
// deploySysCC deploys the given system chaincode on a chain func deploySysCC(chainID string, syscc *SystemChaincode) error { if !syscc.Enabled || !isWhitelisted(syscc) { sysccLogger.Info(fmt.Sprintf("system chaincode (%s,%s) disabled", syscc.Name, syscc.Path)) return nil } if chainID == "" && !syscc.ChainlessCC { return fmt.Errorf("cannot deploy system chaincode %s without chain id", syscc.Name) } else if chainID != "" && syscc.ChainlessCC { return fmt.Errorf("cannot deploy chainless system chaincode %s with chain id %s", syscc.Name, chainID) } var err error ctxt := context.Background() if !syscc.ChainlessCC { lgr := peer.GetLedger(chainID) var txsim ledger.TxSimulator if txsim, err = lgr.NewTxSimulator(); err != nil { return err } ctxt = context.WithValue(ctxt, TXSimulatorKey, txsim) defer txsim.Done() } chaincodeID := &pb.ChaincodeID{Path: syscc.Path, Name: syscc.Name} spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeID: chaincodeID, CtorMsg: &pb.ChaincodeInput{Args: syscc.InitArgs}} // First build and get the deployment spec chaincodeDeploymentSpec, err := buildSysCC(ctxt, spec) if err != nil { sysccLogger.Error(fmt.Sprintf("Error deploying chaincode spec: %v\n\n error: %s", spec, err)) return err } txid := util.GenerateUUID() cccid := NewCCContext(chainID, chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name, "", txid, true, nil) _, _, err = Execute(ctxt, cccid, chaincodeDeploymentSpec) sysccLogger.Infof("system chaincode %s/%s(%s) deployed", syscc.Name, chainID, syscc.Path) return err }
// deregisterSysCC stops the system chaincode and deregisters it from inproccontroller func deregisterSysCC(chainID string, syscc *SystemChaincode) error { chaincodeID := &pb.ChaincodeID{Path: syscc.Path, Name: syscc.Name} spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeID: chaincodeID, CtorMsg: &pb.ChaincodeInput{Args: syscc.InitArgs}} ctx := context.Background() // First build and get the deployment spec chaincodeDeploymentSpec, err := buildSysCC(ctx, spec) if err != nil { sysccLogger.Error(fmt.Sprintf("Error deploying chaincode spec: %v\n\n error: %s", spec, err)) return err } chaincodeSupport := GetChain() if chaincodeSupport != nil { cccid := NewCCContext(chainID, syscc.Name, "", "", true, nil) err = chaincodeSupport.Stop(ctx, cccid, chaincodeDeploymentSpec) } return err }
func getJoinCCSpec() (*pb.ChaincodeSpec, error) { if genesisBlockPath == common.UndefinedParamValue { return nil, fmt.Errorf("Must supply genesis block file.\n") } gb, err := ioutil.ReadFile(genesisBlockPath) if err != nil { return nil, err } spec := &pb.ChaincodeSpec{} // Build the spec input := &pb.ChaincodeInput{Args: [][]byte{[]byte("JoinChain"), gb}} spec = &pb.ChaincodeSpec{ Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeID: &pb.ChaincodeID{Name: "cscc"}, CtorMsg: input, } return spec, nil }
func getChaincodeSpecification(cmd *cobra.Command) (*pb.ChaincodeSpec, error) { spec := &pb.ChaincodeSpec{} if err := checkChaincodeCmdParams(cmd); err != nil { return spec, err } // Build the spec input := &pb.ChaincodeInput{} if err := json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil { return spec, fmt.Errorf("Chaincode argument error: %s", err) } var attributes []string if err := json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil { return spec, fmt.Errorf("Chaincode argument error: %s", err) } chaincodeLang = strings.ToUpper(chaincodeLang) spec = &pb.ChaincodeSpec{ Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]), ChaincodeID: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName}, CtorMsg: input, Attributes: attributes, } // If security is enabled, add client login token if core.SecurityEnabled() { if chaincodeUsr == common.UndefinedParamValue { return spec, errors.New("Must supply username for chaincode when security is enabled") } // Retrieve the CLI data storage path // Returns /var/openchain/production/client/ localStore := util.GetCliFilePath() // Check if the user is logged in before sending transaction if _, err := os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil { logger.Infof("Local user '%s' is already logged in. Retrieving login token.\n", chaincodeUsr) // Read in the login token token, err := ioutil.ReadFile(localStore + "loginToken_" + chaincodeUsr) if err != nil { panic(fmt.Errorf("Fatal error when reading client login token: %s\n", err)) } // Add the login token to the chaincodeSpec spec.SecureContext = string(token) // If privacy is enabled, mark chaincode as confidential if viper.GetBool("security.privacy") { logger.Info("Set confidentiality level to CONFIDENTIAL.\n") spec.ConfidentialityLevel = pb.ConfidentialityLevel_CONFIDENTIAL } } else { // Check if the token is not there and fail if os.IsNotExist(err) { return spec, fmt.Errorf("User '%s' not logged in. Use the 'peer network login' command to obtain a security token.", chaincodeUsr) } // Unexpected error panic(fmt.Errorf("Fatal error when checking for client login token: %s\n", err)) } } else { if chaincodeUsr != common.UndefinedParamValue { logger.Warning("Username supplied but security is disabled.") } if viper.GetBool("security.privacy") { panic(errors.New("Privacy cannot be enabled as requested because security is disabled")) } } return spec, nil }