Skip to content

Commit

Permalink
[FAB-9814] Use enum in handler conditionals
Browse files Browse the repository at this point in the history
Change-Id: I5a5346783e61ba044adf5cf92381b7e4862483b0
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed May 2, 2018
1 parent 426b5dd commit 1496b78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
16 changes: 8 additions & 8 deletions core/chaincode/chaincode_support_test.go
Expand Up @@ -1086,7 +1086,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
chnl := "test"
txid := "1"
// test getTxContext for TEST channel, tx=1, msgType=IVNOKE_CHAINCODE and empty payload - empty payload => expect to return empty txContext
txContext, _ := h.getTxContextForMessage(chnl, "1", pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), []byte(""), "[%s]No ledger context for %s. Sending %s", 12345, "TestCC", pb.ChaincodeMessage_ERROR)
txContext, _ := h.getTxContextForMessage(chnl, "1", pb.ChaincodeMessage_INVOKE_CHAINCODE, []byte(""), "[%s]No ledger context for %s. Sending %s", 12345, "TestCC", pb.ChaincodeMessage_ERROR)
if txContext != nil {
t.Fatalf("expected empty txContext for empty payload")
}
Expand All @@ -1105,14 +1105,14 @@ func TestGetTxContextFromHandler(t *testing.T) {
txCtxGenerated, payload := genNewPldAndCtxFromLdgr(t, "shimTestCC", chnl, txid, pldgr, &h)

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

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

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

// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
Expand All @@ -1148,7 +1148,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
chnl = ""
txCtxGenerated, payload = genNewPldAndCtxFromLdgr(t, "shimTestCC", chnl, txid, pldgr, &h)
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
if txContext == nil || ccMsg != nil || txContext != txCtxGenerated {
t.Fatalf("expected successful txContext for non empty payload and INVOKE_CHAINCODE msgType. triggerNextStateMsg: %s.", ccMsg)
Expand All @@ -1158,7 +1158,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
txid = "5"
payload = genNewPld(t, "shimTestCC")
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
if txContext != nil || ccMsg == nil {
t.Fatal("expected nil txContext for non empty payload and INVOKE_CHAINCODE msgType without the ledger generating a TxContext . unexpected non nil tcContext")
Expand All @@ -1168,7 +1168,7 @@ func TestGetTxContextFromHandler(t *testing.T) {
txid = "6"
payload = genNewPld(t, "lscc")
// test getting a TxContext with an SCC with channel TEST => expect to return a non empty txContext
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), payload,
txContext, ccMsg = h.getTxContextForMessage(chnl, txid, pb.ChaincodeMessage_INVOKE_CHAINCODE, payload,
"[%s]No ledger context for %s. Sending %s", 12345, pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), pb.ChaincodeMessage_ERROR)
if txContext != nil || ccMsg == nil {
t.Fatal("expected nil txContext for non empty payload and INVOKE_CHAINCODE msgType without the ledger generating a TxContext . unexpected non nil tcContext")
Expand Down
14 changes: 8 additions & 6 deletions core/chaincode/handler.go
Expand Up @@ -922,10 +922,10 @@ func isCollectionSet(collection string) bool {
return true
}

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

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

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

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

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

} else if msg.Type.String() == pb.ChaincodeMessage_DEL_STATE.String() {
case pb.ChaincodeMessage_DEL_STATE:
// Invoke ledger to delete state
delState := &pb.DelState{}
unmarshalErr := proto.Unmarshal(msg.Payload, delState)
Expand All @@ -1028,7 +1029,8 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {
} else {
err = txContext.txsimulator.DeleteState(chaincodeID, delState.Key)
}
} else if msg.Type.String() == pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {

case pb.ChaincodeMessage_INVOKE_CHAINCODE:
chaincodeLogger.Debugf("[%s] C-call-C", shorttxid(msg.Txid))
chaincodeSpec := &pb.ChaincodeSpec{}
unmarshalErr := proto.Unmarshal(msg.Payload, chaincodeSpec)
Expand Down

0 comments on commit 1496b78

Please sign in to comment.