Skip to content

Commit e3d7051

Browse files
author
Jason Yellick
committed
FAB-10978 Remove spec from container_runtime
The container runtime currently takes both a cccid and a deployment spec. And pulls some information from each. Instead, it should take a structure which contains exactly the information it needs. Change-Id: I6c421a0f943aecef481be0b8b6ad6b3a7f5c4b0c Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent b1dd84b commit e3d7051

File tree

9 files changed

+207
-187
lines changed

9 files changed

+207
-187
lines changed

core/chaincode/chaincode_suite_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ type packageProvider interface {
6363
chaincode.PackageProvider
6464
}
6565

66-
//go:generate counterfeiter -o mock/lifecycle.go --fake-name Lifecycle . lifecycle
67-
type lifecycle interface {
66+
// This is a bit weird, we need to import the chaincode/lifecycle package, but there is an error,
67+
// even if we alias it to another name, so, calling 'lifecycleIface' instead of 'lifecycle'
68+
//go:generate counterfeiter -o mock/lifecycle.go --fake-name Lifecycle . lifecycleIface
69+
type lifecycleIface interface {
6870
chaincode.Lifecycle
6971
}
7072

core/chaincode/chaincode_support.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525

2626
// Runtime is used to manage chaincode runtime instances.
2727
type Runtime interface {
28-
Start(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error
29-
Stop(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error
28+
Start(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error
29+
Stop(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo) error
3030
}
3131

3232
// Launcher is used to launch chaincode runtimes.
@@ -164,7 +164,13 @@ func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContex
164164
cname := cccid.GetCanonicalName()
165165
defer cs.HandlerRegistry.Deregister(cname)
166166

167-
err := cs.Runtime.Stop(ctx, cccid, cds)
167+
err := cs.Runtime.Stop(ctx, &lifecycle.ChaincodeContainerInfo{
168+
Name: cds.Name(),
169+
Version: cccid.Version,
170+
Path: cds.Path(),
171+
Type: cds.CCType(),
172+
ContainerType: getVMType(cds),
173+
})
168174
if err != nil {
169175
return err
170176
}

core/chaincode/chaincode_support_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/hyperledger/fabric/core/aclmgmt/mocks"
3131
"github.com/hyperledger/fabric/core/aclmgmt/resources"
3232
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
33+
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
3334
"github.com/hyperledger/fabric/core/chaincode/mock"
3435
"github.com/hyperledger/fabric/core/chaincode/platforms"
3536
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
@@ -960,7 +961,7 @@ func getHistory(t *testing.T, chainID, ccname string, ccSide *mockpeer.MockCCCom
960961
func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
961962
gt := NewGomegaWithT(t)
962963
ccContext := ccprovider.NewCCContext("dummyChannelId", "mycc", "v0", "dummyTxid", false, nil, nil)
963-
lc, err := cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG)
964+
lc, err := cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG.String())
964965
if err != nil {
965966
t.Fatalf("calling getLaunchConfigs() failed with error %s", err)
966967
}
@@ -991,7 +992,7 @@ func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
991992
}
992993

993994
cr.CertGenerator = nil // disable TLS
994-
lc, err = cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_NODE)
995+
lc, err = cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_NODE.String())
995996
assert.NoError(t, err)
996997
args = lc.Args
997998

@@ -1003,7 +1004,7 @@ func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
10031004
t.Fatalf("calling getLaunchConfigs() should have returned the start command for node.js chaincode, but got %v", args)
10041005
}
10051006

1006-
lc, err = cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG)
1007+
lc, err = cr.LaunchConfig(ccContext.GetCanonicalName(), pb.ChaincodeSpec_GOLANG.String())
10071008
assert.NoError(t, err)
10081009

