From 5db3e488a290bc8f94624b1bb626158a329f6123 Mon Sep 17 00:00:00 2001 From: Artem Barger Date: Tue, 28 Feb 2017 16:53:41 +0200 Subject: [PATCH] Cleanup messaging and error formating, peer cli Peer cli chaincode command code has a few formatting and logging warning and inconsistences, this commit clean this issues. Change-Id: Iaf31daa3fcc63ca6b909ffbdf046e687fa7c16e4 Signed-off-by: Artem Barger --- peer/chaincode/common.go | 20 ++++++++++---------- peer/chaincode/install.go | 18 ++++++++++-------- peer/chaincode/install_test.go | 6 +++--- peer/chaincode/instantiate.go | 17 +++++++++-------- peer/chaincode/invoke.go | 2 +- peer/chaincode/package.go | 6 ++++-- peer/chaincode/query.go | 2 +- peer/chaincode/upgrade.go | 15 +++++++-------- peer/chaincode/upgrade_test.go | 7 +++---- 9 files changed, 48 insertions(+), 45 deletions(-) diff --git a/peer/chaincode/common.go b/peer/chaincode/common.go index 56ae154465a..fd46508e3b3 100644 --- a/peer/chaincode/common.go +++ b/peer/chaincode/common.go @@ -106,12 +106,12 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool, cf * logger.Infof("Invoke result: %v", proposalResp) } else { if proposalResp == nil { - return fmt.Errorf("Error query %s by endorsing: %s\n", chainFuncName, err) + return fmt.Errorf("Error query %s by endorsing: %s", chainFuncName, err) } if chaincodeQueryRaw { if chaincodeQueryHex { - err = errors.New("Options --raw (-r) and --hex (-x) are not compatible\n") + err = errors.New("Options --raw (-r) and --hex (-x) are not compatible") return } fmt.Print("Query Result (Raw): ") @@ -130,7 +130,7 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool, cf * func checkChaincodeCmdParams(cmd *cobra.Command) error { //we need chaincode name for everything, including deploy if chaincodeName == common.UndefinedParamValue { - return fmt.Errorf("Must supply value for %s name parameter.\n", chainFuncName) + return fmt.Errorf("Must supply value for %s name parameter.", chainFuncName) } if cmd.Name() == instantiate_cmdname || cmd.Name() == install_cmdname || cmd.Name() == upgrade_cmdname { @@ -142,35 +142,35 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error { // if it's not a deploy or an upgrade we don't need policy, escc and vscc if cmd.Name() != instantiate_cmdname && cmd.Name() != upgrade_cmdname { if escc != common.UndefinedParamValue { - return fmt.Errorf("escc should be supplied only to chaincode deploy requests") + return errors.New("escc should be supplied only to chaincode deploy requests") } if vscc != common.UndefinedParamValue { - return fmt.Errorf("vscc should be supplied only to chaincode deploy requests") + return errors.New("vscc should be supplied only to chaincode deploy requests") } if policy != common.UndefinedParamValue { - return fmt.Errorf("policy should be supplied only to chaincode deploy requests") + return errors.New("policy should be supplied only to chaincode deploy requests") } } else { if escc != common.UndefinedParamValue { logger.Infof("Using escc %s", escc) } else { - logger.Infof("Using default escc") + logger.Info("Using default escc") escc = "escc" } if vscc != common.UndefinedParamValue { logger.Infof("Using vscc %s", vscc) } else { - logger.Infof("Using default vscc") + logger.Info("Using default vscc") vscc = "vscc" } if policy != common.UndefinedParamValue { p, err := cauthdsl.FromString(policy) if err != nil { - return fmt.Errorf("Invalid policy %s\n", policy) + return fmt.Errorf("Invalid policy %s", policy) } policyMarhsalled = putils.MarshalOrPanic(p) } else { @@ -199,7 +199,7 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error { _, argsPresent := sm["args"] _, funcPresent := sm["function"] if !argsPresent || (len(m) == 2 && !funcPresent) || len(m) > 2 { - return fmt.Errorf("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'") + return errors.New("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'") } } else { if cmd == nil || cmd != chaincodeInstallCmd { diff --git a/peer/chaincode/install.go b/peer/chaincode/install.go index a0a3e8f74f0..49c099642f7 100644 --- a/peer/chaincode/install.go +++ b/peer/chaincode/install.go @@ -33,12 +33,14 @@ var chaincodeInstallCmd *cobra.Command const install_cmdname = "install" +const install_desc = "Package the specified chaincode into a deployment spec and save it on the peer's path." + // installCmd returns the cobra command for Chaincode Deploy func installCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeInstallCmd = &cobra.Command{ Use: "install", - Short: fmt.Sprintf("Package the specified chaincode into a deployment spec and save it on the peer's path."), - Long: fmt.Sprintf(`Package the specified chaincode into a deployment spec and save it on the peer's path.`), + Short: fmt.Sprintf(install_desc), + Long: fmt.Sprintf(install_desc), ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodeInstall(cmd, args, cf) @@ -52,27 +54,27 @@ func installCmd(cf *ChaincodeCmdFactory) *cobra.Command { func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDeploymentSpec, cf *ChaincodeCmdFactory) error { creator, err := cf.Signer.Serialize() if err != nil { - return fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err) + return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) } prop, _, err := utils.CreateInstallProposalFromCDS(cds, creator) if err != nil { - return fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err) + return fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err) } var signedProp *pb.SignedProposal signedProp, err = utils.GetSignedProposal(prop, cf.Signer) if err != nil { - return fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err) + return fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err) } proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp) if err != nil { - return fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err) + return fmt.Errorf("Error endorsing %s: %s", chainFuncName, err) } if proposalResponse != nil { - fmt.Printf("Installed remotely %v\n", proposalResponse) + logger.Debug("Installed remotely %v", proposalResponse) } return nil @@ -81,7 +83,7 @@ func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDep // chaincodeInstall installs the chaincode. If remoteinstall, does it via a lccc call func chaincodeInstall(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error { if chaincodePath == common.UndefinedParamValue || chaincodeVersion == common.UndefinedParamValue { - return fmt.Errorf("Must supply value for %s path and version parameters.\n", chainFuncName) + return fmt.Errorf("Must supply value for %s path and version parameters.", chainFuncName) } var err error diff --git a/peer/chaincode/install_test.go b/peer/chaincode/install_test.go index 1e82d491ba1..f4a51b7ec86 100644 --- a/peer/chaincode/install_test.go +++ b/peer/chaincode/install_test.go @@ -69,7 +69,7 @@ func TestBadVersion(t *testing.T) { cmd.SetArgs(args) if err := cmd.Execute(); err == nil { - t.Fatalf("Expected error executing install command for version not specified") + t.Fatal("Expected error executing install command for version not specified") } } @@ -84,11 +84,11 @@ func TestNonExistentCC(t *testing.T) { cmd.SetArgs(args) if err := cmd.Execute(); err == nil { - t.Fatalf("Expected error executing install command for bad chaincode") + t.Fatal("Expected error executing install command for bad chaincode") } if _, err := os.Stat(fsPath + "/chaincodes/badexample02.testversion"); err == nil { - t.Fatalf("chaincode example02.testversion should not exist") + t.Fatal("chaincode example02.testversion should not exist") } } diff --git a/peer/chaincode/instantiate.go b/peer/chaincode/instantiate.go index e8c4fae6a43..b1e489fac19 100644 --- a/peer/chaincode/instantiate.go +++ b/peer/chaincode/instantiate.go @@ -19,24 +19,25 @@ package chaincode import ( "fmt" - "golang.org/x/net/context" - protcommon "github.com/hyperledger/fabric/protos/common" pb "github.com/hyperledger/fabric/protos/peer" "github.com/hyperledger/fabric/protos/utils" "github.com/spf13/cobra" + "golang.org/x/net/context" ) var chaincodeInstantiateCmd *cobra.Command const instantiate_cmdname = "instantiate" +const instantiate_desc = "Deploy the specified chaincode to the network." + // instantiateCmd returns the cobra command for Chaincode Deploy func instantiateCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeInstantiateCmd = &cobra.Command{ Use: instantiate_cmdname, - Short: fmt.Sprintf("Deploy the specified chaincode to the network."), - Long: fmt.Sprintf(`Deploy the specified chaincode to the network.`), + Short: fmt.Sprintf(instantiate_desc), + Long: fmt.Sprintf(instantiate_desc), ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodeDeploy(cmd, args, cf) @@ -60,23 +61,23 @@ func instantiate(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envel creator, err := cf.Signer.Serialize() if err != nil { - return nil, fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err) + return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) } prop, _, err := utils.CreateDeployProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc)) if err != nil { - return nil, fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err) } var signedProp *pb.SignedProposal signedProp, err = utils.GetSignedProposal(prop, cf.Signer) if err != nil { - return nil, fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err) } proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp) if err != nil { - return nil, fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err) } if proposalResponse != nil { diff --git a/peer/chaincode/invoke.go b/peer/chaincode/invoke.go index c74268ebf69..b55d5918710 100644 --- a/peer/chaincode/invoke.go +++ b/peer/chaincode/invoke.go @@ -29,7 +29,7 @@ func invokeCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeInvokeCmd = &cobra.Command{ Use: "invoke", Short: fmt.Sprintf("Invoke the specified %s.", chainFuncName), - Long: fmt.Sprintf(`Invoke the specified %s. It will try to commit the endorsed transaction to the network.`, chainFuncName), + Long: fmt.Sprintf("Invoke the specified %s. It will try to commit the endorsed transaction to the network.", chainFuncName), ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodeInvoke(cmd, args, cf) diff --git a/peer/chaincode/package.go b/peer/chaincode/package.go index 9afae72cc77..cf061ad10fa 100644 --- a/peer/chaincode/package.go +++ b/peer/chaincode/package.go @@ -25,12 +25,14 @@ import ( "github.com/spf13/cobra" ) +const package_desc = "Package the specified chaincode into a deployment spec." + // deployCmd returns the cobra command for Chaincode Deploy func packageCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeInstantiateCmd = &cobra.Command{ Use: "package", - Short: fmt.Sprintf("Package the specified chaincode into a deployment spec."), - Long: fmt.Sprintf(`Package the specified chaincode into a deployment spec.`), + Short: package_desc, + Long: package_desc, ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodePackage(cmd, args, cf) diff --git a/peer/chaincode/query.go b/peer/chaincode/query.go index eed03f2ed77..b65edf1bee3 100644 --- a/peer/chaincode/query.go +++ b/peer/chaincode/query.go @@ -29,7 +29,7 @@ func queryCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeQueryCmd = &cobra.Command{ Use: "query", Short: fmt.Sprintf("Query using the specified %s.", chainFuncName), - Long: fmt.Sprintf(`Get endorsed result of %s function call and print it. It won't generate transaction.`, chainFuncName), + Long: fmt.Sprintf("Get endorsed result of %s function call and print it. It won't generate transaction.", chainFuncName), ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodeQuery(cmd, args, cf) diff --git a/peer/chaincode/upgrade.go b/peer/chaincode/upgrade.go index 2044366ed92..4d7ee3fd55e 100644 --- a/peer/chaincode/upgrade.go +++ b/peer/chaincode/upgrade.go @@ -19,12 +19,11 @@ package chaincode import ( "fmt" - "golang.org/x/net/context" - protcommon "github.com/hyperledger/fabric/protos/common" pb "github.com/hyperledger/fabric/protos/peer" "github.com/hyperledger/fabric/protos/utils" "github.com/spf13/cobra" + "golang.org/x/net/context" ) var chaincodeUpgradeCmd *cobra.Command @@ -35,8 +34,8 @@ const upgrade_cmdname = "upgrade" func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command { chaincodeUpgradeCmd = &cobra.Command{ Use: upgrade_cmdname, - Short: fmt.Sprintf("Upgrade chaincode."), - Long: fmt.Sprintf(`Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed.`), + Short: "Upgrade chaincode.", + Long: "Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed.", ValidArgs: []string{"1"}, RunE: func(cmd *cobra.Command, args []string) error { return chaincodeUpgrade(cmd, args, cf) @@ -60,24 +59,24 @@ func upgrade(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, creator, err := cf.Signer.Serialize() if err != nil { - return nil, fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err) + return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) } prop, _, err := utils.CreateUpgradeProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc)) if err != nil { - return nil, fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err) } logger.Debugf("Get upgrade proposal for chaincode <%v>", spec.ChaincodeId) var signedProp *pb.SignedProposal signedProp, err = utils.GetSignedProposal(prop, cf.Signer) if err != nil { - return nil, fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err) } proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp) if err != nil { - return nil, fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err) + return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err) } logger.Debugf("endorse upgrade proposal, get response <%v>", proposalResponse.Response) diff --git a/peer/chaincode/upgrade_test.go b/peer/chaincode/upgrade_test.go index 663ee5f6b55..e5099b53b6f 100644 --- a/peer/chaincode/upgrade_test.go +++ b/peer/chaincode/upgrade_test.go @@ -17,16 +17,15 @@ package chaincode import ( + "errors" "fmt" "os" "sync" "testing" + "github.com/hyperledger/fabric/msp/mgmt/testtools" "github.com/hyperledger/fabric/peer/common" pb "github.com/hyperledger/fabric/protos/peer" - // "github.com/hyperledger/fabric/protos/utils" - - "github.com/hyperledger/fabric/msp/mgmt/testtools" ) var once sync.Once @@ -141,7 +140,7 @@ func TestUpgradeCmdSendTXFail(t *testing.T) { mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) - sendErr := fmt.Errorf("send tx failed") + sendErr := errors.New("send tx failed") mockBroadcastClient := common.GetMockBroadcastClient(sendErr) mockCF := &ChaincodeCmdFactory{