Skip to content

Commit 5074d35

Browse files
author
Jason Yellick
committed
FAB-16158 Store chaincodeID as string in handler
The handler presently never references any field of its chaincodeID member other than .Name, which is in fact, the chaincode id. So, it makes no sense to be storing this as a confusing nested structure, removing. Change-Id: I9303a4dd4346459e5055852d2593e9e189e7e08e Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 0e07c1d commit 5074d35

File tree

6 files changed

+32
-43
lines changed

6 files changed

+32
-43
lines changed

core/chaincode/chaincode_support_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ func TestCCFramework(t *testing.T) {
11281128
initializeCC(t, chainID, ccname, ccSide, chaincodeSupport)
11291129

11301130
//chaincode support should not allow dups
1131-
handler := &Handler{chaincodeID: &pb.ChaincodeID{Name: ccname + ":0"}, BuiltinSCCs: chaincodeSupport.BuiltinSCCs}
1131+
handler := &Handler{chaincodeID: ccname + ":0", BuiltinSCCs: chaincodeSupport.BuiltinSCCs}
11321132
if err := chaincodeSupport.HandlerRegistry.Register(handler); err == nil {
11331133
t.Fatalf("expected re-register to fail")
11341134
}

core/chaincode/handler.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type Handler struct {
127127
// ready.
128128
state State
129129
// chaincodeID holds the ID of the chaincode that registered with the peer.
130-
chaincodeID *pb.ChaincodeID
130+
chaincodeID string
131131

132132
// serialLock is used to serialize sends across the grpc chat stream.
133133
serialLock sync.Mutex
@@ -229,11 +229,10 @@ func (h *Handler) HandleTransaction(msg *pb.ChaincodeMessage, delegate handleFun
229229
txContext, err = h.isValidTxSim(msg.ChannelId, msg.Txid, "no ledger context")
230230
}
231231

232-
chaincodeName := h.chaincodeID.Name + ":" + h.chaincodeID.Version
233232
meterLabels := []string{
234233
"type", msg.Type.String(),
235234
"channel", msg.ChannelId,
236-
"chaincode", chaincodeName,
235+
"chaincode", h.chaincodeID,
237236
}
238237
h.Metrics.ShimRequestsReceived.With(meterLabels...).Add(1)
239238

@@ -341,11 +340,8 @@ func (h *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal,
341340
}
342341

343342
func (h *Handler) deregister() {
344-
if h.chaincodeID != nil {
345-
// FIXME: this works once again because h.chaincodeID.Name
346-
// is not the chaincode name but the concatenation of name
347-
// and version (FAB-14630)
348-
h.Registry.Deregister(h.chaincodeID.Name)
343+
if h.chaincodeID != "" {
344+
h.Registry.Deregister(h.chaincodeID)
349345
}
350346
}
351347

@@ -417,18 +413,18 @@ func (h *Handler) ProcessStream(stream ccintf.ChaincodeStream) error {
417413

418414
// sendReady sends READY to chaincode serially (just like REGISTER)
419415
func (h *Handler) sendReady() error {
420-
chaincodeLogger.Debugf("sending READY for chaincode %+v", h.chaincodeID)
416+
chaincodeLogger.Debugf("sending READY for chaincode %s", h.chaincodeID)
421417
ccMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_READY}
422418

423419
// if error in sending tear down the h
424420
if err := h.serialSend(ccMsg); err != nil {
425-
chaincodeLogger.Errorf("error sending READY (%s) for chaincode %+v", err, h.chaincodeID)
421+
chaincodeLogger.Errorf("error sending READY (%s) for chaincode %s", err, h.chaincodeID)
426422
return err
427423
}
428424

429425
h.state = Ready
430426

431-
chaincodeLogger.Debugf("Changed to state ready for chaincode %+v", h.chaincodeID)
427+
chaincodeLogger.Debugf("Changed to state ready for chaincode %s", h.chaincodeID)
432428

433429
return nil
434430
}
@@ -441,12 +437,12 @@ func (h *Handler) notifyRegistry(err error) {
441437
}
442438

443439
if err != nil {
444-
h.Registry.Failed(h.chaincodeID.Name, err)
440+
h.Registry.Failed(h.chaincodeID, err)
445441
chaincodeLogger.Errorf("failed to start %s -- %s", h.chaincodeID, err)
446442
return
447443
}
448444

449-
h.Registry.Ready(h.chaincodeID.Name)
445+
h.Registry.Ready(h.chaincodeID)
450446
}
451447

452448
// handleRegister is invoked when chaincode tries to register.
@@ -460,14 +456,17 @@ func (h *Handler) HandleRegister(msg *pb.ChaincodeMessage) {
460456
}
461457

462458
// Now register with the chaincodeSupport
463-
h.chaincodeID = chaincodeID
459+
// Note: chaincodeID.Name is actually of the form name:version for older chaincodes, and
460+
// of the form label:hash for newer chaincodes. Either way, it is the handle by which
461+
// we track the chaincode's registration.
462+
h.chaincodeID = chaincodeID.Name
464463
err = h.Registry.Register(h)
465464
if err != nil {
466465
h.notifyRegistry(err)
467466
return
468467
}
469468

470-
chaincodeLogger.Debugf("Got %s for chaincodeID = %s, sending back %s", pb.ChaincodeMessage_REGISTER, chaincodeID, pb.ChaincodeMessage_REGISTERED)
469+
chaincodeLogger.Debugf("Got %s for chaincodeID = %s, sending back %s", pb.ChaincodeMessage_REGISTER, h.chaincodeID, pb.ChaincodeMessage_REGISTERED)
471470
if err := h.serialSend(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTERED}); err != nil {
472471
chaincodeLogger.Errorf("error sending %s: %s", pb.ChaincodeMessage_REGISTERED, err)
473472
h.notifyRegistry(err)
@@ -476,7 +475,7 @@ func (h *Handler) HandleRegister(msg *pb.ChaincodeMessage) {
476475

477476
h.state = Established
478477

479-
chaincodeLogger.Debugf("Changed state to established for %+v", h.chaincodeID)
478+
chaincodeLogger.Debugf("Changed state to established for %s", h.chaincodeID)
480479

481480
// for dev mode this will also move to ready automatically
482481
h.notifyRegistry(nil)
@@ -513,11 +512,11 @@ func (h *Handler) registerTxid(msg *pb.ChaincodeMessage) bool {
513512
}
514513

515514
// Log the issue and drop the request
516-
chaincodeName := "unknown"
517-
if h.chaincodeID != nil {
518-
chaincodeName = h.chaincodeID.Name
515+
chaincodeID := "unknown"
516+
if h.chaincodeID != "" {
517+
chaincodeID = h.chaincodeID
519518
}
520-
chaincodeLogger.Errorf("[%s] Another request pending for this CC: %s, Txid: %s, ChannelID: %s. Cannot process.", shorttxid(msg.Txid), chaincodeName, msg.Txid, msg.ChannelId)
519+
chaincodeLogger.Errorf("[%s] Another request pending for this CC: %s, Txid: %s, ChannelID: %s. Cannot process.", shorttxid(msg.Txid), chaincodeID, msg.Txid, msg.ChannelId)
521520
return false
522521
}
523522

core/chaincode/handler_internal_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ package chaincode
88

99
import (
1010
"github.com/hyperledger/fabric/core/container/ccintf"
11-
pb "github.com/hyperledger/fabric/protos/peer"
1211
)
1312

1413
// Helpers to access unexported state.
1514

16-
func SetHandlerChaincodeID(h *Handler, chaincodeID *pb.ChaincodeID) {
15+
func SetHandlerChaincodeID(h *Handler, chaincodeID string) {
1716
h.chaincodeID = chaincodeID
1817
}
1918

core/chaincode/handler_registry.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,20 @@ func (r *HandlerRegistry) Register(h *Handler) error {
130130
r.mutex.Lock()
131131
defer r.mutex.Unlock()
132132

133-
// FIXME: the ccid must be its own field in the handler
134-
// This hack works because currently h.chaincodeID.Name is
135-
// actually set as the concatenation of chaincode name and
136-
// chaincode ID, which is set at build time. While it's ok
137-
// for the chaincode to communicate back its packageID, the
138-
// usage of the chaincodeID field is misleading (FAB-14630)
139-
ccid := h.chaincodeID.Name
140-
141-
if r.handlers[ccid] != nil {
142-
chaincodeLogger.Debugf("duplicate registered handler(key:%s) return error", ccid)
143-
return errors.Errorf("duplicate chaincodeID: %s", h.chaincodeID.Name)
133+
if r.handlers[h.chaincodeID] != nil {
134+
chaincodeLogger.Debugf("duplicate registered handler(key:%s) return error", h.chaincodeID)
135+
return errors.Errorf("duplicate chaincodeID: %s", h.chaincodeID)
144136
}
145137

146138
// This chaincode was not launched by the peer but is attempting
147139
// to register. Only allowed in development mode.
148-
if r.launching[ccid] == nil && !r.allowUnsolicitedRegistration {
149-
return errors.Errorf("peer will not accept external chaincode connection %v (except in dev mode)", h.chaincodeID.Name)
140+
if r.launching[h.chaincodeID] == nil && !r.allowUnsolicitedRegistration {
141+
return errors.Errorf("peer will not accept external chaincode connection %s (except in dev mode)", h.chaincodeID)
150142
}
151143

152-
r.handlers[ccid] = h
144+
r.handlers[h.chaincodeID] = h
153145

154-
chaincodeLogger.Debugf("registered handler complete for chaincode %s", ccid)
146+
chaincodeLogger.Debugf("registered handler complete for chaincode %s", h.chaincodeID)
155147
return nil
156148
}
157149

core/chaincode/handler_registry_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hyperledger/fabric/core/chaincode"
1111
"github.com/hyperledger/fabric/core/chaincode/mock"
1212
"github.com/hyperledger/fabric/core/common/ccprovider"
13-
pb "github.com/hyperledger/fabric/protos/peer"
1413
. "github.com/onsi/ginkgo"
1514
. "github.com/onsi/gomega"
1615
"github.com/pkg/errors"
@@ -23,7 +22,7 @@ var _ = Describe("HandlerRegistry", func() {
2322
BeforeEach(func() {
2423
hr = chaincode.NewHandlerRegistry(true)
2524
handler = &chaincode.Handler{}
26-
chaincode.SetHandlerChaincodeID(handler, &pb.ChaincodeID{Name: "chaincode-name"})
25+
chaincode.SetHandlerChaincodeID(handler, "chaincode-name")
2726
})
2827

2928
Describe("Launching", func() {
@@ -310,7 +309,7 @@ var _ = Describe("TxSimulatorGetter", func() {
310309

311310
When("No TxContext is created", func() {
312311
BeforeEach(func() {
313-
chaincode.SetHandlerChaincodeID(handler, &pb.ChaincodeID{Name: "package-ID"})
312+
chaincode.SetHandlerChaincodeID(handler, "package-ID")
314313
hr.Register(handler)
315314
})
316315

@@ -322,7 +321,7 @@ var _ = Describe("TxSimulatorGetter", func() {
322321

323322
When("Handler is created for package-ID and TxContext is created for channel-ID/tx-ID", func() {
324323
BeforeEach(func() {
325-
chaincode.SetHandlerChaincodeID(handler, &pb.ChaincodeID{Name: "package-ID"})
324+
chaincode.SetHandlerChaincodeID(handler, "package-ID")
326325
hr.Register(handler)
327326
handler.TXContexts.Create(&ccprovider.TransactionParams{
328327
ChannelID: "channel-ID",

core/chaincode/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var _ = Describe("Handler", func() {
126126
Metrics: chaincodeMetrics,
127127
}
128128
chaincode.SetHandlerChatStream(handler, fakeChatStream)
129-
chaincode.SetHandlerChaincodeID(handler, &pb.ChaincodeID{Name: "test-handler-name", Version: "1.0"})
129+
chaincode.SetHandlerChaincodeID(handler, "test-handler-name:1.0")
130130
})
131131

132132
Describe("HandleTransaction", func() {

0 commit comments

Comments
 (0)