Skip to content

Commit 8ce0169

Browse files
committed
remove duplicated code in peer/chaincode
The chaincode specification code creation was repeated line by line for the deploy and invoke/query commands of the peer. The code is now placed in a function called "getChaincodeSpecification". Change-Id: Iae60d8c3640562049a1fad140dd06f32a8af8bfb Signed-off-by: Ivan Puddu <[email protected]>
1 parent 2443643 commit 8ce0169

File tree

2 files changed

+41
-113
lines changed

2 files changed

+41
-113
lines changed

peer/chaincode/common.go

+38-36
Original file line numberDiff line numberDiff line change
@@ -38,59 +38,43 @@ type container struct {
3838
Args []string
3939
}
4040

41-
// chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
42-
// INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
43-
// the query result on STDOUT. A command-line flag (-r, --raw) determines
44-
// whether the query result is output as raw bytes, or as a printable string.
45-
// The printable form is optionally (-x, --hex) a hexadecimal representation
46-
// of the query response. If the query response is NIL, nothing is output.
47-
func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err error) {
48-
49-
if err = checkChaincodeCmdParams(cmd); err != nil {
50-
return
41+
func getChaincodeSpecification(cmd *cobra.Command) (*pb.ChaincodeSpec, error) {
42+
spec := &pb.ChaincodeSpec{}
43+
if err := checkChaincodeCmdParams(cmd); err != nil {
44+
return spec, err
5145
}
5246

53-
if chaincodeName == "" {
54-
err = errors.New("Name not given for invoke/query")
55-
return
56-
}
57-
58-
devopsClient, err := common.GetDevopsClient(cmd)
59-
if err != nil {
60-
err = fmt.Errorf("Error building %s: %s", chainFuncName, err)
61-
return
62-
}
6347
// Build the spec
64-
input := &pb.ChaincodeInput{}
6548
inputc := container{}
66-
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
67-
err = fmt.Errorf("Chaincode argument error: %s", err)
68-
return
49+
if err := json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
50+
return spec, fmt.Errorf("Chaincode argument error: %s", err)
6951
}
70-
input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
52+
input := &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
53+
7154
var attributes []string
72-
if err = json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
73-
err = fmt.Errorf("Chaincode argument error: %s", err)
74-
return
55+
if err := json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
56+
return spec, fmt.Errorf("Chaincode argument error: %s", err)
7557
}
7658

7759
chaincodeLang = strings.ToUpper(chaincodeLang)
78-
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
79-
ChaincodeID: &pb.ChaincodeID{Name: chaincodeName}, CtorMsg: input, Attributes: attributes}
80-
60+
spec = &pb.ChaincodeSpec{
61+
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
62+
ChaincodeID: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName},
63+
CtorMsg: input,
64+
Attributes: attributes,
65+
}
8166
// If security is enabled, add client login token
8267
if core.SecurityEnabled() {
8368
if chaincodeUsr == common.UndefinedParamValue {
84-
err = errors.New("Must supply username for chaincode when security is enabled")
85-
return
69+
return spec, errors.New("Must supply username for chaincode when security is enabled")
8670
}
8771

8872
// Retrieve the CLI data storage path
8973
// Returns /var/openchain/production/client/
9074
localStore := util.GetCliFilePath()
9175

9276
// Check if the user is logged in before sending transaction
93-
if _, err = os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil {
77+
if _, err := os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil {
9478
logger.Infof("Local user '%s' is already logged in. Retrieving login token.\n", chaincodeUsr)
9579

9680
// Read in the login token
@@ -110,8 +94,7 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
11094
} else {
11195
// Check if the token is not there and fail
11296
if os.IsNotExist(err) {
113-
err = fmt.Errorf("User '%s' not logged in. Use the 'login' command to obtain a security token.", chaincodeUsr)
114-
return
97+
return spec, fmt.Errorf("User '%s' not logged in. Use the 'login' command to obtain a security token.", chaincodeUsr)
11598
}
11699
// Unexpected error
117100
panic(fmt.Errorf("Fatal error when checking for client login token: %s\n", err))
@@ -124,6 +107,25 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
124107
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
125108
}
126109
}
110+
return spec, nil
111+
}
112+
113+
// chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
114+
// INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
115+
// the query result on STDOUT. A command-line flag (-r, --raw) determines
116+
// whether the query result is output as raw bytes, or as a printable string.
117+
// The printable form is optionally (-x, --hex) a hexadecimal representation
118+
// of the query response. If the query response is NIL, nothing is output.
119+
func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err error) {
120+
spec, err := getChaincodeSpecification(cmd)
121+
if err != nil {
122+
return err
123+
}
124+
125+
devopsClient, err := common.GetDevopsClient(cmd)
126+
if err != nil {
127+
return fmt.Errorf("Error building %s: %s", chainFuncName, err)
128+
}
127129

128130
// Build the ChaincodeInvocationSpec message
129131
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}

