Skip to content

Commit 1416a10

Browse files
committed
[FAB-14770] remove unnecessary String calls
If something is a fmt.Stringer, the fmt package knows how to deal with it already. And, when a constant is a typed scalar, there's no reason to "stringify" when comparing. Change-Id: I90e141c80c82640843addffe04f911091bc099eb Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 505855f commit 1416a10

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

core/chaincode/shim/handler.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -203,23 +203,23 @@ func (h *Handler) handleInit(msg *pb.ChaincodeMessage, errc chan error) {
203203
// Get the function and args from Payload
204204
input := &pb.ChaincodeInput{}
205205
unmarshalErr := proto.Unmarshal(msg.Payload, input)
206-
if nextStateMsg = errFunc(unmarshalErr, nil, nil, "[%s] Incorrect payload format. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
206+
if nextStateMsg = errFunc(unmarshalErr, nil, nil, "[%s] Incorrect payload format. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
207207
return
208208
}
209209

210210
// Call chaincode's Run
211211
// Create the ChaincodeStub which the chaincode can use to callback
212212
stub := new(ChaincodeStub)
213213
err := stub.init(h, msg.ChannelId, msg.Txid, input, msg.Proposal)
214-
if nextStateMsg = errFunc(err, nil, stub.chaincodeEvent, "[%s] Init get error response. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
214+
if nextStateMsg = errFunc(err, nil, stub.chaincodeEvent, "[%s] Init get error response. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
215215
return
216216
}
217217
res := h.cc.Init(stub)
218218
chaincodeLogger.Debugf("[%s] Init get response status: %d", shorttxid(msg.Txid), res.Status)
219219

220220
if res.Status >= ERROR {
221221
err = errors.New(res.Message)
222-
if nextStateMsg = errFunc(err, []byte(res.Message), stub.chaincodeEvent, "[%s] Init get error response. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
222+
if nextStateMsg = errFunc(err, []byte(res.Message), stub.chaincodeEvent, "[%s] Init get error response. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
223223
return
224224
}
225225
}
@@ -263,22 +263,22 @@ func (h *Handler) handleTransaction(msg *pb.ChaincodeMessage, errc chan error) {
263263
// Get the function and args from Payload
264264
input := &pb.ChaincodeInput{}
265265
unmarshalErr := proto.Unmarshal(msg.Payload, input)
266-
if nextStateMsg = errFunc(unmarshalErr, nil, "[%s] Incorrect payload format. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
266+
if nextStateMsg = errFunc(unmarshalErr, nil, "[%s] Incorrect payload format. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
267267
return
268268
}
269269

270270
// Call chaincode's Run
271271
// Create the ChaincodeStub which the chaincode can use to callback
272272
stub := new(ChaincodeStub)
273273
err := stub.init(h, msg.ChannelId, msg.Txid, input, msg.Proposal)
274-
if nextStateMsg = errFunc(err, stub.chaincodeEvent, "[%s] Transaction execution failed. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
274+
if nextStateMsg = errFunc(err, stub.chaincodeEvent, "[%s] Transaction execution failed. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
275275
return
276276
}
277277
res := h.cc.Invoke(stub)
278278

279279
// Endorser will handle error contained in Response.
280280
resBytes, err := proto.Marshal(&res)
281-
if nextStateMsg = errFunc(err, stub.chaincodeEvent, "[%s] Transaction execution failed. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR.String()); nextStateMsg != nil {
281+
if nextStateMsg = errFunc(err, stub.chaincodeEvent, "[%s] Transaction execution failed. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR); nextStateMsg != nil {
282282
return
283283
}
284284

@@ -315,12 +315,12 @@ func (h *Handler) handleGetState(collection string, key string, channelId string
315315
return nil, errors.WithMessage(err, fmt.Sprintf("[%s] error sending %s", shorttxid(txid), pb.ChaincodeMessage_GET_STATE))
316316
}
317317

318-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
318+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
319319
// Success response
320320
chaincodeLogger.Debugf("[%s] GetState received payload %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
321321
return responseMsg.Payload, nil
322322
}
323-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
323+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
324324
// Error response
325325
chaincodeLogger.Errorf("[%s] GetState received error %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
326326
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -343,12 +343,12 @@ func (h *Handler) handleGetPrivateDataHash(collection string, key string, channe
343343
return nil, errors.WithMessage(err, fmt.Sprintf("[%s] error sending %s", shorttxid(txid), pb.ChaincodeMessage_GET_PRIVATE_DATA_HASH))
344344
}
345345

346-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
346+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
347347
// Success response
348348
chaincodeLogger.Debugf("[%s] GetPrivateDataHash received payload %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
349349
return responseMsg.Payload, nil
350350
}
351-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
351+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
352352
// Error response
353353
chaincodeLogger.Errorf("[%s] GetPrivateDataHash received error %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
354354
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -371,7 +371,7 @@ func (h *Handler) handleGetStateMetadata(collection string, key string, channelI
371371
return nil, errors.WithMessage(err, fmt.Sprintf("[%s] error sending %s", shorttxid(txID), pb.ChaincodeMessage_GET_STATE_METADATA))
372372
}
373373

374-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
374+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
375375
// Success response
376376
chaincodeLogger.Debugf("[%s]GetStateMetadata received payload %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
377377
var mdResult pb.StateMetadataResult
@@ -387,7 +387,7 @@ func (h *Handler) handleGetStateMetadata(collection string, key string, channelI
387387

388388
return metadata, nil
389389
}
390-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
390+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
391391
// Error response
392392
chaincodeLogger.Errorf("[%s]GetStateMetadata received error %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
393393
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -413,13 +413,13 @@ func (h *Handler) handlePutState(collection string, key string, value []byte, ch
413413
return errors.WithMessage(err, fmt.Sprintf("[%s] error sending %s", msg.Txid, pb.ChaincodeMessage_PUT_STATE))
414414
}
415415

416-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
416+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
417417
// Success response
418418
chaincodeLogger.Debugf("[%s] Received %s. Successfully updated state", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
419419
return nil
420420
}
421421

422-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
422+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
423423
// Error response
424424
chaincodeLogger.Errorf("[%s] Received %s. Payload: %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR, responseMsg.Payload)
425425
return errors.New(string(responseMsg.Payload[:]))
@@ -444,13 +444,13 @@ func (h *Handler) handlePutStateMetadataEntry(collection string, key string, met
444444
return errors.WithMessage(err, fmt.Sprintf("[%s] error sending %s", msg.Txid, pb.ChaincodeMessage_PUT_STATE_METADATA))
445445
}
446446

447-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
447+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
448448
// Success response
449449
chaincodeLogger.Debugf("[%s]Received %s. Successfully updated state metadata", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
450450
return nil
451451
}
452452

453-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
453+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
454454
// Error response
455455
chaincodeLogger.Errorf("[%s]Received %s. Payload: %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR, responseMsg.Payload)
456456
return errors.New(string(responseMsg.Payload[:]))
@@ -475,12 +475,12 @@ func (h *Handler) handleDelState(collection string, key string, channelId string
475475
return errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_DEL_STATE)
476476
}
477477

478-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
478+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
479479
// Success response
480480
chaincodeLogger.Debugf("[%s] Received %s. Successfully deleted state", msg.Txid, pb.ChaincodeMessage_RESPONSE)
481481
return nil
482482
}
483-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
483+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
484484
// Error response
485485
chaincodeLogger.Errorf("[%s] Received %s. Payload: %s", msg.Txid, pb.ChaincodeMessage_ERROR, responseMsg.Payload)
486486
return errors.New(string(responseMsg.Payload[:]))
@@ -505,7 +505,7 @@ func (h *Handler) handleGetStateByRange(collection, startKey, endKey string, met
505505
return nil, errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_GET_STATE_BY_RANGE)
506506
}
507507

508-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
508+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
509509
// Success response
510510
chaincodeLogger.Debugf("[%s] Received %s. Successfully got range", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
511511

@@ -518,7 +518,7 @@ func (h *Handler) handleGetStateByRange(collection, startKey, endKey string, met
518518

519519
return rangeQueryResponse, nil
520520
}
521-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
521+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
522522
// Error response
523523
chaincodeLogger.Errorf("[%s] Received %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
524524
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -552,7 +552,7 @@ func (h *Handler) handleQueryStateNext(id, channelId, txid string) (*pb.QueryRes
552552
return nil, errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_QUERY_STATE_NEXT)
553553
}
554554

555-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
555+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
556556
// Success response
557557
chaincodeLogger.Debugf("[%s] Received %s. Successfully got range", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
558558

@@ -564,7 +564,7 @@ func (h *Handler) handleQueryStateNext(id, channelId, txid string) (*pb.QueryRes
564564

565565
return queryResponse, nil
566566
}
567-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
567+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
568568
// Error response
569569
chaincodeLogger.Errorf("[%s] Received %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
570570
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -598,7 +598,7 @@ func (h *Handler) handleQueryStateClose(id, channelId, txid string) (*pb.QueryRe
598598
return nil, errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_QUERY_STATE_CLOSE)
599599
}
600600

601-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
601+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
602602
// Success response
603603
chaincodeLogger.Debugf("[%s] Received %s. Successfully got range", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
604604

@@ -610,7 +610,7 @@ func (h *Handler) handleQueryStateClose(id, channelId, txid string) (*pb.QueryRe
610610

611611
return queryResponse, nil
612612
}
613-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
613+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
614614
// Error response
615615
chaincodeLogger.Errorf("[%s] Received %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
616616
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -635,7 +635,7 @@ func (h *Handler) handleGetQueryResult(collection string, query string, metadata
635635
return nil, errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_GET_QUERY_RESULT)
636636
}
637637

638-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
638+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
639639
// Success response
640640
chaincodeLogger.Debugf("[%s] Received %s. Successfully got range", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
641641

@@ -647,7 +647,7 @@ func (h *Handler) handleGetQueryResult(collection string, query string, metadata
647647

648648
return executeQueryResponse, nil
649649
}
650-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
650+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
651651
// Error response
652652
chaincodeLogger.Errorf("[%s] Received %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
653653
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -681,7 +681,7 @@ func (h *Handler) handleGetHistoryForKey(key string, channelId string, txid stri
681681
return nil, errors.Errorf("[%s] error sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_GET_HISTORY_FOR_KEY)
682682
}
683683

684-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
684+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
685685
// Success response
686686
chaincodeLogger.Debugf("[%s] Received %s. Successfully got range", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
687687

@@ -693,7 +693,7 @@ func (h *Handler) handleGetHistoryForKey(key string, channelId string, txid stri
693693

694694
return getHistoryForKeyResponse, nil
695695
}
696-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
696+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
697697
// Error response
698698
chaincodeLogger.Errorf("[%s] Received %s", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
699699
return nil, errors.New(string(responseMsg.Payload[:]))
@@ -732,7 +732,7 @@ func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, cha
732732
return h.createResponse(ERROR, []byte(errStr))
733733
}
734734

735-
if responseMsg.Type.String() == pb.ChaincodeMessage_RESPONSE.String() {
735+
if responseMsg.Type == pb.ChaincodeMessage_RESPONSE {
736736
// Success response
737737
chaincodeLogger.Debugf("[%s] Received %s. Successfully invoked chaincode", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_RESPONSE)
738738
respMsg := &pb.ChaincodeMessage{}
@@ -753,7 +753,7 @@ func (h *Handler) handleInvokeChaincode(chaincodeName string, args [][]byte, cha
753753
chaincodeLogger.Errorf("[%s] Received %s. Error from chaincode", shorttxid(responseMsg.Txid), respMsg.Type)
754754
return h.createResponse(ERROR, responseMsg.Payload)
755755
}
756-
if responseMsg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
756+
if responseMsg.Type == pb.ChaincodeMessage_ERROR {
757757
// Error response
758758
chaincodeLogger.Errorf("[%s] Received %s.", shorttxid(responseMsg.Txid), pb.ChaincodeMessage_ERROR)
759759
return h.createResponse(ERROR, responseMsg.Payload)

0 commit comments

Comments
 (0)