Skip to content

Commit 3eebcf3

Browse files
author
Jason Yellick
committed
FAB-11000 Remove unnecessary context
There is a context passed into all of the Start/Stop methods for the chaincode container management. Historically, the only ever reference to it was via a value on the context to get back the chaincode support. However, since this dependency has been remeoved, it no longer serves any point, other than to complicate method signatures and cause general confusion. It's clear whenever it was wired, the intent was to eventually make the dockerclient etc. have cancelable contexts. However, this work was never done, and it is sufficiently low priority as to not even appear in the backlog. It is trivial enough to add back the context dependency if it's ever shown that it's useful. Change-Id: I8bcf0922594a5e40cb187687849b49ad5653ae89 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent aa52af1 commit 3eebcf3

27 files changed

+287
-381
lines changed

core/chaincode/ccproviderimpl.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2017 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package chaincode
@@ -67,6 +57,6 @@ func (c *CCProviderImpl) ExecuteInit(ctxt context.Context, cccid *ccprovider.CCC
6757
}
6858

6959
// Stop stops the chaincode given context and spec
70-
func (c *CCProviderImpl) Stop(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) error {
71-
return c.cs.Stop(ctxt, cccid, spec)
60+
func (c *CCProviderImpl) Stop(cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) error {
61+
return c.cs.Stop(cccid, spec)
7262
}

core/chaincode/chaincode_support.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import (
2626

2727
// Runtime is used to manage chaincode runtime instances.
2828
type Runtime interface {
29-
Start(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error
30-
Stop(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo) error
29+
Start(ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error
30+
Stop(ccci *lifecycle.ChaincodeContainerInfo) error
3131
}
3232

3333
// Launcher is used to launch chaincode runtimes.
3434
type Launcher interface {
35-
Launch(context context.Context, ccci *lifecycle.ChaincodeContainerInfo) error
35+
Launch(ccci *lifecycle.ChaincodeContainerInfo) error
3636
}
3737

3838
// Lifecycle provides a way to retrieve chaincode definitions and the packages necessary to run them
@@ -112,24 +112,22 @@ func NewChaincodeSupport(
112112
// LaunchForInit bypasses getting the chaincode spec from the LSCC table
113113
// as in the case of v1.0-v1.2 lifecycle, the chaincode will not yet be
114114
// defined in the LSCC table
115-
func (cs *ChaincodeSupport) LaunchInit(ctx context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) error {
115+
func (cs *ChaincodeSupport) LaunchInit(cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) error {
116116
cname := cccid.GetCanonicalName()
117117
if cs.HandlerRegistry.Handler(cname) != nil {
118118
return nil
119119
}
120120

121-
ctx = context.WithValue(ctx, ccintf.GetCCHandlerKey(), cs)
122-
123121
ccci := lifecycle.DeploymentSpecToChaincodeContainerInfo(spec)
124122
ccci.Version = cccid.Version
125123

126-
return cs.Launcher.Launch(ctx, ccci)
124+
return cs.Launcher.Launch(ccci)
127125
}
128126

129127
// Launch starts executing chaincode if it is not already running. This method
130128
// blocks until the peer side handler gets into ready state or encounters a fatal
131129
// error. If the chaincode is already running, it simply returns.
132-
func (cs *ChaincodeSupport) Launch(ctx context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) error {
130+
func (cs *ChaincodeSupport) Launch(cccid *ccprovider.CCContext, spec *pb.ChaincodeInvocationSpec) error {
133131
cname := cccid.GetCanonicalName()
134132
if cs.HandlerRegistry.Handler(cname) != nil {
135133
return nil
@@ -142,28 +140,24 @@ func (cs *ChaincodeSupport) Launch(ctx context.Context, cccid *ccprovider.CCCont
142140
)
143141
}
144142

145-
// This is hacky. The only user of this context value is the in-process controller
146-
// used to support system chaincode. It should really be instantiated with the
147-
// appropriate reference to ChaincodeSupport.
148-
ctx = context.WithValue(ctx, ccintf.GetCCHandlerKey(), cs)
149143
chaincodeName := spec.GetChaincodeSpec().Name()
150144

151145
ccci, err := cs.Lifecycle.ChaincodeContainerInfo(cccid.ChainID, chaincodeName)
152146
if err != nil {
153147
return errors.Wrapf(err, "[channel %s] failed to get chaincode container info for %s", cccid.ChainID, chaincodeName)
154148
}
155149

156-
return cs.Launcher.Launch(ctx, ccci)
150+
return cs.Launcher.Launch(ccci)
157151
}
158152

159153
// Stop stops a chaincode if running.
160-
func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
154+
func (cs *ChaincodeSupport) Stop(cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
161155
cname := cccid.GetCanonicalName()
162156
defer cs.HandlerRegistry.Deregister(cname)
163157

164158
ccci := lifecycle.DeploymentSpecToChaincodeContainerInfo(cds)
165159
ccci.Version = cccid.Version
166-
err := cs.Runtime.Stop(ctx, ccci)
160+
err := cs.Runtime.Stop(ccci)
167161
if err != nil {
168162
return err
169163
}
@@ -172,10 +166,7 @@ func (cs *ChaincodeSupport) Stop(ctx context.Context, cccid *ccprovider.CCContex
172166
}
173167

174168
// HandleChaincodeStream implements ccintf.HandleChaincodeStream for all vms to call with appropriate stream
175-
func (cs *ChaincodeSupport) HandleChaincodeStream(ctxt context.Context, stream ccintf.ChaincodeStream) error {
176-
deadline, ok := ctxt.Deadline()
177-
chaincodeLogger.Debugf("Current context deadline = %s, ok = %v", deadline, ok)
178-
169+
func (cs *ChaincodeSupport) HandleChaincodeStream(stream ccintf.ChaincodeStream) error {
179170
handler := &Handler{
180171
Invoker: cs,
181172
DefinitionGetter: cs.Lifecycle,
@@ -197,7 +188,7 @@ func (cs *ChaincodeSupport) HandleChaincodeStream(ctxt context.Context, stream c
197188

198189
// Register the bidi stream entry point called by chaincode to register with the Peer.
199190
func (cs *ChaincodeSupport) Register(stream pb.ChaincodeSupport_RegisterServer) error {
200-
return cs.HandleChaincodeStream(stream.Context(), stream)
191+
return cs.HandleChaincodeStream(stream)
201192
}
202193

203194
// createCCMessage creates a transaction message.
@@ -260,7 +251,7 @@ func processChaincodeExecutionResult(cccid *ccprovider.CCContext, resp *pb.Chain
260251
func (cs *ChaincodeSupport) InvokeInit(ctxt context.Context, cccid *ccprovider.CCContext, spec *pb.ChaincodeDeploymentSpec) (*pb.ChaincodeMessage, error) {
261252
cctyp := pb.ChaincodeMessage_INIT
262253

263-
err := cs.LaunchInit(ctxt, cccid, spec)
254+
err := cs.LaunchInit(cccid, spec)
264255
if err != nil {
265256
return nil, err
266257
}
@@ -290,7 +281,7 @@ func (cs *ChaincodeSupport) Invoke(ctxt context.Context, cccid *ccprovider.CCCon
290281
return nil, errors.New("chaincode spec is nil")
291282
}
292283

293-
err := cs.Launch(ctxt, cccid, spec)
284+
err := cs.Launch(cccid, spec)
294285
if err != nil {
295286
return nil, err
296287
}

core/chaincode/chaincode_support_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func startCC(t *testing.T, channelID string, ccname string, chaincodeSupport *Ch
336336
flogging.SetModuleLevel("chaincode", "debug")
337337
//register peer side with ccsupport
338338
go func() {
339-
chaincodeSupport.HandleChaincodeStream(context.Background(), peerSide)
339+
chaincodeSupport.HandleChaincodeStream(peerSide)
340340
}()
341341

342342
done := setuperror()
@@ -1026,7 +1026,7 @@ func getLaunchConfigs(t *testing.T, cr *ContainerRuntime) {
10261026
func TestStartAndWaitSuccess(t *testing.T) {
10271027
handlerRegistry := NewHandlerRegistry(false)
10281028
fakeRuntime := &mock.Runtime{}
1029-
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
1029+
fakeRuntime.StartStub = func(_ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10301030
handlerRegistry.Ready("testcc:0")
10311031
return nil
10321032
}
@@ -1050,7 +1050,7 @@ func TestStartAndWaitSuccess(t *testing.T) {
10501050
}
10511051

10521052
//actual test - everythings good
1053-
err := launcher.Launch(context.Background(), ccci)
1053+
err := launcher.Launch(ccci)
10541054
if err != nil {
10551055
t.Fatalf("expected success but failed with error %s", err)
10561056
}
@@ -1059,7 +1059,7 @@ func TestStartAndWaitSuccess(t *testing.T) {
10591059
//test timeout error
10601060
func TestStartAndWaitTimeout(t *testing.T) {
10611061
fakeRuntime := &mock.Runtime{}
1062-
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
1062+
fakeRuntime.StartStub = func(_ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10631063
time.Sleep(time.Second)
10641064
return nil
10651065
}
@@ -1083,7 +1083,7 @@ func TestStartAndWaitTimeout(t *testing.T) {
10831083
}
10841084

10851085
//the actual test - timeout 1000 > 500
1086-
err := launcher.Launch(context.Background(), ccci)
1086+
err := launcher.Launch(ccci)
10871087
if err == nil {
10881088
t.Fatalf("expected error but succeeded")
10891089
}
@@ -1092,7 +1092,7 @@ func TestStartAndWaitTimeout(t *testing.T) {
10921092
//test container return error
10931093
func TestStartAndWaitLaunchError(t *testing.T) {
10941094
fakeRuntime := &mock.Runtime{}
1095-
fakeRuntime.StartStub = func(_ context.Context, _ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
1095+
fakeRuntime.StartStub = func(_ *lifecycle.ChaincodeContainerInfo, _ []byte) error {
10961096
return errors.New("Bad lunch; upset stomach")
10971097
}
10981098

@@ -1115,7 +1115,7 @@ func TestStartAndWaitLaunchError(t *testing.T) {
11151115
}
11161116

11171117
//actual test - container launch gives error
1118-
err := launcher.Launch(context.Background(), ccci)
1118+
err := launcher.Launch(ccci)
11191119
if err == nil {
11201120
t.Fatalf("expected error but succeeded")
11211121
}

core/chaincode/concurrency_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package chaincode
@@ -57,7 +47,7 @@ func TestExecuteConcurrentInvokes(t *testing.T) {
5747

5848
cccid := ccprovider.NewCCContext(chainID, "nkpi", "0", "", false, nil, nil)
5949

60-
defer chaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
50+
defer chaincodeSupport.Stop(cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
6151

6252
var nextBlockNumber uint64
6353
_, err = deploy(ctxt, cccid, spec, nextBlockNumber, chaincodeSupport)

core/chaincode/container_runtime.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import (
1919
"github.com/hyperledger/fabric/core/container/ccintf"
2020
pb "github.com/hyperledger/fabric/protos/peer"
2121
"github.com/pkg/errors"
22-
"golang.org/x/net/context"
2322
)
2423

2524
// Processor processes vm and container requests.
2625
type Processor interface {
27-
Process(ctxt context.Context, vmtype string, req container.VMCReq) error
26+
Process(vmtype string, req container.VMCReq) error
2827
}
2928

3029
// CertGenerator generates client certificates for chaincode.
@@ -45,7 +44,7 @@ type ContainerRuntime struct {
4544
}
4645

4746
// Start launches chaincode in a runtime environment.
48-
func (c *ContainerRuntime) Start(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error {
47+
func (c *ContainerRuntime) Start(ccci *lifecycle.ChaincodeContainerInfo, codePackage []byte) error {
4948
cname := ccci.Name + ":" + ccci.Version
5049

5150
lc, err := c.LaunchConfig(cname, ccci.Type)
@@ -75,15 +74,15 @@ func (c *ContainerRuntime) Start(ctxt context.Context, ccci *lifecycle.Chaincode
7574
},
7675
}
7776

78-
if err := c.Processor.Process(ctxt, ccci.ContainerType, scr); err != nil {
77+
if err := c.Processor.Process(ccci.ContainerType, scr); err != nil {
7978
return errors.WithMessage(err, "error starting container")
8079
}
8180

8281
return nil
8382
}
8483

8584
// Stop terminates chaincode and its container runtime environment.
86-
func (c *ContainerRuntime) Stop(ctxt context.Context, ccci *lifecycle.ChaincodeContainerInfo) error {
85+
func (c *ContainerRuntime) Stop(ccci *lifecycle.ChaincodeContainerInfo) error {
8786
scr := container.StopContainerReq{
8887
CCID: ccintf.CCID{
8988
Name: ccci.Name,
@@ -93,7 +92,7 @@ func (c *ContainerRuntime) Stop(ctxt context.Context, ccci *lifecycle.ChaincodeC
9392
Dontremove: false,
9493
}
9594

96-
if err := c.Processor.Process(ctxt, ccci.ContainerType, scr); err != nil {
95+
if err := c.Processor.Process(ccci.ContainerType, scr); err != nil {
9796
return errors.WithMessage(err, "error stopping container")
9897
}
9998

core/chaincode/container_runtime_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package chaincode_test
88

99
import (
10-
"context"
1110
"testing"
1211

1312
"github.com/hyperledger/fabric/core/chaincode"
@@ -171,12 +170,11 @@ func TestContainerRuntimeStart(t *testing.T) {
171170
ContainerType: "container-type",
172171
}
173172

174-
err := cr.Start(context.Background(), ccci, nil)
173+
err := cr.Start(ccci, nil)
175174
assert.NoError(t, err)
176175

177176
assert.Equal(t, 1, fakeProcessor.ProcessCallCount())
178-
ctx, vmType, req := fakeProcessor.ProcessArgsForCall(0)
179-
assert.Equal(t, context.Background(), ctx)
177+
vmType, req := fakeProcessor.ProcessArgsForCall(0)
180178
assert.Equal(t, vmType, "container-type")
181179
startReq, ok := req.(container.StartContainerReq)
182180
assert.True(t, ok)
@@ -216,7 +214,7 @@ func TestContainerRuntimeStartErrors(t *testing.T) {
216214
Version: "chaincode-version",
217215
}
218216

219-
err := cr.Start(context.Background(), ccci, nil)
217+
err := cr.Start(ccci, nil)
220218
assert.EqualError(t, err, tc.errValue)
221219
}
222220
}
@@ -234,12 +232,11 @@ func TestContainerRuntimeStop(t *testing.T) {
234232
ContainerType: "container-type",
235233
}
236234

237-
err := cr.Stop(context.Background(), ccci)
235+
err := cr.Stop(ccci)
238236
assert.NoError(t, err)
239237

240238
assert.Equal(t, 1, fakeProcessor.ProcessCallCount())
241-
ctx, vmType, req := fakeProcessor.ProcessArgsForCall(0)
242-
assert.Equal(t, context.Background(), ctx)
239+
vmType, req := fakeProcessor.ProcessArgsForCall(0)
243240
assert.Equal(t, vmType, "container-type")
244241
stopReq, ok := req.(container.StopContainerReq)
245242
assert.True(t, ok)
@@ -275,7 +272,7 @@ func TestContainerRuntimeStopErrors(t *testing.T) {
275272
ContainerType: "container-type",
276273
}
277274

278-
err := cr.Stop(context.Background(), ccci)
275+
err := cr.Stop(ccci)
279276
if err != nil || tc.errValue != "" {
280277
assert.EqualError(t, err, tc.errValue)
281278
continue

0 commit comments

Comments
 (0)