Skip to content

Commit e896e6e

Browse files
author
Jason Yellick
committed
FAB-16124 Use BuiltinSCC real
We are currently faking the 'SCCProvider.IsSysCC' in a number of places. This code is now so simple and can be trivially initialized as a real, removing the fakes. Change-Id: I447d2434145dd44079c54ec7543b388835f023a1 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent fd42b79 commit e896e6e

File tree

13 files changed

+104
-303
lines changed

13 files changed

+104
-303
lines changed

core/chaincode/chaincode_suite_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ type transactionRegistry interface {
8585
chaincode.TransactionRegistry
8686
}
8787

88-
//go:generate counterfeiter -o mock/system_chaincode_provider.go --fake-name SystemCCProvider . systemCCProvider
89-
type systemCCProvider interface {
90-
chaincode.SystemCCProvider
91-
}
92-
9388
//go:generate counterfeiter -o mock/acl_provider.go --fake-name ACLProvider . aclProvider
9489
type aclProvider interface {
9590
chaincode.ACLProvider

core/chaincode/chaincode_support.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric/core/container/ccintf"
1919
"github.com/hyperledger/fabric/core/ledger"
2020
"github.com/hyperledger/fabric/core/peer"
21+
"github.com/hyperledger/fabric/core/scc"
2122
pb "github.com/hyperledger/fabric/protos/peer"
2223
"github.com/pkg/errors"
2324
)
@@ -56,6 +57,7 @@ type Lifecycle interface {
5657
type ChaincodeSupport struct {
5758
ACLProvider ACLProvider
5859
AppConfig ApplicationConfigRetriever
60+
BuiltinSCCs scc.BuiltinSCCs
5961
DeployedCCInfoProvider ledger.DeployedChaincodeInfoProvider
6062
ExecuteTimeout time.Duration
6163
HandlerMetrics *HandlerMetrics
@@ -65,7 +67,6 @@ type ChaincodeSupport struct {
6567
Lifecycle Lifecycle
6668
Peer *peer.Peer
6769
Runtime Runtime
68-
SystemCCProvider SystemCCProvider
6970
TotalQueryLimit int
7071
UserRunsCC bool
7172
}
@@ -123,7 +124,7 @@ func (cs *ChaincodeSupport) HandleChaincodeStream(stream ccintf.ChaincodeStream)
123124
ACLProvider: cs.ACLProvider,
124125
TXContexts: NewTransactionContexts(),
125126
ActiveTransactions: NewActiveTransactions(),
126-
SystemCCProvider: cs.SystemCCProvider,
127+
BuiltinSCCs: cs.BuiltinSCCs,
127128
SystemCCVersion: util.GetSysCCVersion(),
128129
InstantiationPolicyChecker: CheckInstantiationPolicyFunc(ccprovider.CheckInstantiationPolicy),
129130
QueryResponseBuilder: &QueryResponseGenerator{MaxResultLimit: 100},
@@ -209,7 +210,7 @@ func processChaincodeExecutionResult(txid, ccName string, resp *pb.ChaincodeMess
209210
// Invoke will invoke chaincode and return the message containing the response.
210211
// The chaincode will be launched if it is not already running.
211212
func (cs *ChaincodeSupport) Invoke(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, input *pb.ChaincodeInput) (*pb.ChaincodeMessage, error) {
212-
if cs.SystemCCProvider.IsSysCC(cccid.Name) {
213+
if cs.BuiltinSCCs.IsSysCC(cccid.Name) {
213214
return cs.invokeSystem(txParams, cccid, input)
214215
}
215216

core/chaincode/chaincode_support_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func initMockPeer(chainIDs ...string) (*peer.Peer, *ChaincodeSupport, func(), er
177177
globalConfig := GlobalConfig()
178178
globalConfig.StartupTimeout = 10 * time.Second
179179
globalConfig.ExecuteTimeout = 1 * time.Second
180-
lsccImpl := lscc.New(scc.BuiltinSCCs{}, sccp, mockAclProvider, peerInstance.GetMSPIDs, newPolicyChecker(peerInstance))
180+
lsccImpl := lscc.New(map[string]struct{}{"lscc": {}}, sccp, mockAclProvider, peerInstance.GetMSPIDs, newPolicyChecker(peerInstance))
181181
ml := &mock.Lifecycle{}
182182
ml.ChaincodeContainerInfoStub = func(_, name string, _ ledger.SimpleQueryExecutor) (*ccprovider.ChaincodeContainerInfo, error) {
183183
switch name {
@@ -242,7 +242,7 @@ func initMockPeer(chainIDs ...string) (*peer.Peer, *ChaincodeSupport, func(), er
242242
Lifecycle: ml,
243243
Peer: peerInstance,
244244
Runtime: containerRuntime,
245-
SystemCCProvider: scc.BuiltinSCCs{},
245+
BuiltinSCCs: map[string]struct{}{"lscc": {}},
246246
TotalQueryLimit: globalConfig.TotalQueryLimit,
247247
UserRunsCC: userRunsCC,
248248
}
@@ -1029,8 +1029,8 @@ func TestGetTxContextFromHandler(t *testing.T) {
10291029
defer cleanup()
10301030

10311031
h := Handler{
1032-
TXContexts: NewTransactionContexts(),
1033-
SystemCCProvider: &scc.BuiltinSCCs{},
1032+
TXContexts: NewTransactionContexts(),
1033+
BuiltinSCCs: map[string]struct{}{"lscc": {}},
10341034
}
10351035

10361036
txid := "1"
@@ -1224,7 +1224,7 @@ func TestCCFramework(t *testing.T) {
12241224
initializeCC(t, chainID, ccname, ccSide, chaincodeSupport)
12251225

12261226
//chaincode support should not allow dups
1227-
handler := &Handler{chaincodeID: &pb.ChaincodeID{Name: ccname + ":0"}, SystemCCProvider: chaincodeSupport.SystemCCProvider}
1227+
handler := &Handler{chaincodeID: &pb.ChaincodeID{Name: ccname + ":0"}, BuiltinSCCs: chaincodeSupport.BuiltinSCCs}
12281228
if err := chaincodeSupport.HandlerRegistry.Register(handler); err == nil {
12291229
t.Fatalf("expected re-register to fail")
12301230
}

core/chaincode/exectransaction_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ func initPeer(chainIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSuppor
109109
ca, _ := tlsgen.NewCA()
110110
pr := platforms.NewRegistry(&golang.Platform{})
111111
mockAclProvider := &mock.ACLProvider{}
112-
lsccImpl := lscc.New(scc.BuiltinSCCs{}, sccp, mockAclProvider, peerInstance.GetMSPIDs, newPolicyChecker(peerInstance))
112+
builtinSCCs := map[string]struct{}{"lscc": {}}
113+
lsccImpl := lscc.New(builtinSCCs, sccp, mockAclProvider, peerInstance.GetMSPIDs, newPolicyChecker(peerInstance))
113114
ml := &cm.Lifecycle{}
114115
ml.ChaincodeContainerInfoStub = func(_, name string, _ ledger.SimpleQueryExecutor) (*ccprovider.ChaincodeContainerInfo, error) {
115116
switch name {
@@ -183,7 +184,7 @@ func initPeer(chainIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSuppor
183184
Lifecycle: ml,
184185
Peer: peerInstance,
185186
Runtime: containerRuntime,
186-
SystemCCProvider: scc.BuiltinSCCs{},
187+
BuiltinSCCs: builtinSCCs,
187188
TotalQueryLimit: globalConfig.TotalQueryLimit,
188189
UserRunsCC: userRunsCC,
189190
}

core/chaincode/handler.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/hyperledger/fabric/core/common/sysccprovider"
2525
"github.com/hyperledger/fabric/core/container/ccintf"
2626
"github.com/hyperledger/fabric/core/ledger"
27+
"github.com/hyperledger/fabric/core/scc"
2728
"github.com/hyperledger/fabric/protos/common"
2829
pb "github.com/hyperledger/fabric/protos/peer"
2930
"github.com/pkg/errors"
@@ -50,11 +51,6 @@ type Invoker interface {
5051
Invoke(txParams *ccprovider.TransactionParams, cccid *ccprovider.CCContext, spec *pb.ChaincodeInput) (*pb.ChaincodeMessage, error)
5152
}
5253

53-
// SystemCCProvider provides system chaincode metadata.
54-
type SystemCCProvider interface {
55-
IsSysCC(name string) bool
56-
}
57-
5854
// TransactionRegistry tracks active transactions for each channel.
5955
type TransactionRegistry interface {
6056
Add(channelID, txID string) bool
@@ -137,8 +133,8 @@ type Handler struct {
137133
TXContexts ContextRegistry
138134
// activeTransactions holds active transaction identifiers.
139135
ActiveTransactions TransactionRegistry
140-
// SystemCCProvider provides access to system chaincode metadata
141-
SystemCCProvider SystemCCProvider
136+
// BuiltinSCCs can be used to determine if a name is associated with a system chaincode
137+
BuiltinSCCs scc.BuiltinSCCs
142138
// InstantiationPolicyChecker is used to evaluate the chaincode instantiation policies.
143139
InstantiationPolicyChecker InstantiationPolicyChecker
144140
// QueryResponeBuilder is used to build query responses
@@ -356,7 +352,7 @@ func (h *Handler) checkACL(signedProp *pb.SignedProposal, proposal *pb.Proposal,
356352
// - an application chaincode
357353
// (and we still need to determine whether the invoker can invoke it)
358354

359-
if h.SystemCCProvider.IsSysCC(ccIns.ChaincodeName) {
355+
if h.BuiltinSCCs.IsSysCC(ccIns.ChaincodeName) {
360356
// Allow this call
361357
return nil
362358
}
@@ -1017,7 +1013,7 @@ func (h *Handler) getTxContextForInvoke(channelID string, txid string, payload [
10171013
// If targetInstance is not an SCC, isValidTxSim should be called which will return an err.
10181014
// We do not want to propagate calls to user CCs when the original call was to a SCC
10191015
// without a channel context (ie, no ledger context).
1020-
if !h.SystemCCProvider.IsSysCC(targetInstance.ChaincodeName) {
1016+
if !h.BuiltinSCCs.IsSysCC(targetInstance.ChaincodeName) {
10211017
// normal path - UCC invocation with an empty ("") channel: isValidTxSim will return an error
10221018
return h.isValidTxSim("", txid, "could not get valid transaction")
10231019
}
@@ -1193,7 +1189,7 @@ func (h *Handler) HandleInvokeChaincode(msg *pb.ChaincodeMessage, txContext *Tra
11931189
version := h.SystemCCVersion
11941190
var idBytes []byte
11951191
requiresInit := false
1196-
if !h.SystemCCProvider.IsSysCC(targetInstance.ChaincodeName) {
1192+
if !h.BuiltinSCCs.IsSysCC(targetInstance.ChaincodeName) {
11971193
// if its a user chaincode, get the details
11981194
cd, err := h.DefinitionGetter.ChaincodeDefinition(targetInstance.ChainID, targetInstance.ChaincodeName, txParams.TXSimulator)
11991195
if err != nil {

core/chaincode/handler_test.go

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hyperledger/fabric/core/common/ccprovider"
2121
"github.com/hyperledger/fabric/core/common/sysccprovider"
2222
"github.com/hyperledger/fabric/core/container/ccintf"
23+
"github.com/hyperledger/fabric/core/scc"
2324
pb "github.com/hyperledger/fabric/protos/peer"
2425
. "github.com/onsi/ginkgo"
2526
. "github.com/onsi/ginkgo/extensions/table"
@@ -32,7 +33,7 @@ var _ = Describe("Handler", func() {
3233
fakeTransactionRegistry *mock.TransactionRegistry
3334
fakeContextRegistry *fake.ContextRegistry
3435
fakeChatStream *mock.ChaincodeStream
35-
fakeSystemCCProvider *mock.SystemCCProvider
36+
builtinSCCs scc.BuiltinSCCs
3637
fakeTxSimulator *mock.TxSimulator
3738
fakeHistoryQueryExecutor *mock.HistoryQueryExecutor
3839
fakeQueryResponseBuilder *fake.QueryResponseBuilder
@@ -61,7 +62,6 @@ var _ = Describe("Handler", func() {
6162
fakeTransactionRegistry.AddReturns(true)
6263

6364
fakeChatStream = &mock.ChaincodeStream{}
64-
fakeSystemCCProvider = &mock.SystemCCProvider{}
6565

6666
fakeTxSimulator = &mock.TxSimulator{}
6767
fakeHistoryQueryExecutor = &mock.HistoryQueryExecutor{}
@@ -106,6 +106,8 @@ var _ = Describe("Handler", func() {
106106
fakeExecuteTimeouts = &metricsfakes.Counter{}
107107
fakeExecuteTimeouts.WithReturns(fakeExecuteTimeouts)
108108

109+
builtinSCCs = map[string]struct{}{}
110+
109111
chaincodeMetrics := &chaincode.HandlerMetrics{
110112
ShimRequestsReceived: fakeShimRequestsReceived,
111113
ShimRequestsCompleted: fakeShimRequestsCompleted,
@@ -122,7 +124,7 @@ var _ = Describe("Handler", func() {
122124
InstantiationPolicyChecker: fakeInstantiationPolicyChecker,
123125
QueryResponseBuilder: fakeQueryResponseBuilder,
124126
Registry: fakeHandlerRegistry,
125-
SystemCCProvider: fakeSystemCCProvider,
127+
BuiltinSCCs: builtinSCCs,
126128
SystemCCVersion: "system-cc-version",
127129
TXContexts: fakeContextRegistry,
128130
UUIDGenerator: chaincode.UUIDGeneratorFunc(func() string {
@@ -416,60 +418,46 @@ var _ = Describe("Handler", func() {
416418
incomingMessage.ChannelId = ""
417419
})
418420

419-
It("checks if the target is system chaincode", func() {
421+
It("validates the transaction context", func() {
422+
txContext.TXSimulator = nil
420423
handler.HandleTransaction(incomingMessage, fakeMessageHandler.Handle)
421424

422-
Expect(fakeSystemCCProvider.IsSysCCCallCount()).To(Equal(1))
423-
name := fakeSystemCCProvider.IsSysCCArgsForCall(0)
424-
Expect(name).To(Equal("target-chaincode-name"))
425+
Eventually(fakeChatStream.SendCallCount).Should(Equal(1))
426+
msg := fakeChatStream.SendArgsForCall(0)
427+
Expect(msg).To(Equal(&pb.ChaincodeMessage{
428+
Type: pb.ChaincodeMessage_ERROR,
429+
Payload: []byte("INVOKE_CHAINCODE failed: transaction ID: tx-id: could not get valid transaction"),
430+
Txid: "tx-id",
431+
}))
425432
})
426433

427-
Context("when the target chaincode is not system chaincode", func() {
434+
Context("when the target is system chaincode", func() {
428435
BeforeEach(func() {
429-
fakeSystemCCProvider.IsSysCCReturns(false)
436+
builtinSCCs["target-chaincode-name"] = struct{}{}
430437
})
431438

432-
It("validates the transaction context", func() {
439+
It("gets the transaction context without validation", func() {
433440
txContext.TXSimulator = nil
434441
handler.HandleTransaction(incomingMessage, fakeMessageHandler.Handle)
435442

443+
Expect(fakeContextRegistry.GetCallCount()).To(Equal(1))
436444
Eventually(fakeChatStream.SendCallCount).Should(Equal(1))
437445
msg := fakeChatStream.SendArgsForCall(0)
438-
Expect(msg).To(Equal(&pb.ChaincodeMessage{
439-
Type: pb.ChaincodeMessage_ERROR,
440-
Payload: []byte("INVOKE_CHAINCODE failed: transaction ID: tx-id: could not get valid transaction"),
441-
Txid: "tx-id",
442-
}))
446+
Expect(msg).To(Equal(expectedResponse))
443447
})
444448

445-
Context("when the target is system chaincode", func() {
446-
BeforeEach(func() {
447-
fakeSystemCCProvider.IsSysCCReturns(true)
448-
})
449-
450-
It("gets the transaction context without validation", func() {
451-
txContext.TXSimulator = nil
449+
Context("and the transaction context is missing", func() {
450+
It("returns an error", func() {
451+
fakeContextRegistry.GetReturns(nil)
452452
handler.HandleTransaction(incomingMessage, fakeMessageHandler.Handle)
453453

454-
Expect(fakeContextRegistry.GetCallCount()).To(Equal(1))
455454
Eventually(fakeChatStream.SendCallCount).Should(Equal(1))
456455
msg := fakeChatStream.SendArgsForCall(0)
457-
Expect(msg).To(Equal(expectedResponse))
458-
})
459-
460-
Context("and the transaction context is missing", func() {
461-
It("returns an error", func() {
462-
fakeContextRegistry.GetReturns(nil)
463-
handler.HandleTransaction(incomingMessage, fakeMessageHandler.Handle)
464-
465-
Eventually(fakeChatStream.SendCallCount).Should(Equal(1))
466-
msg := fakeChatStream.SendArgsForCall(0)
467-
Expect(msg).To(Equal(&pb.ChaincodeMessage{
468-
Type: pb.ChaincodeMessage_ERROR,
469-
Payload: []byte("INVOKE_CHAINCODE failed: transaction ID: tx-id: failed to get transaction context"),
470-
Txid: "tx-id",
471-
}))
472-
})
456+
Expect(msg).To(Equal(&pb.ChaincodeMessage{
457+
Type: pb.ChaincodeMessage_ERROR,
458+
Payload: []byte("INVOKE_CHAINCODE failed: transaction ID: tx-id: failed to get transaction context"),
459+
Txid: "tx-id",
460+
}))
473461
})
474462
})
475463
})
@@ -2197,15 +2185,6 @@ var _ = Describe("Handler", func() {
21972185
fakeInvoker.InvokeReturns(responseMessage, nil)
21982186
})
21992187

2200-
It("checks if the target is a system chaincode", func() {
2201-
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
2202-
Expect(err).NotTo(HaveOccurred())
2203-
2204-
Expect(fakeSystemCCProvider.IsSysCCCallCount() >= 1).To(BeTrue())
2205-
name := fakeSystemCCProvider.IsSysCCArgsForCall(0)
2206-
Expect(name).To(Equal("target-chaincode-name"))
2207-
})
2208-
22092188
It("evaluates the access control policy", func() {
22102189
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
22112190
Expect(err).NotTo(HaveOccurred())
@@ -2331,7 +2310,7 @@ var _ = Describe("Handler", func() {
23312310

23322311
Context("when the target is a system chaincode", func() {
23332312
BeforeEach(func() {
2334-
fakeSystemCCProvider.IsSysCCReturns(true)
2313+
builtinSCCs["target-chaincode-name"] = struct{}{}
23352314
})
23362315

23372316
It("does not perform acl checks", func() {
@@ -2352,10 +2331,6 @@ var _ = Describe("Handler", func() {
23522331
})
23532332

23542333
Context("when the target is not a system chaincode", func() {
2355-
BeforeEach(func() {
2356-
fakeSystemCCProvider.IsSysCCReturns(false)
2357-
})
2358-
23592334
It("gets the chaincode definition", func() {
23602335
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
23612336
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)