10091010
envs = lc.Envs
@@ -1020,7 +1021,7 @@ func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
10201021
func TestStartAndWaitSuccess(t *testing.T) {
10211022
handlerRegistry := NewHandlerRegistry(false)
10221023
fakeRuntime := &mock.Runtime{}
1023-
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
1024+
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10241025
handlerRegistry.Ready("testcc:0")
10251026
return nil
10261027
}
@@ -1045,7 +1046,7 @@ func TestStartAndWaitSuccess(t *testing.T) {
10451046
//test timeout error
10461047
func TestStartAndWaitTimeout(t *testing.T) {
10471048
fakeRuntime := &mock.Runtime{}
1048-
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
1049+
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10491050
time.Sleep(time.Second)
10501051
return nil
10511052
}
@@ -1070,7 +1071,7 @@ func TestStartAndWaitTimeout(t *testing.T) {
10701071
//test container return error
10711072
func TestStartAndWaitLaunchError(t *testing.T) {
10721073
fakeRuntime := &mock.Runtime{}
1073-
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
1074+
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10741075
return errors.New("Bad lunch; upset stomach")
10751076
}
10761077

core/chaincode/container_runtime.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"strings"
1414

1515
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
16+
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
1617
"github.com/hyperledger/fabric/core/chaincode/platforms"
17-
"github.com/hyperledger/fabric/core/common/ccprovider"
1818
"github.com/hyperledger/fabric/core/container"
1919
"github.com/hyperledger/fabric/core/container/ccintf"
2020
"github.com/hyperledger/fabric/core/container/dockercontroller"
@@ -47,10 +47,10 @@ type ContainerRuntime struct {
4747
}
4848

4949
// Start launches chaincode in a runtime environment.
50-
func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
51-
cname := cccid.GetCanonicalName()
50+
func (c *ContainerRuntime) Start(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error {
51+
cname := ccci.Name + ":" + ccci.Version
5252

53-
lc, err := c.LaunchConfig(cname, cds.ChaincodeSpec.Type)
53+
lc, err := c.LaunchConfig(cname, ccci.Type)
5454
if err != nil {
5555
return err
5656
}
@@ -61,43 +61,41 @@ func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCConte
6161

6262
scr := container.StartContainerReq{
6363
Builder: &container.PlatformBuilder{
64-
Type: cds.CCType(),
65-
Name: cds.Name(),
66-
Version: cds.Version(),
67-
Path: cds.Path(),
68-
CodePackage: cds.Bytes(),
64+
Type: ccci.Type,
65+
Name: ccci.Name,
66+
Version: ccci.Version,
67+
Path: ccci.Path,
68+
CodePackage: codePackage,
6969
PlatformRegistry: c.PlatformRegistry,
7070
},
7171
Args: lc.Args,
7272
Env: lc.Envs,
7373
FilesToUpload: lc.Files,
7474
CCID: ccintf.CCID{
75-
Name: cds.ChaincodeSpec.ChaincodeId.Name,
76-
Version: cccid.Version,
75+
Name: ccci.Name,
76+
Version: ccci.Version,
7777
},
7878
}
7979

80-
vmtype := getVMType(cds)
81-
82-
if err := c.Processor.Process(ctxt, vmtype, scr); err != nil {
80+
if err := c.Processor.Process(ctxt, ccci.ContainerType, scr); err != nil {
8381
return errors.WithMessage(err, "error starting container")
8482
}
8583

8684
return nil
8785
}
8886

8987
// Stop terminates chaincode and its container runtime environment.
90-
func (c *ContainerRuntime) Stop(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
88+
func (c *ContainerRuntime) Stop(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo) error {
9189
scr := container.StopContainerReq{
9290
CCID: ccintf.CCID{
93-
Name: cds.ChaincodeSpec.ChaincodeId.Name,
94-
Version: cccid.Version,
91+
Name: ccci.Name,
92+
Version: ccci.Version,
9593
},
9694
Timeout: 0,
9795
Dontremove: false,
9896
}
9997

100-
if err := c.Processor.Process(ctxt, getVMType(cds), scr); err != nil {
98+
if err := c.Processor.Process(ctxt, ccci.ContainerType, scr); err != nil {
10199
return errors.WithMessage(err, "error stopping container")
102100
}
103101

@@ -138,19 +136,19 @@ type LaunchConfig struct {
138136
}
139137

140138
// LaunchConfig creates the LaunchConfig for chaincode running in a container.
141-
func (c *ContainerRuntime) LaunchConfig(cname string, ccType pb.ChaincodeSpec_Type) (*LaunchConfig, error) {
139+
func (c *ContainerRuntime) LaunchConfig(cname string, ccType string) (*LaunchConfig, error) {
142140
var lc LaunchConfig
143141

144142
// common environment variables
145143
lc.Envs = append(c.CommonEnv, "CORE_CHAINCODE_ID_NAME="+cname)
146144

147145
// language specific arguments
148146
switch ccType {
149-
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:
147+
case pb.ChaincodeSpec_GOLANG.String(), pb.ChaincodeSpec_CAR.String():
150148
lc.Args = []string{"chaincode", fmt.Sprintf("-peer.address=%s", c.PeerAddress)}
151-
case pb.ChaincodeSpec_JAVA:
149+
case pb.ChaincodeSpec_JAVA.String():
152150
lc.Args = []string{"/root/chaincode-java/start", "--peerAddress", c.PeerAddress}
153-
case pb.ChaincodeSpec_NODE:
151+
case pb.ChaincodeSpec_NODE.String():
154152
lc.Args = []string{"/bin/sh", "-c", fmt.Sprintf("cd /usr/local/src; npm start -- --peer.address %s", c.PeerAddress)}
155153
default:
156154
return nil, errors.Errorf("unknown chaincodeType: %s", ccType)

0 commit comments

Comments
 (0)