Skip to content

Commit 382057e

Browse files
committed
[FAB-9766] Miscellaneous cleanup in cc support
- stop exporting the execute method on ChaincodeSupport - use switch instead of if/elsif/else where apppropriate - cleanup strings used in errors - remove createCIS Change-Id: I8a403c175f29a000f082ae018762fb582fff9a09 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent b7bd8b7 commit 382057e

File tree

2 files changed

+74
-80
lines changed

2 files changed

+74
-80
lines changed

core/chaincode/chaincode_support.go

+72-78
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type PackageProvider interface {
4242
GetChaincode(ccname string, ccversion string) (ccprovider.CCPackage, error)
4343
}
4444

45-
// NewChaincodeSupport creates a new ChaincodeSupport instance
45+
// NewChaincodeSupport creates a new ChaincodeSupport instance.
4646
func NewChaincodeSupport(
4747
config *Config,
4848
peerAddress string,
@@ -108,7 +108,7 @@ type ChaincodeSupport struct {
108108
// SetSysCCProvider is a bit of a hack to make a latent dependency of ChaincodeSupport
109109
// be an explicit dependency. Because the chaincode support must be registered before
110110
// the sysccprovider implementation can be created, we cannot make the sccp part of the
111-
// constructor for ChaincodeSupport
111+
// constructor for ChaincodeSupport.
112112
func (cs *ChaincodeSupport) SetSysCCProvider(sccp sysccprovider.SystemChaincodeProvider) {
113113
cs.sccp = sccp
114114
}
@@ -141,7 +141,7 @@ func (cs *ChaincodeSupport) launchAndWaitForReady(ctx context.Context, cccid *cc
141141
case <-ready:
142142
case err = <-launchFail:
143143
case <-time.After(cs.ccStartupTimeout):
144-
err = errors.Errorf("timeout expired while starting chaincode %s(tx:%s)", cname, cccid.TxID)
144+
err = errors.Errorf("timeout expired while starting chaincode %s for transaction %s", cname, cccid.TxID)
145145
}
146146

147147
if err != nil {
@@ -155,7 +155,7 @@ func (cs *ChaincodeSupport) launchAndWaitForReady(ctx context.Context, cccid *cc
155155
return nil
156156
}
157157

158-
//Stop stops a chaincode if running
158+
// Stop stops a chaincode if running.
159159
func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
160160
cname := cccid.GetCanonicalName()
161161
defer cs.HandlerRegistry.Deregister(cname)
@@ -256,27 +256,29 @@ func (cs *ChaincodeSupport) Register(stream pb.ChaincodeSupport_RegisterServer)
256256
}
257257

258258
// createCCMessage creates a transaction message.
259-
func createCCMessage(typ pb.ChaincodeMessage_Type, cid string, txid string, cMsg *pb.ChaincodeInput) (*pb.ChaincodeMessage, error) {
259+
func createCCMessage(messageType pb.ChaincodeMessage_Type, cid string, txid string, cMsg *pb.ChaincodeInput) (*pb.ChaincodeMessage, error) {
260260
payload, err := proto.Marshal(cMsg)
261261
if err != nil {
262-
fmt.Printf(err.Error())
263262
return nil, err
264263
}
265-
return &pb.ChaincodeMessage{Type: typ, Payload: payload, Txid: txid, ChannelId: cid}, nil
264+
ccmsg := &pb.ChaincodeMessage{
265+
Type: messageType,
266+
Payload: payload,
267+
Txid: txid,
268+
ChannelId: cid,
269+
}
270+
return ccmsg, nil
266271
}
267272

268-
// Execute executes a transaction and waits for it to complete until a timeout value.
269-
func (cs *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
270-
chaincodeLogger.Debugf("Entry")
271-
defer chaincodeLogger.Debugf("Exit")
273+
// execute executes a transaction and waits for it to complete until a timeout value.
274+
func (cs *ChaincodeSupport) execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
272275
cname := cccid.GetCanonicalName()
276+
chaincodeLogger.Debugf("canonical name: %s", cname)
273277

274-
chaincodeLogger.Debugf("chaincode canonical name: %s", cname)
275-
//we expect the chaincode to be running... sanity check
276278
handler := cs.HandlerRegistry.Handler(cname)
277279
if handler == nil {
278-
chaincodeLogger.Debugf("cannot execute-chaincode is not running: %s", cname)
279-
return nil, errors.Errorf("cannot execute transaction for %s", cname)
280+
chaincodeLogger.Debugf("chaincode is not running: %s", cname)
281+
return nil, errors.Errorf("unable to invoke chaincode %s", cname)
280282
}
281283

282284
ccresp, err := handler.Execute(ctxt, cccid, msg, timeout)
@@ -289,17 +291,15 @@ func (cs *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCCo
289291

290292
//Execute - execute proposal, return original response of chaincode
291293
func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.CCContext, spec ccprovider.ChaincodeSpecGetter) (*pb.Response, *pb.ChaincodeEvent, error) {
292-
var err error
293-
var cds *pb.ChaincodeDeploymentSpec
294-
var ci *pb.ChaincodeInvocationSpec
295-
296-
//init will call the Init method of a on a chain
297-
cctyp := pb.ChaincodeMessage_INIT
298-
if cds, _ = spec.(*pb.ChaincodeDeploymentSpec); cds == nil {
299-
if ci, _ = spec.(*pb.ChaincodeInvocationSpec); ci == nil {
300-
panic("Execute should be called with deployment or invocation spec")
301-
}
294+
var cctyp pb.ChaincodeMessage_Type
295+
296+
switch spec.(type) {
297+
case *pb.ChaincodeDeploymentSpec: // init
298+
cctyp = pb.ChaincodeMessage_INIT
299+
case *pb.ChaincodeInvocationSpec: // invoke
302300
cctyp = pb.ChaincodeMessage_TRANSACTION
301+
default:
302+
return nil, nil, errors.New("a deployment or invocation spec is required")
303303
}
304304

305305
cMsg, err := cs.Launch(ctxt, cccid, spec)
@@ -308,101 +308,95 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
308308
}
309309

310310
cMsg.Decorations = cccid.ProposalDecorations
311-
312-
var ccMsg *pb.ChaincodeMessage
313-
ccMsg, err = createCCMessage(cctyp, cccid.ChainID, cccid.TxID, cMsg)
311+
ccMsg, err := createCCMessage(cctyp, cccid.ChainID, cccid.TxID, cMsg)
314312
if err != nil {
315313
return nil, nil, errors.WithMessage(err, "failed to create chaincode message")
316314
}
317315

318-
resp, err := cs.Execute(ctxt, cccid, ccMsg, cs.executetimeout)
316+
resp, err := cs.execute(ctxt, cccid, ccMsg, cs.executetimeout)
319317
if err != nil {
320-
// Rollback transaction
321-
return nil, nil, errors.WithMessage(err, "failed to execute transaction")
322-
} else if resp == nil {
323-
// Rollback transaction
324-
return nil, nil, errors.Errorf("failed to receive a response for txid (%s)", cccid.TxID)
318+
return nil, nil, errors.WithMessage(err, "failed to execute transaction %s")
319+
}
320+
if resp == nil {
321+
return nil, nil, errors.Errorf("nil response from transaction %s", cccid.TxID)
325322
}
326323

327324
if resp.ChaincodeEvent != nil {
328325
resp.ChaincodeEvent.ChaincodeId = cccid.Name
329326
resp.ChaincodeEvent.TxId = cccid.TxID
330327
}
331328

332-
if resp.Type == pb.ChaincodeMessage_COMPLETED {
329+
switch resp.Type {
330+
case pb.ChaincodeMessage_COMPLETED:
333331
res := &pb.Response{}
334-
unmarshalErr := proto.Unmarshal(resp.Payload, res)
335-
if unmarshalErr != nil {
336-
return nil, nil, errors.Wrap(unmarshalErr, fmt.Sprintf("failed to unmarshal response for txid (%s)", cccid.TxID))
332+
err := proto.Unmarshal(resp.Payload, res)
333+
if err != nil {
334+
return nil, nil, errors.Wrapf(err, "failed to unmarshal response for transaction %s", cccid.TxID)
337335
}
338-
339-
// Success
340336
return res, resp.ChaincodeEvent, nil
341-
} else if resp.Type == pb.ChaincodeMessage_ERROR {
342-
// Rollback transaction
343-
return nil, resp.ChaincodeEvent, errors.Errorf("transaction returned with failure: %s", string(resp.Payload))
344-
}
345337

346-
//TODO - this should never happen ... a panic is more appropriate but will save that for future
347-
return nil, nil, errors.Errorf("receive a response for txid (%s) but in invalid state (%d)", cccid.TxID, resp.Type)
348-
}
338+
case pb.ChaincodeMessage_ERROR:
339+
return nil, resp.ChaincodeEvent, errors.Errorf("transaction returned with failure: %s", resp.Payload)
349340

350-
//create a chaincode invocation spec
351-
func createCIS(ccname string, args [][]byte) (*pb.ChaincodeInvocationSpec, error) {
352-
var err error
353-
spec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeId: &pb.ChaincodeID{Name: ccname}, Input: &pb.ChaincodeInput{Args: args}}}
354-
if nil != err {
355-
return nil, err
341+
default:
342+
return nil, nil, errors.Errorf("unexpected response type %d for transaction %s", resp.Type, cccid.TxID)
356343
}
357-
return spec, nil
358344
}
359345

360346
// GetCDS retrieves a chaincode deployment spec for the required chaincode
361347
func (cs *ChaincodeSupport) GetCDS(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) ([]byte, error) {
362348
version := util.GetSysCCVersion()
363349
cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
364-
res, _, err := cs.ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
350+
351+
args := util.ToChaincodeArgs("getdepspec", chainID, chaincodeID)
352+
res, _, err := cs.ExecuteChaincode(ctxt, cccid, args)
365353
if err != nil {
366-
return nil, errors.WithMessage(err, fmt.Sprintf("execute getdepspec(%s, %s) of LSCC error", chainID, chaincodeID))
354+
return nil, errors.Wrapf(err, "getdepspec %s/%s", chainID, chaincodeID)
367355
}
368356
if res.Status != shim.OK {
369-
return nil, errors.Errorf("get ChaincodeDeploymentSpec for %s/%s from LSCC error: %s", chaincodeID, chainID, res.Message)
357+
return nil, errors.Errorf("getdepspec %s/%s: %s", chainID, chaincodeID, res.Message)
370358
}
371359

372360
return res.Payload, nil
373361
}
374362

375-
// GetChaincodeDefinition returns ccprovider.ChaincodeDefinition for the chaincode with the supplied name
363+
// GetChaincodeDefinition returns a resourcesconfig.ChaincodeDefinition for the chaincode
364+
// associated with the provided channel and name.
376365
func (cs *ChaincodeSupport) GetChaincodeDefinition(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (ccprovider.ChaincodeDefinition, error) {
377366
version := util.GetSysCCVersion()
378367
cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
379-
res, _, err := cs.ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
380-
if err == nil {
381-
if res.Status != shim.OK {
382-
return nil, errors.New(res.Message)
383-
}
384-
cd := &ccprovider.ChaincodeData{}
385-
err = proto.Unmarshal(res.Payload, cd)
386-
if err != nil {
387-
return nil, err
388-
}
389-
return cd, nil
368+
369+
args := util.ToChaincodeArgs("getccdata", chainID, chaincodeID)
370+
res, _, err := cs.ExecuteChaincode(ctxt, cccid, args)
371+
if err != nil {
372+
return nil, errors.Wrapf(err, "getccdata %s/%s", chainID, chaincodeID)
373+
}
374+
if res.Status != shim.OK {
375+
return nil, errors.Errorf("getccdata %s/%s: %s", chainID, chaincodeID, res.Message)
390376
}
391377

392-
return nil, err
378+
cd := &ccprovider.ChaincodeData{}
379+
err = proto.Unmarshal(res.Payload, cd)
380+
if err != nil {
381+
return nil, errors.Wrap(err, "failed to unmarshal chaincode definition")
382+
}
383+
384+
return cd, nil
393385
}
394386

395-
// ExecuteChaincode executes a given chaincode given chaincode name and arguments
387+
// ExecuteChaincode invokes chaincode with the provided arguments.
396388
func (cs *ChaincodeSupport) ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
397-
var spec *pb.ChaincodeInvocationSpec
398-
var err error
399-
var res *pb.Response
400-
var ccevent *pb.ChaincodeEvent
389+
invocationSpec := &pb.ChaincodeInvocationSpec{
390+
ChaincodeSpec: &pb.ChaincodeSpec{
391+
Type: pb.ChaincodeSpec_GOLANG,
392+
ChaincodeId: &pb.ChaincodeID{Name: cccid.Name},
393+
Input: &pb.ChaincodeInput{Args: args},
394+
},
395+
}
401396

402-
spec, err = createCIS(cccid.Name, args)
403-
res, ccevent, err = cs.ExecuteSpec(ctxt, cccid, spec)
397+
res, ccevent, err := cs.ExecuteSpec(ctxt, cccid, invocationSpec)
404398
if err != nil {
405-
err = errors.WithMessage(err, "error executing chaincode")
399+
err = errors.WithMessage(err, "error invoking chaincode")
406400
chaincodeLogger.Errorf("%+v", err)
407401
return nil, nil, err
408402
}

core/chaincode/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Registry interface {
5656
type handlerSupport interface {
5757
GetChaincodeDefinition(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (ccprovider.ChaincodeDefinition, error)
5858
Launch(context context.Context, cccid *ccprovider.CCContext, spec ccprovider.ChaincodeSpecGetter) (*pb.ChaincodeInput, error)
59-
Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error)
59+
execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error)
6060
}
6161

6262
// Handler responsible for management of Peer's side of chaincode stream
@@ -1133,7 +1133,7 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
11331133
ccMsg, _ := createCCMessage(pb.ChaincodeMessage_TRANSACTION, calledCcIns.ChainID, msg.Txid, chaincodeInput)
11341134

11351135
// Execute the chaincode... this CANNOT be an init at least for now
1136-
response, execErr := h.handlerSupport.Execute(ctxt, cccid, ccMsg, timeout)
1136+
response, execErr := h.handlerSupport.execute(ctxt, cccid, ccMsg, timeout)
11371137

11381138
//payload is marshalled and send to the calling chaincode's shim which unmarshals and
11391139
//sends it to chaincode

0 commit comments

Comments
 (0)