github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/chaincode/exectransaction.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package chaincode 18 19 import ( 20 "errors" 21 "fmt" 22 "time" 23 24 "github.com/golang/protobuf/proto" 25 "golang.org/x/net/context" 26 27 "github.com/hyperledger/fabric/core/chaincode/shim" 28 "github.com/hyperledger/fabric/core/common/ccprovider" 29 "github.com/hyperledger/fabric/events/producer" 30 pb "github.com/hyperledger/fabric/protos/peer" 31 ) 32 33 //Execute - execute proposal, return original response of chaincode 34 func Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) { 35 var err error 36 var cds *pb.ChaincodeDeploymentSpec 37 var ci *pb.ChaincodeInvocationSpec 38 39 //init will call the Init method of a on a chain 40 cctyp := pb.ChaincodeMessage_INIT 41 if cds, _ = spec.(*pb.ChaincodeDeploymentSpec); cds == nil { 42 if ci, _ = spec.(*pb.ChaincodeInvocationSpec); ci == nil { 43 panic("Execute should be called with deployment or invocation spec") 44 } 45 cctyp = pb.ChaincodeMessage_TRANSACTION 46 } 47 48 cID, cMsg, err := theChaincodeSupport.Launch(ctxt, cccid, spec) 49 if err != nil { 50 return nil, nil, fmt.Errorf("%s", err) 51 } 52 53 //this should work because it worked above... 54 chaincode := cID.Name 55 56 if err != nil { 57 return nil, nil, fmt.Errorf("Failed to stablish stream to container %s", chaincode) 58 } 59 60 // TODO: Need to comment next line and uncomment call to getTimeout, when transaction blocks are being created 61 timeout := time.Duration(30000) * time.Millisecond 62 63 if err != nil { 64 return nil, nil, fmt.Errorf("Failed to retrieve chaincode spec(%s)", err) 65 } 66 67 var ccMsg *pb.ChaincodeMessage 68 ccMsg, err = createCCMessage(cctyp, cccid.TxID, cMsg) 69 if err != nil { 70 return nil, nil, fmt.Errorf("Failed to transaction message(%s)", err) 71 } 72 73 resp, err := theChaincodeSupport.Execute(ctxt, cccid, ccMsg, timeout) 74 if err != nil { 75 // Rollback transaction 76 return nil, nil, fmt.Errorf("Failed to execute transaction (%s)", err) 77 } else if resp == nil { 78 // Rollback transaction 79 return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", cccid.TxID) 80 } 81 82 if resp.ChaincodeEvent != nil { 83 resp.ChaincodeEvent.ChaincodeId = cccid.Name 84 resp.ChaincodeEvent.TxId = cccid.TxID 85 } 86 87 if resp.Type == pb.ChaincodeMessage_COMPLETED { 88 res := &pb.Response{} 89 unmarshalErr := proto.Unmarshal(resp.Payload, res) 90 if unmarshalErr != nil { 91 return nil, nil, fmt.Errorf("Failed to unmarshal response for (%s): %s", cccid.TxID, unmarshalErr) 92 } 93 94 // Success 95 return res, resp.ChaincodeEvent, nil 96 } else if resp.Type == pb.ChaincodeMessage_ERROR { 97 // Rollback transaction 98 return nil, resp.ChaincodeEvent, fmt.Errorf("Transaction returned with failure: %s", string(resp.Payload)) 99 } 100 101 //TODO - this should never happen ... a panic is more appropriate but will save that for future 102 return nil, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", cccid.TxID, resp.Type) 103 } 104 105 // ExecuteWithErrorFilter is similar to Execute, but filters error contained in chaincode response and returns Payload of response only. 106 // Mostly used by unit-test. 107 func ExecuteWithErrorFilter(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) { 108 res, event, err := Execute(ctxt, cccid, spec) 109 if err != nil { 110 chaincodeLogger.Errorf("ExecuteWithErrorFilter %s error: %s", cccid.Name, err) 111 return nil, nil, err 112 } 113 114 if res == nil { 115 chaincodeLogger.Errorf("ExecuteWithErrorFilter %s get nil response without error", cccid.Name) 116 return nil, nil, err 117 } 118 119 if res.Status != shim.OK { 120 return nil, nil, fmt.Errorf("%s", res.Message) 121 } 122 123 return res.Payload, event, nil 124 } 125 126 // GetSecureContext returns the security context from the context object or error 127 // Security context is nil if security is off from core.yaml file 128 // func GetSecureContext(ctxt context.Context) (crypto.Peer, error) { 129 // var err error 130 // temp := ctxt.Value("security") 131 // if nil != temp { 132 // if secCxt, ok := temp.(crypto.Peer); ok { 133 // return secCxt, nil 134 // } 135 // err = errors.New("Failed to convert security context type") 136 // } 137 // return nil, err 138 // } 139 140 var errFailedToGetChainCodeSpecForTransaction = errors.New("Failed to get ChainCodeSpec from Transaction") 141 142 func sendTxRejectedEvent(tx *pb.Transaction, errorMsg string) { 143 producer.Send(producer.CreateRejectionEvent(tx, errorMsg)) 144 }