Skip to content

Commit

Permalink
[FAB-9813] Handler uses txContexts without wrapper
Browse files Browse the repository at this point in the history
Change-Id: Ifd6385ba6ecfc47d326f3e4550cbb069874523c6
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed May 2, 2018
1 parent 05c8272 commit 426b5dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_suite_test.go
Expand Up @@ -74,5 +74,5 @@ type launchRegistry interface {
// Helpers to access unexported state.

func SetHandlerTxContexts(h *Handler, txContexts *TransactionContexts) {
h.txCtxs = txContexts
h.txContexts = txContexts
}
4 changes: 2 additions & 2 deletions core/chaincode/chaincode_support_test.go
Expand Up @@ -1081,7 +1081,7 @@ func TestStartAndWaitLaunchError(t *testing.T) {
}

func TestGetTxContextFromHandler(t *testing.T) {
h := Handler{txCtxs: NewTransactionContexts(), sccp: &scc.ProviderImpl{Peer: peer.Default, PeerSupport: peer.DefaultSupport}}
h := Handler{txContexts: NewTransactionContexts(), sccp: &scc.ProviderImpl{Peer: peer.Default, PeerSupport: peer.DefaultSupport}}

chnl := "test"
txid := "1"
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func genNewPldAndCtxFromLdgr(t *testing.T, ccName string, chnl string, txid stri
// get a context for this txsim
ctxt := context.WithValue(context.Background(), TXSimulatorKey, txsim)
// create a new txContext in the handler to be retrieved by the tested function (ie: getTxContextForMessage)
newTxCtxt, err := h.createTxContext(ctxt, chnl, txid, nil, nil)
newTxCtxt, err := h.txContexts.Create(ctxt, chnl, txid, nil, nil)
if err != nil {
t.Fatalf("Error creating TxContext by the handler for cc %s and channel '%s': %s", ccName, chnl, err)
}
Expand Down
58 changes: 21 additions & 37 deletions core/chaincode/handler.go
Expand Up @@ -75,7 +75,7 @@ type Handler struct {

// Map of tx txid to either invoke tx. Each tx will be
// added prior to execute and remove when done execute
txCtxs *TransactionContexts
txContexts *TransactionContexts

// set of active transaction identifiers
activeTransactions *ActiveTransactions
Expand All @@ -92,7 +92,7 @@ func newChaincodeSupportHandler(chaincodeSupport *ChaincodeSupport, peerChatStre
handlerSupport: chaincodeSupport,
state: created,
errChan: make(chan error, 1),
txCtxs: NewTransactionContexts(),
txContexts: NewTransactionContexts(),
activeTransactions: NewActiveTransactions(),
keepalive: chaincodeSupport.Keepalive,
aclProvider: chaincodeSupport.ACLProvider,
Expand Down Expand Up @@ -235,25 +235,6 @@ func (h *Handler) serialSendAsync(msg *pb.ChaincodeMessage, sendErr bool) {
}()
}

// transaction context id should be composed of chainID and txid. While
// needed for CC-2-CC, it also allows users to concurrently send proposals
// with the same TXID to the SAME CC on multiple channels
func (h *Handler) getTxCtxId(chainID string, txid string) string {
return chainID + txid
}

func (h *Handler) createTxContext(ctxt context.Context, chainID string, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal) (*TransactionContext, error) {
return h.txCtxs.Create(ctxt, chainID, txid, signedProp, prop)
}

func (h *Handler) getTxContext(chainID, txid string) *TransactionContext {
return h.txCtxs.Get(chainID, txid)
}

func (h *Handler) deleteTxContext(chainID, txid string) {
h.txCtxs.Delete(chainID, txid)
}

// Check if the transactor is allow to call this chaincode on this channel
func (h *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal, ccIns *sysccprovider.ChaincodeInstance) error {
// ensure that we don't invoke a system chaincode
Expand Down Expand Up @@ -464,7 +445,7 @@ func (h *Handler) handleRegister(msg *pb.ChaincodeMessage) {
}

func (h *Handler) notify(msg *pb.ChaincodeMessage) {
tctx := h.txCtxs.Get(msg.ChannelId, msg.Txid)
tctx := h.txContexts.Get(msg.ChannelId, msg.Txid)
if tctx == nil {
chaincodeLogger.Debugf("notifier Txid:%s, channelID:%s does not exist for handleing message %s", msg.Txid, msg.ChannelId, msg.Type)
return
Expand All @@ -477,7 +458,7 @@ func (h *Handler) notify(msg *pb.ChaincodeMessage) {

// is this a txid for which there is a valid txsim
func (h *Handler) isValidTxSim(channelID string, txid string, fmtStr string, args ...interface{}) (*TransactionContext, *pb.ChaincodeMessage) {
txContext := h.getTxContext(channelID, txid)
txContext := h.txContexts.Get(channelID, txid)
if txContext == nil || txContext.txsimulator == nil {
// Send error msg back to chaincode. No ledger context
errStr := fmt.Sprintf(fmtStr, args...)
Expand Down Expand Up @@ -547,13 +528,11 @@ func (h *Handler) handleGetState(msg *pb.ChaincodeMessage) {
if err != nil {
// Send error msg back to chaincode. GetState will not trigger event
payload := []byte(err.Error())
chaincodeLogger.Errorf("[%s]Failed to get chaincode state(%s). Sending %s",
shorttxid(msg.Txid), err, pb.ChaincodeMessage_ERROR)
chaincodeLogger.Errorf("[%s]Failed to get chaincode state(%s). Sending %s", shorttxid(msg.Txid), err, pb.ChaincodeMessage_ERROR)
serialSendMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Txid: msg.Txid, ChannelId: msg.ChannelId}
} else if res == nil {
// The state object being requested does not exist
chaincodeLogger.Debugf("[%s]No state associated with key: %s. Sending %s with an empty payload",
shorttxid(msg.Txid), key, pb.ChaincodeMessage_RESPONSE)
chaincodeLogger.Debugf("[%s]No state associated with key: %s. Sending %s with an empty payload", shorttxid(msg.Txid), key, pb.ChaincodeMessage_RESPONSE)
serialSendMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Payload: res, Txid: msg.Txid, ChannelId: msg.ChannelId}
} else {
// Send response msg back to chaincode. GetState will not trigger event
Expand Down Expand Up @@ -709,7 +688,7 @@ func (h *Handler) handleQueryStateNext(msg *pb.ChaincodeMessage) {
return
}

txContext = h.getTxContext(msg.ChannelId, msg.Txid)
txContext = h.txContexts.Get(msg.ChannelId, msg.Txid)
if txContext == nil {
errHandler([]byte("transaction context not found (timed out ?)"), nil, "[%s]Failed to get transaction context. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
return
Expand Down Expand Up @@ -765,7 +744,7 @@ func (h *Handler) handleQueryStateClose(msg *pb.ChaincodeMessage) {
return
}

txContext := h.getTxContext(msg.ChannelId, msg.Txid)
txContext := h.txContexts.Get(msg.ChannelId, msg.Txid)
if txContext == nil {
errHandler([]byte("transaction context not found (timed out ?)"), "[%s]Failed to get transaction context. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
return
Expand Down Expand Up @@ -981,7 +960,7 @@ func (h *Handler) getTxContextForMessage(channelID string, txid string, msgType

// Calling SCC without a ChainID, then the assumption this is an external SCC called by the client (special case) and no UCC involved,
// so no Transaction Simulator validation needed as there are no commits to the ledger, get the txContext directly if it is not nil
if txContext = h.getTxContext(channelID, txid); txContext == nil {
if txContext = h.txContexts.Get(channelID, txid); txContext == nil {
errStr := fmt.Sprintf(fmtStr, args)
triggerNextStateMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(errStr), Txid: txid}
return nil, triggerNextStateMsg
Expand Down Expand Up @@ -1074,10 +1053,15 @@ func (h *Handler) handleModState(msg *pb.ChaincodeMessage) {

err := h.checkACL(txContext.signedProp, txContext.proposal, calledCcIns)
if err != nil {
chaincodeLogger.Errorf("[%s] C-call-C %s on channel %s failed check ACL [%v]: [%s]",
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID, txContext.signedProp, err)
triggerNextStateMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR,
Payload: []byte(err.Error()), Txid: msg.Txid}
chaincodeLogger.Errorf(
"[%s] C-call-C %s on channel %s failed check ACL [%v]: [%s]",
shorttxid(msg.Txid),
calledCcIns.ChaincodeName,
calledCcIns.ChainID,
txContext.signedProp,
err,
)
triggerNextStateMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(err.Error()), Txid: msg.Txid}
return
}

Expand Down Expand Up @@ -1213,13 +1197,13 @@ func (h *Handler) Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg
}

// our responsibility to delete transaction context if sendExecuteMessage succeeded
h.deleteTxContext(msg.ChannelId, msg.Txid)
h.txContexts.Delete(msg.ChannelId, msg.Txid)

return ccresp, err
}

func (h *Handler) sendExecuteMessage(ctxt context.Context, chainID string, msg *pb.ChaincodeMessage, signedProp *pb.SignedProposal, prop *pb.Proposal) (chan *pb.ChaincodeMessage, error) {
txctx, err := h.createTxContext(ctxt, chainID, msg.Txid, signedProp, prop)
txctx, err := h.txContexts.Create(ctxt, chainID, msg.Txid, signedProp, prop)
if err != nil {
return nil, err
}
Expand All @@ -1237,5 +1221,5 @@ func (h *Handler) sendExecuteMessage(ctxt context.Context, chainID string, msg *
}

func (h *Handler) Close() {
h.txCtxs.Close()
h.txContexts.Close()
}

0 comments on commit 426b5dd

Please sign in to comment.