Skip to content

Commit a247e07

Browse files
committed
[FAB-8906] launcher uses canonicalName and version
Change-Id: I02705ffff6cd241c1bf5cde4ab51dfb8d9847f7c Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 427e75f commit a247e07

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

core/chaincode/chaincode_support.go

+48-32
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ func (chaincodeSupport *ChaincodeSupport) getTLSFiles(keyPair *accesscontrol.Cer
315315
}
316316

317317
//get args and env given chaincodeID
318-
func (chaincodeSupport *ChaincodeSupport) getLaunchConfigs(cccid *ccprovider.CCContext, cLang pb.ChaincodeSpec_Type) (args []string, envs []string, filesToUpload map[string][]byte, err error) {
319-
canName := cccid.GetCanonicalName()
318+
func (chaincodeSupport *ChaincodeSupport) getLaunchConfigs(canName string, cLang pb.ChaincodeSpec_Type) (args []string, envs []string, filesToUpload map[string][]byte, err error) {
320319
envs = []string{"CORE_CHAINCODE_ID_NAME=" + canName}
321320

322321
// ----------------------------------------------------------------------------
@@ -331,9 +330,9 @@ func (chaincodeSupport *ChaincodeSupport) getLaunchConfigs(cccid *ccprovider.CCC
331330
// ----------------------------------------------------------------------------
332331
var certKeyPair *accesscontrol.CertAndPrivKeyPair
333332
if chaincodeSupport.peerTLS {
334-
certKeyPair, err = chaincodeSupport.certGenerator.Generate(cccid.GetCanonicalName())
333+
certKeyPair, err = chaincodeSupport.certGenerator.Generate(canName)
335334
if err != nil {
336-
return nil, nil, nil, errors.WithMessage(err, fmt.Sprintf("failed generating TLS cert for %s", cccid.GetCanonicalName()))
335+
return nil, nil, nil, errors.WithMessage(err, fmt.Sprintf("failed generating TLS cert for %s", canName))
337336
}
338337
envs = append(envs, "CORE_PEER_TLS_ENABLED=true")
339338
envs = append(envs, fmt.Sprintf("CORE_TLS_CLIENT_KEY_PATH=%s", TLSClientKeyPath))
@@ -378,51 +377,61 @@ func (chaincodeSupport *ChaincodeSupport) getLaunchConfigs(cccid *ccprovider.CCC
378377

379378
//---------- Begin - launchAndWaitForRegister related functionality --------
380379

381-
//a launcher interface to encapsulate chaincode execution. This
382-
//helps with UT of launchAndWaitForRegister
380+
// launcherIntf encapsulate chaincode execution. This helps with UT of
381+
// launchAndWaitForRegister.
383382
type launcherIntf interface {
384-
launch(ctxt context.Context, notfy chan bool) (container.VMCResp, error)
383+
launch(ctxt context.Context, notfiy chan bool) (container.VMCResp, error)
385384
}
386385

387386
//ccLaucherImpl will use the container launcher mechanism to launch the actual chaincode
388387
type ccLauncherImpl struct {
389-
ctxt context.Context
390-
ccSupport *ChaincodeSupport
391-
cccid *ccprovider.CCContext
392-
cds *pb.ChaincodeDeploymentSpec
393-
builder api.BuildSpecFactory
388+
support *ChaincodeSupport
389+
canonicalName string
390+
version string
391+
cds *pb.ChaincodeDeploymentSpec
392+
builder api.BuildSpecFactory
394393
}
395394

396395
//launches the chaincode using the supplied context and notifier
397-
func (ccl *ccLauncherImpl) launch(ctxt context.Context, notfy chan bool) (container.VMCResp, error) {
398-
//launch the chaincode
399-
args, env, filesToUpload, err := ccl.ccSupport.getLaunchConfigs(ccl.cccid, ccl.cds.ChaincodeSpec.Type)
396+
func (ccl *ccLauncherImpl) launch(ctxt context.Context, notify chan bool) (container.VMCResp, error) {
397+
// launch the chaincode
398+
args, env, filesToUpload, err := ccl.support.getLaunchConfigs(ccl.canonicalName, ccl.cds.ChaincodeSpec.Type)
400399
if err != nil {
401400
return container.VMCResp{}, err
402401
}
403402

404-
canName := ccl.cccid.GetCanonicalName()
405-
406-
chaincodeLogger.Debugf("start container: %s(networkid:%s,peerid:%s)", canName, ccl.ccSupport.peerNetworkID, ccl.ccSupport.peerID)
403+
chaincodeLogger.Debugf("start container: %s(networkid:%s,peerid:%s)", ccl.canonicalName, ccl.support.peerNetworkID, ccl.support.peerID)
407404
chaincodeLogger.Debugf("start container with args: %s", strings.Join(args, " "))
408405
chaincodeLogger.Debugf("start container with env:\n\t%s", strings.Join(env, "\n\t"))
409406

410-
//set up the shadow handler JIT before container launch to
411-
//reduce window of when an external chaincode can sneak in
412-
//and use the launching context and make it its own
407+
// set up the shadow handler JIT before container launch to
408+
// reduce window of when an external chaincode can sneak in
409+
// and use the launching context and make it its own
413410
preLaunchFunc := func() error {
414-
ccl.ccSupport.preLaunchSetup(canName, notfy)
411+
ccl.support.preLaunchSetup(ccl.canonicalName, notify)
415412
return nil
416413
}
417414

418-
ccid := ccintf.CCID{ChaincodeSpec: ccl.cds.ChaincodeSpec, NetworkID: ccl.ccSupport.peerNetworkID, PeerID: ccl.ccSupport.peerID, Version: ccl.cccid.Version}
419-
sir := container.StartImageReq{CCID: ccid, Builder: ccl.builder, Args: args, Env: env, FilesToUpload: filesToUpload, PrelaunchFunc: preLaunchFunc}
420-
ipcCtxt := context.WithValue(ctxt, ccintf.GetCCHandlerKey(), ccl.ccSupport)
421-
422-
vmtype, _ := ccl.ccSupport.getVMType(ccl.cds)
423-
resp, err := container.VMCProcess(ipcCtxt, vmtype, sir)
415+
ipcCtxt := context.WithValue(ctxt, ccintf.GetCCHandlerKey(), ccl.support)
416+
sir := container.StartImageReq{
417+
CCID: ccintf.CCID{
418+
ChaincodeSpec: ccl.cds.ChaincodeSpec,
419+
NetworkID: ccl.support.peerNetworkID,
420+
PeerID: ccl.support.peerID,
421+
Version: ccl.version,
422+
},
423+
Builder: ccl.builder,
424+
Args: args,
425+
Env: env,
426+
FilesToUpload: filesToUpload,
427+
PrelaunchFunc: preLaunchFunc,
428+
}
429+
vmtype, err := ccl.support.getVMType(ccl.cds)
430+
if err != nil {
431+
return container.VMCResp{}, err
432+
}
424433

425-
return resp, err
434+
return container.VMCProcess(ipcCtxt, vmtype, sir)
426435
}
427436

428437
//launchAndWaitForRegister will launch container if not already running. Use
@@ -673,9 +682,16 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
673682
}
674683
}
675684

676-
builder := func() (io.Reader, error) { return platforms.GenerateDockerBuild(cds) }
677-
678-
err = chaincodeSupport.launchAndWaitForRegister(context, cccid, cds, &ccLauncherImpl{context, chaincodeSupport, cccid, cds, builder})
685+
launcher := &ccLauncherImpl{
686+
support: chaincodeSupport,
687+
canonicalName: cccid.GetCanonicalName(),
688+
version: cccid.Version,
689+
cds: cds,
690+
builder: func() (io.Reader, error) {
691+
return platforms.GenerateDockerBuild(cds)
692+
},
693+
}
694+
err = chaincodeSupport.launchAndWaitForRegister(context, cccid, cds, launcher)
679695
if err != nil {
680696
chaincodeLogger.Errorf("launchAndWaitForRegister failed: %+v", err)
681697
return cID, cMsg, err

core/chaincode/chaincode_support_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ func getLaunchConfigs(t *testing.T, certGenerator CertGenerator) {
823823
newCCSupport.certGenerator = certGenerator
824824

825825
ccContext := ccprovider.NewCCContext("dummyChannelId", "mycc", "v0", "dummyTxid", false, nil, nil)
826-
args, envs, filesToUpload, err := newCCSupport.getLaunchConfigs(ccContext, pb.ChaincodeSpec_GOLANG)
826+
args, envs, filesToUpload, err := newCCSupport.getLaunchConfigs(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG)
827827
if err != nil {
828828
t.Fatalf("calling getLaunchConfigs() failed with error %s", err)
829829
}
@@ -847,7 +847,7 @@ func getLaunchConfigs(t *testing.T, certGenerator CertGenerator) {
847847
t.Fatalf("calling getLaunchConfigs() with TLS enabled should have returned an array of 3 elements for filesToUpload, but got %v", len(filesToUpload))
848848
}
849849

850-
args, envs, _, err = newCCSupport.getLaunchConfigs(ccContext, pb.ChaincodeSpec_NODE)
850+
args, envs, _, err = newCCSupport.getLaunchConfigs(ccContext.GetCanonicalName(), pb.ChaincodeSpec_NODE)
851851
if len(args) != 3 {
852852
t.Fatalf("calling getLaunchConfigs() for node chaincode should have returned an array of 3 elements for Args, but got %v", args)
853853
}
@@ -857,7 +857,7 @@ func getLaunchConfigs(t *testing.T, certGenerator CertGenerator) {
857857
}
858858

859859
newCCSupport.peerTLS = false
860-
args, envs, _, err = newCCSupport.getLaunchConfigs(ccContext, pb.ChaincodeSpec_GOLANG)
860+
args, envs, _, err = newCCSupport.getLaunchConfigs(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG)
861861
if len(envs) != 4 {
862862
t.Fatalf("calling getLaunchConfigs() with TLS disabled should have returned an array of 4 elements for Envs, but got %v", envs)
863863
}

0 commit comments

Comments
 (0)