Skip to content

Commit 1496b78

Browse files
committed
[FAB-9814] Use enum in handler conditionals
Change-Id: I5a5346783e61ba044adf5cf92381b7e4862483b0 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 426b5dd commit 1496b78

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

core/chaincode/chaincode_support_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
10861086
chnl := "test"
10871087
txid := "1"
10881088
// test getTxContext for TEST channel, tx=1, msgType=IVNOKE_CHAINCODE and empty payload - empty payload => expect to return empty txContext
1089-
txContext, _ := h.getTxContextForMessage(chnl, "1", pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), []byte(""), "[%s]No ledger context for %s. Sending %s", 12345, "TestCC", pb.ChaincodeMessage_ERROR)
1089+
txContext, _ := h.getTxContextForMessage(chnl, "1", pb.ChaincodeMessage_INVOKE_CHAINCODE, []byte(""), "[%s]No ledger context for %s. Sending %s", 12345, "TestCC", pb.ChaincodeMessage_ERROR)
10901090
if txContext != nil {
10911091
t.Fatalf("expected empty txContext for empty payload")
10921092
}
@@ -1105,14 +1105,14 @@ func TestGetTxContextFromHandler(t *testing.T) {
11051105
txCtxGenerated, payload := genNewPldAndCtxFromLdgr(t, "shimTestCC", chnl, txid, pldgr, &h)
11061106

11071107
// test getTxContext for TEST channel, tx=1, msgType=IVNOKE_CHAINCODE and non empty payload => must return a non empty txContext
1108-
txContext, ccMsg := h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1108+
txContext, ccMsg := h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11091109
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11101110
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
11111111
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
11121112
}
11131113

11141114
// test for another msgType (PUT_STATE) with the same payload ==> must return a non empty txContext
1115-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_PUT_STATE.String(), payload,
1115+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_PUT_STATE, payload,
11161116
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_PUT_STATE.String(), pb.ChaincodeMessage_ERROR)
11171117
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
11181118
t.Fatalf("expected successful txContext for non empty payload and PUT_STATE msgType. triggerNextStateMsg: %s.", ccMsg)
@@ -1125,7 +1125,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
11251125
txCtxGenerated, payload = genNewPldAndCtxFromLdgr(t, "lscc", chnl, txid, pldgr, &h)
11261126

11271127
// test getting a TxContext with an SCC without a channel => expect to return a non empty txContext
1128-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1128+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11291129
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11301130
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
11311131
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
@@ -1137,7 +1137,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
11371137
txCtxGenerated, payload = genNewPldAndCtxFromLdgr(t, "lscc", chnl, txid, pldgr, &h)
11381138

11391139
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
1140-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1140+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11411141
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11421142
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
11431143
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
@@ -1148,7 +1148,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
11481148
chnl = ""
11491149
txCtxGenerated, payload = genNewPldAndCtxFromLdgr(t, "shimTestCC", chnl, txid, pldgr, &h)
11501150
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
1151-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1151+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11521152
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11531153
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
11541154
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
@@ -1158,7 +1158,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
11581158
txid = "5"
11591159
payload = genNewPld(t, "shimTestCC")
11601160
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
1161-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1161+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11621162
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11631163
if txContext != nil || ccMsg == nil {
11641164
t.Fatal("expected nil txContext for non empty payload and INVOKE_CHAINCODE msgType without the ledger generating a TxContext . unexpected non nil tcContext")
@@ -1168,7 +1168,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
11681168
txid = "6"
11691169
payload = genNewPld(t, "lscc")
11701170
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
1171-
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
1171+
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
11721172
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
11731173
if txContext != nil || ccMsg == nil {
11741174
t.Fatal("expected nil txContext for non empty payload and INVOKE_CHAINCODE msgType without the ledger generating a TxContext . unexpected non nil tcContext")

core/chaincode/handler.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,10 @@ func isCollectionSet(collection string) bool {
922922
return true
923923
}
924924

925-
func (h *Handler) getTxContextForMessage(channelID string, txid string, msgType string, payload []byte, fmtStr string, args ...interface{}) (*TransactionContext, *pb.ChaincodeMessage) {
925+
func (h *Handler) getTxContextForMessage(channelID string, txid string, msgType pb.ChaincodeMessage_Type, payload []byte, fmtStr string, args ...interface{}) (*TransactionContext, *pb.ChaincodeMessage) {
926926
// if we have a channelID, just get the txsim from isValidTxSim
927927
// if this is NOT an INVOKE_CHAINCODE, then let isValidTxSim handle retrieving the txContext
928-
if channelID != "" || msgType != pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {
928+
if channelID != "" || msgType != pb.ChaincodeMessage_INVOKE_CHAINCODE {
929929
return h.isValidTxSim(channelID, txid, fmtStr, args)
930930
}
931931

@@ -980,7 +980,7 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
980980

981981
var triggerNextStateMsg *pb.ChaincodeMessage
982982
var txContext *TransactionContext
983-
txContext, triggerNextStateMsg = h.getTxContextForMessage(msg.ChannelId, msg.Txid, msg.Type.String(), msg.Payload,
983+
txContext, triggerNextStateMsg = h.getTxContextForMessage(msg.ChannelId, msg.Txid, msg.Type, msg.Payload,
984984
"[%s]No ledger context for %s. Sending %s", shorttxid(msg.Txid), msg.Type, pb.ChaincodeMessage_ERROR)
985985

986986
defer func() {
@@ -1000,7 +1000,8 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
10001000
var err error
10011001
var res []byte
10021002

1003-
if msg.Type.String() == pb.ChaincodeMessage_PUT_STATE.String() {
1003+
switch msg.Type {
1004+
case pb.ChaincodeMessage_PUT_STATE:
10041005
putState := &pb.PutState{}
10051006
unmarshalErr := proto.Unmarshal(msg.Payload, putState)
10061007
if unmarshalErr != nil {
@@ -1014,7 +1015,7 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
10141015
err = txContext.txsimulator.SetState(chaincodeID, putState.Key, putState.Value)
10151016
}
10161017

1017-
} else if msg.Type.String() == pb.ChaincodeMessage_DEL_STATE.String() {
1018+
case pb.ChaincodeMessage_DEL_STATE:
10181019
// Invoke ledger to delete state
10191020
delState := &pb.DelState{}
10201021
unmarshalErr := proto.Unmarshal(msg.Payload, delState)
@@ -1028,7 +1029,8 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
10281029
} else {
10291030
err = txContext.txsimulator.DeleteState(chaincodeID, delState.Key)
10301031
}
1031-
} else if msg.Type.String() == pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {
1032+
1033+
case pb.ChaincodeMessage_INVOKE_CHAINCODE:
10321034
chaincodeLogger.Debugf("[%s] C-call-C", shorttxid(msg.Txid))
10331035
chaincodeSpec := &pb.ChaincodeSpec{}
10341036
unmarshalErr := proto.Unmarshal(msg.Payload, chaincodeSpec)

0 commit comments

Comments
 (0)