Skip to content

Commit df69c5f

Browse files
committed
[FAB-3480] Replace ccParts with ChaincodeInstance
These tow structs have same content, can be merged to one struct. Replace ccParts in core/chaincode with sysccprovider.ChaincodeInstance. Move ccParts.String() to ChaincodeInstance.String(). Remove one line leftover code. Change-Id: I1be3a10bedfab712a481134cb27270473009cedf Signed-off-by: jiangyaoguo <[email protected]>
1 parent f30fc74 commit df69c5f

File tree

3 files changed

+36
-45
lines changed

3 files changed

+36
-45
lines changed

core/chaincode/handler.go

+32-44
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,6 @@ type nextStateInfo struct {
8080
sendSync bool
8181
}
8282

83-
//chaincode registered name is of the form
84-
// <name>:<version>/<suffix>
85-
type ccParts struct {
86-
name string //the main name of the chaincode
87-
version string //the version param if any (used for upgrade)
88-
suffix string //for now just the chain name
89-
}
90-
91-
func (p *ccParts) String() string {
92-
return p.suffix + "." + p.name + "#" + p.version
93-
}
94-
9583
// Handler responsbile for management of Peer's side of chaincode stream
9684
type Handler struct {
9785
sync.RWMutex
@@ -100,7 +88,7 @@ type Handler struct {
10088
ChatStream ccintf.ChaincodeStream
10189
FSM *fsm.FSM
10290
ChaincodeID *pb.ChaincodeID
103-
ccCompParts *ccParts
91+
ccInstance *sysccprovider.ChaincodeInstance
10492

10593
chaincodeSupport *ChaincodeSupport
10694
registered bool
@@ -124,26 +112,26 @@ func shorttxid(txid string) string {
124112
return txid[0:8]
125113
}
126114

127-
//gets component parts from the canonical name of the chaincode.
115+
//gets chaincode instance from the canonical name of the chaincode.
128116
//Called exactly once per chaincode when registering chaincode.
129117
//This is needed for the "one-instance-per-chain" model when
130118
//starting up the chaincode for each chain. It will still
131119
//work for the "one-instance-for-all-chains" as the version
132120
//and suffix will just be absent (also note that LSCC reserves
133121
//"/:[]${}" as special chars mainly for such namespace uses)
134122
func (handler *Handler) decomposeRegisteredName(cid *pb.ChaincodeID) {
135-
handler.ccCompParts = chaincodeIDParts(cid.Name)
123+
handler.ccInstance = getChaincodeInstance(cid.Name)
136124
}
137125

138-
func chaincodeIDParts(ccName string) *ccParts {
126+
func getChaincodeInstance(ccName string) *sysccprovider.ChaincodeInstance {
139127
b := []byte(ccName)
140-
p := &ccParts{}
128+
ci := &sysccprovider.ChaincodeInstance{}
141129

142130
//compute suffix (ie, chain name)
143131
i := bytes.IndexByte(b, '/')
144132
if i >= 0 {
145133
if i < len(b)-1 {
146-
p.suffix = string(b[i+1:])
134+
ci.ChainID = string(b[i+1:])
147135
}
148136
b = b[:i]
149137
}
@@ -152,18 +140,18 @@ func chaincodeIDParts(ccName string) *ccParts {
152140
i = bytes.IndexByte(b, ':')
153141
if i >= 0 {
154142
if i < len(b)-1 {
155-
p.version = string(b[i+1:])
143+
ci.ChaincodeVersion = string(b[i+1:])
156144
}
157145
b = b[:i]
158146
}
159147
// remaining is the chaincode name
160-
p.name = string(b)
148+
ci.ChaincodeName = string(b)
161149

162-
return p
150+
return ci
163151
}
164152

165153
func (handler *Handler) getCCRootName() string {
166-
return handler.ccCompParts.name
154+
return handler.ccInstance.ChaincodeName
167155
}
168156

169157
//serialSend serializes msgs so gRPC will be happy
@@ -246,11 +234,11 @@ func (handler *Handler) deleteQueryIterator(txContext *transactionContext, txid
246234
}
247235

248236
// Check if the transactor is allow to call this chaincode on this channel
249-
func (handler *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal, calledCC *ccParts) error {
237+
func (handler *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal, ccIns *sysccprovider.ChaincodeInstance) error {
250238
// ensure that we don't invoke a system chaincode
251239
// that is not invokable through a cc2cc invocation
252-
if sysccprovider.GetSystemChaincodeProvider().IsSysCCAndNotInvokableCC2CC(calledCC.name) {
253-
return fmt.Errorf("System chaincode %s cannot be invoked with a cc2cc invocation", calledCC.name)
240+
if sysccprovider.GetSystemChaincodeProvider().IsSysCCAndNotInvokableCC2CC(ccIns.ChaincodeName) {
241+
return fmt.Errorf("System chaincode %s cannot be invoked with a cc2cc invocation", ccIns.ChaincodeName)
254242
}
255243

256244
// if we are here, all we know is that the invoked chaincode is either
@@ -260,17 +248,17 @@ func (handler *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Pro
260248
// - an application chaincode (and we still need to determine
261249
// whether the invoker can invoke it)
262250

263-
if sysccprovider.GetSystemChaincodeProvider().IsSysCC(calledCC.name) {
251+
if sysccprovider.GetSystemChaincodeProvider().IsSysCC(ccIns.ChaincodeName) {
264252
// Allow this call
265253
return nil
266254
}
267255

268256
// A Nil signedProp will be rejected for non-system chaincodes
269257
if signedProp == nil {
270-
return fmt.Errorf("Signed Proposal must not be nil from caller [%s]", calledCC.String())
258+
return fmt.Errorf("Signed Proposal must not be nil from caller [%s]", ccIns.String())
271259
}
272260

273-
return handler.policyChecker.CheckPolicy(calledCC.suffix, policies.ChannelApplicationWriters, signedProp)
261+
return handler.policyChecker.CheckPolicy(ccIns.ChainID, policies.ChannelApplicationWriters, signedProp)
274262
}
275263

276264
//THIS CAN BE REMOVED ONCE WE FULL SUPPORT (Invoke) CONFIDENTIALITY WITH CC-CALLING-CC
@@ -1267,21 +1255,21 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
12671255
// Get the chaincodeID to invoke. The chaincodeID to be called may
12681256
// contain composite info like "chaincode-name:version/channel-name"
12691257
// We are not using version now but default to the latest
1270-
calledCcParts := chaincodeIDParts(chaincodeSpec.ChaincodeId.Name)
1271-
chaincodeSpec.ChaincodeId.Name = calledCcParts.name
1272-
if calledCcParts.suffix == "" {
1258+
calledCcIns := getChaincodeInstance(chaincodeSpec.ChaincodeId.Name)
1259+
chaincodeSpec.ChaincodeId.Name = calledCcIns.ChaincodeName
1260+
if calledCcIns.ChainID == "" {
12731261
// use caller's channel as the called chaincode is in the same channel
1274-
calledCcParts.suffix = txContext.chainID
1262+
calledCcIns.ChainID = txContext.chainID
12751263
}
12761264
if chaincodeLogger.IsEnabledFor(logging.DEBUG) {
12771265
chaincodeLogger.Debugf("[%s] C-call-C %s on channel %s",
1278-
shorttxid(msg.Txid), calledCcParts.name, calledCcParts.suffix)
1266+
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID)
12791267
}
12801268

1281-
err := handler.checkACL(txContext.signedProp, txContext.proposal, calledCcParts)
1269+
err := handler.checkACL(txContext.signedProp, txContext.proposal, calledCcIns)
12821270
if err != nil {
12831271
chaincodeLogger.Errorf("[%s] C-call-C %s on channel %s failed check ACL [%v]: [%s]",
1284-
shorttxid(msg.Txid), calledCcParts.name, calledCcParts.suffix, txContext.signedProp, err)
1272+
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID, txContext.signedProp, err)
12851273
triggerNextStateMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR,
12861274
Payload: []byte(err.Error()), Txid: msg.Txid}
12871275
return
@@ -1292,10 +1280,10 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
12921280
ctxt := context.Background()
12931281
txsim := txContext.txsimulator
12941282
historyQueryExecutor := txContext.historyQueryExecutor
1295-
if calledCcParts.suffix != txContext.chainID {
1296-
lgr := peer.GetLedger(calledCcParts.suffix)
1283+
if calledCcIns.ChainID != txContext.chainID {
1284+
lgr := peer.GetLedger(calledCcIns.ChainID)
12971285
if lgr == nil {
1298-
payload := "Failed to find ledger for called channel " + calledCcParts.suffix
1286+
payload := "Failed to find ledger for called channel " + calledCcIns.ChainID
12991287
triggerNextStateMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR,
13001288
Payload: []byte(payload), Txid: msg.Txid}
13011289
return
@@ -1314,19 +1302,19 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
13141302

13151303
if chaincodeLogger.IsEnabledFor(logging.DEBUG) {
13161304
chaincodeLogger.Debugf("[%s] calling lscc to get chaincode data for %s on channel %s",
1317-
shorttxid(msg.Txid), calledCcParts.name, calledCcParts.suffix)
1305+
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID)
13181306
}
13191307

13201308
//Call LSCC to get the called chaincode artifacts
13211309

13221310
//is the chaincode a system chaincode ?
1323-
isscc := sysccprovider.GetSystemChaincodeProvider().IsSysCC(calledCcParts.name)
1311+
isscc := sysccprovider.GetSystemChaincodeProvider().IsSysCC(calledCcIns.ChaincodeName)
13241312

13251313
var cd *ccprovider.ChaincodeData
13261314
if !isscc {
13271315
//if its a user chaincode, get the details from LSCC
13281316
//Call LSCC to get the called chaincode artifacts
1329-
cd, err = GetChaincodeDataFromLSCC(ctxt, msg.Txid, txContext.signedProp, txContext.proposal, calledCcParts.suffix, calledCcParts.name)
1317+
cd, err = GetChaincodeDataFromLSCC(ctxt, msg.Txid, txContext.signedProp, txContext.proposal, calledCcIns.ChainID, calledCcIns.ChaincodeName)
13301318
if err != nil {
13311319
payload := []byte(err.Error())
13321320
chaincodeLogger.Debugf("[%s]Failed to get chaincoed data (%s) for invoked chaincode. Sending %s",
@@ -1336,15 +1324,15 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
13361324
}
13371325
} else {
13381326
//this is a system cc, just call it directly
1339-
cd = &ccprovider.ChaincodeData{Name: calledCcParts.name, Version: util.GetSysCCVersion()}
1327+
cd = &ccprovider.ChaincodeData{Name: calledCcIns.ChaincodeName, Version: util.GetSysCCVersion()}
13401328
}
13411329

1342-
cccid := ccprovider.NewCCContext(calledCcParts.suffix, calledCcParts.name, cd.Version, msg.Txid, false, txContext.signedProp, txContext.proposal)
1330+
cccid := ccprovider.NewCCContext(calledCcIns.ChainID, calledCcIns.ChaincodeName, cd.Version, msg.Txid, false, txContext.signedProp, txContext.proposal)
13431331

13441332
// Launch the new chaincode if not already running
13451333
if chaincodeLogger.IsEnabledFor(logging.DEBUG) {
13461334
chaincodeLogger.Debugf("[%s] launching chaincode %s on channel %s",
1347-
shorttxid(msg.Txid), calledCcParts.name, calledCcParts.suffix)
1335+
shorttxid(msg.Txid), calledCcIns.ChaincodeName, calledCcIns.ChainID)
13481336
}
13491337
cciSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: chaincodeSpec}
13501338
_, chaincodeInput, launchErr := handler.chaincodeSupport.Launch(ctxt, cccid, cciSpec)

core/committer/txvalidator/validator.go

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ type Validator interface {
7070
// and vscc execution, in order to increase
7171
// testability of txValidator
7272
type vsccValidator interface {
73-
// VSCCValidateTx(payload *common.Payload, envBytes []byte, env *common.Envelope) (*sysccprovider.VsccOutputData, error)
7473
VSCCValidateTx(payload *common.Payload, envBytes []byte) (*sysccprovider.ChaincodeInstance, *sysccprovider.VsccOutputData, error)
7574
}
7675

core/common/sysccprovider/sysccprovider.go

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type ChaincodeInstance struct {
6262
ChaincodeVersion string
6363
}
6464

65+
func (ci *ChaincodeInstance) String() string {
66+
return ci.ChainID + "." + ci.ChaincodeName + "#" + ci.ChaincodeVersion
67+
}
68+
6569
// VsccOutputData contains all the data returned from vscc.
6670
type VsccOutputData struct {
6771
// ProposalResponse bytes array validated by vscc

0 commit comments

Comments
 (0)