@@ -278,7 +278,7 @@ func (h *Handler) callPeerWithChaincodeMsg(msg *pb.ChaincodeMessage, channelID,
278
278
// handleGetState communicates with the peer to fetch the requested state information from the ledger.
279
279
func (h * Handler ) handleGetState (collection string , key string , channelId string , txid string ) ([]byte , error ) {
280
280
// Construct payload for GET_STATE
281
- payloadBytes , _ := proto . Marshal (& pb.GetState {Collection : collection , Key : key })
281
+ payloadBytes := marshalOrPanic (& pb.GetState {Collection : collection , Key : key })
282
282
283
283
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_STATE , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
284
284
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_STATE )
@@ -306,7 +306,7 @@ func (h *Handler) handleGetState(collection string, key string, channelId string
306
306
307
307
func (h * Handler ) handleGetPrivateDataHash (collection string , key string , channelId string , txid string ) ([]byte , error ) {
308
308
// Construct payload for GET_PRIVATE_DATA_HASH
309
- payloadBytes , _ := proto . Marshal (& pb.GetState {Collection : collection , Key : key })
309
+ payloadBytes := marshalOrPanic (& pb.GetState {Collection : collection , Key : key })
310
310
311
311
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_PRIVATE_DATA_HASH , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
312
312
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_PRIVATE_DATA_HASH )
@@ -334,7 +334,7 @@ func (h *Handler) handleGetPrivateDataHash(collection string, key string, channe
334
334
335
335
func (h * Handler ) handleGetStateMetadata (collection string , key string , channelID string , txID string ) (map [string ][]byte , error ) {
336
336
// Construct payload for GET_STATE_METADATA
337
- payloadBytes , _ := proto . Marshal (& pb.GetStateMetadata {Collection : collection , Key : key })
337
+ payloadBytes := marshalOrPanic (& pb.GetStateMetadata {Collection : collection , Key : key })
338
338
339
339
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_STATE_METADATA , Payload : payloadBytes , Txid : txID , ChannelId : channelID }
340
340
chaincodeLogger .Debugf ("[%s]Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_STATE_METADATA )
@@ -374,7 +374,7 @@ func (h *Handler) handleGetStateMetadata(collection string, key string, channelI
374
374
// handlePutState communicates with the peer to put state information into the ledger.
375
375
func (h * Handler ) handlePutState (collection string , key string , value []byte , channelId string , txid string ) error {
376
376
// Construct payload for PUT_STATE
377
- payloadBytes , _ := proto . Marshal (& pb.PutState {Collection : collection , Key : key , Value : value })
377
+ payloadBytes := marshalOrPanic (& pb.PutState {Collection : collection , Key : key , Value : value })
378
378
379
379
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_PUT_STATE , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
380
380
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_PUT_STATE )
@@ -405,7 +405,7 @@ func (h *Handler) handlePutState(collection string, key string, value []byte, ch
405
405
func (h * Handler ) handlePutStateMetadataEntry (collection string , key string , metakey string , metadata []byte , channelID string , txID string ) error {
406
406
// Construct payload for PUT_STATE_METADATA
407
407
md := & pb.StateMetadata {Metakey : metakey , Value : metadata }
408
- payloadBytes , _ := proto . Marshal (& pb.PutStateMetadata {Collection : collection , Key : key , Metadata : md })
408
+ payloadBytes := marshalOrPanic (& pb.PutStateMetadata {Collection : collection , Key : key , Metadata : md })
409
409
410
410
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_PUT_STATE_METADATA , Payload : payloadBytes , Txid : txID , ChannelId : channelID }
411
411
chaincodeLogger .Debugf ("[%s]Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_PUT_STATE_METADATA )
@@ -435,7 +435,7 @@ func (h *Handler) handlePutStateMetadataEntry(collection string, key string, met
435
435
436
436
// handleDelState communicates with the peer to delete a key from the state in the ledger.
437
437
func (h * Handler ) handleDelState (collection string , key string , channelId string , txid string ) error {
438
- payloadBytes , _ := proto . Marshal (& pb.DelState {Collection : collection , Key : key })
438
+ payloadBytes := marshalOrPanic (& pb.DelState {Collection : collection , Key : key })
439
439
440
440
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_DEL_STATE , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
441
441
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_DEL_STATE )
@@ -465,8 +465,7 @@ func (h *Handler) handleDelState(collection string, key string, channelId string
465
465
func (h * Handler ) handleGetStateByRange (collection , startKey , endKey string , metadata []byte ,
466
466
channelId string , txid string ) (* pb.QueryResponse , error ) {
467
467
// Send GET_STATE_BY_RANGE message to peer chaincode support
468
- // we constructed a valid object. No need to check for error
469
- payloadBytes , _ := proto .Marshal (& pb.GetStateByRange {Collection : collection , StartKey : startKey , EndKey : endKey , Metadata : metadata })
468
+ payloadBytes := marshalOrPanic (& pb.GetStateByRange {Collection : collection , StartKey : startKey , EndKey : endKey , Metadata : metadata })
470
469
471
470
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_STATE_BY_RANGE , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
472
471
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_STATE_BY_RANGE )
@@ -510,8 +509,7 @@ func (h *Handler) handleQueryStateNext(id, channelId, txid string) (*pb.QueryRes
510
509
defer h .deleteChannel (channelId , txid )
511
510
512
511
// Send QUERY_STATE_NEXT message to peer chaincode support
513
- // we constructed a valid object. No need to check for error
514
- payloadBytes , _ := proto .Marshal (& pb.QueryStateNext {Id : id })
512
+ payloadBytes := marshalOrPanic (& pb.QueryStateNext {Id : id })
515
513
516
514
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_QUERY_STATE_NEXT , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
517
515
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_QUERY_STATE_NEXT )
@@ -556,8 +554,7 @@ func (h *Handler) handleQueryStateClose(id, channelId, txid string) (*pb.QueryRe
556
554
defer h .deleteChannel (channelId , txid )
557
555
558
556
// Send QUERY_STATE_CLOSE message to peer chaincode support
559
- // we constructed a valid object. No need to check for error
560
- payloadBytes , _ := proto .Marshal (& pb.QueryStateClose {Id : id })
557
+ payloadBytes := marshalOrPanic (& pb.QueryStateClose {Id : id })
561
558
562
559
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_QUERY_STATE_CLOSE , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
563
560
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_QUERY_STATE_CLOSE )
@@ -595,8 +592,7 @@ func (h *Handler) handleQueryStateClose(id, channelId, txid string) (*pb.QueryRe
595
592
func (h * Handler ) handleGetQueryResult (collection string , query string , metadata []byte ,
596
593
channelId string , txid string ) (* pb.QueryResponse , error ) {
597
594
// Send GET_QUERY_RESULT message to peer chaincode support
598
- // we constructed a valid object. No need to check for error
599
- payloadBytes , _ := proto .Marshal (& pb.GetQueryResult {Collection : collection , Query : query , Metadata : metadata })
595
+ payloadBytes := marshalOrPanic (& pb.GetQueryResult {Collection : collection , Query : query , Metadata : metadata })
600
596
601
597
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_QUERY_RESULT , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
602
598
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_QUERY_RESULT )
@@ -639,8 +635,7 @@ func (h *Handler) handleGetHistoryForKey(key string, channelId string, txid stri
639
635
defer h .deleteChannel (channelId , txid )
640
636
641
637
// Send GET_HISTORY_FOR_KEY message to peer chaincode support
642
- // we constructed a valid object. No need to check for error
643
- payloadBytes , _ := proto .Marshal (& pb.GetHistoryForKey {Key : key })
638
+ payloadBytes := marshalOrPanic (& pb.GetHistoryForKey {Key : key })
644
639
645
640
msg := & pb.ChaincodeMessage {Type : pb .ChaincodeMessage_GET_HISTORY_FOR_KEY , Payload : payloadBytes , Txid : txid , ChannelId : channelId }
646
641
chaincodeLogger .Debugf ("[%s] Sending %s" , shorttxid (msg .Txid ), pb .ChaincodeMessage_GET_HISTORY_FOR_KEY )
@@ -681,8 +676,7 @@ func (h *Handler) createResponse(status int32, payload []byte) pb.Response {
681
676
682
677
// handleInvokeChaincode communicates with the peer to invoke another chaincode.
683
678
func (h * Handler ) handleInvokeChaincode (chaincodeName string , args [][]byte , channelId string , txid string ) pb.Response {
684
- // we constructed a valid object. No need to check for error
685
- payloadBytes , _ := proto .Marshal (& pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : chaincodeName }, Input : & pb.ChaincodeInput {Args : args }})
679
+ payloadBytes := marshalOrPanic (& pb.ChaincodeSpec {ChaincodeId : & pb.ChaincodeID {Name : chaincodeName }, Input : & pb.ChaincodeInput {Args : args }})
686
680
687
681
// Create the channel on which to communicate the response from validating peer
688
682
respChan , err := h .createChannel (channelId , txid )
@@ -812,3 +806,13 @@ func (h *Handler) handleMessage(msg *pb.ChaincodeMessage, errc chan error) error
812
806
813
807
return nil
814
808
}
809
+
810
+ // marshalOrPanic attempts to marshal the provided protobbuf message but will panic
811
+ // when marshaling fails instead of returning an error.
812
+ func marshalOrPanic (msg proto.Message ) []byte {
813
+ bytes , err := proto .Marshal (msg )
814
+ if err != nil {
815
+ panic (err )
816
+ }
817
+ return bytes
818
+ }
0 commit comments