Skip to content

Commit 8ddc47e

Browse files
committed
[FAB-9752] Wire registry into handler
This sets up for additional refactoring and unit tests for the Handler. Change-Id: Ib0db4a16463185b5ad1aa5608ca1156dfc5ced62 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 43ece2d commit 8ddc47e

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

core/chaincode/chaincode_support.go

+6-18
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewChaincodeSupport(
6262
ccStartupTimeout: ccstartuptimeout,
6363
keepalive: config.Keepalive,
6464
executetimeout: config.ExecuteTimeout,
65-
handlerRegistry: NewHandlerRegistry(userrunsCC),
65+
HandlerRegistry: NewHandlerRegistry(userrunsCC),
6666
PackageProvider: packageProvider,
6767
ACLProvider: aclProvider,
6868
}
@@ -92,7 +92,6 @@ func NewChaincodeSupport(
9292
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
9393
type ChaincodeSupport struct {
9494
caCert []byte
95-
handlerRegistry *HandlerRegistry
9695
peerAddress string
9796
ccStartupTimeout time.Duration
9897
peerNetworkID string
@@ -103,6 +102,7 @@ type ChaincodeSupport struct {
103102
ContainerRuntime Runtime
104103
PackageProvider PackageProvider
105104
ACLProvider ACLProvider
105+
HandlerRegistry *HandlerRegistry
106106
sccp sysccprovider.SystemChaincodeProvider
107107
}
108108

@@ -114,24 +114,12 @@ func (cs *ChaincodeSupport) SetSysCCProvider(sccp sysccprovider.SystemChaincodeP
114114
cs.sccp = sccp
115115
}
116116

117-
func (cs *ChaincodeSupport) registerHandler(chaincodehandler *Handler) error {
118-
return cs.handlerRegistry.Register(chaincodehandler)
119-
}
120-
121-
func (cs *ChaincodeSupport) deregisterHandler(chaincodehandler *Handler) error {
122-
return cs.handlerRegistry.Deregister(chaincodehandler.ChaincodeID.GetName())
123-
}
124-
125-
func (cs *ChaincodeSupport) ready(chaincodehandler *Handler) {
126-
cs.handlerRegistry.Ready(chaincodehandler.ChaincodeID.GetName())
127-
}
128-
129117
// launchAndWaitForReady launches a container for the specified chaincode
130118
// context if one is not already running. It then waits for the chaincode
131119
// registration to complete or for the process to time out.
132120
func (cs *ChaincodeSupport) launchAndWaitForReady(ctx context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
133121
cname := cccid.GetCanonicalName()
134-
ready, err := cs.handlerRegistry.Launching(cname)
122+
ready, err := cs.HandlerRegistry.Launching(cname)
135123
if err != nil {
136124
return err
137125
}
@@ -175,7 +163,7 @@ func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContex
175163
return errors.New("chaincode name not set")
176164
}
177165

178-
defer cs.handlerRegistry.Deregister(cname)
166+
defer cs.HandlerRegistry.Deregister(cname)
179167

180168
err := cs.ContainerRuntime.Stop(ctx, cccid, cds)
181169
if err != nil {
@@ -191,7 +179,7 @@ func (cs *ChaincodeSupport) Launch(context context.Context, cccid *ccprovider.CC
191179
cID := spec.GetChaincodeSpec().ChaincodeId
192180
cMsg := spec.GetChaincodeSpec().Input
193181

194-
if cs.handlerRegistry.Handler(cname) != nil {
182+
if cs.HandlerRegistry.Handler(cname) != nil {
195183
return cID, cMsg, nil
196184
}
197185

@@ -290,7 +278,7 @@ func (cs *ChaincodeSupport) Execute(ctxt context.Context, cccid *ccprovider.CCCo
290278

291279
chaincodeLogger.Debugf("chaincode canonical name: %s", canName)
292280
//we expect the chaincode to be running... sanity check
293-
handler := cs.handlerRegistry.Handler(canName)
281+
handler := cs.HandlerRegistry.Handler(canName)
294282
if handler == nil {
295283
chaincodeLogger.Debugf("cannot execute-chaincode is not running: %s", canName)
296284
return nil, errors.Errorf("cannot execute transaction for %s", canName)

core/chaincode/chaincode_support_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ func TestLaunchAndWaitSuccess(t *testing.T) {
10151015
ccStartupTimeout: time.Duration(10) * time.Second,
10161016
peerNetworkID: "networkID",
10171017
peerID: "peerID",
1018-
handlerRegistry: handlerRegistry,
1018+
HandlerRegistry: handlerRegistry,
10191019
ContainerRuntime: fakeRuntime,
10201020
}
10211021
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}}
@@ -1042,7 +1042,7 @@ func TestLaunchAndWaitTimeout(t *testing.T) {
10421042
ccStartupTimeout: 500 * time.Millisecond,
10431043
peerNetworkID: "networkID",
10441044
peerID: "peerID",
1045-
handlerRegistry: NewHandlerRegistry(false),
1045+
HandlerRegistry: NewHandlerRegistry(false),
10461046
ContainerRuntime: fakeRuntime,
10471047
}
10481048
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}}
@@ -1068,7 +1068,7 @@ func TestLaunchAndWaitLaunchError(t *testing.T) {
10681068
ccStartupTimeout: time.Duration(10) * time.Second,
10691069
peerNetworkID: "networkID",
10701070
peerID: "peerID",
1071-
handlerRegistry: NewHandlerRegistry(false),
1071+
HandlerRegistry: NewHandlerRegistry(false),
10721072
ContainerRuntime: fakeRuntime,
10731073
}
10741074
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}}
@@ -1298,7 +1298,7 @@ func TestCCFramework(t *testing.T) {
12981298
initializeCC(t, chainID, ccname, ccSide, chaincodeSupport)
12991299

13001300
//chaincode support should not allow dups
1301-
if err := chaincodeSupport.registerHandler(&Handler{ChaincodeID: &pb.ChaincodeID{Name: ccname + ":0"}, sccp: chaincodeSupport.sccp}); err == nil {
1301+
if err := chaincodeSupport.HandlerRegistry.Register(&Handler{ChaincodeID: &pb.ChaincodeID{Name: ccname + ":0"}, sccp: chaincodeSupport.sccp}); err == nil {
13021302
t.Fatalf("expected re-register to fail")
13031303
}
13041304

core/chaincode/handler.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ type ACLProvider interface {
4747
CheckACL(resName string, channelID string, idinfo interface{}) error
4848
}
4949

50+
type Registry interface {
51+
Register(*Handler) error
52+
Ready(cname string)
53+
Deregister(cname string) error
54+
}
55+
5056
// internal interface to scope dependencies on ChaincodeSupport
5157
type handlerSupport interface {
52-
deregisterHandler(*Handler) error
53-
registerHandler(*Handler) error
54-
ready(*Handler)
55-
5658
GetChaincodeDefinition(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (resourcesconfig.ChaincodeDefinition, error)
5759
Launch(context context.Context, cccid *ccprovider.CCContext, spec ccprovider.ChaincodeSpecGetter) (*pb.ChaincodeID, *pb.ChaincodeInput, error)
5860
Execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error)
@@ -89,6 +91,7 @@ type Handler struct {
8991
keepalive time.Duration
9092
userRunsCC bool
9193

94+
registry Registry
9295
aclProvider ACLProvider
9396
}
9497

@@ -217,7 +220,7 @@ func (h *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal,
217220
}
218221

219222
func (h *Handler) deregister() {
220-
h.handlerSupport.deregisterHandler(h)
223+
h.registry.Deregister(h.ChaincodeID.Name)
221224
}
222225

223226
func (h *Handler) waitForKeepaliveTimer() <-chan time.Time {
@@ -333,6 +336,7 @@ func newChaincodeSupportHandler(chaincodeSupport *ChaincodeSupport, peerChatStre
333336
keepalive: chaincodeSupport.keepalive,
334337
userRunsCC: chaincodeSupport.userRunsCC,
335338
aclProvider: chaincodeSupport.ACLProvider,
339+
registry: chaincodeSupport.HandlerRegistry,
336340
sccp: sccp,
337341
}
338342

@@ -381,7 +385,7 @@ func (h *Handler) sendReady() error {
381385
}
382386

383387
h.state = ready
384-
h.handlerSupport.ready(h)
388+
h.registry.Ready(h.ChaincodeID.Name)
385389

386390
chaincodeLogger.Debugf("Changed to state ready for chaincode %+v", h.ChaincodeID)
387391

@@ -411,7 +415,7 @@ func (h *Handler) handleRegister(msg *pb.ChaincodeMessage) {
411415

412416
// Now register with the chaincodeSupport
413417
h.ChaincodeID = chaincodeID
414-
err = h.handlerSupport.registerHandler(h)
418+
err = h.registry.Register(h)
415419
if err != nil {
416420
h.notifyDuringStartup(false)
417421
return

0 commit comments

Comments
 (0)