Skip to content

Commit d681fad

Browse files
author
Jason Yellick
committed
FAB-16158 Pass chaincode name instead of ID
The endorser.go code labors under the false assumption that a user passes in anything in the chaincode ID other than name. It then goes and overrides these values anyway. There's no point to passing the ID around when the only piece of info it contains is the string name. Fixing. Change-Id: If54546736b7927182420304706734420e2181e14 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent b200982 commit d681fad

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

core/endorser/endorser.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ func NewEndorserServer(privDist privateDataDistributor, s Support, pr *packaging
117117
}
118118

119119
// call specified chaincode (system or user)
120-
func (e *Endorser) callChaincode(txParams *ccprovider.TransactionParams, idBytes []byte, requiresInit bool, input *pb.ChaincodeInput, cid *pb.ChaincodeID) (*pb.Response, *pb.ChaincodeEvent, error) {
121-
endorserLogger.Infof("[%s][%s] Entry chaincode: %s", txParams.ChannelID, shorttxid(txParams.TxID), cid)
120+
func (e *Endorser) callChaincode(txParams *ccprovider.TransactionParams, idBytes []byte, requiresInit bool, input *pb.ChaincodeInput, chaincodeName string) (*pb.Response, *pb.ChaincodeEvent, error) {
121+
endorserLogger.Infof("[%s][%s] Entry chaincode: %s", txParams.ChannelID, shorttxid(txParams.TxID), chaincodeName)
122122
defer func(start time.Time) {
123123
logger := endorserLogger.WithOptions(zap.AddCallerSkip(1))
124124
elapsedMilliseconds := time.Since(start).Round(time.Millisecond) / time.Millisecond
125-
logger.Infof("[%s][%s] Exit chaincode: %s (%dms)", txParams.ChannelID, shorttxid(txParams.TxID), cid, elapsedMilliseconds)
125+
logger.Infof("[%s][%s] Exit chaincode: %s (%dms)", txParams.ChannelID, shorttxid(txParams.TxID), chaincodeName, elapsedMilliseconds)
126126
}(time.Now())
127127

128-
res, ccevent, err := e.s.Execute(txParams, txParams.ChannelID, cid.Name, txParams.TxID, idBytes, requiresInit, txParams.SignedProp, txParams.Proposal, input)
128+
res, ccevent, err := e.s.Execute(txParams, txParams.ChannelID, chaincodeName, txParams.TxID, idBytes, requiresInit, txParams.SignedProp, txParams.Proposal, input)
129129
if err != nil {
130130
return nil, nil, err
131131
}
@@ -145,7 +145,7 @@ func (e *Endorser) callChaincode(txParams *ccprovider.TransactionParams, idBytes
145145
//
146146
// NOTE that if there's an error all simulation, including the chaincode
147147
// table changes in lscc will be thrown away
148-
if cid.Name == "lscc" && len(input.Args) >= 3 && (string(input.Args[0]) == "deploy" || string(input.Args[0]) == "upgrade") {
148+
if chaincodeName == "lscc" && len(input.Args) >= 3 && (string(input.Args[0]) == "deploy" || string(input.Args[0]) == "upgrade") {
149149
userCDS, err := protoutil.GetChaincodeDeploymentSpec(input.Args[2])
150150
if err != nil {
151151
return nil, nil, err
@@ -197,8 +197,8 @@ func (e *Endorser) SanitizeUserCDS(userCDS *pb.ChaincodeDeploymentSpec) (*pb.Cha
197197
}
198198

199199
// SimulateProposal simulates the proposal by calling the chaincode
200-
func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, cid *pb.ChaincodeID) (ccprovider.ChaincodeDefinition, *pb.Response, []byte, *pb.ChaincodeEvent, error) {
201-
endorserLogger.Debugf("[%s][%s] Entry chaincode: %s", txParams.ChannelID, shorttxid(txParams.TxID), cid)
200+
func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, chaincodeName string) (ccprovider.ChaincodeDefinition, *pb.Response, []byte, *pb.ChaincodeEvent, error) {
201+
endorserLogger.Debugf("[%s][%s] Entry chaincode: %s", txParams.ChannelID, shorttxid(txParams.TxID), chaincodeName)
202202
defer endorserLogger.Debugf("[%s][%s] Exit", txParams.ChannelID, shorttxid(txParams.TxID))
203203
// we do expect the payload to be a ChaincodeInvocationSpec
204204
// if we are supporting other payloads in future, this be glaringly point
@@ -208,9 +208,9 @@ func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, cid
208208
return nil, nil, nil, nil, err
209209
}
210210

211-
cdLedger, err := e.s.GetChaincodeDefinition(txParams.ChannelID, cid.Name, txParams.TXSimulator)
211+
cdLedger, err := e.s.GetChaincodeDefinition(txParams.ChannelID, chaincodeName, txParams.TXSimulator)
212212
if err != nil {
213-
return nil, nil, nil, nil, errors.WithMessagef(err, "make sure the chaincode %s has been successfully defined on channel %s and try again", cid.Name, txParams.ChannelID)
213+
return nil, nil, nil, nil, errors.WithMessagef(err, "make sure the chaincode %s has been successfully defined on channel %s and try again", chaincodeName, txParams.ChannelID)
214214
}
215215

216216
err = e.s.CheckInstantiationPolicy(cdLedger.CCID(), cdLedger)
@@ -219,9 +219,9 @@ func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, cid
219219
}
220220

221221
// ---3. execute the proposal and get simulation results
222-
res, ccevent, err := e.callChaincode(txParams, cdLedger.Hash(), cdLedger.RequiresInit(), cis.ChaincodeSpec.Input, cid)
222+
res, ccevent, err := e.callChaincode(txParams, cdLedger.Hash(), cdLedger.RequiresInit(), cis.ChaincodeSpec.Input, chaincodeName)
223223
if err != nil {
224-
endorserLogger.Errorf("[%s][%s] failed to invoke chaincode %s, error: %+v", txParams.ChannelID, shorttxid(txParams.TxID), cid, err)
224+
endorserLogger.Errorf("[%s][%s] failed to invoke chaincode %s, error: %+v", txParams.ChannelID, shorttxid(txParams.TxID), chaincodeName, err)
225225
return nil, nil, nil, nil, err
226226
}
227227

@@ -240,7 +240,7 @@ func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, cid
240240
}
241241

242242
if simResult.PvtSimulationResults != nil {
243-
if cid.Name == "lscc" {
243+
if chaincodeName == "lscc" {
244244
// TODO: remove once we can store collection configuration outside of LSCC
245245
return nil, nil, nil, nil, errors.New("Private data is forbidden to be used in instantiate")
246246
}
@@ -276,13 +276,13 @@ func (e *Endorser) SimulateProposal(txParams *ccprovider.TransactionParams, cid
276276
}
277277

278278
// endorse the proposal by calling the ESCC
279-
func (e *Endorser) endorseProposal(chainID string, txid string, signedProp *pb.SignedProposal, proposal *pb.Proposal, response *pb.Response, simRes []byte, event *pb.ChaincodeEvent, visibility []byte, ccid *pb.ChaincodeID, txsim ledger.TxSimulator, cd ccprovider.ChaincodeDefinition) (*pb.ProposalResponse, error) {
280-
endorserLogger.Debugf("[%s][%s] Entry chaincode: %s", chainID, shorttxid(txid), ccid)
279+
func (e *Endorser) endorseProposal(chainID string, txid string, signedProp *pb.SignedProposal, proposal *pb.Proposal, response *pb.Response, simRes []byte, event *pb.ChaincodeEvent, visibility []byte, chaincodeName string, txsim ledger.TxSimulator, cd ccprovider.ChaincodeDefinition) (*pb.ProposalResponse, error) {
280+
endorserLogger.Debugf("[%s][%s] Entry chaincode: %s", chainID, shorttxid(txid), chaincodeName)
281281
defer endorserLogger.Debugf("[%s][%s] Exit", chainID, shorttxid(txid))
282282

283283
escc := cd.Endorsement()
284284

285-
endorserLogger.Debugf("[%s][%s] escc for chaincode %s is %s", chainID, shorttxid(txid), ccid, escc)
285+
endorserLogger.Debugf("[%s][%s] escc for chaincode %s is %s", chainID, shorttxid(txid), chaincodeName, escc)
286286

287287
// marshalling event bytes
288288
var err error
@@ -294,19 +294,20 @@ func (e *Endorser) endorseProposal(chainID string, txid string, signedProp *pb.S
294294
}
295295
}
296296

297-
ccid.Version = cd.CCVersion()
298-
299297
ctx := Context{
300298
PluginName: escc,
301299
Channel: chainID,
302300
SignedProposal: signedProp,
303-
ChaincodeID: ccid,
304-
Event: eventBytes,
305-
SimRes: simRes,
306-
Response: response,
307-
Visibility: visibility,
308-
Proposal: proposal,
309-
TxID: txid,
301+
ChaincodeID: &pb.ChaincodeID{
302+
Name: chaincodeName,
303+
Version: cd.CCVersion(),
304+
},
305+
Event: eventBytes,
306+
SimRes: simRes,
307+
Response: response,
308+
Visibility: visibility,
309+
Proposal: proposal,
310+
TxID: txid,
310311
}
311312
return e.s.EndorseWithPlugin(ctx)
312313
}
@@ -421,7 +422,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
421422
// Also obtain a history query executor for history queries, since tx simulator does not cover history
422423
var txsim ledger.TxSimulator
423424
var historyQueryExecutor ledger.HistoryQueryExecutor
424-
if acquireTxSimulator(chainID, vr.hdrExt.ChaincodeId) {
425+
if acquireTxSimulator(chainID, vr.hdrExt.ChaincodeId.Name) {
425426
if txsim, err = e.s.GetTxSimulator(chainID, txid); err != nil {
426427
return &pb.ProposalResponse{Response: &pb.Response{Status: 500, Message: err.Error()}}, nil
427428
}
@@ -456,7 +457,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
456457
// to validate the supplied action before endorsing it
457458

458459
// 1 -- simulate
459-
cd, res, simulationResult, ccevent, err := e.SimulateProposal(txParams, hdrExt.ChaincodeId)
460+
cd, res, simulationResult, ccevent, err := e.SimulateProposal(txParams, hdrExt.ChaincodeId.Name)
460461
if err != nil {
461462
return &pb.ProposalResponse{Response: &pb.Response{Status: 500, Message: err.Error()}}, nil
462463
}
@@ -488,7 +489,7 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
488489
pResp = &pb.ProposalResponse{Response: res}
489490
} else {
490491
// Note: To endorseProposal(), we pass the released txsim. Hence, an error would occur if we try to use this txsim
491-
pResp, err = e.endorseProposal(chainID, txid, signedProp, prop, res, simulationResult, ccevent, hdrExt.PayloadVisibility, hdrExt.ChaincodeId, txsim, cd)
492+
pResp, err = e.endorseProposal(chainID, txid, signedProp, prop, res, simulationResult, ccevent, hdrExt.PayloadVisibility, hdrExt.ChaincodeId.Name, txsim, cd)
492493

493494
// if error, capture endorsement failure metric
494495
meterLabels := []string{
@@ -525,15 +526,15 @@ func (e *Endorser) ProcessProposal(ctx context.Context, signedProp *pb.SignedPro
525526

526527
// determine whether or not a transaction simulator should be
527528
// obtained for a proposal.
528-
func acquireTxSimulator(chainID string, ccid *pb.ChaincodeID) bool {
529+
func acquireTxSimulator(chainID string, chaincodeName string) bool {
529530
if chainID == "" {
530531
return false
531532
}
532533

533534
// ¯\_(ツ)_/¯ locking.
534535
// Don't get a simulator for the query and config system chaincode.
535536
// These don't need the simulator and its read lock results in deadlocks.
536-
switch ccid.Name {
537+
switch chaincodeName {
537538
case "qscc", "cscc":
538539
return false
539540
default:

core/endorser/endorser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ func TestEndorserChaincodeCallLogging(t *testing.T) {
564564
es.ProcessProposal(context.Background(), getSignedProp("chaincode-name", "chaincode-version", t))
565565

566566
t.Logf("contents:\n%s", buf.Contents())
567-
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Entry chaincode: name:"chaincode-name" version:"chaincode-version"`))
568-
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Exit chaincode: name:"chaincode-name" version:"chaincode-version" (.*ms)`))
567+
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Entry chaincode: chaincode-name`))
568+
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Exit chaincode: chaincode-name (.*ms)`))
569569
}
570570

571571
func TestEndorserLSCC(t *testing.T) {
@@ -728,7 +728,7 @@ func TestSimulateProposal(t *testing.T) {
728728
},
729729
}, packaging.NewRegistry(&golang.Platform{}), &disabled.Provider{})
730730

731-
_, _, _, _, err := es.SimulateProposal(&ccprovider.TransactionParams{}, nil)
731+
_, _, _, _, err := es.SimulateProposal(&ccprovider.TransactionParams{}, "")
732732
assert.Error(t, err)
733733
}
734734

0 commit comments

Comments
 (0)