Skip to content

Commit 1c4ff73

Browse files
committed
[FAB-9587] keepalive and userRunsCC on handler
- Explicitly propagate configuration from chaincode support to handler. - Create internal interface to handler dependencies on chaincode support. This should be temporary. Change-Id: I27c231a9d7e29fed4e128745ba3ede1e8a59c98b Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 8c15a34 commit 1c4ff73

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

core/chaincode/handler.go

+33-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/golang/protobuf/proto"
1717
"github.com/hyperledger/fabric/common/flogging"
1818
commonledger "github.com/hyperledger/fabric/common/ledger"
19+
"github.com/hyperledger/fabric/common/resourcesconfig"
1920
"github.com/hyperledger/fabric/common/util"
2021
"github.com/hyperledger/fabric/core/aclmgmt"
2122
"github.com/hyperledger/fabric/core/aclmgmt/resources"
@@ -61,6 +62,16 @@ type pendingQueryResult struct {
6162

6263
type stateHandlers map[pb.ChaincodeMessage_Type]func(*pb.ChaincodeMessage)
6364

65+
// internal interface to scope dependencies on ChaincodeSupport
66+
type handlerSupport interface {
67+
deregisterHandler(*Handler) error
68+
registerHandler(*Handler) error
69+
70+
GetChaincodeDefinition(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (resourcesconfig.ChaincodeDefinition, error)
71+
Launch(context context.Context, cccid *ccprovider.CCContext, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error)
72+
Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error)
73+
}
74+
6475
// Handler responsible for management of Peer's side of chaincode stream
6576
type Handler struct {
6677
sync.Mutex
@@ -71,9 +82,9 @@ type Handler struct {
7182
ChaincodeID *pb.ChaincodeID
7283
ccInstance *sysccprovider.ChaincodeInstance
7384

74-
chaincodeSupport *ChaincodeSupport
75-
registered bool
76-
readyNotify chan bool
85+
handlerSupport handlerSupport
86+
registered bool
87+
readyNotify chan bool
7788

7889
//chan to pass error in sync and nonsync mode
7990
errChan chan error
@@ -86,6 +97,9 @@ type Handler struct {
8697
//handlers for each state of the handler
8798
readyStateHandlers stateHandlers
8899
createStateHandlers stateHandlers
100+
101+
keepalive time.Duration
102+
userRunsCC bool
89103
}
90104

91105
func shorttxid(txid string) string {
@@ -261,14 +275,14 @@ func (handler *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Pro
261275

262276
func (handler *Handler) deregister() error {
263277
if handler.registered {
264-
handler.chaincodeSupport.deregisterHandler(handler)
278+
handler.handlerSupport.deregisterHandler(handler)
265279
}
266280
return nil
267281
}
268282

269283
func (handler *Handler) waitForKeepaliveTimer() <-chan time.Time {
270-
if handler.chaincodeSupport.keepalive > 0 {
271-
c := time.After(handler.chaincodeSupport.keepalive)
284+
if handler.keepalive > 0 {
285+
c := time.After(handler.keepalive)
272286
return c
273287
}
274288
//no one will signal this channel, listener blocks forever
@@ -339,8 +353,8 @@ func (handler *Handler) processStream() error {
339353
chaincodeLogger.Errorf("%s", err)
340354
return err
341355
case <-handler.waitForKeepaliveTimer():
342-
if handler.chaincodeSupport.keepalive <= 0 {
343-
chaincodeLogger.Errorf("Invalid select: keepalive not on (keepalive=%d)", handler.chaincodeSupport.keepalive)
356+
if handler.keepalive <= 0 {
357+
chaincodeLogger.Errorf("Invalid select: keepalive not on (keepalive=%d)", handler.keepalive)
344358
continue
345359
}
346360

@@ -369,10 +383,12 @@ func HandleChaincodeStream(chaincodeSupport *ChaincodeSupport, ctxt context.Cont
369383

370384
func newChaincodeSupportHandler(chaincodeSupport *ChaincodeSupport, peerChatStream ccintf.ChaincodeStream) *Handler {
371385
v := &Handler{
372-
ChatStream: peerChatStream,
373-
chaincodeSupport: chaincodeSupport,
374-
state: created,
375-
errChan: make(chan error, 1),
386+
ChatStream: peerChatStream,
387+
handlerSupport: chaincodeSupport,
388+
state: created,
389+
errChan: make(chan error, 1),
390+
keepalive: chaincodeSupport.keepalive,
391+
userRunsCC: chaincodeSupport.userRunsCC,
376392
}
377393

378394
v.readyStateHandlers = stateHandlers{
@@ -466,7 +482,7 @@ func (handler *Handler) notifyDuringStartup(val bool) {
466482
//environment where we can attach a chaincode manually. This could be
467483
//useful .... but for now lets just be conservative and allow manual
468484
//chaincode only in dev mode (ie, peer started with --peer-chaincodedev=true)
469-
if handler.chaincodeSupport.userRunsCC {
485+
if handler.userRunsCC {
470486
if val {
471487
handler.sendReady()
472488
} else {
@@ -489,7 +505,7 @@ func (handler *Handler) handleRegister(msg *pb.ChaincodeMessage) {
489505

490506
// Now register with the chaincodeSupport
491507
handler.ChaincodeID = chaincodeID
492-
err = handler.chaincodeSupport.registerHandler(handler)
508+
err = handler.handlerSupport.registerHandler(handler)
493509
if err != nil {
494510
handler.notifyDuringStartup(false)
495511
return
@@ -1191,7 +1207,7 @@ func (handler *Handler) handleModState(msg *pb.ChaincodeMessage) {
11911207
var version string
11921208
if !isscc {
11931209
//if its a user chaincode, get the details
1194-
cd, err := handler.chaincodeSupport.GetChaincodeDefinition(ctxt, msg.Txid, txContext.signedProp, txContext.proposal, calledCcIns.ChainID, calledCcIns.ChaincodeName)
1210+
cd, err := handler.handlerSupport.GetChaincodeDefinition(ctxt, msg.Txid, txContext.signedProp, txContext.proposal, calledCcIns.ChainID, calledCcIns.ChaincodeName)
11951211
if err != nil {
11961212
errHandler([]byte(err.Error()), "[%s]Failed to get chaincode data (%s) for invoked chaincode. Sending %s", shorttxid(msg.Txid), err, pb.ChaincodeMessage_ERROR)
11971213
return
@@ -1215,7 +1231,7 @@ func (handler *Handler) handleModState(msg *pb.ChaincodeMessage) {
12151231
chaincodeLogger.Debugf("[%s] launching chaincode %s on channel %s",
12161232
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID)
12171233
cciSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: chaincodeSpec}
1218-
_, chaincodeInput, launchErr := handler.chaincodeSupport.Launch(ctxt, cccid, cciSpec)
1234+
_, chaincodeInput, launchErr := handler.handlerSupport.Launch(ctxt, cccid, cciSpec)
12191235
if launchErr != nil {
12201236
payload := []byte(launchErr.Error())
12211237
chaincodeLogger.Debugf("[%s]Failed to launch invoked chaincode. Sending %s",
@@ -1230,7 +1246,7 @@ func (handler *Handler) handleModState(msg *pb.ChaincodeMessage) {
12301246
ccMsg, _ := createCCMessage(pb.ChaincodeMessage_TRANSACTION, calledCcIns.ChainID, msg.Txid, chaincodeInput)
12311247

12321248
// Execute the chaincode... this CANNOT be an init at least for now
1233-
response, execErr := handler.chaincodeSupport.Execute(ctxt, cccid, ccMsg, timeout)
1249+
response, execErr := handler.handlerSupport.Execute(ctxt, cccid, ccMsg, timeout)
12341250

12351251
//payload is marshalled and send to the calling chaincode's shim which unmarshals and
12361252
//sends it to chaincode

0 commit comments

Comments
 (0)