Skip to content

Commit

Permalink
Merge "Cleanup messaging and error formating, peer cli"
Browse files Browse the repository at this point in the history
  • Loading branch information
yacovm authored and Gerrit Code Review committed Feb 28, 2017
2 parents eba8b28 + 5db3e48 commit 21ce6b2
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 45 deletions.
20 changes: 10 additions & 10 deletions peer/chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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): ")
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 10 additions & 8 deletions peer/chaincode/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions peer/chaincode/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -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")
}
}

Expand Down
17 changes: 9 additions & 8 deletions peer/chaincode/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions peer/chaincode/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions peer/chaincode/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions peer/chaincode/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 21ce6b2

Please sign in to comment.