Skip to content

Commit 4af259b

Browse files
committed
[FAB-14770] stop ignoring marshal errors
Ensure our assumptions are valid by replacing ignored errors with a panic. If ever our assumptions are wrong, this will catch them with minimal fallout. Change-Id: If567bfc89b72d9f9cc24fb1b5c5acbb79ea9c1fe Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent cc18231 commit 4af259b

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

core/chaincode/shim/handler.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (h *Handler) callPeerWithChaincodeMsg(msg *pb.ChaincodeMessage, channelID,
278278
// handleGetState communicates with the peer to fetch the requested state information from the ledger.
279279
func (h *Handler) handleGetState(collection string, key string, channelId string, txid string) ([]byte, error) {
280280
// Construct payload for GET_STATE
281-
payloadBytes, _ := proto.Marshal(&pb.GetState{Collection: collection, Key: key})
281+
payloadBytes := marshalOrPanic(&pb.GetState{Collection: collection, Key: key})
282282

283283
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_STATE, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
284284
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
306306

307307
func (h *Handler) handleGetPrivateDataHash(collection string, key string, channelId string, txid string) ([]byte, error) {
308308
// 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})
310310

311311
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_PRIVATE_DATA_HASH, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
312312
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
334334

335335
func (h *Handler) handleGetStateMetadata(collection string, key string, channelID string, txID string) (map[string][]byte, error) {
336336
// 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})
338338

339339
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_STATE_METADATA, Payload: payloadBytes, Txid: txID, ChannelId: channelID}
340340
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
374374
// handlePutState communicates with the peer to put state information into the ledger.
375375
func (h *Handler) handlePutState(collection string, key string, value []byte, channelId string, txid string) error {
376376
// 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})
378378

379379
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_PUT_STATE, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
380380
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
405405
func (h *Handler) handlePutStateMetadataEntry(collection string, key string, metakey string, metadata []byte, channelID string, txID string) error {
406406
// Construct payload for PUT_STATE_METADATA
407407
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})
409409

410410
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_PUT_STATE_METADATA, Payload: payloadBytes, Txid: txID, ChannelId: channelID}
411411
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
435435

436436
// handleDelState communicates with the peer to delete a key from the state in the ledger.
437437
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})
439439

440440
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_DEL_STATE, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
441441
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
465465
func (h *Handler) handleGetStateByRange(collection, startKey, endKey string, metadata []byte,
466466
channelId string, txid string) (*pb.QueryResponse, error) {
467467
// 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})
470469

471470
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_STATE_BY_RANGE, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
472471
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
510509
defer h.deleteChannel(channelId, txid)
511510

512511
// 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})
515513

516514
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_QUERY_STATE_NEXT, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
517515
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
556554
defer h.deleteChannel(channelId, txid)
557555

558556
// 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})
561558

562559
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_QUERY_STATE_CLOSE, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
563560
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
595592
func (h *Handler) handleGetQueryResult(collection string, query string, metadata []byte,
596593
channelId string, txid string) (*pb.QueryResponse, error) {
597594
// 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})
600596

601597
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_QUERY_RESULT, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
602598
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
639635
defer h.deleteChannel(channelId, txid)
640636

641637
// 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})
644639

645640
msg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_GET_HISTORY_FOR_KEY, Payload: payloadBytes, Txid: txid, ChannelId: channelId}
646641
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 {
681676

682677
// handleInvokeChaincode communicates with the peer to invoke another chaincode.
683678
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}})
686680

687681
// Create the channel on which to communicate the response from validating peer
688682
respChan, err := h.createChannel(channelId, txid)
@@ -812,3 +806,13 @@ func (h *Handler) handleMessage(msg *pb.ChaincodeMessage, errc chan error) error
812806

813807
return nil
814808
}
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

Comments
 (0)