Skip to content

Commit 893381c

Browse files
committed
[FAB-8904] Make CCContext implement fmt.Stringer
... and cleanup comments along the way Change-Id: I8bdcffa4a5096cf985e00094f51eef2e26474842 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 516ff4d commit 893381c

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

core/common/ccprovider/ccprovider.go

+63-65
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2017 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package ccprovider
@@ -35,7 +25,7 @@ var ccproviderLogger = flogging.MustGetLogger("ccprovider")
3525

3626
var chaincodeInstallPath string
3727

38-
//CCPackage encapsulates a chaincode package which can be
28+
// CCPackage encapsulates a chaincode package which can be
3929
// raw ChaincodeDeploymentSpec
4030
// SignedChaincodeDeploymentSpec
4131
// Attempt to keep the interface at a level with minimal
@@ -71,7 +61,7 @@ type CCPackage interface {
7161
GetId() []byte
7262
}
7363

74-
//SetChaincodesPath sets the chaincode path for this peer
64+
// SetChaincodesPath sets the chaincode path for this peer
7565
func SetChaincodesPath(path string) {
7666
if s, err := os.Stat(path); err != nil {
7767
if os.IsNotExist(err) {
@@ -88,7 +78,7 @@ func SetChaincodesPath(path string) {
8878
chaincodeInstallPath = path
8979
}
9080

91-
//GetChaincodePackage returns the chaincode package from the file system
81+
// GetChaincodePackage returns the chaincode package from the file system
9282
func GetChaincodePackage(ccname string, ccversion string) ([]byte, error) {
9383
path := fmt.Sprintf("%s/%s.%s", chaincodeInstallPath, ccname, ccversion)
9484
var ccbytes []byte
@@ -99,7 +89,7 @@ func GetChaincodePackage(ccname string, ccversion string) ([]byte, error) {
9989
return ccbytes, nil
10090
}
10191

102-
//ChaincodePackageExists returns whether the chaincode package exists in the file system
92+
// ChaincodePackageExists returns whether the chaincode package exists in the file system
10393
func ChaincodePackageExists(ccname string, ccversion string) (bool, error) {
10494
path := filepath.Join(chaincodeInstallPath, ccname+"."+ccversion)
10595
_, err := os.Stat(path)
@@ -111,7 +101,7 @@ func ChaincodePackageExists(ccname string, ccversion string) (bool, error) {
111101
}
112102

113103
type CCCacheSupport interface {
114-
//GetChaincode is needed by the cache to get chaincode data
104+
// GetChaincode is needed by the cache to get chaincode data
115105
GetChaincode(ccname string, ccversion string) (CCPackage, error)
116106
}
117107

@@ -121,11 +111,11 @@ type CCInfoFSImpl struct{}
121111

122112
// GetChaincodeFromFS this is a wrapper for hiding package implementation.
123113
func (*CCInfoFSImpl) GetChaincode(ccname string, ccversion string) (CCPackage, error) {
124-
//try raw CDS
114+
// try raw CDS
125115
cccdspack := &CDSPackage{}
126116
_, _, err := cccdspack.InitFromFS(ccname, ccversion)
127117
if err != nil {
128-
//try signed CDS
118+
// try signed CDS
129119
ccscdspack := &SignedCDSPackage{}
130120
_, _, err = ccscdspack.InitFromFS(ccname, ccversion)
131121
if err != nil {
@@ -243,10 +233,10 @@ func CheckInstantiationPolicy(name, version string, cdLedger *ChaincodeData) err
243233
// GetCCPackage tries each known package implementation one by one
244234
// till the right package is found
245235
func GetCCPackage(buf []byte) (CCPackage, error) {
246-
//try raw CDS
236+
// try raw CDS
247237
cccdspack := &CDSPackage{}
248238
if _, err := cccdspack.InitFromBuffer(buf); err != nil {
249-
//try signed CDS
239+
// try signed CDS
250240
ccscdspack := &SignedCDSPackage{}
251241
if _, err := ccscdspack.InitFromBuffer(buf); err != nil {
252242
return nil, err
@@ -314,97 +304,105 @@ func GetInstalledChaincodes() (*pb.ChaincodeQueryResponse, error) {
314304
return cqr, nil
315305
}
316306

317-
//CCContext pass this around instead of string of args
307+
// CCContext pass this around instead of string of args
318308
type CCContext struct {
319-
//ChainID chain id
309+
// ChainID chain id
320310
ChainID string
321311

322-
//Name chaincode name
312+
// Name chaincode name
323313
Name string
324314

325-
//Version used to construct the chaincode image and register
315+
// Version used to construct the chaincode image and register
326316
Version string
327317

328-
//TxID is the transaction id for the proposal (if any)
318+
// TxID is the transaction id for the proposal (if any)
329319
TxID string
330320

331-
//Syscc is this a system chaincode
321+
// Syscc is this a system chaincode
332322
Syscc bool
333323

334-
//SignedProposal for this invoke (if any)
335-
//this is kept here for access control and in case we need to pass something
336-
//from this to the chaincode
324+
// SignedProposal for this invoke (if any) this is kept here for access
325+
// control and in case we need to pass something from this to the chaincode
337326
SignedProposal *pb.SignedProposal
338327

339-
//Proposal for this invoke (if any)
340-
//this is kept here just in case we need to pass something
341-
//from this to the chaincode
328+
// Proposal for this invoke (if any) this is kept here just in case we need to
329+
// pass something from this to the chaincode
342330
Proposal *pb.Proposal
343331

344-
//this is not set but computed (note that this is not exported. use GetCanonicalName)
332+
// canonicalName is not set but computed
345333
canonicalName string
346334

347335
// this is additional data passed to the chaincode
348336
ProposalDecorations map[string][]byte
349337
}
350338

351-
//NewCCContext just construct a new struct with whatever args
352-
func NewCCContext(cid, name, version, txid string, syscc bool, signedProp *pb.SignedProposal, prop *pb.Proposal) *CCContext {
353-
//version CANNOT be empty. The chaincode namespace has to use version and chain name.
354-
//All system chaincodes share the same version given by utils.GetSysCCVersion. Note
355-
//that neither Chain Name or Version are stored in a chaincodes state on the ledger
356-
if version == "" {
357-
panic(fmt.Sprintf("---empty version---(chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p", cid, name, version, txid, syscc, prop))
339+
// NewCCContext just construct a new struct with whatever args
340+
func NewCCContext(cname, name, version, txid string, syscc bool, signedProp *pb.SignedProposal, prop *pb.Proposal) *CCContext {
341+
cccid := &CCContext{
342+
ChainID: cname,
343+
Name: name,
344+
Version: version,
345+
TxID: txid,
346+
Syscc: syscc,
347+
SignedProposal: signedProp,
348+
Proposal: prop,
349+
canonicalName: name + ":" + version,
350+
ProposalDecorations: nil,
358351
}
359352

360-
canName := name + ":" + version
361-
362-
cccid := &CCContext{cid, name, version, txid, syscc, signedProp, prop, canName, nil}
363-
364-
ccproviderLogger.Debugf("NewCCCC (chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p,canname=%s", cid, name, version, txid, syscc, prop, cccid.canonicalName)
353+
// The version CANNOT be empty. The chaincode namespace has to use version and chain name.
354+
// Note that neither channel name nor version are stored on the ledger.
355+
if version == "" {
356+
panic(fmt.Sprintf("---empty version---(%s)", cccid))
357+
}
365358

359+
ccproviderLogger.Debugf("NewCCCC(%s)", cccid)
366360
return cccid
367361
}
368362

369-
//GetCanonicalName returns the canonical name associated with the proposal context
363+
func (cccid *CCContext) String() string {
364+
return fmt.Sprintf("chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t,proposal=%p,canname=%s",
365+
cccid.ChainID, cccid.Name, cccid.Version, cccid.TxID, cccid.Syscc, cccid.Proposal, cccid.canonicalName)
366+
}
367+
368+
// GetCanonicalName returns the canonical name associated with the proposal context
370369
func (cccid *CCContext) GetCanonicalName() string {
371370
if cccid.canonicalName == "" {
372-
panic(fmt.Sprintf("cccid not constructed using NewCCContext(chain=%s,chaincode=%s,version=%s,txid=%s,syscc=%t)", cccid.ChainID, cccid.Name, cccid.Version, cccid.TxID, cccid.Syscc))
371+
panic(fmt.Sprintf("missing canonical name: %s", cccid))
373372
}
374373

375374
return cccid.canonicalName
376375
}
377376

378377
//-------- ChaincodeData is stored on the LSCC -------
379378

380-
//ChaincodeData defines the datastructure for chaincodes to be serialized by proto
381-
//Type provides an additional check by directing to use a specific package after instantiation
382-
//Data is Type specifc (see CDSPackage and SignedCDSPackage)
379+
// ChaincodeData defines the datastructure for chaincodes to be serialized by proto
380+
// Type provides an additional check by directing to use a specific package after instantiation
381+
// Data is Type specifc (see CDSPackage and SignedCDSPackage)
383382
type ChaincodeData struct {
384-
//Name of the chaincode
383+
// Name of the chaincode
385384
Name string `protobuf:"bytes,1,opt,name=name"`
386385

387-
//Version of the chaincode
386+
// Version of the chaincode
388387
Version string `protobuf:"bytes,2,opt,name=version"`
389388

390-
//Escc for the chaincode instance
389+
// Escc for the chaincode instance
391390
Escc string `protobuf:"bytes,3,opt,name=escc"`
392391

393-
//Vscc for the chaincode instance
392+
// Vscc for the chaincode instance
394393
Vscc string `protobuf:"bytes,4,opt,name=vscc"`
395394

396-
//Policy endorsement policy for the chaincode instance
395+
// Policy endorsement policy for the chaincode instance
397396
Policy []byte `protobuf:"bytes,5,opt,name=policy,proto3"`
398397

399-
//Data data specific to the package
398+
// Data data specific to the package
400399
Data []byte `protobuf:"bytes,6,opt,name=data,proto3"`
401400

402-
//Id of the chaincode that's the unique fingerprint for the CC
403-
//This is not currently used anywhere but serves as a good
404-
//eyecatcher
401+
// Id of the chaincode that's the unique fingerprint for the CC This is not
402+
// currently used anywhere but serves as a good eyecatcher
405403
Id []byte `protobuf:"bytes,7,opt,name=id,proto3"`
406404

407-
//InstantiationPolicy for the chaincode
405+
// InstantiationPolicy for the chaincode
408406
InstantiationPolicy []byte `protobuf:"bytes,8,opt,name=instantiation_policy,proto3"`
409407
}
410408

@@ -439,15 +437,15 @@ func (cd *ChaincodeData) Endorsement() string {
439437
return cd.Escc
440438
}
441439

442-
//implement functions needed from proto.Message for proto's mar/unmarshal functions
440+
// implement functions needed from proto.Message for proto's mar/unmarshal functions
443441

444-
//Reset resets
442+
// Reset resets
445443
func (cd *ChaincodeData) Reset() { *cd = ChaincodeData{} }
446444

447-
//String converts to string
445+
// String converts to string
448446
func (cd *ChaincodeData) String() string { return proto.CompactTextString(cd) }
449447

450-
//ProtoMessage just exists to make proto happy
448+
// ProtoMessage just exists to make proto happy
451449
func (*ChaincodeData) ProtoMessage() {}
452450

453451
// ChaincodeProvider provides an abstraction layer that is

0 commit comments

Comments
 (0)