Skip to content

Commit 816ef69

Browse files
committed
[FAB-9647] Rename ContainerRuntime and cleanup
Fix some doc comments, remove unused member data, config code mistakenly left behind. Change-Id: I8dfe358e8e04aefbc6d3ba91a472ce456c321492 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 5df456e commit 816ef69

9 files changed

+94
-103
lines changed

core/chaincode/chaincode_support.go

+47-72
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/golang/protobuf/proto"
14-
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
1514
"github.com/hyperledger/fabric/core/common/ccprovider"
1615
"github.com/hyperledger/fabric/core/common/sysccprovider"
1716
"github.com/hyperledger/fabric/core/container"
@@ -21,54 +20,48 @@ import (
2120
"golang.org/x/net/context"
2221
)
2322

24-
// CertGenerator generate client certificates for chaincode
25-
type CertGenerator interface {
26-
// Generate returns a certificate and private key and associates
27-
// the hash of the certificates with the given chaincode name
28-
Generate(ccName string) (*accesscontrol.CertAndPrivKeyPair, error)
29-
}
30-
3123
// Runtime is used to manage chaincode runtime instances.
3224
type Runtime interface {
3325
Start(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error
3426
Stop(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error
3527
}
3628

37-
// PackageProvider is responsible for getting the chaincode package from
38-
// the filesystem.
39-
type PackageProvider interface {
40-
GetChaincode(ccname string, ccversion string) (ccprovider.CCPackage, error)
29+
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
30+
type ChaincodeSupport struct {
31+
Keepalive time.Duration
32+
ExecuteTimeout time.Duration
33+
UserRunsCC bool
34+
Runtime Runtime
35+
ACLProvider ACLProvider
36+
HandlerRegistry *HandlerRegistry
37+
Launcher *Launcher
38+
sccp sysccprovider.SystemChaincodeProvider
4139
}
4240

4341
// NewChaincodeSupport creates a new ChaincodeSupport instance.
4442
func NewChaincodeSupport(
4543
config *Config,
4644
peerAddress string,
47-
userrunsCC bool,
45+
userRunsCC bool,
4846
caCert []byte,
4947
certGenerator CertGenerator,
5048
packageProvider PackageProvider,
5149
aclProvider ACLProvider,
5250
) *ChaincodeSupport {
5351
cs := &ChaincodeSupport{
54-
caCert: caCert,
55-
peerNetworkID: config.PeerNetworkID,
56-
peerID: config.PeerID,
57-
userRunsCC: userrunsCC,
58-
ccStartupTimeout: config.StartupTimeout,
59-
keepalive: config.Keepalive,
60-
executetimeout: config.ExecuteTimeout,
61-
HandlerRegistry: NewHandlerRegistry(userrunsCC),
62-
PackageProvider: packageProvider,
63-
ACLProvider: aclProvider,
52+
UserRunsCC: userRunsCC,
53+
Keepalive: config.Keepalive,
54+
ExecuteTimeout: config.ExecuteTimeout,
55+
HandlerRegistry: NewHandlerRegistry(userRunsCC),
56+
ACLProvider: aclProvider,
6457
}
6558

6659
// Keep TestQueries working
6760
if !config.TLSEnabled {
6861
certGenerator = nil
6962
}
7063

71-
cs.ContainerRuntime = &ContainerRuntime{
64+
cs.Runtime = &ContainerRuntime{
7265
CertGenerator: certGenerator,
7366
Processor: ProcessFunc(container.VMCProcess),
7467
CACert: caCert,
@@ -83,34 +76,16 @@ func NewChaincodeSupport(
8376
}
8477

8578
cs.Launcher = &Launcher{
86-
Runtime: cs.ContainerRuntime,
79+
Runtime: cs.Runtime,
8780
Registry: cs.HandlerRegistry,
88-
PackageProvider: cs.PackageProvider,
81+
PackageProvider: packageProvider,
8982
Lifecycle: &Lifecycle{Executor: cs},
9083
StartupTimeout: config.StartupTimeout,
9184
}
9285

9386
return cs
9487
}
9588

96-
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
97-
type ChaincodeSupport struct {
98-
caCert []byte
99-
peerAddress string
100-
ccStartupTimeout time.Duration
101-
peerNetworkID string
102-
peerID string
103-
keepalive time.Duration
104-
executetimeout time.Duration
105-
userRunsCC bool
106-
ContainerRuntime Runtime
107-
PackageProvider PackageProvider
108-
ACLProvider ACLProvider
109-
HandlerRegistry *HandlerRegistry
110-
Launcher *Launcher
111-
sccp sysccprovider.SystemChaincodeProvider
112-
}
113-
11489
// SetSysCCProvider is a bit of a hack to make a latent dependency of ChaincodeSupport
11590
// be an explicit dependency. Because the chaincode support must be registered before
11691
// the sysccprovider implementation can be created, we cannot make the sccp part of the
@@ -127,7 +102,7 @@ func (cs *ChaincodeSupport) Launch(ctx context.Context, cccid *ccprovider.CCCont
127102
}
128103

129104
// TODO: There has to be a better way to do this...
130-
if cs.userRunsCC && !cccid.Syscc {
105+
if cs.UserRunsCC && !cccid.Syscc {
131106
chaincodeLogger.Error(
132107
"You are attempting to perform an action other than Deploy on Chaincode that is not ready and you are in developer mode. Did you forget to Deploy your chaincode?",
133108
)
@@ -146,7 +121,7 @@ func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContex
146121
cname := cccid.GetCanonicalName()
147122
defer cs.HandlerRegistry.Deregister(cname)
148123

149-
err := cs.ContainerRuntime.Stop(ctx, cccid, cds)
124+
err := cs.Runtime.Stop(ctx, cccid, cds)
150125
if err != nil {
151126
return err
152127
}
@@ -179,23 +154,24 @@ func createCCMessage(messageType pb.ChaincodeMessage_Type, cid string, txid stri
179154
return ccmsg, nil
180155
}
181156

182-
// execute executes a transaction and waits for it to complete until a timeout value.
183-
func (cs *ChaincodeSupport) execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage, timeout time.Duration) (*pb.ChaincodeMessage, error) {
184-
cname := cccid.GetCanonicalName()
185-
chaincodeLogger.Debugf("canonical name: %s", cname)
186-
187-
handler := cs.HandlerRegistry.Handler(cname)
188-
if handler == nil {
189-
chaincodeLogger.Debugf("chaincode is not running: %s", cname)
190-
return nil, errors.Errorf("unable to invoke chaincode %s", cname)
157+
// ExecuteChaincode invokes chaincode with the provided arguments.
158+
func (cs *ChaincodeSupport) ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
159+
invocationSpec := &pb.ChaincodeInvocationSpec{
160+
ChaincodeSpec: &pb.ChaincodeSpec{
161+
Type: pb.ChaincodeSpec_GOLANG,
162+
ChaincodeId: &pb.ChaincodeID{Name: cccid.Name},
163+
Input: &pb.ChaincodeInput{Args: args},
164+
},
191165
}
192166

193-
ccresp, err := handler.Execute(ctxt, cccid, msg, timeout)
167+
res, ccevent, err := cs.ExecuteSpec(ctxt, cccid, invocationSpec)
194168
if err != nil {
195-
return nil, errors.WithMessage(err, fmt.Sprintf("error sending"))
169+
err = errors.WithMessage(err, "error invoking chaincode")
170+
chaincodeLogger.Errorf("%+v", err)
171+
return nil, nil, err
196172
}
197173

198-
return ccresp, nil
174+
return res, ccevent, err
199175
}
200176

201177
//Execute - execute proposal, return original response of chaincode
@@ -222,7 +198,7 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
222198
return nil, nil, errors.WithMessage(err, "failed to create chaincode message")
223199
}
224200

225-
resp, err := cs.execute(ctxt, cccid, ccMsg, cs.executetimeout)
201+
resp, err := cs.execute(ctxt, cccid, ccMsg)
226202
if err != nil {
227203
return nil, nil, errors.Wrapf(err, "failed to execute transaction %s", cccid.TxID)
228204
}
@@ -252,22 +228,21 @@ func (cs *ChaincodeSupport) ExecuteSpec(ctxt context.Context, cccid *ccprovider.
252228
}
253229
}
254230

255-
// ExecuteChaincode invokes chaincode with the provided arguments.
256-
func (cs *ChaincodeSupport) ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
257-
invocationSpec := &pb.ChaincodeInvocationSpec{
258-
ChaincodeSpec: &pb.ChaincodeSpec{
259-
Type: pb.ChaincodeSpec_GOLANG,
260-
ChaincodeId: &pb.ChaincodeID{Name: cccid.Name},
261-
Input: &pb.ChaincodeInput{Args: args},
262-
},
231+
// execute executes a transaction and waits for it to complete until a timeout value.
232+
func (cs *ChaincodeSupport) execute(ctxt context.Context, cccid *ccprovider.CCContext, msg *pb.ChaincodeMessage) (*pb.ChaincodeMessage, error) {
233+
cname := cccid.GetCanonicalName()
234+
chaincodeLogger.Debugf("canonical name: %s", cname)
235+
236+
handler := cs.HandlerRegistry.Handler(cname)
237+
if handler == nil {
238+
chaincodeLogger.Debugf("chaincode is not running: %s", cname)
239+
return nil, errors.Errorf("unable to invoke chaincode %s", cname)
263240
}
264241

265-
res, ccevent, err := cs.ExecuteSpec(ctxt, cccid, invocationSpec)
242+
ccresp, err := handler.Execute(ctxt, cccid, msg, cs.ExecuteTimeout)
266243
if err != nil {
267-
err = errors.WithMessage(err, "error invoking chaincode")
268-
chaincodeLogger.Errorf("%+v", err)
269-
return nil, nil, err
244+
return nil, errors.WithMessage(err, fmt.Sprintf("error sending"))
270245
}
271246

272-
return res, ccevent, err
247+
return ccresp, nil
273248
}

core/chaincode/chaincode_support_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
10051005
}
10061006

10071007
//success case
1008-
func TestLaunchAndWaitSuccess(t *testing.T) {
1008+
func TestStartAndWaitSuccess(t *testing.T) {
10091009
handlerRegistry := NewHandlerRegistry(false)
10101010
fakeRuntime := &mock.Runtime{}
10111011
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
@@ -1024,14 +1024,14 @@ func TestLaunchAndWaitSuccess(t *testing.T) {
10241024
cccid := ccprovider.NewCCContext("testchannel", "testcc", "0", "landwtimertest_txid", false, nil, nil)
10251025

10261026
//actual test - everythings good
1027-
err := launcher.launchAndWaitForReady(context.Background(), cccid, cds)
1027+
err := launcher.startAndWaitForReady(context.Background(), cccid, cds)
10281028
if err != nil {
10291029
t.Fatalf("expected success but failed with error %s", err)
10301030
}
10311031
}
10321032

10331033
//test timeout error
1034-
func TestLaunchAndWaitTimeout(t *testing.T) {
1034+
func TestStartAndWaitTimeout(t *testing.T) {
10351035
fakeRuntime := &mock.Runtime{}
10361036
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
10371037
time.Sleep(time.Second)
@@ -1049,14 +1049,14 @@ func TestLaunchAndWaitTimeout(t *testing.T) {
10491049
cccid := ccprovider.NewCCContext("testchannel", "testcc", "0", "landwtimertest_txid", false, nil, nil)
10501050

10511051
//the actual test - timeout 1000 > 500
1052-
err := launcher.launchAndWaitForReady(context.Background(), cccid, cds)
1052+
err := launcher.startAndWaitForReady(context.Background(), cccid, cds)
10531053
if err == nil {
10541054
t.Fatalf("expected error but succeeded")
10551055
}
10561056
}
10571057

10581058
//test container return error
1059-
func TestLaunchAndWaitLaunchError(t *testing.T) {
1059+
func TestStartAndWaitLaunchError(t *testing.T) {
10601060
fakeRuntime := &mock.Runtime{}
10611061
fakeRuntime.StartStub = func(_ context.Context, _ *ccprovider.CCContext, _ *pb.ChaincodeDeploymentSpec) error {
10621062
return errors.New("Bad lunch; upset stomach")
@@ -1073,7 +1073,7 @@ func TestLaunchAndWaitLaunchError(t *testing.T) {
10731073
cccid := ccprovider.NewCCContext("testchannel", "testcc", "0", "landwtimertest_txid", false, nil, nil)
10741074

10751075
//actual test - container launch gives error
1076-
err := launcher.launchAndWaitForReady(context.Background(), cccid, cds)
1076+
err := launcher.startAndWaitForReady(context.Background(), cccid, cds)
10771077
if err == nil {
10781078
t.Fatalf("expected error but succeeded")
10791079
}
@@ -1324,7 +1324,7 @@ func TestCCFramework(t *testing.T) {
13241324
getHistory(t, chainID, ccname, ccSide, chaincodeSupport)
13251325

13261326
//just use the previous certGenerator for generating TLS key/pair
1327-
cr := chaincodeSupport.ContainerRuntime.(*ContainerRuntime)
1327+
cr := chaincodeSupport.Runtime.(*ContainerRuntime)
13281328
getLaunchConfigs(t, cr)
13291329

13301330
ccSide.Quit()

core/chaincode/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *Config) load() {
5656
}
5757
c.StartupTimeout = viper.GetDuration("chaincode.startuptimeout")
5858
if c.StartupTimeout < minimumStartupTimeout {
59-
c.StartupTimeout = 5 * time.Second
59+
c.StartupTimeout = minimumStartupTimeout
6060
}
6161

6262
c.LogFormat = viper.GetString("chaincode.logging.format")

core/chaincode/container_runtime.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func (v ProcessFunc) Process(ctxt context.Context, vmtype string, req container.
3636
return v(ctxt, vmtype, req)
3737
}
3838

39+
// CertGenerator generates client certificates for chaincode.
40+
type CertGenerator interface {
41+
// Generate returns a certificate and private key and associates
42+
// the hash of the certificates with the given chaincode name
43+
Generate(ccName string) (*accesscontrol.CertAndPrivKeyPair, error)
44+
}
45+
3946
// ContainerRuntime is responsible for managing containerized chaincode.
4047
type ContainerRuntime struct {
4148
CertGenerator CertGenerator
@@ -47,7 +54,7 @@ type ContainerRuntime struct {
4754
PeerNetworkID string
4855
}
4956

50-
// Start launches chaincode in a container runtime environment.
57+
// Start launches chaincode in a runtime environment.
5158
func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
5259
cname := cccid.GetCanonicalName()
5360

@@ -60,7 +67,6 @@ func (c *ContainerRuntime) Start(ctxt context.Context, cccid *ccprovider.CCConte
6067
chaincodeLogger.Debugf("start container with args: %s", strings.Join(lc.Args, " "))
6168
chaincodeLogger.Debugf("start container with env:\n\t%s", strings.Join(lc.Envs, "\n\t"))
6269

63-
// ipcCtxt := context.WithValue(ctxt, ccintf.GetCCHandlerKey(), ccl.support)
6470
builder := func() (io.Reader, error) { return platforms.GenerateDockerBuild(cds) }
6571
scr := container.StartContainerReq{
6672
Builder: builder,

core/chaincode/exectransaction_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,15 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
554554

555555
if destroyImage {
556556
chaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
557-
dir := container.DestroyImageReq{CCID: ccintf.CCID{ChaincodeSpec: spec, NetworkID: chaincodeSupport.peerNetworkID, PeerID: chaincodeSupport.peerID}, Force: true, NoPrune: true}
557+
dir := container.DestroyImageReq{
558+
CCID: ccintf.CCID{
559+
ChaincodeSpec: spec,
560+
// NetworkID: chaincodeSupport.peerNetworkID,
561+
// PeerID: chaincodeSupport.peerID,
562+
},
563+
Force: true,
564+
NoPrune: true,
565+
}
558566

559567
_, err = container.VMCProcess(ctxt, container.DOCKER, dir)
560568
if err != nil {
@@ -1106,8 +1114,8 @@ func TestQueries(t *testing.T) {
11061114
//the peer should handle this gracefully and not die
11071115

11081116
//save the original timeout and set a new timeout of 1 sec
1109-
origTimeout := chaincodeSupport.executetimeout
1110-
chaincodeSupport.executetimeout = time.Duration(1) * time.Second
1117+
origTimeout := chaincodeSupport.ExecuteTimeout
1118+
chaincodeSupport.ExecuteTimeout = time.Duration(1) * time.Second
11111119

11121120
//chaincode to sleep for 2 secs with timeout 1
11131121
args = util.ToChaincodeArgs(f, "marble001", "marble002", "2000")
@@ -1122,7 +1130,7 @@ func TestQueries(t *testing.T) {
11221130
}
11231131

11241132
//restore timeout
1125-
chaincodeSupport.executetimeout = origTimeout
1133+
chaincodeSupport.ExecuteTimeout = origTimeout
11261134

11271135
// querying for all marbles will return 101 marbles
11281136
// this query should return exactly 101 results (one call to Next())

core/chaincode/executetransaction_pvtdata_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ func TestQueriesPrivateData(t *testing.T) {
276276
//the peer should handle this gracefully and not die
277277

278278
//save the original timeout and set a new timeout of 1 sec
279-
origTimeout := chaincodeSupport.executetimeout
280-
chaincodeSupport.executetimeout = time.Duration(1) * time.Second
279+
origTimeout := chaincodeSupport.ExecuteTimeout
280+
chaincodeSupport.ExecuteTimeout = time.Duration(1) * time.Second
281281

282282
//chaincode to sleep for 2 secs with timeout 1
283283
args = util.ToChaincodeArgs(f, "marble001", "marble002", "2000")
@@ -292,7 +292,7 @@ func TestQueriesPrivateData(t *testing.T) {
292292
}
293293

294294
//restore timeout
295-
chaincodeSupport.executetimeout = origTimeout
295+
chaincodeSupport.ExecuteTimeout = origTimeout
296296

297297
// querying for all marbles will return 101 marbles
298298
// this query should return exactly 101 results (one call to Next())

0 commit comments

Comments
 (0)