peer/chaincode/deploy.go

+3-77
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,12 @@ limitations under the License.
1717
package chaincode
1818

1919
import (
20-
"encoding/json"
21-
"errors"
2220
"fmt"
23-
"io/ioutil"
24-
"os"
25-
"strings"
2621

2722
"golang.org/x/net/context"
2823

29-
"github.com/hyperledger/fabric/core"
30-
"github.com/hyperledger/fabric/core/chaincode/shim"
3124
"github.com/hyperledger/fabric/peer/common"
32-
"github.com/hyperledger/fabric/peer/util"
33-
pb "github.com/hyperledger/fabric/protos"
3425
"github.com/spf13/cobra"
35-
"github.com/spf13/viper"
3626
)
3727

3828
// Cmd returns the cobra command for Chaincode Deploy
@@ -54,79 +44,15 @@ var chaincodeDeployCmd = &cobra.Command{
5444
// (hash) is printed to STDOUT for use by subsequent chaincode-related CLI
5545
// commands.
5646
func chaincodeDeploy(cmd *cobra.Command, args []string) error {
57-
if err := checkChaincodeCmdParams(cmd); err != nil {
47+
spec, err := getChaincodeSpecification(cmd)
48+
if err != nil {
5849
return err
5950
}
51+
6052
devopsClient, err := common.GetDevopsClient(cmd)
6153
if err != nil {
6254
return fmt.Errorf("Error building %s: %s", chainFuncName, err)
6355
}
64-
// Build the spec
65-
input := &pb.ChaincodeInput{}
66-
inputc := container{}
67-
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
68-
return fmt.Errorf("Chaincode argument error: %s", err)
69-
}
70-
input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
71-
72-
var attributes []string
73-
if err := json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
74-
return fmt.Errorf("Chaincode argument error: %s", err)
75-
}
76-
77-
chaincodeLang = strings.ToUpper(chaincodeLang)
78-
spec := &pb.ChaincodeSpec{
79-
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
80-
ChaincodeID: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName},
81-
CtorMsg: input,
82-
Attributes: attributes,
83-
}
84-
85-
// If security is enabled, add client login token
86-
if core.SecurityEnabled() {
87-
logger.Debug("Security is enabled. Include security context in deploy spec")
88-
if chaincodeUsr == common.UndefinedParamValue {
89-
return errors.New("Must supply username for chaincode when security is enabled")
90-
}
91-
92-
// Retrieve the CLI data storage path
93-
// Returns /var/openchain/production/client/
94-
localStore := util.GetCliFilePath()
95-
96-
// Check if the user is logged in before sending transaction
97-
if _, err = os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil {
98-
logger.Infof("Local user '%s' is already logged in. Retrieving login token.\n", chaincodeUsr)
99-
100-
// Read in the login token
101-
token, err := ioutil.ReadFile(localStore + "loginToken_" + chaincodeUsr)
102-
if err != nil {
103-
panic(fmt.Errorf("Fatal error when reading client login token: %s\n", err))
104-
}
105-
106-
// Add the login token to the chaincodeSpec
107-
spec.SecureContext = string(token)
108-
109-
// If privacy is enabled, mark chaincode as confidential
110-
if viper.GetBool("security.privacy") {
111-
logger.Info("Set confidentiality level to CONFIDENTIAL.\n")
112-
spec.ConfidentialityLevel = pb.ConfidentialityLevel_CONFIDENTIAL
113-
}
114-
} else {
115-
// Check if the token is not there and fail
116-
if os.IsNotExist(err) {
117-
return fmt.Errorf("User '%s' not logged in. Use the 'login' command to obtain a security token.", chaincodeUsr)
118-
}
119-
// Unexpected error
120-
panic(fmt.Errorf("Fatal error when checking for client login token: %s\n", err))
121-
}
122-
} else {
123-
if chaincodeUsr != common.UndefinedParamValue {
124-
logger.Warning("Username supplied but security is disabled.")
125-
}
126-
if viper.GetBool("security.privacy") {
127-
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
128-
}
129-
}
13056

13157
chaincodeDeploymentSpec, err := devopsClient.Deploy(context.Background(), spec)
13258
if err != nil {

0 commit comments

Comments
 (0)