From b969b28f8caaa6862fedc9645b6af548380a1b09 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Fri, 22 May 2020 14:25:21 -0400 Subject: [PATCH] Address linter issues in integration/chaincode - handle errors - remove dead code Signed-off-by: Matthew Sykes --- integration/chaincode/keylevelep/chaincode.go | 22 ++++++++++++++----- .../chaincode/marbles/marbles_chaincode.go | 19 +++++----------- .../chaincode/marbles_private/chaincode.go | 5 ++++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/integration/chaincode/keylevelep/chaincode.go b/integration/chaincode/keylevelep/chaincode.go index 809777d3513..18ab11063f7 100644 --- a/integration/chaincode/keylevelep/chaincode.go +++ b/integration/chaincode/keylevelep/chaincode.go @@ -120,13 +120,18 @@ func delOrgs(stub shim.ChaincodeStubInterface) pb.Response { // get the endorsement policy for the key var epBytes []byte var err error - if parameters[0] == "pub" { + switch parameters[0] { + case "pub": epBytes, err = stub.GetStateValidationParameter("pub") - } else if parameters[0] == "priv" { + case "priv": epBytes, err = stub.GetPrivateDataValidationParameter("col", "priv") - } else { + default: return shim.Error("Unknown key specified") } + if err != nil { + return shim.Error(err.Error()) + } + ep, err := statebased.NewStateEP(epBytes) if err != nil { return shim.Error(err.Error()) @@ -163,13 +168,18 @@ func listOrgs(stub shim.ChaincodeStubInterface) pb.Response { // get the endorsement policy for the key var epBytes []byte var err error - if parameters[0] == "pub" { + switch parameters[0] { + case "pub": epBytes, err = stub.GetStateValidationParameter("pub") - } else if parameters[0] == "priv" { + case "priv": epBytes, err = stub.GetPrivateDataValidationParameter("col", "priv") - } else { + default: return shim.Error("Unknown key specified") } + if err != nil { + return shim.Error(err.Error()) + } + ep, err := statebased.NewStateEP(epBytes) if err != nil { return shim.Error(err.Error()) diff --git a/integration/chaincode/marbles/marbles_chaincode.go b/integration/chaincode/marbles/marbles_chaincode.go index e451c7554d0..fd86411a2da 100644 --- a/integration/chaincode/marbles/marbles_chaincode.go +++ b/integration/chaincode/marbles/marbles_chaincode.go @@ -104,16 +104,6 @@ type marble struct { Owner string `json:"owner"` } -// =================================================================================== -// Main -// =================================================================================== -func main() { - err := shim.Start(new(SimpleChaincode)) - if err != nil { - fmt.Printf("Error starting Simple chaincode: %s", err) - } -} - // Init initializes chaincode // =========================== func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { @@ -228,7 +218,10 @@ func (t *SimpleChaincode) initMarble(stub shim.ChaincodeStubInterface, args []st // Save index entry to state. Only the key name is needed, no need to store a duplicate copy of the marble. // Note - passing a 'nil' value will effectively delete the key from state, therefore we pass null character as value value := []byte{0x00} - stub.PutState(colorNameIndexKey, value) + err = stub.PutState(colorNameIndexKey, value) + if err != nil { + return shim.Error(err.Error()) + } // ==== Marble saved and indexed. Return success ==== fmt.Println("- end init marble") @@ -361,7 +354,7 @@ func constructQueryResponseFromIterator(resultsIterator shim.StateQueryIteratorI return nil, err } // Add a comma before array members, suppress it for the first array member - if bArrayMemberAlreadyWritten == true { + if bArrayMemberAlreadyWritten { buffer.WriteString(",") } buffer.WriteString("{\"Key\":") @@ -717,7 +710,7 @@ func (t *SimpleChaincode) getHistoryForMarble(stub shim.ChaincodeStubInterface, return shim.Error(err.Error()) } // Add a comma before array members, suppress it for the first array member - if bArrayMemberAlreadyWritten == true { + if bArrayMemberAlreadyWritten { buffer.WriteString(",") } buffer.WriteString("{\"TxId\":") diff --git a/integration/chaincode/marbles_private/chaincode.go b/integration/chaincode/marbles_private/chaincode.go index a59f2059c5c..8bc87000e0a 100644 --- a/integration/chaincode/marbles_private/chaincode.go +++ b/integration/chaincode/marbles_private/chaincode.go @@ -191,7 +191,10 @@ func (t *MarblesPrivateChaincode) initMarble(stub shim.ChaincodeStubInterface, a // Save index entry to state. Only the key name is needed, no need to store a duplicate copy of the marble. // Note - passing a 'nil' value will effectively delete the key from state, therefore we pass null character as value value := []byte{0x00} - stub.PutPrivateData("collectionMarbles", colorNameIndexKey, value) + err = stub.PutPrivateData("collectionMarbles", colorNameIndexKey, value) + if err != nil { + return shim.Error(err.Error()) + } // ==== Marble saved and indexed. Return success ==== fmt.Println("- end init marble")