Skip to content

Commit

Permalink
FAB-11034 Replace context with explicit params
Browse files Browse the repository at this point in the history
Presently, there is a "context" which is passed into the chaincode
package.  Rather than actually be used for its intended purpose (of
waiting for cancelation), the context is never actually checked.
Instead, it simply carries the transaction simulators around in an
opaque fashion.

It's much better to make these transaction simulators to be passed in
explicitly.  Additionally, some other facets like TxID and the assorted
proposals really fit much more naturally with the simulators and not
with the chaincode context.  So, this CR brings them in as well.

Change-Id: I7e0c43af22f1e96b8d302e5cf877e7beec36598b
Signed-off-by: Jason Yellick <[email protected]>
  • Loading branch information
Jason Yellick committed Jul 16, 2018
1 parent b7157a4 commit 4645c3a
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 323 deletions.
12 changes: 6 additions & 6 deletions core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ func (c *CCProviderImpl) GetContext(ledger ledger.PeerLedger, txid string) (cont
}

// ExecuteChaincode executes the chaincode specified in the context with the specified arguments
func (c *CCProviderImpl) ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
func (c *CCProviderImpl) ExecuteChaincode(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
invocationSpec := &pb.ChaincodeInvocationSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_GOLANG,
ChaincodeId: &pb.ChaincodeID{Name: cccid.Name},
Input: &pb.ChaincodeInput{Args: args},
},
}
return c.cs.Execute(ctxt, cccid, invocationSpec)
return c.cs.Execute(txParams, cccid, invocationSpec)
}

// Execute executes the chaincode given context and spec (invocation or deploy)
func (c *CCProviderImpl) Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
return c.cs.Execute(ctxt, cccid, spec)
func (c *CCProviderImpl) Execute(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
return c.cs.Execute(txParams, cccid, spec)
}

// ExecuteInit executes the chaincode given context and spec (invocation or deploy)
func (c *CCProviderImpl) ExecuteInit(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
return c.cs.ExecuteInit(ctxt, cccid, spec)
func (c *CCProviderImpl) ExecuteInit(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
return c.cs.ExecuteInit(txParams, cccid, spec)
}

// Stop stops the chaincode given context and spec
Expand Down
21 changes: 10 additions & 11 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/hyperledger/fabric/core/peer"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
"golang.org/x/net/context"
)

// Runtime is used to manage chaincode runtime instances.
Expand Down Expand Up @@ -196,14 +195,14 @@ func createCCMessage(messageType pb.ChaincodeMessage_Type, cid string, txid stri
}

// Execute init invokes chaincode and returns the original response.
func (cs *ChaincodeSupport) ExecuteInit(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
resp, err := cs.InvokeInit(ctxt, cccid, spec)
func (cs *ChaincodeSupport) ExecuteInit(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
resp, err := cs.InvokeInit(txParams, cccid, spec)
return processChaincodeExecutionResult(cccid, resp, err)
}

// Execute invokes chaincode and returns the original response.
func (cs *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
resp, err := cs.Invoke(ctxt, cccid, spec)
func (cs *ChaincodeSupport) Execute(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.Response, *pb.ChaincodeEvent, error) {
resp, err := cs.Invoke(txParams, cccid, spec)
return processChaincodeExecutionResult(cccid, resp, err)
}

Expand Down Expand Up @@ -237,7 +236,7 @@ func processChaincodeExecutionResult(cccid *ccprovider.CCContext, resp *pb.Chain
}
}

func (cs *ChaincodeSupport) InvokeInit(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.ChaincodeMessage, error) {
func (cs *ChaincodeSupport) InvokeInit(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.ChaincodeMessage, error) {
cctyp := pb.ChaincodeMessage_INIT

ccci := ccprovider.DeploymentSpecToChaincodeContainerInfo(spec)
Expand All @@ -260,12 +259,12 @@ func (cs *ChaincodeSupport) InvokeInit(ctxt context.Context, cccid *ccprovider.C
return nil, errors.WithMessage(err, "failed to create chaincode message")
}

return cs.execute(ctxt, cccid, ccMsg)
return cs.execute(txParams, cccid, ccMsg)
}

// Invoke will invoke chaincode and return the message containing the response.
// The chaincode will be launched if it is not already running.
func (cs *ChaincodeSupport) Invoke(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.ChaincodeMessage, error) {
func (cs *ChaincodeSupport) Invoke(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) (*pb.ChaincodeMessage, error) {
cctyp := pb.ChaincodeMessage_TRANSACTION

chaincodeSpec := spec.GetChaincodeSpec()
Expand All @@ -285,11 +284,11 @@ func (cs *ChaincodeSupport) Invoke(ctxt context.Context, cccid *ccprovider.CCCon
return nil, errors.WithMessage(err, "failed to create chaincode message")
}

return cs.execute(ctxt, cccid, ccMsg)
return cs.execute(txParams, cccid, ccMsg)
}

// execute executes a transaction and waits for it to complete until a timeout value.
func (cs *ChaincodeSupport) execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage) (*pb.ChaincodeMessage, error) {
func (cs *ChaincodeSupport) execute(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage) (*pb.ChaincodeMessage, error) {
cname := cccid.GetCanonicalName()
chaincodeLogger.Debugf("canonical name: %s", cname)

Expand All @@ -299,7 +298,7 @@ func (cs *ChaincodeSupport) execute(ctxt context.Context, cccid *ccprovider.CCCo
return nil, errors.Errorf("unable to invoke chaincode %s", cname)
}

ccresp, err := handler.Execute(ctxt, cccid, msg, cs.ExecuteTimeout)
ccresp, err := handler.Execute(txParams, cccid, msg, cs.ExecuteTimeout)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("error sending"))
}
Expand Down
Loading

0 comments on commit 4645c3a

Please sign in to comment.