Skip to content

Commit

Permalink
FAB-1357 simpl. cc API param passing using a ctxt obj
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1357

List of parameters to many of the chaincode APIs is growing
   . chaincode name
   . chain ID
   . version (needed for upgrade)
   . Proposal
   . txid

Before getting into the meat of upgrade, simplify the APIs
using a CCContext structure. In addition to cleaning and
simplifying APIs, this will make the upgrade CR easier to
follow

Change-Id: Ic7ea5f4e082d347cda3072fc2e39077006d866c1
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Dec 12, 2016
1 parent ce296d2 commit 7f51840
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 180 deletions.
123 changes: 87 additions & 36 deletions core/chaincode/chaincode_support.go

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions core/chaincode/chaincodeexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ func createCIS(ccname string, args [][]byte) (*pb.ChaincodeInvocationSpec, error

// GetCDSFromLCCC gets chaincode deployment spec from LCCC
func GetCDSFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) ([]byte, error) {
payload, _, err := ExecuteChaincode(ctxt, chainID, txid, prop, "lccc", [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
cccid := NewCCContext(chainID, "lccc", "", txid, true, prop)
payload, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
return payload, err
}

// ExecuteChaincode executes a given chaincode given chaincode name and arguments
func ExecuteChaincode(ctxt context.Context, chainID string, txid string, prop *pb.Proposal, ccname string, args [][]byte) ([]byte, *pb.ChaincodeEvent, error) {
func ExecuteChaincode(ctxt context.Context, cccid *CCContext, args [][]byte) ([]byte, *pb.ChaincodeEvent, error) {
var spec *pb.ChaincodeInvocationSpec
var err error
var b []byte
var ccevent *pb.ChaincodeEvent

spec, err = createCIS(ccname, args)
b, ccevent, err = Execute(ctxt, chainID, txid, prop, spec)
spec, err = createCIS(cccid.Name, args)
b, ccevent, err = Execute(ctxt, cccid, spec)
if err != nil {
return nil, nil, fmt.Errorf("Error deploying chaincode: %s", err)
}
Expand Down
20 changes: 10 additions & 10 deletions core/chaincode/exectransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

//Execute - execute proposal
func Execute(ctxt context.Context, chainID string, txid string, prop *pb.Proposal, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
func Execute(ctxt context.Context, cccid *CCContext, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
var err error
var cds *pb.ChaincodeDeploymentSpec
var ci *pb.ChaincodeInvocationSpec
Expand All @@ -39,18 +39,18 @@ func Execute(ctxt context.Context, chainID string, txid string, prop *pb.Proposa
}

if cds != nil {
_, err := theChaincodeSupport.Deploy(ctxt, chainID, cds)
_, err := theChaincodeSupport.Deploy(ctxt, cccid, cds)
if err != nil {
return nil, nil, fmt.Errorf("Failed to deploy chaincode spec(%s)", err)
}

_, _, err = theChaincodeSupport.Launch(ctxt, chainID, txid, prop, cds)
_, _, err = theChaincodeSupport.Launch(ctxt, cccid, cds)
if err != nil {
return nil, nil, fmt.Errorf("%s", err)
}
} else {
//will launch if necessary (and wait for ready)
cID, cMsg, err := theChaincodeSupport.Launch(ctxt, chainID, txid, prop, ci)
cID, cMsg, err := theChaincodeSupport.Launch(ctxt, cccid, ci)
if err != nil {
return nil, nil, fmt.Errorf("Failed to launch chaincode spec(%s)", err)
}
Expand All @@ -70,22 +70,22 @@ func Execute(ctxt context.Context, chainID string, txid string, prop *pb.Proposa
}

var ccMsg *pb.ChaincodeMessage
ccMsg, err = createTransactionMessage(txid, cMsg)
ccMsg, err = createTransactionMessage(cccid.TxID, cMsg)
if err != nil {
return nil, nil, fmt.Errorf("Failed to transaction message(%s)", err)
}

resp, err := theChaincodeSupport.Execute(ctxt, chainID, chaincode, ccMsg, timeout, prop)
resp, err := theChaincodeSupport.Execute(ctxt, cccid, ccMsg, timeout)
if err != nil {
// Rollback transaction
return nil, nil, fmt.Errorf("Failed to execute transaction (%s)", err)
} else if resp == nil {
// Rollback transaction
return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", txid)
return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", cccid.TxID)
} else {
if resp.ChaincodeEvent != nil {
resp.ChaincodeEvent.ChaincodeID = chaincode
resp.ChaincodeEvent.TxID = txid
resp.ChaincodeEvent.ChaincodeID = cccid.Name
resp.ChaincodeEvent.TxID = cccid.TxID
}

if resp.Type == pb.ChaincodeMessage_COMPLETED {
Expand All @@ -95,7 +95,7 @@ func Execute(ctxt context.Context, chainID string, txid string, prop *pb.Proposa
// Rollback transaction
return nil, resp.ChaincodeEvent, fmt.Errorf("Transaction returned with failure: %s", string(resp.Payload))
}
return resp.Payload, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", txid, resp.Type)
return resp.Payload, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", cccid.TxID, resp.Type)
}

}
Expand Down
Loading

0 comments on commit 7f51840

Please sign in to comment.