Skip to content

Commit d861bbf

Browse files
author
Jason Yellick
committed
FAB-10998 GetChaincodeData to use LSCC directly
Now that LSCC can be queried directly for chaincode definitions instead of having to go the roundabout way of invoking chaincode, the assorted internal users of 'getccdata' can be converted to use the new interface. Change-Id: Ic435742817643d2dc08d83afe2f66980c649b56a Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 8ec2ffa commit d861bbf

16 files changed

+153
-242
lines changed

core/chaincode/chaincode_support.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/hyperledger/fabric/core/common/ccprovider"
1818
"github.com/hyperledger/fabric/core/common/sysccprovider"
1919
"github.com/hyperledger/fabric/core/container/ccintf"
20+
"github.com/hyperledger/fabric/core/ledger"
2021
"github.com/hyperledger/fabric/core/peer"
2122
pb "github.com/hyperledger/fabric/protos/peer"
2223
"github.com/pkg/errors"
@@ -37,14 +38,7 @@ type Launcher interface {
3738
// Lifecycle provides a way to retrieve chaincode definitions and the packages necessary to run them
3839
type Lifecycle interface {
3940
// GetChaincodeDefinition returns the details for a chaincode by name
40-
GetChaincodeDefinition(
41-
ctx context.Context,
42-
txid string,
43-
signedProp *pb.SignedProposal,
44-
prop *pb.Proposal,
45-
chainID string,
46-
chaincodeID string,
47-
) (ccprovider.ChaincodeDefinition, error)
41+
GetChaincodeDefinition(chaincodeName string, txSim ledger.QueryExecutor) (ccprovider.ChaincodeDefinition, error)
4842

4943
// ChaincodeContainerInfo returns the package necessary to launch a chaincode
5044
ChaincodeContainerInfo(chainID string, chaincodeID string) (*lifecycle.ChaincodeContainerInfo, error)
@@ -71,7 +65,7 @@ func NewChaincodeSupport(
7165
caCert []byte,
7266
certGenerator CertGenerator,
7367
packageProvider PackageProvider,
74-
chaincodeStore lifecycle.InstantiatedChaincodeStore,
68+
lifecycle Lifecycle,
7569
aclProvider ACLProvider,
7670
processor Processor,
7771
sccp sysccprovider.SystemChaincodeProvider,
@@ -84,18 +78,14 @@ func NewChaincodeSupport(
8478
HandlerRegistry: NewHandlerRegistry(userRunsCC),
8579
ACLProvider: aclProvider,
8680
sccp: sccp,
81+
Lifecycle: lifecycle,
8782
}
8883

8984
// Keep TestQueries working
9085
if !config.TLSEnabled {
9186
certGenerator = nil
9287
}
9388

94-
cs.Lifecycle = &lifecycle.Lifecycle{
95-
Executor: cs,
96-
InstantiatedChaincodeStore: chaincodeStore,
97-
}
98-
9989
cs.Runtime = &ContainerRuntime{
10090
CertGenerator: certGenerator,
10191
Processor: processor,
@@ -188,7 +178,7 @@ func (cs *ChaincodeSupport) HandleChaincodeStream(ctxt context.Context, stream c
188178

189179
handler := &Handler{
190180
Invoker: cs,
191-
DefinitionGetter: &lifecycle.Lifecycle{Executor: cs},
181+
DefinitionGetter: cs.Lifecycle,
192182
Keepalive: cs.Keepalive,
193183
Registry: cs.HandlerRegistry,
194184
ACLProvider: cs.ACLProvider,

core/chaincode/chaincode_support_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
183183
ca.CertBytes(),
184184
certGenerator,
185185
&ccprovider.CCInfoFSImpl{},
186-
lsccImpl,
186+
&lifecycle.Lifecycle{
187+
InstantiatedChaincodeStore: lsccImpl,
188+
},
187189
mockAclProvider,
188190
container.NewVMController(
189191
map[string]container.VMProvider{

core/chaincode/exectransaction_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/hyperledger/fabric/core/aclmgmt"
3535
aclmocks "github.com/hyperledger/fabric/core/aclmgmt/mocks"
3636
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
37+
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
3738
"github.com/hyperledger/fabric/core/chaincode/platforms"
3839
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
3940
"github.com/hyperledger/fabric/core/chaincode/shim"
@@ -137,7 +138,9 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
137138
ca.CertBytes(),
138139
certGenerator,
139140
&ccprovider.CCInfoFSImpl{},
140-
lsccImpl,
141+
&lifecycle.Lifecycle{
142+
InstantiatedChaincodeStore: lsccImpl,
143+
},
141144
aclmgmt.NewACLProvider(func(string) channelconfig.Resources { return nil }),
142145
container.NewVMController(
143146
map[string]container.VMProvider{

core/chaincode/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type QueryResponseBuilder interface {
8888
// ChaincodeDefinitionGetter is responsible for retrieving a chaincode definition
8989
// from the system. The definition is used by the InstantiationPolicyChecker.
9090
type ChaincodeDefinitionGetter interface {
91-
GetChaincodeDefinition(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (ccprovider.ChaincodeDefinition, error)
91+
GetChaincodeDefinition(chaincodeName string, txSim ledger.QueryExecutor) (ccprovider.ChaincodeDefinition, error)
9292
}
9393

9494
// LedgerGetter is used to get ledgers for chaincode.
@@ -858,7 +858,7 @@ func (h *Handler) HandleInvokeChaincode(msg *pb.ChaincodeMessage, txContext *Tra
858858
version := h.SystemCCVersion
859859
if !h.SystemCCProvider.IsSysCC(targetInstance.ChaincodeName) {
860860
// if its a user chaincode, get the details
861-
cd, err := h.DefinitionGetter.GetChaincodeDefinition(ctxt, msg.Txid, txContext.SignedProp, txContext.Proposal, targetInstance.ChainID, targetInstance.ChaincodeName)
861+
cd, err := h.DefinitionGetter.GetChaincodeDefinition(targetInstance.ChaincodeName, txsim)
862862
if err != nil {
863863
return nil, errors.WithStack(err)
864864
}

core/chaincode/handler_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,12 +1583,9 @@ var _ = Describe("Handler", func() {
15831583
Expect(err).NotTo(HaveOccurred())
15841584

15851585
Expect(fakeDefinitionGetter.GetChaincodeDefinitionCallCount()).To(Equal(1))
1586-
_, txid, signedProp, prop, chainID, ccname := fakeDefinitionGetter.GetChaincodeDefinitionArgsForCall(0)
1587-
Expect(txid).To(Equal("tx-id"))
1588-
Expect(signedProp).To(Equal(expectedSignedProp))
1589-
Expect(prop).To(Equal(expectedProposal))
1590-
Expect(chainID).To(Equal("channel-id"))
1586+
ccname, txSim := fakeDefinitionGetter.GetChaincodeDefinitionArgsForCall(0)
15911587
Expect(ccname).To(Equal("target-chaincode-name"))
1588+
Expect(txSim).To(Equal(newTxSimulator))
15921589
})
15931590

15941591
It("checks the instantiation policy on the target", func() {

core/chaincode/lifecycle/lifecycle.go

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,20 @@ SPDX-License-Identifier: Apache-2.0
77
package lifecycle
88

99
import (
10-
"github.com/golang/protobuf/proto"
11-
"github.com/hyperledger/fabric/common/util"
12-
"github.com/hyperledger/fabric/core/chaincode/shim"
1310
"github.com/hyperledger/fabric/core/common/ccprovider"
11+
"github.com/hyperledger/fabric/core/ledger"
1412
pb "github.com/hyperledger/fabric/protos/peer"
1513
"github.com/pkg/errors"
16-
"golang.org/x/net/context"
1714
)
1815

19-
// Executor is used to invoke chaincode.
20-
type Executor interface {
21-
Execute(ctxt context.Context, cccid *ccprovider.CCContext, cis *pb.ChaincodeInvocationSpec) (*pb.Response, *pb.ChaincodeEvent, error)
22-
}
23-
2416
// InstantiatedChaincodeStore returns information on chaincodes which are instantiated
2517
type InstantiatedChaincodeStore interface {
2618
ChaincodeDeploymentSpec(channelID, chaincodeName string) (*pb.ChaincodeDeploymentSpec, error)
19+
ChaincodeDefinition(chaincodeName string, txSim ledger.QueryExecutor) (ccprovider.ChaincodeDefinition, error)
2720
}
2821

2922
// Lifecycle provides methods to invoke the lifecycle system chaincode.
3023
type Lifecycle struct {
31-
Executor Executor
3224
InstantiatedChaincodeStore InstantiatedChaincodeStore
3325
}
3426

@@ -54,42 +46,9 @@ func (l *Lifecycle) ChaincodeContainerInfo(channelID, chaincodeName string) (*Ch
5446
}
5547

5648
// GetChaincodeDefinition returns a ccprovider.ChaincodeDefinition for the chaincode
57-
// associated with the provided channel and name.
58-
func (l *Lifecycle) GetChaincodeDefinition(
59-
ctx context.Context,
60-
txid string,
61-
signedProp *pb.SignedProposal,
62-
prop *pb.Proposal,
63-
chainID string,
64-
chaincodeID string,
65-
) (ccprovider.ChaincodeDefinition, error) {
66-
version := util.GetSysCCVersion()
67-
cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
68-
69-
invocationSpec := &pb.ChaincodeInvocationSpec{
70-
ChaincodeSpec: &pb.ChaincodeSpec{
71-
Type: pb.ChaincodeSpec_GOLANG,
72-
ChaincodeId: &pb.ChaincodeID{Name: cccid.Name},
73-
Input: &pb.ChaincodeInput{
74-
Args: util.ToChaincodeArgs("getccdata", chainID, chaincodeID),
75-
},
76-
},
77-
}
78-
res, _, err := l.Executor.Execute(ctx, cccid, invocationSpec)
79-
if err != nil {
80-
return nil, errors.Wrapf(err, "getccdata %s/%s failed", chainID, chaincodeID)
81-
}
82-
if res.Status != shim.OK {
83-
return nil, errors.Errorf("getccdata %s/%s responded with error: %s", chainID, chaincodeID, res.Message)
84-
}
85-
86-
cd := &ccprovider.ChaincodeData{}
87-
err = proto.Unmarshal(res.Payload, cd)
88-
if err != nil {
89-
return nil, errors.Wrap(err, "failed to unmarshal chaincode definition")
90-
}
91-
92-
return cd, nil
49+
// associated with the provided txsim and name.
50+
func (l *Lifecycle) GetChaincodeDefinition(chaincodeName string, txSim ledger.QueryExecutor) (ccprovider.ChaincodeDefinition, error) {
51+
return l.InstantiatedChaincodeStore.ChaincodeDefinition(chaincodeName, txSim)
9352
}
9453

9554
func DeploymentSpecToChaincodeContainerInfo(cds *pb.ChaincodeDeploymentSpec) *ChaincodeContainerInfo {

core/chaincode/lifecycle/lifecycle_suite_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import (
1515
"testing"
1616
)
1717

18-
//go:generate counterfeiter -o mock/executor.go --fake-name Executor . executor
19-
type executor interface {
20-
lifecycle.Executor
21-
}
22-
2318
//go:generate counterfeiter -o mock/instantiated_cc_store.go --fake-name InstantiatedChaincodeStore . instantiatedChaincodeStore
2419
type instantiatedChaincodeStore interface {
2520
lifecycle.InstantiatedChaincodeStore

core/chaincode/lifecycle/lifecycle_test.go

Lines changed: 18 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@ SPDX-License-Identifier: Apache-2.0
77
package lifecycle_test
88

99
import (
10-
"github.com/golang/protobuf/proto"
11-
"github.com/hyperledger/fabric/common/util"
1210
lc "github.com/hyperledger/fabric/core/chaincode/lifecycle"
1311
"github.com/hyperledger/fabric/core/chaincode/lifecycle/mock"
14-
"github.com/hyperledger/fabric/core/chaincode/shim"
1512
"github.com/hyperledger/fabric/core/common/ccprovider"
1613
pb "github.com/hyperledger/fabric/protos/peer"
1714
. "github.com/onsi/ginkgo"
1815
. "github.com/onsi/gomega"
1916
"github.com/pkg/errors"
20-
"golang.org/x/net/context"
2117
)
2218

2319
var _ = Describe("Lifecycle", func() {
2420
var (
25-
lifecycle *lc.Lifecycle
21+
lifecycle *lc.Lifecycle
22+
fakeInstantiatedCCStore *mock.InstantiatedChaincodeStore
2623
)
2724

2825
BeforeEach(func() {
26+
fakeInstantiatedCCStore = &mock.InstantiatedChaincodeStore{}
27+
28+
lifecycle = &lc.Lifecycle{
29+
InstantiatedChaincodeStore: fakeInstantiatedCCStore,
30+
}
2931
})
3032

3133
Describe("ChaincodeContainerInfo", func() {
3234
var (
33-
fakeInstantiatedCCStore *mock.InstantiatedChaincodeStore
34-
deploymentSpec *pb.ChaincodeDeploymentSpec
35+
deploymentSpec *pb.ChaincodeDeploymentSpec
3536
)
3637

3738
BeforeEach(func() {
@@ -47,12 +48,7 @@ var _ = Describe("Lifecycle", func() {
4748
ExecEnv: pb.ChaincodeDeploymentSpec_SYSTEM,
4849
}
4950

50-
fakeInstantiatedCCStore = &mock.InstantiatedChaincodeStore{}
5151
fakeInstantiatedCCStore.ChaincodeDeploymentSpecReturns(deploymentSpec, nil)
52-
53-
lifecycle = &lc.Lifecycle{
54-
InstantiatedChaincodeStore: fakeInstantiatedCCStore,
55-
}
5652
})
5753

5854
It("invokes lscc getdepspec with the correct args", func() {
@@ -85,94 +81,24 @@ var _ = Describe("Lifecycle", func() {
8581
Describe("GetChaincodeDefinition", func() {
8682
var (
8783
chaincodeData *ccprovider.ChaincodeData
88-
89-
fakeExecutor *mock.Executor
90-
signedProp *pb.SignedProposal
91-
proposal *pb.Proposal
9284
)
9385

9486
BeforeEach(func() {
95-
fakeExecutor = &mock.Executor{}
96-
signedProp = &pb.SignedProposal{ProposalBytes: []byte("some-proposal-bytes")}
97-
proposal = &pb.Proposal{Payload: []byte("some-payload-bytes")}
98-
99-
lifecycle = &lc.Lifecycle{
100-
Executor: fakeExecutor,
101-
}
102-
10387
chaincodeData = &ccprovider.ChaincodeData{
104-
Name: "george",
105-
Version: "old",
106-
}
107-
payload, err := proto.Marshal(chaincodeData)
108-
Expect(err).NotTo(HaveOccurred())
109-
110-
response := &pb.Response{
111-
Status: shim.OK,
112-
Payload: payload,
88+
Name: "chaincode-data-name",
11389
}
114-
fakeExecutor.ExecuteReturns(response, nil, nil)
115-
})
11690

117-
It("invokes lscc getccdata with the correct args", func() {
118-
cd, err := lifecycle.GetChaincodeDefinition(context.Background(), "tx-id", signedProp, proposal, "chain-id", "chaincode-id")
119-
Expect(err).NotTo(HaveOccurred())
120-
Expect(cd).To(Equal(chaincodeData))
121-
122-
Expect(fakeExecutor.ExecuteCallCount()).To(Equal(1))
123-
ctx, cccid, cis := fakeExecutor.ExecuteArgsForCall(0)
124-
Expect(ctx).To(Equal(context.Background()))
125-
Expect(cccid).To(Equal(ccprovider.NewCCContext("chain-id", "lscc", "latest", "tx-id", true, signedProp, proposal)))
126-
Expect(cis).To(Equal(&pb.ChaincodeInvocationSpec{
127-
ChaincodeSpec: &pb.ChaincodeSpec{
128-
Type: pb.ChaincodeSpec_GOLANG,
129-
ChaincodeId: &pb.ChaincodeID{Name: "lscc"},
130-
Input: &pb.ChaincodeInput{
131-
Args: util.ToChaincodeArgs("getccdata", "chain-id", "chaincode-id"),
132-
},
133-
},
134-
}))
91+
fakeInstantiatedCCStore.ChaincodeDefinitionReturns(chaincodeData, errors.New("fake-error"))
13592
})
13693

137-
Context("when the executor fails", func() {
138-
BeforeEach(func() {
139-
fakeExecutor.ExecuteReturns(nil, nil, errors.New("mango-tango"))
140-
})
141-
142-
It("returns a wrapped error", func() {
143-
_, err := lifecycle.GetChaincodeDefinition(context.Background(), "tx-id", signedProp, proposal, "chain-id", "chaincode-id")
144-
Expect(err).To(MatchError("getccdata chain-id/chaincode-id failed: mango-tango"))
145-
})
146-
})
147-
148-
Context("when the executor returns an error response", func() {
149-
BeforeEach(func() {
150-
response := &pb.Response{
151-
Status: shim.ERROR,
152-
Message: "danger-danger",
153-
}
154-
fakeExecutor.ExecuteReturns(response, nil, nil)
155-
})
156-
157-
It("returns a wrapped error", func() {
158-
_, err := lifecycle.GetChaincodeDefinition(context.Background(), "tx-id", signedProp, proposal, "chain-id", "chaincode-id")
159-
Expect(err).To(MatchError("getccdata chain-id/chaincode-id responded with error: danger-danger"))
160-
})
161-
})
162-
163-
Context("when unmarshaling the response fails", func() {
164-
BeforeEach(func() {
165-
response := &pb.Response{
166-
Status: shim.OK,
167-
Payload: []byte("totally-bogus-payload"),
168-
}
169-
fakeExecutor.ExecuteReturns(response, nil, nil)
170-
})
171-
172-
It("returns a wrapped error", func() {
173-
_, err := lifecycle.GetChaincodeDefinition(context.Background(), "tx-id", signedProp, proposal, "chain-id", "chaincode-id")
174-
Expect(err).To(MatchError(HavePrefix("failed to unmarshal chaincode definition: proto: ")))
175-
})
94+
It("passes through to the underlying implementation", func() {
95+
chaincodeDefinition, err := lifecycle.GetChaincodeDefinition("foo", nil)
96+
Expect(chaincodeDefinition.(*ccprovider.ChaincodeData)).To(Equal(chaincodeData))
97+
Expect(err).To(MatchError("fake-error"))
98+
Expect(fakeInstantiatedCCStore.ChaincodeDefinitionCallCount()).To(Equal(1))
99+
ccNameArg, txSimArg := fakeInstantiatedCCStore.ChaincodeDefinitionArgsForCall(0)
100+
Expect(ccNameArg).To(Equal("foo"))
101+
Expect(txSimArg).To(BeNil())
176102
})
177103
})
178104
})

0 commit comments

Comments
 (0)